When Go Scheduler Creates New M and P
In the Go runtime, goroutines, operating system threads (OS threads), and contexts/processors (M and P) cooperate to manage concurrency. Understanding when new M and P entities are created is crucial for efficient performance.
M and P Creation
-
M (Machine): An M represents an OS thread and is created when a goroutine is scheduled to run. The number of M is initially set by the runtime.GOMAXPROCS environment variable.
-
P (Processor): A P is a processor that executes goroutines. Each P is associated with an M. When an M blocks, a new M may be created to execute goroutines on that P.
Scenario Analysis
In your code example, you create multiple goroutines that perform blocking operations. In this scenario:
- The first batch of goroutines will be scheduled on the existing P instances.
- If all P's local queues are full, new M will be created to handle the overflow goroutines.
- However, since your goroutines perform blocking operations within the go func(), the M associated with blocked goroutines will be removed from the P and put on the idle thread pool.
- New M will be created to replace the blocked M and continue executing goroutines on that P.
Conclusion
In summary, the Go scheduler creates a new M when a goroutine is scheduled to run and a free OS thread is unavailable. A new P is created when the local queue of an existing P is full. However, in the case of blocking goroutines, the number of M created can exceed the number of virtual cores, as each blocking operation requires a separate M.
Additional Resources
- [GOMAXPROCS in the Go Blog](https://blog.golang.org/GOMAXPROCS)
- [Go Goroutine, OS Thread, and CPU Management](https://godoc.org/runtime/debug#SetMaxThreads)
- [GMP Basics](https://www.programmersought.com/article/79557885527/)
The above is the detailed content of When Does the Go Scheduler Create New M and P?. For more information, please follow other related articles on the PHP Chinese website!