Thanks to the Go language’s excellent runtime scheduling system, developers can easily develop concurrent programs even if they have no experience in multi-threaded programming.
The core of the scheduling system is the design of the GMP, which should be seen by all readers who want to understand the Go language design in depth. However, when learning through blogs or source code, if you can’t combine it with real code, you may not understand it well enough.
This article introduces a way to visualize the Go runtime scheduling process by using the GODEBUG tool to run the actual code.
Scheduling in a Nutshell
First, let’s start with a brief review of the Go scheduling system by parsing the snapshot state through a schematic of the scheduling snapshot at a point in the program.
As shown above, we have set GOMAXPROCS=2, i.e. 2 processors.
At the current moment, P0 and P1 are hosting OS threads M1 and M4, which are executing code from G8 and G17, respectively; P0’s LRQ (Local Run Queue) has 3 Gs queued up, while P1’s LRQ has no waiting Gs; GRQ (Global Run Queue) has 5 Gs.
The network poller Net Poller has a G9 caught in an asynchronous network call; M2 is blocking due to some synchronous system call from G11; M3 is idle and ready to be used when M1 or M4 is blocked.
Since P1’s LRQ has no more waiting G, when G17 is scheduled, it will perform Wrok Stealing, with stealing sources from other processors P’s LRQ (in this case P1’s LRQ), GRQ and Net Poller, as specified in the
Enabling the GODEBUG utility is as simple as setting the environment variable GODEBUG. It allows Go programs to output debugging information during runtime and to visualize details such as scheduler or garbage collection based on parameter configuration.
A detailed description of GODEBUG can be found in the source
In this article we are interested in debugging the scheduler, so we will only use the two parameters of GODEBUG, schedtrace and scheddetail.
- schedtrace=n: sets the runtime to output a line of scheduler summary information every n milliseconds.
- scheddetail: output more detailed scheduler information.
The sample code we use is as follows.
The code is relatively simple. We start 20 CPU-intensive G tasks, which are constrained by the WaitGroup. The program will only end execution when all the compute tasks of G have completed their respective accumulation.
schedtrace Scheduling Summary Output
Below, we set GODEBUG=schedtrace=1000, which means that the program’s scheduling summary is output once every 1s.
- SCHED: represents the running time between program start and when the current line is output, this output interval is affected by the schedtrace value.
- gomaxprocs: the GOMAXPROCS value, here we set it to 4.
- idleprocs: the number of idle P’s.
- threads: the number of threads managed at runtime.
- spinningthreads: spin threads, the number of threads in the “spin” state (to avoid frequent thread creation and destruction).
- idlethreads: number of idle threads.
- runqueue: number of G’s in the global queue GRQ.
- [2 1 2 1]: the number of Gs in the local queue LRQ representing 4 P’s is 2, 1, 2, 1 respectively.
scheddetail scheduling detail output
When we want to see more detailed scheduling information, we need to add the scheddetail parameter.
When the scheddetail parameter is added, the output message not only contains a one-line overview of SCHED, but also a detailed description of the status of the three GPM entities.
status: the running status of P. See the source code
runtime/runtime2.gofor a detailed classification and description.
- schedtick: The number of times P has been scheduled, added up with each scheduling action.
- syscalltick: Accumulated with each system call behavior, representing the number of system calls of P.
- m: M number of the binding, e.g. at SCHED 1000ms, m=2 for P0, and p=0 for M2.
- runqsize: the number of G’s for LRQ.
- gfreecnt: the number whose status is Gdead, i.e. status = 6.
- timerslen: the number of timers.
- p: P-number of the binding.
- curg: the G number of the code currently being executed on M.
- mallocing: whether there is an allocating memory operation in progress.
- throwing: if or not an exception is being thrown.
- preemptoff: if preemptoff ! = “”, then keep curg running on this M.
- locks: the number of locks for M.
- dying: the dying value of M, which has four handling cases of 0, 1, 2 and other values.
- spinning: if or not it is in self-selected state.
- blocked: if or not it is blocked.
- lockedg: corresponds to lockedm of G. They are of type uintptr and record M that is not tracked by the garbage collector versus G that bypasses the write barrier.
- status: similar to P’s status, see also the source
runtime/runtime2.gofor a detailed classification and description; if status==Gwaiting, i.e., if status is 4, it will also output the specific reason for waiting in parentheses.
- m: the M number of the binding, if its value is -1, it means no binding.
- lockedm: corresponds to lockedg in M.
Visual Scheduling Snapshot
Once you understand the meaning of the above indicators. To make the plotting simple, we set GOMAXPROCS to 2 and select the first second of the output for visual analysis.
A snapshot of the scheduling situation at that moment is shown below.
We can get the specific runs of P, M, and G based on the details. Note, however, that one thing we cannot be sure of is which specific Gs are waiting in the queue for each P’s LRQ and GRQ, but this does not affect the big picture (so the LRQs and GRQs in the figure above may not be the actual G numbers).
This article has shown that by adding the environment variable GODEBUG, we can easily view the scheduling of Go programs without making any code changes or adding additional plugins.
Readers who want to understand Go’s GMP and scheduling system more intuitively may want to give it a try.