📖 Go Runtime Mastery
M:N Scheduling Model
Components
- G (Goroutine): User-space thread, ~2KB initial stack
- M (Machine): OS thread, executes goroutines
- P (Processor): Logical processor, holds runnable queue
Scheduling Mechanics
- GOMAXPROCS: Number of Ps (default = CPU cores)
- Local run queue: Per-P, 256 goroutines max
- Global run queue: Overflow, load balancing
- Work-stealing: Idle P steals from busy P's queue
Blocking Behavior
- Syscall blocking: M blocks, P handed off to another M
- Channel blocking: G parked, M continues with other Gs
- Network I/O: Netpoller (epoll/kqueue), non-blocking
Stack Management
- Initial size: 2KB (tiny!)
- Growth: Copy to larger stack, update pointers