liveBook · Manning

Global queue

As explained in 100 Go Mistakes and How to Avoid Them

A global queue is a scheduler queue shared among processor contexts for holding runnable goroutines.

A global queue is a scheduler queue shared among all Ps for goroutines that are runnable but cannot execute immediately. It complements one local queue per P. section 8.2.1

When selecting runnable goroutines, the scheduler:

  • Checks the global queue every sixty-first execution. section 8.2.1
  • Checks the current P’s local queue when no global-queue goroutine is selected. section 8.2.1
  • Can steal goroutines from other Ps when both queues are empty, allowing an idle processor to find work. section 8.2.1
runtime.schedule() {
    // Only 1/61 of the time, check the global runnable queue for a G.
    // If not found, check the local queue.
    // If not found,
    //     Try to steal from other Ps.
    //     If not, check the global runnable queue.
    //     If not found, poll network.
}

Use the scheduling pseudocode to trace global-queue checks, local-queue checks, work stealing, and network polling. section 8.2.1

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Traces queue-selection order and work stealing, but does not detail the global queue’s internal implementation.
Examples
Illustrates six pending goroutines across local and global queues on a four-core machine, with one P idle.
What it assumes
Assumes goroutine lifecycle states and the scheduler entities P, M, and G.
What you can do after
Evaluate scheduler behavior by tracing global-queue checks, local-queue fallback, and work stealing.
How it connects
Connects goroutines with Ps, Ms, local queues, GOMAXPROCS, the scheduler, and work stealing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.