追溯Go中sysmon的启动过程

开发 后端
在一个死循环之中不停的执行一系列的监控操作,通过这些监控操作来更好的服务于整个Go进程,它就是——sysmon监控线程。

[[408782]]

在Go中有一个特殊的线程,它不与其他任何P进行绑定。在一个死循环之中不停的执行一系列的监控操作,通过这些监控操作来更好的服务于整个Go进程,它就是——sysmon监控线程。

你可能会好奇它的作用,这里简单总结一下:

  • 释放闲置超过5分钟的span物理内存
  • 超过2分钟没有垃圾回收,强制启动垃圾回收
  • 将长时间没有处理的netpoll结果添加到任务队列
  • 向长时间执行的G任务发起抢占调度
  • 收回因syscall而长时间阻塞的P

因此可以看出,sysmon线程就像监工一样,监控着整个进程的状态。你会不会跟我一样好奇这个线程是怎么启动起来的,一起来追溯吧。

1. 准备工作

  • Go源码:v1.16.5
  • IDE:goland
  • 操作系统:Centos
  • 知识储备:了解Go启动过程,见笔者文章《Go程序启动过程的一次追溯》

Go的启动过程大概分为三个阶段:

  • Go程序的引导过程
  • runtime的启动以及初始化过程(runtime.main)
  • 执行用户代码(main.main)

2. sysmon启动过程追溯

由Go的启动过程大概可以猜出来,sysmon的启动过程在runtime的启动以及初始化过程之中。所以,我们从runtime.main开始一步步的追溯代码,来寻找sysmon的启动步骤。

