日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
追溯Go中sysmon的啟動(dòng)過程

在Go中有一個(gè)特殊的線程,它不與其他任何P進(jìn)行綁定。在一個(gè)死循環(huán)之中不停的執(zhí)行一系列的監(jiān)控操作,通過這些監(jiān)控操作來更好的服務(wù)于整個(gè)Go進(jìn)程,它就是——sysmon監(jiān)控線程。

你可能會(huì)好奇它的作用,這里簡(jiǎn)單總結(jié)一下:

  • 釋放閑置超過5分鐘的span物理內(nèi)存
  • 超過2分鐘沒有垃圾回收,強(qiáng)制啟動(dòng)垃圾回收
  • 將長(zhǎng)時(shí)間沒有處理的netpoll結(jié)果添加到任務(wù)隊(duì)列
  • 向長(zhǎng)時(shí)間執(zhí)行的G任務(wù)發(fā)起搶占調(diào)度
  • 收回因syscall而長(zhǎng)時(shí)間阻塞的P

因此可以看出,sysmon線程就像監(jiān)工一樣,監(jiān)控著整個(gè)進(jìn)程的狀態(tài)。你會(huì)不會(huì)跟我一樣好奇這個(gè)線程是怎么啟動(dòng)起來的,一起來追溯吧。

1. 準(zhǔn)備工作

  • Go源碼:v1.16.5
  • IDE:goland
  • 操作系統(tǒng):Centos
  • 知識(shí)儲(chǔ)備:了解Go啟動(dòng)過程,見筆者文章《Go程序啟動(dòng)過程的一次追溯》

Go的啟動(dòng)過程大概分為三個(gè)階段:

  • Go程序的引導(dǎo)過程
  • runtime的啟動(dòng)以及初始化過程(runtime.main)
  • 執(zhí)行用戶代碼(main.main)

2. sysmon啟動(dòng)過程追溯

由Go的啟動(dòng)過程大概可以猜出來,sysmon的啟動(dòng)過程在runtime的啟動(dòng)以及初始化過程之中。所以,我們從runtime.main開始一步步的追溯代碼,來尋找sysmon的啟動(dòng)步驟。

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.       // !!! 找到了 啟動(dòng)sysmon的代碼 
  9.       // 在系統(tǒng)棧內(nèi)生成一個(gè)新的M來啟動(dòng)sysmon 
  10.       atomic.Store(&sched.sysmonStarting, 1) 
  11.         systemstack(func() { 
  12.           newm(sysmon, nil, -1) 
  13.       }) 
  14.     } 
  15.   ... 
  16.  
  17. // 創(chuàng)建一個(gè)新的系統(tǒng)線程 
  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結(jié)構(gòu)體,并進(jìn)行部分字段的初始化 
  26.     // allocm方法非常重要?。。?nbsp;
  27.     // 該方法獲取并初始化M的結(jié)構(gòu)體,還在M里面設(shè)置了系統(tǒng)線程將要執(zhí)行的方法fn,這里是sysmon 
  28.     mp := allocm(_p_, fn, id) 
  29.     ... 
  30.    
  31.      // M在Go中屬于用戶態(tài)代碼中的一個(gè)結(jié)構(gòu)體,跟系統(tǒng)線程是一對(duì)一的關(guān)系 
  32.      // 每個(gè)系統(tǒng)線程怎么執(zhí)行代碼,從哪里開始執(zhí)行,則是由M的結(jié)構(gòu)體中參數(shù)來指明 
  33.     // 創(chuàng)建GPM中結(jié)構(gòu)體M結(jié)構(gòu)體之后,開始創(chuàng)建對(duì)應(yīng)的底層系統(tǒng)線程 
  34.     newm1(mp) 
  35.  
  36. // 給M分配一個(gè)系統(tǒng)線程 
  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.     // 創(chuàng)建新的M,并且進(jìn)行一些初始化操作 
  49.     mp := new(m) 
  50.     // M 的執(zhí)行方法, 在runtime.mstart()方法中最終調(diào)用fn 
  51.     mp.mstartfn = fn 
  52.     ... 
  53.  
  54. // 楷書創(chuàng)建系統(tǒng)線程的邏輯 
  55. func newm1(mp *m) { 
  56.       ... 
  57.       // ?。?!創(chuàng)建系統(tǒng)線程?。?! 
  58.       newosproc(mp) 
  59.       ... 

 runtime/os_linux.go

 
 
 
  1. // 通過clone創(chuàng)建系統(tǒng)線程 
  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個(gè)參數(shù) 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沒有具體方法體,具體實(shí)現(xiàn)使用匯編編寫 
  16. func clone(flags int32, stk, mp, gp, fn unsafe.Pointer) int32 

 clone()函數(shù)在linux系統(tǒng)中,用來創(chuàng)建輕量級(jí)進(jìn)程

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寄存器存儲(chǔ)fn的地址 
  10.     ... 
  11.      
  12.     // 判斷是父進(jìn)程,則直接返回 
  13.     // 子進(jìn)程則跳到 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,  調(diào)用 fn,即 runtime.mstart 
  37.     MOVD    R12, R0 // R12中存放的是fn的地址 
  38.     BL  (R0)  // BL是一個(gè)跳轉(zhuǎn)指令,跳轉(zhuǎn)到fn 
  39. ... 

 runtime.proc.go

 
 
 
  1. // mstart是一個(gè)M的執(zhí)行入口 
  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. // 開始執(zhí)行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.     // 即:執(zhí)行 runtime.sysmon 
  25.     // sysmon方法是一個(gè)死循環(huán),所以說執(zhí)行sysmon的線程會(huì)一直在這里 
  26.     fn() 
  27.     } 
  28.     ... 

最終執(zhí)行的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結(jié)果 
  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's 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長(zhǎng)時(shí)間阻塞的P,向長(zhǎng)時(shí)間阻塞的P發(fā)起搶占調(diào)度 
  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.        // 檢查是否需要強(qiáng)制執(zhí)行垃圾回收 
  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.    ... 

總結(jié)

由以上可知,sysmon線程的創(chuàng)建過程經(jīng)過幾個(gè)階段:

  1. 創(chuàng)建M結(jié)構(gòu)體,對(duì)該結(jié)構(gòu)初始化并綁定系統(tǒng)線程將要執(zhí)行的方法sysmon
  2. 為M創(chuàng)建對(duì)應(yīng)的底層系統(tǒng)線程(不同的操作系統(tǒng)生成方式不同)
  3. 引導(dǎo)系統(tǒng)線程從mstart方法開始執(zhí)行sysmon邏輯(sysmon方法是死循環(huán))

sysmon線程啟動(dòng)之后就進(jìn)入監(jiān)控整個(gè)Go進(jìn)程的邏輯中,至于sysmon都做了些什么,有機(jī)會(huì)再一起探討。


網(wǎng)站題目:追溯Go中sysmon的啟動(dòng)過程
分享URL:http://www.5511xx.com/article/dpchojo.html