英文:
Go Scheduler who fills the local runqueue of P?
问题
看这个来自morsmachine.dk/go-scheduler的著名图片。
灰色列表是P的本地运行队列。如果这个队列为空,它们将从全局运行队列中获取goroutine。
问题是,是谁填充P的本地运行队列?
调度器,没有同步,还是每个P自己填充(互斥锁)?
附注:文章中省略了这个信息。
英文:
Look at this well known image taken from morsmachine.dk/go-scheduler
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.
答案1
得分: 5
这些内容来自golang.org/src/runtime/proc.go:
函数schedule
(调度器)调用findrunnable
函数,该函数尝试从另一个P
中窃取一个G
。如果失败,它将从全局运行队列返回一个G
。然后,在“当前”的M
上执行该G
。
此外,schedule
偶尔会检查全局运行队列以确保“公平性”:
// 偶尔检查全局可运行队列以确保公平性。
// 否则,两个goroutine可以通过不断地重生对方来完全占用本地运行队列。
在所有这些过程中,只涉及一个锁,即lock(&sched.lock)
。
英文:
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)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论