Go's GMP Model: The Core of Goroutine Scheduling
Go's scheduler leverages a G-M-P model, mapping many Goroutines (G) onto a smaller number of OS Threads (M) via logical Processors (P), each with a local run queue, to efficiently manage concurrency.
The top 3
- Go's Scheduler Evolution: From Global Lock to GMP: Before Go 1.1, the scheduler used a G-M model with a single global run queue protected by a global mutex, leading to scalability bottlenecks and contention on multi-core systems. The GMP model, introduced to address these issues, decentralized scheduling with per-P local queues, drastically reducing lock contention.
- GMP's Core Trio: G, M, and P's Distinct Roles: The GMP model defines three key components: G (Goroutine) as the lightweight unit of work, M (Machine) as the OS thread executing code, and P (Processor) as the logical context managing a local run queue of Gs. Crucially, P does not execute code itself but provides the resources and scheduling for an M to run Gs.
- Work-Stealing: GMP's Dynamic Load Balancing Strategy: The GMP model employs a 'work-stealing' mechanism where an idle P (and its bound M) will attempt to steal half of the runnable goroutines from another busy P's local queue. This dynamic load balancing ensures that CPU cores remain highly utilized, preventing starvation and maximizing throughput without central coordination.
Sources
Open the full topic