goroutine的一点东西

news2024/10/5 13:53:10

前面的两篇,从相对比较简单的锁的内容入手(也是干货满满),开始了go的系列。这篇开始,进入更核心的内容。我们知道,go应该是第一门在语言层面支持协程的编程语言(可能是我孤陋寡闻),goroutine也完全算的上是go的门面。golang围绕着goroutine构建了一整套用户态的调度体系,并不断演进至当前的GMP模型。接下来相当的一段时间,我们应该都会在介绍GMP以及调度机制中度过。

本篇呢,我们就从goroutine开始说起。之所以从goroutine开始说起,是因为从我的角度来说,相比M和P,G是最简单的。G完全就是一个用户态的任务,唯一要做的就是记录任务的状态,并管理任务(或者说被管理)。其中管理任务包括,选择一个ready的任务运行、将阻塞的任务挂在到相应的阻塞队列中、将ready的任务移动到就绪队列。

当然,实际的实现远远比这复杂,但不妨碍我们先忽略一些细节,比如gc相关的内容等,先将主干抽离出来,理解其设计主线。

本文的内容主要是围绕下面的状态图,当然里面的内容不够全面。但就像前面说的,先理解主干,更多的细节在完整介绍完GMP后再进行补充。

对象

g

goroutine本质就是一个任务,可以被运行,可以等待,可以被调度。基于此,首先要有一个结构体,记录任务相关的信息。基本的信息包括任务的内容、任务的状态、运行任务所需的资源等。不只goroutine,包括其他一些计算机领域更广为人知的典型的任务,比如进程、线程等,都是如此。不过不同的任务,基于其自身的特性以及各自的迭代又会有特有的字段。

goroutine对应的对象如下。字段看上去不少,但是刨除一些gc、pprof(观测,不确定都是pprof相关)的字段,其实内容并不多,主要如下图所示。接下来我们一一介绍。

  • 栈相关。
    stack表示goroutine的栈,栈是一块从高向低增长的线性内存,所以用lo和hi两个指针完全可以表示。
type stack struct {
   lo uintptr
   hi uintptr
}

stackguard0的作用是为了判断栈的扩张。

goroutine初始化的时候只会分配固定大小的栈,并且初始化的栈一定不会分配太大(2KB)。当goroutine运行过程中分配的栈内存越来越多,栈向下增长超过lo+StackGuard时就需要对栈进行扩张。同时stackguard0还可以设置为stackPreempt,表示该协程需要被抢占。goroutine检查到stackPreempt后会主动调度退出运行。stackguard0被检查的时机就是在发生函数调用时,所以我们说goroutine主动调度的时机除了阻塞时,就是在函数调用时。

stackguard1的作用和stackguard0的作用完全相同,stackguard1用来做c的栈的判断,这块我是完全不懂。

  • _panic和_defer。这是golang的panic和defer特性,其实现是绑定于goroutine的,和我之前想的不一样。后面可以开一篇单独介绍。
  • 调度相关。sched字段在goroutine被调度时记录其状态,主要是sp和pc,这两个字段可以记录goroutine的运行状态。
type gobuf struct {
   sp   uintptr
   pc   uintptr
   g    guintptr
   ctxt unsafe.Pointer
   ret  uintptr
   lr   uintptr
   bp   uintptr // for framepointer-enabled architectures
}
  • 其他。其他的字段比如atomicstatus、goid、m等相对比较简单,就不占篇幅在这里说。

g结构体如下。

