SlideShare a Scribd company logo
Goroutine stack and local
variable allocation in Go
Cherie Hsieh
Golang Taiwan Community
GDG Hsinchu
Women Techmakers Community
Outline
1. The Run-Time Stack
2. User Stack in C Program
3. User Stack in Go goroutine
4. Goroutine Stack allocation
5. System Stack and User Stack
6. Stack Growing Mechanism
The Run-Time Stack
The Run-Time Stack
In common Linux systems, each process/thread has two stacks:
1. Kernel stack (system calls, interrupt handlers)
2. User stack (execute user-space applications)
The Run-Time Stack
User Stack provides last-in, first out memory management for procedure-calling.
source: https://www.mdpi.com/computers/computers-09-00048/article_deploy/html/images/computers-09-00048-g005.png
User Stack in C Program
User Stack in C Program
The memory layout of a process with main thread.
source: https://cseweb.ucsd.edu/classes/sp16/cse120-a/applications/ln/proccontext.jpg
User Stack in C Program
The memory layout of a process with multiple threads.
source: https://cseweb.ucsd.edu/classes/sp16/cse120-a/applications/ln/thdcontext.jpg
Stack Size in C Program
Default user stack size of main thread: 8k
Use ulimit cmd-tool to check or change the default stack size.
execve() causes the program that is currently being run by the calling process to be
replaced with a new program, with newly initialized stack, heap, and data segments.
cherie@coscup:~$ ulimit -a
stack size (kbytes, -s) 8192
max user processes (-u) 59988
Stack Size in C Program
Create a new thread with a customized stack size using system call clone()
The stack argument specifies the location of the stack used by the child threads.
int clone(int (*fn)(void *), void *stack, int flags, void *arg, ...
/* pid_t *parent_tid, void *tls, pid_t *child_tid */);
User Stack in Go goroutine
User Stack in Go goroutine
Goroutine is lightweight, costing little more than the allocation of stack space. And the
stacks start small, so they are cheap.
/* source code: runtime/runtime2.go */
type stack struct {
lo uintptr
hi uintptr
}
type g struct {
// Stack parameters.
// stack describes the actual stack memory: [stack.lo, stack.hi).
stack stack
// ...more members
}
Stack Allocation in Go goroutine
The Free List in stackCache and stackPool
Four size classes of free list: 2k, 4k, 8k, and 16k.
/* source code: runtime/malloc.go */
/* We want to cache 2KB, 4KB, 8KB, and 16KB stacks. Larger stacks */
/* will be allocated directly. */
/* Since FixedStack is different on different systems, we */
/* must vary NumStackOrders to keep the same maximum cached size. */
/* OS | FixedStack | NumStackOrders */
/* -----------------+------------+--------------- */
/* linux/darwin/bsd | 2KB | 4 */
/* windows/32 | 4KB | 3 */
/* windows/64 | 8KB | 2 */
/* plan9 | 4KB | 3 */
The size classes would be different due to different operating systems and
architectures.
The Free List in stackCache and stackPool
Considering the cache line of each architecture, a cache line padding is added to item
structure for performance optimization.
/* Global pool of spans that have free stacks. */
/* Stacks are assigned an order according to size. */
/* There is a free list for each order. */
var stackpool [_NumStackOrders]struct {
item stackpoolItem
_ [cpu.CacheLinePadSize -
unsafe.Sizeof(stackpoolItem{})%cpu.CacheLinePadSize]byte
}
System Stack in Go
System Stack in Go
There are two stacks in Go
1. User stack Every non-dead G has a user stack associated with it, which is what
user Go code executes on.
2. System stack Every M has a system stack associated with it (also known as the
M's "g0") . System and signal stacks cannot grow, but are large enough to execute
runtime and cgo code (8K).
Code running on the system stack is implicitly non-preemptible and the garbage
collector does not scan system stacks.
System Stack in Go
A main thread with a single goroutine
User Stack Extension in Go goroutine
User Stack Extension in Go goroutine
A goroutines stack starts small, so they are cheap, and grow by allocating (and freeing)
heap storage as required.
const (
StackSmall = 128
StackBig = 4096
)
type g struct {
stack stack
stackguard0 uintptr
// ...more members
}
User Stack Extension in Go goroutine
Stack extension check:
1. frame size <= StackSmall
Comparing the stack pointer with the stack guard, then extend the stack if the stack pointer
is below the stack guard.
2. StackSmall < frame size <= StackBig
Moving down the stack pointer and comparing the stack pointer with the stack guard.
Extending the stack if the stack pointer is below the stack guard.
3. frame size > StackBig
Always does stack extension.
User Stack Extension in Go goroutine
The Process of Stack Growth
How stack growth works?
1. record the stack map (global) for pointer checking.
2. adjust the pointer value according to adjustInfo.
Experiments
Stack Overflow
package main
const (
size = 2048
maxDepth = 5000000
)
func bigFrame(args [size]uint64, depth int) {
if depth < maxDepth {
bigFrame(args, depth+1)
}
}
func main() {
var args [size]uint64
bigFrame(args, 0)
}
Stack Overflow
Stack Overflow
Error!
Stack Overflow
Default Maxstacksize
/* source code: runtime/proc.go */
if sys.PtrSize == 8 {
maxstacksize = 1000000000
} else {
maxstacksize = 250000000
}
Goroutine Initialization with > 2k function args
package main
/* change arrSize to 260 and check the result */
const arrSize = 4
var c = make(chan bool)
func bigArray(arr [arrSize]uint64) {
var sum uint64 = 1
for i := 0; i < arrSize; i++ {
sum = sum + arr[i]
}
c <- true
}
func main() {
var arr [arrSize]uint64
go bigArray(arr)
<-c
}
Further Study
1. stack frame is not always required
2. stack allocation with large frame size.
3. MemStats.StackInuse
memStats := new(runtime.MemStats)
runtime.ReadMemStats(memStats)
Thank You.
Blog: https://yushuanhsieh.github.io/
Linkedin: https://www.linkedin.com/in/yu-shuan-hsieh-96994491/

More Related Content

What's hot

Protocol Buffers 入門
Protocol Buffers 入門Protocol Buffers 入門
Protocol Buffers 入門Yuichi Ito
 
第六回渋谷Java Java8のJVM監視を考える
第六回渋谷Java Java8のJVM監視を考える第六回渋谷Java Java8のJVM監視を考える
第六回渋谷Java Java8のJVM監視を考えるchonaso
 
DDD&Scalaで作られたプロダクトはその後どうなったか?(Current state of products made with DDD & Scala)
DDD&Scalaで作られたプロダクトはその後どうなったか?(Current state of products made with DDD & Scala)DDD&Scalaで作られたプロダクトはその後どうなったか?(Current state of products made with DDD & Scala)
DDD&Scalaで作られたプロダクトはその後どうなったか?(Current state of products made with DDD & Scala)MicroAd, Inc.(Engineer)
 
kube-system落としてみました
kube-system落としてみましたkube-system落としてみました
kube-system落としてみましたShuntaro Saiba
 
クラスローダーについて
クラスローダーについてクラスローダーについて
クラスローダーについてSuguru ARAKAWA
 
Planet-scale Data Ingestion Pipeline: Bigdam
Planet-scale Data Ingestion Pipeline: BigdamPlanet-scale Data Ingestion Pipeline: Bigdam
Planet-scale Data Ingestion Pipeline: BigdamSATOSHI TAGOMORI
 
Git을 조금 더 알아보자!
Git을 조금 더 알아보자!Git을 조금 더 알아보자!
Git을 조금 더 알아보자!Young Kim
 
async/await のしくみ
async/await のしくみasync/await のしくみ
async/await のしくみ信之 岩永
 
[수정본] 우아한 객체지향
[수정본] 우아한 객체지향[수정본] 우아한 객체지향
[수정본] 우아한 객체지향Young-Ho Cho
 
使ってみよう!JDK Flight Recorder
使ってみよう!JDK Flight Recorder使ってみよう!JDK Flight Recorder
使ってみよう!JDK Flight RecorderYoshiro Tokumasu
 
HashiCorp Vault 紹介
HashiCorp Vault 紹介HashiCorp Vault 紹介
HashiCorp Vault 紹介hashicorpjp
 
Concurrent Mark-Sweep Garbage Collection #jjug_ccc
Concurrent Mark-Sweep Garbage Collection #jjug_cccConcurrent Mark-Sweep Garbage Collection #jjug_ccc
Concurrent Mark-Sweep Garbage Collection #jjug_cccYuji Kubota
 

What's hot (20)

Java11へのマイグレーションガイド ~Apache Hadoopの事例~
Java11へのマイグレーションガイド ~Apache Hadoopの事例~Java11へのマイグレーションガイド ~Apache Hadoopの事例~
Java11へのマイグレーションガイド ~Apache Hadoopの事例~
 
Protocol Buffers 入門
Protocol Buffers 入門Protocol Buffers 入門
Protocol Buffers 入門
 
第六回渋谷Java Java8のJVM監視を考える
第六回渋谷Java Java8のJVM監視を考える第六回渋谷Java Java8のJVM監視を考える
第六回渋谷Java Java8のJVM監視を考える
 
Hadoop入門
Hadoop入門Hadoop入門
Hadoop入門
 
Concurrency With Go
Concurrency With GoConcurrency With Go
Concurrency With Go
 
gRPC入門
gRPC入門gRPC入門
gRPC入門
 
DDD&Scalaで作られたプロダクトはその後どうなったか?(Current state of products made with DDD & Scala)
DDD&Scalaで作られたプロダクトはその後どうなったか?(Current state of products made with DDD & Scala)DDD&Scalaで作られたプロダクトはその後どうなったか?(Current state of products made with DDD & Scala)
DDD&Scalaで作られたプロダクトはその後どうなったか?(Current state of products made with DDD & Scala)
 
kube-system落としてみました
kube-system落としてみましたkube-system落としてみました
kube-system落としてみました
 
pg_bigmを用いた全文検索のしくみ(後編)
pg_bigmを用いた全文検索のしくみ(後編)pg_bigmを用いた全文検索のしくみ(後編)
pg_bigmを用いた全文検索のしくみ(後編)
 
クラスローダーについて
クラスローダーについてクラスローダーについて
クラスローダーについて
 
Planet-scale Data Ingestion Pipeline: Bigdam
Planet-scale Data Ingestion Pipeline: BigdamPlanet-scale Data Ingestion Pipeline: Bigdam
Planet-scale Data Ingestion Pipeline: Bigdam
 
Git을 조금 더 알아보자!
Git을 조금 더 알아보자!Git을 조금 더 알아보자!
Git을 조금 더 알아보자!
 
async/await のしくみ
async/await のしくみasync/await のしくみ
async/await のしくみ
 
Goとテスト
GoとテストGoとテスト
Goとテスト
 
[수정본] 우아한 객체지향
[수정본] 우아한 객체지향[수정본] 우아한 객체지향
[수정본] 우아한 객체지향
 
使ってみよう!JDK Flight Recorder
使ってみよう!JDK Flight Recorder使ってみよう!JDK Flight Recorder
使ってみよう!JDK Flight Recorder
 
perfを使ったPostgreSQLの解析(前編)
perfを使ったPostgreSQLの解析(前編)perfを使ったPostgreSQLの解析(前編)
perfを使ったPostgreSQLの解析(前編)
 
HashiCorp Vault 紹介
HashiCorp Vault 紹介HashiCorp Vault 紹介
HashiCorp Vault 紹介
 
Paxos
PaxosPaxos
Paxos
 
Concurrent Mark-Sweep Garbage Collection #jjug_ccc
Concurrent Mark-Sweep Garbage Collection #jjug_cccConcurrent Mark-Sweep Garbage Collection #jjug_ccc
Concurrent Mark-Sweep Garbage Collection #jjug_ccc
 

Similar to Goroutine stack and local variable allocation in Go

Kernel Recipes 2014 - What I’m forgetting when designing a new userspace inte...
Kernel Recipes 2014 - What I’m forgetting when designing a new userspace inte...Kernel Recipes 2014 - What I’m forgetting when designing a new userspace inte...
Kernel Recipes 2014 - What I’m forgetting when designing a new userspace inte...Anne Nicolas
 
Devoxx France 2018 : Mes Applications en Production sur Kubernetes
Devoxx France 2018 : Mes Applications en Production sur KubernetesDevoxx France 2018 : Mes Applications en Production sur Kubernetes
Devoxx France 2018 : Mes Applications en Production sur KubernetesMichaël Morello
 
Mastering java in containers - MadridJUG
Mastering java in containers - MadridJUGMastering java in containers - MadridJUG
Mastering java in containers - MadridJUGJorge Morales
 
Java and Containers - Make it Awesome !
Java and Containers - Make it Awesome !Java and Containers - Make it Awesome !
Java and Containers - Make it Awesome !Dinakar Guniguntala
 
LCA14: LCA14-412: GPGPU on ARM SoC session
LCA14: LCA14-412: GPGPU on ARM SoC sessionLCA14: LCA14-412: GPGPU on ARM SoC session
LCA14: LCA14-412: GPGPU on ARM SoC sessionLinaro
 
Developing Realtime Data Pipelines With Apache Kafka
Developing Realtime Data Pipelines With Apache KafkaDeveloping Realtime Data Pipelines With Apache Kafka
Developing Realtime Data Pipelines With Apache KafkaJoe Stein
 
Developing Real-Time Data Pipelines with Apache Kafka
Developing Real-Time Data Pipelines with Apache KafkaDeveloping Real-Time Data Pipelines with Apache Kafka
Developing Real-Time Data Pipelines with Apache KafkaJoe Stein
 
Composing High-Performance Memory Allocators with Heap Layers
Composing High-Performance Memory Allocators with Heap LayersComposing High-Performance Memory Allocators with Heap Layers
Composing High-Performance Memory Allocators with Heap LayersEmery Berger
 
Advanced Namespaces and cgroups
Advanced Namespaces and cgroupsAdvanced Namespaces and cgroups
Advanced Namespaces and cgroupsKernel TLV
 
Investigation report on 64 bit support in Android Open Source Project
Investigation report on 64 bit support in Android Open Source ProjectInvestigation report on 64 bit support in Android Open Source Project
Investigation report on 64 bit support in Android Open Source Projecthidenorly
 
2713897 oracle-unix-oracle
2713897 oracle-unix-oracle2713897 oracle-unix-oracle
2713897 oracle-unix-oraclesivacse09
 
Virtual Bash! A Lunchtime Introduction to Kafka
Virtual Bash! A Lunchtime Introduction to KafkaVirtual Bash! A Lunchtime Introduction to Kafka
Virtual Bash! A Lunchtime Introduction to KafkaJason Bell
 
Java 어플리케이션 성능튜닝 Part1
Java 어플리케이션 성능튜닝 Part1Java 어플리케이션 성능튜닝 Part1
Java 어플리케이션 성능튜닝 Part1상욱 송
 
Check the version with fixes. Link in description
Check the version with fixes. Link in descriptionCheck the version with fixes. Link in description
Check the version with fixes. Link in descriptionPrzemyslaw Koltermann
 
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTMemory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTAkhilMishra50
 
lessons from managing a pulsar cluster
 lessons from managing a pulsar cluster lessons from managing a pulsar cluster
lessons from managing a pulsar clusterShivji Kumar Jha
 
Ece2013 Java Advanced Memorymanagement
Ece2013 Java Advanced MemorymanagementEce2013 Java Advanced Memorymanagement
Ece2013 Java Advanced Memorymanagementda152
 

Similar to Goroutine stack and local variable allocation in Go (20)

Kernel Recipes 2014 - What I’m forgetting when designing a new userspace inte...
Kernel Recipes 2014 - What I’m forgetting when designing a new userspace inte...Kernel Recipes 2014 - What I’m forgetting when designing a new userspace inte...
Kernel Recipes 2014 - What I’m forgetting when designing a new userspace inte...
 
Devoxx France 2018 : Mes Applications en Production sur Kubernetes
Devoxx France 2018 : Mes Applications en Production sur KubernetesDevoxx France 2018 : Mes Applications en Production sur Kubernetes
Devoxx France 2018 : Mes Applications en Production sur Kubernetes
 
Mastering java in containers - MadridJUG
Mastering java in containers - MadridJUGMastering java in containers - MadridJUG
Mastering java in containers - MadridJUG
 
jvm goes to big data
jvm goes to big datajvm goes to big data
jvm goes to big data
 
Java and Containers - Make it Awesome !
Java and Containers - Make it Awesome !Java and Containers - Make it Awesome !
Java and Containers - Make it Awesome !
 
LCA14: LCA14-412: GPGPU on ARM SoC session
LCA14: LCA14-412: GPGPU on ARM SoC sessionLCA14: LCA14-412: GPGPU on ARM SoC session
LCA14: LCA14-412: GPGPU on ARM SoC session
 
Developing Realtime Data Pipelines With Apache Kafka
Developing Realtime Data Pipelines With Apache KafkaDeveloping Realtime Data Pipelines With Apache Kafka
Developing Realtime Data Pipelines With Apache Kafka
 
Developing Real-Time Data Pipelines with Apache Kafka
Developing Real-Time Data Pipelines with Apache KafkaDeveloping Real-Time Data Pipelines with Apache Kafka
Developing Real-Time Data Pipelines with Apache Kafka
 
Composing High-Performance Memory Allocators with Heap Layers
Composing High-Performance Memory Allocators with Heap LayersComposing High-Performance Memory Allocators with Heap Layers
Composing High-Performance Memory Allocators with Heap Layers
 
Advanced Namespaces and cgroups
Advanced Namespaces and cgroupsAdvanced Namespaces and cgroups
Advanced Namespaces and cgroups
 
Investigation report on 64 bit support in Android Open Source Project
Investigation report on 64 bit support in Android Open Source ProjectInvestigation report on 64 bit support in Android Open Source Project
Investigation report on 64 bit support in Android Open Source Project
 
2713897 oracle-unix-oracle
2713897 oracle-unix-oracle2713897 oracle-unix-oracle
2713897 oracle-unix-oracle
 
Virtual Bash! A Lunchtime Introduction to Kafka
Virtual Bash! A Lunchtime Introduction to KafkaVirtual Bash! A Lunchtime Introduction to Kafka
Virtual Bash! A Lunchtime Introduction to Kafka
 
Java 어플리케이션 성능튜닝 Part1
Java 어플리케이션 성능튜닝 Part1Java 어플리케이션 성능튜닝 Part1
Java 어플리케이션 성능튜닝 Part1
 
Scope Stack Allocation
Scope Stack AllocationScope Stack Allocation
Scope Stack Allocation
 
Check the version with fixes. Link in description
Check the version with fixes. Link in descriptionCheck the version with fixes. Link in description
Check the version with fixes. Link in description
 
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPTMemory Allocation & Direct Memory Allocation in C & C++ Language PPT
Memory Allocation & Direct Memory Allocation in C & C++ Language PPT
 
Google File System
Google File SystemGoogle File System
Google File System
 
lessons from managing a pulsar cluster
 lessons from managing a pulsar cluster lessons from managing a pulsar cluster
lessons from managing a pulsar cluster
 
Ece2013 Java Advanced Memorymanagement
Ece2013 Java Advanced MemorymanagementEce2013 Java Advanced Memorymanagement
Ece2013 Java Advanced Memorymanagement
 

Recently uploaded

Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesThousandEyes
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Ramesh Iyer
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Alison B. Lowndes
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2DianaGray10
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...Product School
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Product School
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxAbida Shariff
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...Product School
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Product School
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1DianaGray10
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...Sri Ambati
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Frank van Harmelen
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsVlad Stirbu
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»QADay
 

Recently uploaded (20)

Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
 

Goroutine stack and local variable allocation in Go

  • 1. Goroutine stack and local variable allocation in Go Cherie Hsieh Golang Taiwan Community GDG Hsinchu Women Techmakers Community
  • 2. Outline 1. The Run-Time Stack 2. User Stack in C Program 3. User Stack in Go goroutine 4. Goroutine Stack allocation 5. System Stack and User Stack 6. Stack Growing Mechanism
  • 4. The Run-Time Stack In common Linux systems, each process/thread has two stacks: 1. Kernel stack (system calls, interrupt handlers) 2. User stack (execute user-space applications)
  • 5. The Run-Time Stack User Stack provides last-in, first out memory management for procedure-calling. source: https://www.mdpi.com/computers/computers-09-00048/article_deploy/html/images/computers-09-00048-g005.png
  • 6. User Stack in C Program
  • 7. User Stack in C Program The memory layout of a process with main thread. source: https://cseweb.ucsd.edu/classes/sp16/cse120-a/applications/ln/proccontext.jpg
  • 8. User Stack in C Program The memory layout of a process with multiple threads. source: https://cseweb.ucsd.edu/classes/sp16/cse120-a/applications/ln/thdcontext.jpg
  • 9. Stack Size in C Program Default user stack size of main thread: 8k Use ulimit cmd-tool to check or change the default stack size. execve() causes the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and data segments. cherie@coscup:~$ ulimit -a stack size (kbytes, -s) 8192 max user processes (-u) 59988
  • 10. Stack Size in C Program Create a new thread with a customized stack size using system call clone() The stack argument specifies the location of the stack used by the child threads. int clone(int (*fn)(void *), void *stack, int flags, void *arg, ... /* pid_t *parent_tid, void *tls, pid_t *child_tid */);
  • 11. User Stack in Go goroutine
  • 12. User Stack in Go goroutine Goroutine is lightweight, costing little more than the allocation of stack space. And the stacks start small, so they are cheap. /* source code: runtime/runtime2.go */ type stack struct { lo uintptr hi uintptr } type g struct { // Stack parameters. // stack describes the actual stack memory: [stack.lo, stack.hi). stack stack // ...more members }
  • 13. Stack Allocation in Go goroutine
  • 14. The Free List in stackCache and stackPool Four size classes of free list: 2k, 4k, 8k, and 16k. /* source code: runtime/malloc.go */ /* We want to cache 2KB, 4KB, 8KB, and 16KB stacks. Larger stacks */ /* will be allocated directly. */ /* Since FixedStack is different on different systems, we */ /* must vary NumStackOrders to keep the same maximum cached size. */ /* OS | FixedStack | NumStackOrders */ /* -----------------+------------+--------------- */ /* linux/darwin/bsd | 2KB | 4 */ /* windows/32 | 4KB | 3 */ /* windows/64 | 8KB | 2 */ /* plan9 | 4KB | 3 */ The size classes would be different due to different operating systems and architectures.
  • 15. The Free List in stackCache and stackPool Considering the cache line of each architecture, a cache line padding is added to item structure for performance optimization. /* Global pool of spans that have free stacks. */ /* Stacks are assigned an order according to size. */ /* There is a free list for each order. */ var stackpool [_NumStackOrders]struct { item stackpoolItem _ [cpu.CacheLinePadSize - unsafe.Sizeof(stackpoolItem{})%cpu.CacheLinePadSize]byte }
  • 17. System Stack in Go There are two stacks in Go 1. User stack Every non-dead G has a user stack associated with it, which is what user Go code executes on. 2. System stack Every M has a system stack associated with it (also known as the M's "g0") . System and signal stacks cannot grow, but are large enough to execute runtime and cgo code (8K). Code running on the system stack is implicitly non-preemptible and the garbage collector does not scan system stacks.
  • 18. System Stack in Go A main thread with a single goroutine
  • 19. User Stack Extension in Go goroutine
  • 20. User Stack Extension in Go goroutine A goroutines stack starts small, so they are cheap, and grow by allocating (and freeing) heap storage as required. const ( StackSmall = 128 StackBig = 4096 ) type g struct { stack stack stackguard0 uintptr // ...more members }
  • 21. User Stack Extension in Go goroutine Stack extension check: 1. frame size <= StackSmall Comparing the stack pointer with the stack guard, then extend the stack if the stack pointer is below the stack guard. 2. StackSmall < frame size <= StackBig Moving down the stack pointer and comparing the stack pointer with the stack guard. Extending the stack if the stack pointer is below the stack guard. 3. frame size > StackBig Always does stack extension.
  • 22. User Stack Extension in Go goroutine
  • 23. The Process of Stack Growth How stack growth works? 1. record the stack map (global) for pointer checking. 2. adjust the pointer value according to adjustInfo.
  • 25. Stack Overflow package main const ( size = 2048 maxDepth = 5000000 ) func bigFrame(args [size]uint64, depth int) { if depth < maxDepth { bigFrame(args, depth+1) } } func main() { var args [size]uint64 bigFrame(args, 0) }
  • 27. Stack Overflow Default Maxstacksize /* source code: runtime/proc.go */ if sys.PtrSize == 8 { maxstacksize = 1000000000 } else { maxstacksize = 250000000 }
  • 28. Goroutine Initialization with > 2k function args package main /* change arrSize to 260 and check the result */ const arrSize = 4 var c = make(chan bool) func bigArray(arr [arrSize]uint64) { var sum uint64 = 1 for i := 0; i < arrSize; i++ { sum = sum + arr[i] } c <- true } func main() { var arr [arrSize]uint64 go bigArray(arr) <-c }
  • 29. Further Study 1. stack frame is not always required 2. stack allocation with large frame size. 3. MemStats.StackInuse memStats := new(runtime.MemStats) runtime.ReadMemStats(memStats)
  • 30. Thank You. Blog: https://yushuanhsieh.github.io/ Linkedin: https://www.linkedin.com/in/yu-shuan-hsieh-96994491/