Understanding the Go runtime's scheduling model is crucial for optimizing application performance. One key aspect of this model is the allocation of M (machines) and P (processors).
When Are M and P Created?
M processors are created when:
P processors are created when all the local runqueues are full one goroutine is running its neighbor goroutines ready to run are put in a global queue and it contains a single p.
Blocking Tasks and P Reuse
In the provided test code, the goroutines perform blocking database operations. Blocking tasks remove M processors from P processors.
In this case, creating new M processors is necessary to handle the blocked goroutines. The initial 8 (number of virtual cores) M processors will not be sufficient for the second batch. New M processors will be allocated as needed.
Additional Resources
The above is the detailed content of When and Why Does the Go Scheduler Allocate New M and P Processors?. For more information, please follow other related articles on the PHP Chinese website!