Go调度器是谁填充P的本地运行队列的?

huangapple go评论92阅读模式
英文:

Go Scheduler who fills the local runqueue of P?

问题

看这个来自morsmachine.dk/go-scheduler的著名图片。

Go调度器是谁填充P的本地运行队列的?

灰色列表是P的本地运行队列。如果这个队列为空,它们将从全局运行队列中获取goroutine。

问题是,是谁填充P的本地运行队列

调度器,没有同步,还是每个P自己填充(互斥锁)?

附注:文章中省略了这个信息。

英文:

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

Go调度器是谁填充P的本地运行队列的?

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).

huangapple
  • 本文由 发表于 2015年9月25日 00:11:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/32766160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定