// src/runtime2.go 407
type g struct {
   stack       stack   // offset known to runtime/cgo
   stackguard0 uintptr // offset known to liblink
   stackguard1 uintptr // offset known to liblink

   _panic    *_panic // innermost panic - offset known to liblink
   _defer    *_defer // innermost defer
   m         *m      // current m; offset known to arm liblink
   sched     gobuf
   syscallsp uintptr // if status==Gsyscall, syscallsp = sched.sp to use during gc
   syscallpc uintptr // if status==Gsyscall, syscallpc = sched.pc to use during gc
   stktopsp  uintptr // expected sp at top of stack, to check in traceback

   param        unsafe.Pointer
   atomicstatus uint32
   stackLock    uint32 // sigprof/scang lock; TODO: fold in to atomicstatus
   goid         int64
   schedlink    guintptr
   waitsince    int64      // approx time when the g become blocked
   waitreason   waitReason // if status==Gwaiting

   preempt       bool // preemption signal, duplicates stackguard0 = stackpreempt
   preemptStop   bool // transition to _Gpreempted on preemption; otherwise, just deschedule
   preemptShrink bool // shrink stack at synchronous safe point

   asyncSafePoint bool

   paniconfault bool // panic (instead of crash) on unexpected fault address
   gcscandone   bool // g has scanned stack; protected by _Gscan bit in status
   throwsplit   bool // must not split stack
   activeStackChans bool
   parkingOnChan uint8
   
   // 下面都是观测及gc相关的,可以略过
   raceignore     int8     // ignore race detection events
   sysblocktraced bool     // StartTrace has emitted EvGoInSyscall about this goroutine
   tracking       bool     // whether we're tracking this G for sched latency statistics
   trackingSeq    uint8    // used to decide whether to track this G
   runnableStamp  int64    // timestamp of when the G last became runnable, only used when tracking
   runnableTime   int64    // the amount of time spent runnable, cleared when running, only used when tracking
   sysexitticks   int64    // cputicks when syscall has returned (for tracing)
   traceseq       uint64   // trace event sequencer
   tracelastp     puintptr // last P emitted an event for this goroutine
   lockedm        muintptr
   sig            uint32
   writebuf       []byte
   sigcode0       uintptr
   sigcode1       uintptr
   sigpc          uintptr
   gopc           uintptr         // pc of go statement that created this goroutine
   ancestors      *[]ancestorInfo // ancestor information goroutine(s) that created this goroutine (only used if debug.tracebackancestors)
   startpc        uintptr         // pc of goroutine function
   racectx        uintptr
   waiting        *sudog         // sudog structures this g is waiting on (that have a valid elem ptr); in lock order
   cgoCtxt        []uintptr      // cgo traceback context
   labels         unsafe.Pointer // profiler labels
   timer          *timer         // cached timer for time.Sleep
   selectDone     uint32         // are we participating in a select and did someone win the race?

   goroutineProfiled goroutineProfileStateHolder
   gcAssistBytes int64
}

sudog

除了g对象外,goroutine还涉及到sudog的对象。sudog是为了goroutine的阻塞队列而封装的一层对象。sudog的封装在我看来是出于两点考虑:

  • 一个goroutine可以阻塞在多个资源上,也就是可能存在于多个阻塞队列中。针对这种情况,做一层封装会简化并发操作,每个sudog都是独属于某个阻塞队列的。
  • 阻塞队列本身即具有一定的数据结构,封装sudog可以将阻塞队列的结构和g本身隔离出来,相当于某种程度的分层。例如在之前介绍的golang的sync.Mutex实现中,就涉及到红黑树以及链表的结构。

// src/runtime2.go 338
type sudog struct {
   g *g

   next *sudog
   prev *sudog
   elem unsafe.Pointer // data element (may point to stack)

   acquiretime int64
   releasetime int64
   ticket      uint32

   isSelect bool

   success bool

   parent   *sudog // semaRoot binary tree
   waitlink *sudog // g.waiting list or semaRoot
   waittail *sudog // semaRoot
   c        *hchan // channel
}

g的调度

goroutine的调度通常涉及到三种情况(最基本的三种):

  • goroutine处于running状态,主动调度;
  • goroutine处于running状态,遇到阻塞时间,转换为waiting状态,触发调度;
  • goroutine处于waiting状态,等待条件达成,转换为runnable状态,等待执行;

主动调度

go的runtime包提供了显示调度的方法runtime.Gosched()。
其调用了mcall函数,并将gosched_m函数作为参数传入。

// src/proc.go 316
func Gosched() {
   checkTimeouts()
   mcall(gosched_m)
}

