Go创建协程的过程

参考解析

题目来源:虾皮

答案:

想要启动一个新的 Goroutine 来执行任务时,我们需要使用 Go 语言的 go 关键字,编译器会通过 cmd/compile/internal/gc.state.stmtcmd/compile/internal/gc.state.call 两个方法将该关键字转换成 runtime.newproc 函数调用:

  1. func (s *state) call(n *Node, k callKind) *ssa.Value {
  2. if k == callDeferStack {
  3. ...
  4. } else {
  5. switch {
  6. case k == callGo:
  7. call = s.newValue1A(ssa.OpStaticCall, types.TypeMem, newproc, s.mem())
  8. default:
  9. }
  10. }
  11. ...
  12. }

runtime.newproc 的入参是参数大小和表示函数的指针 funcval,它会获取 Goroutine 以及调用方的程序计数器,然后调用 runtime.newproc1 函数获取新的 Goroutine 结构体、将其加入处理器的运行队列并在满足条件时调用 runtime.wakep 唤醒新的处理执行 Goroutine:

  1. func newproc(siz int32, fn *funcval) {
  2. argp := add(unsafe.Pointer(&fn), sys.PtrSize)
  3. gp := getg()
  4. pc := getcallerpc()
  5. systemstack(func() {
  6. newg := newproc1(fn, argp, siz, gp, pc)
  7. _p_ := getg().m.p.ptr()
  8. runqput(_p_, newg, true)
  9. if mainStarted {
  10. wakep()
  11. }
  12. })
  13. }

runtime.newproc1 会根据传入参数初始化一个 g 结构体,我们可以将该函数分成以下几个部分介绍它的实现:

  1. 获取或者创建新的 Goroutine 结构体;
  2. 将传入的参数移到 Goroutine 的栈上;
  3. 更新 Goroutine 调度相关的属性;

首先是 Goroutine 结构体的创建过程:

  1. func newproc1(fn *funcval, argp unsafe.Pointer, narg int32, callergp *g, callerpc uintptr) *g {
  2. _g_ := getg()
  3. siz := narg
  4. siz = (siz + 7) &^ 7
  5. _p_ := _g_.m.p.ptr()
  6. newg := gfget(_p_)
  7. if newg == nil {
  8. newg = malg(_StackMin)
  9. casgstatus(newg, _Gidle, _Gdead)
  10. allgadd(newg)
  11. }
  12. ...

上述代码会先从处理器的 gFree 列表中查找空闲的 Goroutine,如果不存在空闲的 Goroutine,会通过 runtime.malg 创建一个栈大小足够的新结构体。

接下来,我们会调用 runtime.memmovefn 函数的所有参数拷贝到栈上,argpnarg 分别是参数的内存空间和大小,我们在该方法中会将参数对应的内存空间整块拷贝到栈上:

  1. ...
  2. totalSize := 4*sys.RegSize + uintptr(siz) + sys.MinFrameSize
  3. totalSize += -totalSize & (sys.SpAlign - 1)
  4. sp := newg.stack.hi - totalSize
  5. spArg := sp
  6. if narg > 0 {
  7. memmove(unsafe.Pointer(spArg), argp, uintptr(narg))
  8. }
  9. ...

拷贝了栈上的参数之后,runtime.newproc1 会设置新的 Goroutine 结构体的参数,包括栈指针、程序计数器并更新其状态到 _Grunnable 并返回:

  1. ...
  2. memclrNoHeapPointers(unsafe.Pointer(&newg.sched), unsafe.Sizeof(newg.sched))
  3. newg.sched.sp = sp
  4. newg.stktopsp = sp
  5. newg.sched.pc = funcPC(goexit) + sys.PCQuantum
  6. newg.sched.g = guintptr(unsafe.Pointer(newg))
  7. gostartcallfn(&newg.sched, fn)
  8. newg.gopc = callerpc
  9. newg.startpc = fn.fn
  10. casgstatus(newg, _Gdead, _Grunnable)
  11. newg.goid = int64(_p_.goidcache)
  12. _p_.goidcache++
  13. return newg
  14. }

我们在分析 runtime.newproc 的过程中,保留了主干省略了用于获取结构体的 runtime.gfgetruntime.malg、将 Goroutine 加入运行队列的 runtime.runqput 以及设置调度信息的过程,下面会依次分析这些函数。

初始化结构体

runtime.gfget 通过两种不同的方式获取新的 runtime.g

  1. 从 Goroutine 所在处理器的 gFree 列表或者调度器的 sched.gFree 列表中获取 runtime.g
  2. 调用 runtime.malg 生成一个新的 runtime.g 并将结构体追加到全局的 Goroutine 列表 allgs 中。

golang-newproc-get-goroutine

