g0 traits (for every M has g0)
- Fix and larger stack size(ordinary goroutin occupies 2kb at start).
- g0's stack usually doesn't grow.
- responsible for goroutine creation.
- responsible for defer function allocation.
- GC operations. Including STW, mark and sweep operations.
- Stack growth for running goroutins.
- Schedule goroutine to run. (each goroutine end with calling runtime.goexit() which notify g0 it's finished) Or goroutine can call runtime.Goexit() manually)
- Maintain 2 goroutine queues, one with recycled goroutines with an allocated stack(max 2kb size), one with recycled goroutines with empty stack)
- There are 2 global goroutine queues as well protected by locks.
Each local goroutine queue for P has a maximum capacity of 256, and any new incoming goroutine is pushed to the global queue after local queue is full.
Each P also maintains a queue(64 size) for freed goroutines.(goroutines are recycleable )
Each P also maintains a queue(64 size) for freed goroutines.(goroutines are recycleable )
Work-stealing
When a processor does not have any work, it applies the following rules until one can be satisfied:- pull work from the local queue
- pull work from the global queue
- pull work from network poller
- steal work from the other P’s local queues
Threads can be blocked on system calls and the number of blocked threads is not limited in Go runtime.
Affinity limitation
P's Local goroutin queue will be used for all operations expect system calls
such as blocking operations on channels and selects, waiting on timers and locks.
Two features could restrict the affinity between a goroutine and a thread:
- Work-stealing.
When a processor P does not have enough work in its local queue, it will steal goroutines from others P if the global queue and the network poller are empty. When stolen, the goroutines will then run on another thread. - System calls.
When a syscall occurs (e.g. files operations, http calls, database operations, etc.), Go moves the running OS thread in a blocking mode, letting a new thread processing the local queue on the current P.
However, with goroutins using the same channel will be grouped into same P during scheduling. And for those waiting for channel will be scheduled with high priority then other goroutin to run next.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.