先看下mcall函数。mcall是用汇编写的,这里就不贴汇编代码,感兴趣的小伙伴可以自行了解下plan9。从注释里看,mcall做的事情是:

  • 将curg的PC/SP保存至g->sched中。g->sched在第一小节中我们也提到过,是goroutine被调度时记录其状态的字段。其中主要是PC/SP两个字段,PC记录当前goroutine执行到哪条指令,SP记录的是栈顶。
  • 从curg切换至g0。g0是和每个m绑定的,不会执行用户任务,只执行系统任务。通常也把切换至g0称为切换至系统栈。
  • 将curg作为参数传入fn中。fn做的事通常是对curg做一些操作,然后调度至新的goroutine继续执行。实际上,我们上面说的几种调度的情况,只是通过不同的fn参数来实现。
    mcall的这种实现实际也是一种代码复用和抽象的小技巧。

再回到gosched_m函数,实际是调用了goschedImpl函数。
goschedImpl中将curg的状态从_Grunning置为_Grunnable,因为这里是主动的调度,当前goroutine并没有被阻塞。
然后将curg和m进行解绑,并将curg塞到全局的阻塞队列中。
然后调用schedule函数。schedule会寻找到一个可执行的g,并切换至起执行。
流程图如下。

// Gosched continuation on g0.
func gosched_m(gp *g) {
   if trace.enabled {
      traceGoSched()
   }
   goschedImpl(gp)
}

func goschedImpl(gp *g) {
   status := readgstatus(gp)
   if status&^_Gscan != _Grunning {
      dumpgstatus(gp)
      throw("bad g status")
   }
   casgstatus(gp, _Grunning, _Grunnable)
   dropg()
   lock(&sched.lock)
   globrunqput(gp)
   unlock(&sched.lock)

   schedule()
}

schedule是很核心的函数,行数也比较多。我们还是忽略一些细节(细节的部分我们在m和p都有一定的了解后再来补充),抽出主干,代码如下。

找到一个可执行的g,然后运行。

// src/proc.go 3185
// One round of scheduler: find a runnable goroutine and execute it.
// Never returns.
func schedule() {
   _g_ := getg()

   gp, inheritTime, tryWakeP := findRunnable() // blocks until work is available

   execute(gp, inheritTime)
}

execute会中会做状态的转换,然后运行gogo。gogo的参数是g->sched,gogo同样是汇编实现,其直接设置pc及sp将执行流切换至g。

func execute(gp *g, inheritTime bool) {
   _g_ := getg()
   
   _g_.m.curg = gp
   gp.m = _g_.m
   casgstatus(gp, _Grunnable, _Grunning)
   gp.waitsince = 0
   gp.preempt = false
   gp.stackguard0 = gp.stack.lo + _StackGuard
   if !inheritTime {
      _g_.m.p.ptr().schedtick++
   }

   gogo(&gp.sched)
}

goroutine阻塞

当goroutine运行遇到需要等待某些条件时,就会进入等待状态。将当前goroutine挂载到相应的阻塞队列,并触发调度。schedule的内容同上面没有变化,可见schedule是调度的核心,不同的调度方法只是在封装了在不同场景下的细节 。流程图如下。

func gopark(unlockf func(*g, unsafe.Pointer) bool, lock unsafe.Pointer, reason waitReason, traceEv byte, traceskip int) {
   if reason != waitReasonSleep {
      checkTimeouts() // timeouts may expire while two goroutines keep the scheduler busy
   }
   mp := acquirem()
   gp := mp.curg
   status := readgstatus(gp)
   if status != _Grunning && status != _Gscanrunning {
      throw("gopark: bad g status")
   }
   mp.waitlock = lock
   mp.waitunlockf = unlockf
   gp.waitreason = reason
   mp.waittraceev = traceEv
   mp.waittraceskip = traceskip
   releasem(mp)
   // can't do anything that might move the G between Ms here.
   mcall(park_m)
}
// park continuation on g0.
func park_m(gp *g) {
   _g_ := getg()

   if trace.enabled {
      traceGoPark(_g_.m.waittraceev, _g_.m.waittraceskip)
   }

   casgstatus(gp, _Grunning, _Gwaiting)
   dropg()

   if fn := _g_.m.waitunlockf; fn != nil {
      ok := fn(gp, _g_.m.waitlock)
      _g_.m.waitunlockf = nil
      _g_.m.waitlock = nil
      if !ok {
         if trace.enabled {
            traceGoUnpark(gp, 2)
         }
         casgstatus(gp, _Gwaiting, _Grunnable)
         execute(gp, true) // Schedule it back, never returns.
      }
   }
   schedule()
}

goroutine就绪

