Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Scheduler who fills the local runqueue of P?

Tags:

go

scheduler

Look at this well known image taken from morsmachine.dk/go-scheduler

enter image description here

The gray lists are local runqueues of P. If this queue get empty they will be filled with go routines from the global runqueue.

The Question is, who fills the local runqueue of P?

Scheduler, without synchronisation or every P do it for himself (mutex)?

P.S. The article omits this information.

like image 504
dieter Avatar asked Sep 24 '15 16:09

dieter


1 Answers

All of this is taken from golang.org/src/runtime/proc.go:

Function schedule (the scheduler) calls findrunnable which attempts to steal a G from another P. If that fails, it'll return a G from the global run queue. That G is then executed on the "current" M.

In addition, schedule checks the global run queue occasionally "for fairness":

// Check the global runnable queue once in a while to ensure fairness.
// Otherwise two goroutines can completely occupy the local runqueue
// by constantly respawning each other.

In all of that, only one lock is involved, which is lock(&sched.lock).

like image 129
thwd Avatar answered Sep 29 '22 11:09

thwd