runtime.gfget 中包含两部分逻辑,它会根据处理器中 gFree 列表中 Goroutine 的数量做出不同的决策:

  1. 当处理器的 Goroutine 列表为空时,会将调度器持有的空闲 Goroutine 转移到当前处理器上,直到 gFree 列表中的 Goroutine 数量达到 32;
  2. 当处理器的 Goroutine 数量充足时,会从列表头部返回一个新的 Goroutine;
  1. func gfget(_p_ *p) *g {
  2. retry:
  3. if _p_.gFree.empty() && (!sched.gFree.stack.empty() || !sched.gFree.noStack.empty()) {
  4. for _p_.gFree.n < 32 {
  5. gp := sched.gFree.stack.pop()
  6. if gp == nil {
  7. gp = sched.gFree.noStack.pop()
  8. if gp == nil {
  9. break
  10. }
  11. }
  12. _p_.gFree.push(gp)
  13. }
  14. goto retry
  15. }
  16. gp := _p_.gFree.pop()
  17. if gp == nil {
  18. return nil
  19. }
  20. return gp
  21. }

当调度器的 gFree 和处理器的 gFree 列表都不存在结构体时,运行时会调用 runtime.malg 初始化新的 runtime.g 结构,如果申请的堆栈大小大于 0,这里会通过 runtime.stackalloc 分配 2KB 的栈空间:

  1. func malg(stacksize int32) *g {
  2. newg := new(g)
  3. if stacksize >= 0 {
  4. stacksize = round2(_StackSystem + stacksize)
  5. newg.stack = stackalloc(uint32(stacksize))
  6. newg.stackguard0 = newg.stack.lo + _StackGuard
  7. newg.stackguard1 = ^uintptr(0)
  8. }
  9. return newg
  10. }

runtime.malg 返回的 Goroutine 会存储到全局变量 allgs 中。

简单总结一下,runtime.newproc1 会从处理器或者调度器的缓存中获取新的结构体,也可以调用 runtime.malg 函数创建。

运行队列

runtime.runqput 会将 Goroutine 放到运行队列上,这既可能是全局的运行队列,也可能是处理器本地的运行队列:

  1. func runqput(_p_ *p, gp *g, next bool) {
  2. if next {
  3. retryNext:
  4. oldnext := _p_.runnext
  5. if !_p_.runnext.cas(oldnext, guintptr(unsafe.Pointer(gp))) {
  6. goto retryNext
  7. }
  8. if oldnext == 0 {
  9. return
  10. }
  11. gp = oldnext.ptr()
  12. }
  13. retry:
  14. h := atomic.LoadAcq(&_p_.runqhead)
  15. t := _p_.runqtail
  16. if t-h < uint32(len(_p_.runq)) {
  17. _p_.runq[t%uint32(len(_p_.runq))].set(gp)
  18. atomic.StoreRel(&_p_.runqtail, t+1)
  19. return
  20. }
  21. if runqputslow(_p_, gp, h, t) {
  22. return
  23. }
  24. goto retry
  25. }
  1. nexttrue 时,将 Goroutine 设置到处理器的 runnext 作为下一个处理器执行的任务;
  2. nextfalse 并且本地运行队列还有剩余空间时,将 Goroutine 加入处理器持有的本地运行队列;
  3. 当处理器的本地运行队列已经没有剩余空间时就会把本地队列中的一部分 Goroutine 和待加入的 Goroutine 通过 runtime.runqputslow 添加到调度器持有的全局运行队列上;

处理器本地的运行队列是一个使用数组构成的环形链表,它最多可以存储 256 个待执行任务。

golang-runnable-queue

简单总结一下,Go 语言有两个运行队列,其中一个是处理器本地的运行队列,另一个是调度器持有的全局运行队列,只有在本地运行队列没有剩余空间时才会使用全局队列。

调度信息

运行时创建 Goroutine 时会通过下面的代码设置调度相关的信息,前两行代码会分别将程序计数器和 Goroutine 设置成 runtime.goexit 和新创建 Goroutine 运行的函数:

  1. ...
  2. newg.sched.pc = funcPC(goexit) + sys.PCQuantum
  3. newg.sched.g = guintptr(unsafe.Pointer(newg))
  4. gostartcallfn(&newg.sched, fn)
  5. ...

上述调度信息 sched 不是初始化后的 Goroutine 的最终结果,它还需要经过 runtime.gostartcallfnruntime.gostartcall 的处理:

  1. func gostartcallfn(gobuf *gobuf, fv *funcval) {
  2. gostartcall(gobuf, unsafe.Pointer(fv.fn), unsafe.Pointer(fv))
  3. }
  4. func gostartcall(buf *gobuf, fn, ctxt unsafe.Pointer) {
  5. sp := buf.sp
  6. if sys.RegSize > sys.PtrSize {
  7. sp -= sys.PtrSize
  8. *(*uintptr)(unsafe.Pointer(sp)) = 0
  9. }
  10. sp -= sys.PtrSize
  11. *(*uintptr)(unsafe.Pointer(sp)) = buf.pc
  12. buf.sp = sp
  13. buf.pc = uintptr(fn)
  14. buf.ctxt = ctxt
  15. }

参考资料