goroutine从等待状态转变为就绪状态应该是最简单的,因为其不涉及调度。只是将g的状态改变,并将g从阻塞队列移动至当前的就绪队列。流程图如下。

唯一有点意思的点在于wakep。wakep的作用是 当有新的g就绪,而当前系统的负载又很低时,确保有m和p来及时的运行g。这个后面在m和p的部分回详细介绍。

func goready(gp *g, traceskip int) {
   systemstack(func() {
      ready(gp, traceskip, true)
   })
}
// Mark gp ready to run.
func ready(gp *g, traceskip int, next bool) {
   if trace.enabled {
      traceGoUnpark(gp, traceskip)
   }

   status := readgstatus(gp)

   // Mark runnable.
   _g_ := getg()
   mp := acquirem() // disable preemption because it can be holding p in a local var
   if status&^_Gscan != _Gwaiting {
      dumpgstatus(gp)
      throw("bad g->status in ready")
   }

   // status is Gwaiting or Gscanwaiting, make Grunnable and put on runq
   casgstatus(gp, _Gwaiting, _Grunnable)
   runqput(_g_.m.p.ptr(), gp, next)
   wakep()
   releasem(mp)
}

本篇呢,对goroutine的介绍肯定不算面面俱到。毕竟,抛开M和P来讲G是很难讲全的。但是,我相信,读过本篇一定会对goroutine建立基本的认知。这种认知不够细节,但一定足够本质。就像文章开头说的,goroutine就是一个用户态的任务。我们自己其实也可以很轻易的实现一个任务管理的系统,这本质上就没有区别。当然,goroutine具备了很多的go的特性,肯定是复杂的多。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/950333.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

文件属性查看和修改学习

这个是链接,相当于快捷方式,指向usr/bin这个目录,链接到这个目录

NRF52832一主多从ble_app_multilink_central

下载官方SDK后打开路径:nRF5SDK153059ac345\nRF5_SDK_15.3.0_59ac345\examples\ble_central\ble_app_multilink_central\pca10040\s132\arm5_no_packs 下的工程文件,确定把log开启 编译后下载完程序(要下载协议栈,这里用6.1.1的)&#xff0c…

FPGA原理与结构——时钟IP核原理学习

一、前言 在之前的文章中,我们介绍了FPGA的时钟结构 FPGA原理与结构——时钟资源https://blog.csdn.net/apple_53311083/article/details/132307564?spm1001.2014.3001.5502 在本文中我们将学习xilinx系列的FPGA所提供的时钟IP核,来帮助我们进一…

实现带头双向循环链表

🌈带头双向循环链表 描述:一个节点内包含两个指针,一个指向上一个节点,另一个指向下一个节点。哨兵位指向的下一个节点为头节点,哨兵位的上一个指向尾节点。 结构优势:高效率找尾节点;高效率插入…

RabbitMQ工作模式-主题模式

主题模式 官方文档参考:https://www.rabbitmq.com/tutorials/tutorial-five-python.html 使用topic类型的交换器,队列绑定到交换器、bingingKey时使用通配符,交换器将消息路由转发到具体队列时,会根据消息routingKey模糊匹配&am…

[学习笔记] fhq Treap 平衡树

fhq Treap 也叫无旋Treap (好像?我也不知道) 反正我带旋 Treap 是不会滴,其他的平衡树也不会(但是会平板电视) fhq Treap 好写,码量小,缺点是常数比较大 定义 二叉搜索树 二叉搜…

为什么说模电难学?因为它至少是这27个基础知识的排列组合!

1、基尔1、基尔霍夫定理的内容是什么? 基尔霍夫电流定律:在电路任一节点,流入、流出该节点电流的代数和为零。 基尔霍夫电压定律:在电路中的任一闭合电路,电压的代数和为零。 2、戴维南定理 一个含独立源、线性电阻…

在抖音开店卖货的流程是什么?最全解答如下,建议新手认真看完!

我是王路飞。 同样是在抖音卖货,为何如今大多数人都是选择在抖音开店,而不再是选择做账号、开直播了呢? 原因很简单,因为门槛和变现方式。 相比短视频和直播带货的起号、变现难度越来越大,低门槛的抖音小店显然更适…

Origin热图的做法