runtime/proc.go

  1. func main() { 
  2.     ... 
  3.       if GOARCH != "wasm" { // no threads on wasm yet, so no sysmon 
  4.       // For runtime_syscall_doAllThreadsSyscall, we 
  5.       // register sysmon is not ready for the world to be 
  6.       // stopped. 
  7.        
  8.       // !!! 找到了 启动sysmon的代码 
  9.       // 在系统栈内生成一个新的M来启动sysmon 
  10.       atomic.Store(&sched.sysmonStarting, 1) 
  11.         systemstack(func() { 
  12.           newm(sysmon, nil, -1) 
  13.       }) 
  14.     } 
  15.   ... 
  16.  
  17. // 创建一个新的系统线程 
  18. // Create a new m. It will start off with a call to fn, or else the scheduler. 
  19. // fn needs to be static and not a heap allocated closure. 
  20. // May run with m.p==nil, so write barriers are not allowed. 
  21. // 
  22. // id is optional pre-allocated m ID. Omit by passing -1. 
  23. //go:nowritebarrierrec 
  24. func newm(fn func(), _p_ *p, id int64) { 
  25.     // 获取GPM中M结构体,并进行部分字段的初始化 
  26.     // allocm方法非常重要!!! 
  27.     // 该方法获取并初始化M的结构体,还在M里面设置了系统线程将要执行的方法fn,这里是sysmon 
  28.     mp := allocm(_p_, fn, id) 
  29.     ... 
  30.    
  31.      // M在Go中属于用户态代码中的一个结构体,跟系统线程是一对一的关系 
  32.      // 每个系统线程怎么执行代码,从哪里开始执行,则是由M的结构体中参数来指明 
  33.     // 创建GPM中结构体M结构体之后,开始创建对应的底层系统线程 
  34.     newm1(mp) 
  35.  
  36. // 给M分配一个系统线程 
  37. // Allocate a new m unassociated with any thread. 
  38. // Can use p for allocation context if needed. 
  39. // fn is recorded as the new m's m.mstartfn. 
  40. // id is optional pre-allocated m ID. Omit by passing -1. 
  41. // 
  42. // This function is allowed to have write barriers even if the caller 
  43. // isn't because it borrows _p_. 
  44. // 
  45. //go:yeswritebarrierrec 
  46. func allocm(_p_ *p, fn func(), id int64) *m { 
  47.     ... 
  48.     // 创建新的M,并且进行一些初始化操作 
  49.     mp := new(m) 
  50.     // M 的执行方法, 在runtime.mstart()方法中最终调用fn 
  51.     mp.mstartfn = fn 
  52.     ... 
  53.  
  54. // 楷书创建系统线程的逻辑 
  55. func newm1(mp *m) { 
  56.       ... 
  57.       // !!!创建系统线程!!! 
  58.       newosproc(mp) 
  59.       ... 

 runtime/os_linux.go

  1. // 通过clone创建系统线程 
  2. // May run with m.p==nil, so write barriers are not allowed. 
  3. //go:nowritebarrier 
  4. func newosproc(mp *m) { 
  5.     ... 
  6.     // Disable signals during clone, so that the new thread starts 
  7.     // with signals disabled. It will enable them in minit. 
  8.    // 
  9.    // 注意: 
  10.    // 第5个参数 mstart 是在 runtime.mstart 
  11.     ret := clone(cloneFlags, stk, unsafe.Pointer(mp), unsafe.Pointer(mp.g0), unsafe.Pointer(funcPC(mstart))) 
  12.     ... 
  13.  
  14. //go:noescape 
  15. //clone没有具体方法体,具体实现使用汇编编写 
  16. func clone(flags int32, stk, mp, gp, fn unsafe.Pointer) int32 

 clone()函数在linux系统中,用来创建轻量级进程

runtime/sys_linux_arm64.s

  1. // 注意 这里的void (*fn)(void) 就是 runtime.mstart 方法的地址入口 
  2. // 
  3. // int64 clone(int32 flags, void *stk, M *mp, G *gp, void (*fn)(void)); 
  4. TEXT runtime·clone(SB),NOSPLIT|NOFRAME,$0 
  5.     ... 
  6.     // Copy mp, gp, fn off parent stack for use by child. 
  7.     MOVD    mp+16(FP), R10 
  8.     MOVD    gp+24(FP), R11 
  9.     MOVD    fn+32(FP), R12 // R12寄存器存储fn的地址 
  10.     ... 
  11.      
  12.     // 判断是父进程,则直接返回 
  13.     // 子进程则跳到 child 
  14.     // In parent, return
  15.     CMP ZR, R0 
  16.     BEQ child 
  17.     MOVW    R0, ret+40(FP) 
  18.     RET 
  19.      
  20. child: 
  21.     // In child, on new stack. 
  22.     MOVD    -32(RSP), R10 
  23.     MOVD    $1234, R0 
  24.     CMP R0, R10 
  25.     BEQ good 
  26.     ... 
  27.  
  28. good: 
  29.     ... 
  30.     CMP $0, R10 
  31.     BEQ nog 
  32.     CMP $0, R11 
  33.     BEQ nog 
  34.     ... 
  35. nog: 
  36.     // Call fn,  调用 fn,即 runtime.mstart 
  37.     MOVD    R12, R0 // R12中存放的是fn的地址 
  38.     BL  (R0)  // BL是一个跳转指令,跳转到fn 
  39. ... 

 runtime.proc.go

  1. // mstart是一个M的执行入口 
  2. // mstart is the entry-point for new Ms. 
  3. // 
  4. // This must not split the stack because we may not even have stack 
  5. // bounds set up yet. 
  6. // 
  7. // May run during STW (because it doesn't have a P yet), so write 
  8. // barriers are not allowed. 
  9. // 
  10. //go:nosplit 
  11. //go:nowritebarrierrec 
  12. func mstart() { 
  13.     ... 
  14.     mstart1() 
  15.     ... 
  16.  
  17. // 开始执行M的具体方法 
  18. func mstart1() { 
  19.     _g_ := getg() 
  20.    
  21.     ... 
  22.     // M中mstartfn指向 runtime.sysmon, 即 fn = runtime.sysmon 
  23.     if fn := _g_.m.mstartfn; fn != nil { 
  24.     // 即:执行 runtime.sysmon 
  25.     // sysmon方法是一个死循环,所以说执行sysmon的线程会一直在这里 
  26.     fn() 
  27.     } 
  28.     ... 

最终执行的sysmon方法

  1. // Always runs without a P, so write barriers are not allowed. 
  2. // 
  3. //go:nowritebarrierrec 
  4. func sysmon() { 
  5.     ... 
  6.     for { 
  7.        ... 
  8.       // 获取超过10ms的netpoll结果 
  9.       // 
  10.       // poll network if not polled for more than 10ms 
  11.       lastpoll := int64(atomic.Load64(&sched.lastpoll)) 
  12.       if netpollinited() && lastpoll != 0 && lastpoll+10*1000*1000 < now { 
  13.         atomic.Cas64(&sched.lastpoll, uint64(lastpoll), uint64(now)) 
  14.         list := netpoll(0) // non-blocking - returns list of goroutines 
  15.         if !list.empty() { 
  16.           // Need to decrement number of idle locked M's 
  17.           // (pretending that one more is running) before injectglist. 
  18.           // Otherwise it can lead to the following situation: 
  19.           // injectglist grabs all P's but before it starts M'to run the P's, 
  20.           // another M returns from syscall, finishes running its G, 
  21.           // observes that there is no work to do and no other running M's 
  22.           // and reports deadlock. 
  23.           incidlelocked(-1) 
  24.           injectglist(&list) 
  25.           incidlelocked(1) 
  26.         } 
  27.       } 
  28.  
  29.             ... 
  30.  
  31.       // 抢夺syscall长时间阻塞的P,向长时间阻塞的P发起抢占调度 
  32.       // 
  33.       // retake P's blocked in syscalls 
  34.       // and preempt long running G's 
  35.       if retake(now) != 0 { 
  36.         idle = 0 
  37.       } else { 
  38.         idle++ 
  39.       } 
  40.      
  41.        // 检查是否需要强制执行垃圾回收 
  42.             // check if we need to force a GC 
  43.       if t := (gcTrigger{kind: gcTriggerTime, now: now}); t.test() && atomic.Load(&forcegc.idle) != 0 { 
  44.         lock(&forcegc.lock) 
  45.         forcegc.idle = 0 
  46.         var list gList 
  47.         list.push(forcegc.g) 
  48.         injectglist(&list) 
  49.         unlock(&forcegc.lock) 
  50.       } 
  51.             ... 
  52.     } 
  53.    ... 

总结

由以上可知,sysmon线程的创建过程经过几个阶段:

  1. 创建M结构体,对该结构初始化并绑定系统线程将要执行的方法sysmon
  2. 为M创建对应的底层系统线程(不同的操作系统生成方式不同)
  3. 引导系统线程从mstart方法开始执行sysmon逻辑(sysmon方法是死循环)

sysmon线程启动之后就进入监控整个Go进程的逻辑中,至于sysmon都做了些什么,有机会再一起探讨。

 

责任编辑:姜华 来源: 今日头条
相关推荐

2011-07-28 10:34:38

Cocoa 程序 启动

2014-06-23 10:31:09

Android启动过程

2011-09-05 17:35:18

MTK启动过程RTOS

2009-12-03 10:00:46

Linux系统启动

2011-06-28 13:27:13

ARM Linux

2010-07-05 17:38:39

IIS 7.0 FTP

2010-05-06 14:05:15

Unix系统

2018-03-13 13:00:03

Linux运维启动分析

2012-08-16 09:07:57

Erlang

2012-02-20 14:47:08

JavaPlay

2020-03-19 08:59:15

SpringMVC启动过程

2021-09-28 15:03:06

Linux内核arm

2018-10-18 14:06:15

Linux系统过程

2020-04-20 21:30:51

Tomcat部署架构

2019-05-27 14:43:49

Tomcat架构部署

2023-03-24 14:52:27

AbilityPage应用

2009-08-11 09:03:45

Windows 7系统启动

2024-03-26 08:23:14

Android组件版本

2021-09-08 08:26:06

SpringbootBeanPostPro

2014-06-20 11:05:56

Android应用程序进程启动
点赞
收藏

51CTO技术栈公众号