The pieces of the scheduler
● Status (Idle, running, syscall, gcstop)
● Current M
● Runnable goroutines
● Free gorotuines
● Other metadata
P (the Processor)
src/runtime/runtime2.go:609
Slide 6
Slide 6 text
The pieces of the scheduler
● Current goroutine
● Current P
● Other metadata
M (the Machine)
src/runtime/runtime2.go:526
Slide 7
Slide 7 text
The pieces of the scheduler
● Idle Ms
● Idle Ps
● Global runnable goroutines
● Global free goroutines
● Other metadata
Sched (the Scheduler)
src/runtime/runtime2.go:766
Slide 8
Slide 8 text
The pieces of the scheduler
● Stack (2048 bytes)
● Program counter
● Status
● Current M
● Wait reason
● Other metadata.
G (the Goroutine)
src/runtime/runtime2.go:407
Slide 9
Slide 9 text
All Ms
All Ps
All Goroutines
The whole picture
src/runtime/runtime2.go
Slide 10
Slide 10 text
The birth of a goroutine
Slide 11
Slide 11 text
The goroutine lifecycle
Idle Dead Runnable
Dead Runnable
src/runtime/proc.go:4241 (newproc)
Slide 12
Slide 12 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 13
Slide 13 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 14
Slide 14 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 15
Slide 15 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 16
Slide 16 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 17
Slide 17 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 18
Slide 18 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 19
Slide 19 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 20
Slide 20 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 21
Slide 21 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 22
Slide 22 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 23
Slide 23 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 24
Slide 24 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 25
Slide 25 text
All Ms
All Ps
All Goroutines
The whole picture
Slide 26
Slide 26 text
The life of a goroutine
Slide 27
Slide 27 text
The life of a goroutine
Runnable Running
Waiting Preempted Copystack
Syscall
Slide 28
Slide 28 text
Runnable to running
● Scheduler runs
● Find a goroutine to run
● Assigns it to the M
● Mark as Running
● Executes the code
src/runtime/proc.go:3331 (schedule)
Slide 29
Slide 29 text
Running to waiting
● Park itself
● Detach from M
● Run the scheduler
src/runtime/proc.go:364 (gopark)
Running to syscall and to running or runnable
● On every syscall
● entersyscall is executed (moving to Sycall state)
● The syscall is executed
● exitsyscall is executed (moving back to Running/Runnable)
src/runtime/proc.go:3825 (entersyscall)
src/runtime/proc.go:3920 (exitsyscall)
Slide 33
Slide 33 text
Running to copystack and back
● More stack needed
● Change Running to Copystack
● Grow the stack
● Change back to Running
src/runtime/stack.go:964 (newstack)
Slide 34
Slide 34 text
Waiting to runnable
● goready is called
● Is added to the queue
● Try to get a P
src/runtime/proc.go:390 (goready)
Slide 35
Slide 35 text
Waiting to runnable
● Reactivate a list of goroutines
● Mark all of them as runnable
● Wakes up all the Ps needed
● Add them to the queues
src/runtime/proc.go:3255 (injectglist)
Slide 36
Slide 36 text
Waiting to runnable
● Change to Waiting
● Donʼt need to wait
● Change to Runnable
● And then to Running rights away
src/runtime/proc.go:3487 (park_m)
Slide 37
Slide 37 text
Waiting to runnable
● Finding a goroutine
● Check in the netpoll
● Or wake up tasks for mark assist
src/runtime/proc.go:2672 (findRunnable)
Slide 38
Slide 38 text
Running to preempt, waiting and runnable
● Preempt flag is set
● Change to preempted
● Next GC change to waiting
● Do the GC scan
● Change to Runnable
● Add it back to the queue
src/runtime/preempt.go:104 (suspendG)
src/runtime/preempt.go:257 (resumeG)
Slide 39
Slide 39 text
The goroutine lifecycle
Runnable Running
Waiting Preempted Copystack
Syscall
Slide 40
Slide 40 text
Examples
Slide 41
Slide 41 text
channel
The channel example
data
src/runtime/chan.go:160
Slide 42
Slide 42 text
The channel example
channel
data
src/runtime/chan.go:160
Slide 43
Slide 43 text
The channel example
channel
data
src/runtime/chan.go:457
Slide 44
Slide 44 text
The channel example
channel
data
ready
data
src/runtime/chan.go:615
Slide 45
Slide 45 text
channel
The channel example
src/runtime/chan.go:615
Slide 46
Slide 46 text
channel
data
channel
The channel example
data
channel
data
channel
data
ready
data
channel
Slide 47
Slide 47 text
create
The waitgroup example
add 3
Spawn
Wait Group
src/sync/waitgroup.go:43
Slide 48
Slide 48 text
wait
The waitgroup example
Wait Group
src/sync/waitgroup.go:91
Slide 49
Slide 49 text
The waitgroup example
Wait Group
Slide 50
Slide 50 text
The waitgroup example
Wait Group
Slide 51
Slide 51 text
The waitgroup example
Wait Group
Ready
Done
src/sync/waitgroup.go:43
Slide 52
Slide 52 text
The waitgroup example
Wait Group
Slide 53
Slide 53 text
Wait Group Wait Group
Wait Group
create
The waitgroup example
Add 3
Spawn
wait
Ready Done
Wait Group
Slide 54
Slide 54 text
The death of a goroutine
Slide 55
Slide 55 text
The death of a goroutine
● A goroutine finish it work
● Change state to Dead
● Set most of the goroutine values to zero
● Disconnect the goroutine from the M
● Add the goroutine to the free list of the P
● Call the scheduler
src/runtime/proc.go:3616 (goexit0)
Slide 56
Slide 56 text
The whole life
Slide 57
Slide 57 text
The whole picture
All Ms
All Ps
All Goroutines
Slide 58
Slide 58 text
The whole lifecycle
Runnable Running
Waiting Preempted Copystack
Syscall
Dead
Idle
Slide 59
Slide 59 text
Summary
Slide 60
Slide 60 text
Illustrations
● CC-BY
● Created by Laura Pareja
● http://laurapareja.com
Slide 61
Slide 61 text
A gift from Mattermost
Slide 62
Slide 62 text
What is missing?
● The garbage collector
● The netpoll
● Cgo
● Mark assist
● Sysmon
Slide 63
Slide 63 text
References
● The Go source code
● Illustrated Tales of Go Runtime Scheduler:
https://www.youtube.com/watch?v=KxOwt6z0FvY
● Scheduling In Go:
https://www.ardanlabs.com/blog/2018/08/scheduling-in-g
o-part1.html
Slide 64
Slide 64 text
Conclusions
Slide 65
Slide 65 text
No content
Slide 66
Slide 66 text
Let’s keep in touch
jespinog
jesus-espino
jespino
Slide 67
Slide 67 text
Let’s keep in touch
https://forms.gle/rAFKZwVM4U26JPyn6