1.数据准备 2.绘制-选择带标签热图 3.图表调整 右边标签- 下方标签-复制格式,再到左边或者右边选择 粘贴所有 图中的标签及颜色 直接双击在属性框更改 主框的其他特征在属性框选择 色阶的控制 直接选择色阶的属性框

Endnote中查看一个文献的分组的具体方法——以Endnote X8为例

Endnote中查看一个文献的分组的具体方法——以Endnote X8为例 一、问题 当Endnote中使用分类方法对文献进行分组管理后,有时需要重新调整该文献的分组,则需要找到这个文献在哪个分组中。本文阐述怎样寻找一个文献的分组的位置信息。 二、解决方法 1.选…

Spooling的原理

脱机技术 程序猿先用纸带机把自己的程序数据输入到磁带中,这个输入的过程是由一台专门的外围控制机实现的。之后CPU直接从快速的磁带中读取想要的这些输入数据。输出也类似。 假脱机技术(Spooling技术) 即用软件的方式来模拟脱机技术。要…

Kubernetes技术--k8s核心技术Controller控制器

1.Controller概述 Controller是在集群上管理和运行容器的对象。是一个实际存在的对象。 2.pod和Controller之间的关系 pod通过controller实现应用的运维,包括伸缩、滚动升级等操作。 这里pod和controller通过label标签来建立关系。如下所示: 3.Deployment控制器应用场景 -1:…

RabbitMQ工作模式-发布订阅模式

Publish/Subscribe(发布订阅模式) 官方文档: https://www.rabbitmq.com/tutorials/tutorial-three-python.html 使用fanout类型类型的交换器,routingKey忽略。每个消费者定义生成一个队列关绑定到同一个Exchange,每个…

win10底部任务栏开机后长时间未响应的解决办法

https://blog.csdn.net/hj960511/article/details/128746025?share_tokenAA088876-4477-44B8-B978-5C7C7726D552&tt_fromcopy_link&utm_sourcecopy_link&utm_mediumtoutiao_ios&utm_campaignclient_share win10底部任务栏开机后长时间未响应的解决办法-CSDN博…

亚马逊云科技re:Inforce大会:为企业提供端到端的安全防护能力

2023年,生成式AI带来了无数的创新,并将会在行业应用中产生更多的新能力、新场景。与此同时,关于生成式AI的风险管控成为各方关注焦点,数据隐私、合规保护、防欺诈等,已成为生成式AI时代的安全合规的新话题。 随着云上业…

(一)SpringBoot 整合WebSocket 前端 uniapp 访问

第一次使用WebSocket,所以最需要一个及其简单的例子,跑通之后,增加自己对该技术的理解。(技术基础介绍就免掉了,后面再补) 案例逻辑:目前只有一个用户,而且是一个用户给服务器发送数…

linux系统中详解u-boot之网络移植与调试

​今天给大家讲一讲如何完善u-boot网络部分的移植和调试。 一、前章回顾 上一章,已经讲过如何讲uboot.2022.10版本移植到我们自己的imx6ull开发板上,但是最后编译下载后网络部分未能正确识别,今天我们就来讲一讲网络部分的调试。 上一篇ub…

静力触探数据智能预处理(2)

静力触探数据智能预处理(2) 前言 数据处理方式已由手工1.0、计算机辅助2.0到人工智能3.0的趋势发展。现场采集的静力触探数据通常是由仪器厂家开发的数据采集软件保存,将原始数据导入Excel中,数据格式需要花费一定的时间整理&am…

Git管理本地代码

一、Git配置 当安装完 Git 应该做的第一件事就是设置你的用户名称与邮件地址。 这样做很重要,因为每一个 Git 的提交都会使用这些信息,并且它会写入到你的每一次提交中,不可更改 – global全局配置 通过 --global 选项可以设置全局配置信息 …

第6篇:ESP32连接无源喇叭播放音乐《涛声依旧》

第1篇:Arduino与ESP32开发板的安装方法 第2篇:ESP32 helloword第一个程序示范点亮板载LED 第3篇:vscode搭建esp32 arduino开发环境 第4篇:vscodeplatformio搭建esp32 arduino开发环境 第5篇:doit_esp32_devkit_v1使用pmw呼吸灯实验 D5连接喇叭正极,GND连接喇叭负…