liveBook · Manning

Local queue

As explained in 100 Go Mistakes and How to Avoid Them

A local queue is a scheduler queue associated with one processor context for holding runnable goroutines.

A local queue is a runnable-goroutine queue attached to one P, the scheduler’s logical processor context. It complements the global queue when runnable goroutines cannot execute immediately. section 8.2.1

When a processor needs another goroutine, scheduling proceeds through these choices:

  • Check the global runnable queue on roughly one out of every 61 executions. section 8.2.1
  • Check the processor’s local queue when the global queue provides no goroutine. section 8.2.1
  • Steal runnable goroutines from another processor’s local queue when both queues are empty. section 8.2.1
Figure 8.6 An example of the current state of a Go application executed on a four-core machine. Goroutines that aren’t in an executing state are either runnable (pending being executed) or waiting (pending a blocking operation).

Use the scheduler-state figure to relate Ps, Ms, Gs, local queues, the global queue, and idle capacity. 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 local-queue checks, work stealing, global-queue fallback, 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 and work stealing but stops short of implementation-level queue mechanics.
Examples
Uses a four-core scheduling diagram and runtime scheduling pseudocode to illustrate queue selection.
What it assumes
Assumes goroutine lifecycle terminology and introduces their relationship with Ps, Ms, local queues, and the global queue.
What you can do after
Evaluate scheduler states by tracing runnable goroutines through local queues, the global queue, and work stealing.
How it connects
Connects local queues with goroutines, global queues, Ps, Ms, GOMAXPROCS, and work stealing.

Also explained in

Compare how 2 books explain local queue →

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.