這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
本文分析了Golang的socket檔案描述符和goroutine阻塞調度的原理。代碼中大部分是Go代碼,小部分是彙編代碼。完整理解本文需要Go語言知識,並且用Golang寫過網路程式。更重要的是,需要提前理解goroutine的調度原理。
1. TCP的連線物件:
連線物件:
在net.go中有一個名為Conn的介面,提供了對於串連的讀寫和其他動作:
type Conn interface { Read(b []byte) (n int, err error) Write(b []byte) (n int, err error) Close() error LocalAddr() Addr RemoteAddr() Addr SetReadDeadline(t time.Time) error SetWriteDeadline(t time.Time) error}
這個介面就是對下面的結構體conn的抽象。conn結構體包含了對串連的讀寫和其他動作:
type conn struct { fd *netFD}
從串連讀取資料:
// Read implements the Conn Read method.func (c *conn) Read(b []byte) (int, error) { if !c.ok() { return 0, syscall.EINVAL } return c.fd.Read(b)}
向串連寫入資料:
// Write implements the Conn Write method.func (c *conn) Write(b []byte) (int, error) { if !c.ok() { return 0, syscall.EINVAL } return c.fd.Write(b)}
關閉串連:
// Close closes the connection.func (c *conn) Close() error { if !c.ok() { return syscall.EINVAL } return c.fd.Close()}
設定讀寫逾時:
// SetDeadline implements the Conn SetDeadline method.func (c *conn) SetDeadline(t time.Time) error { if !c.ok() { return syscall.EINVAL } return c.fd.setDeadline(t)}// SetReadDeadline implements the Conn SetReadDeadline method.func (c *conn) SetReadDeadline(t time.Time) error { if !c.ok() { return syscall.EINVAL } return c.fd.setReadDeadline(t)}// SetWriteDeadline implements the Conn SetWriteDeadline method.func (c *conn) SetWriteDeadline(t time.Time) error { if !c.ok() { return syscall.EINVAL } return c.fd.setWriteDeadline(t)}
可以看到,對串連的所有操作,都體現在對*netFD的操作上。我們繼續跟蹤c.fd.Read()函數.
2.檔案描述符
net/fd_unix.go:
網路連接的檔案描述符:
// Network file descriptor.type netFD struct { // locking/lifetime of sysfd + serialize access to Read and Write methods fdmu fdMutex // immutable until Close sysfd int family int sotype int isConnected bool net string laddr Addr raddr Addr // wait server pd pollDesc}
檔案描述符讀取資料:
func (fd *netFD) Read(p []byte) (n int, err error) { if err := fd.readLock(); err != nil { return 0, err } defer fd.readUnlock() if err := fd.pd.PrepareRead(); err != nil { return 0, &OpError{"read", fd.net, fd.raddr, err} } // 調用system call,迴圈從fd.sysfd讀取資料 for { // 系統調用Read讀取資料 n, err = syscall.Read(int(fd.sysfd), p) // 如果發生錯誤,則需要處理 // 並且只處理EAGAIN類型的錯誤,其他錯誤一律返回給調用者 if err != nil { n = 0 // 對於非阻塞的網路連接的檔案描述符,如果錯誤是EAGAIN // 說明Socket的緩衝區為空白,未讀取到任何資料 // 則調用fd.pd.WaitRead, if err == syscall.EAGAIN { if err = fd.pd.WaitRead(); err == nil { continue } } } err = chkReadErr(n, err, fd) break } if err != nil && err != io.EOF { err = &OpError{"read", fd.net, fd.raddr, err} } return}
網路輪詢者
網路輪詢者是Golang中針對每個socket檔案描述符建立的輪詢機制。此處的輪詢並不是一般意義上的輪詢,而是Golang的runtime在調度goroutine或者GC完成之後或者指定時間之內,調用epoll_wait擷取所有產生IO事件的socket檔案描述符。當然在runtime輪詢之前,需要將socket檔案描述符和當前goroutine的相關資訊加入epoll維護的資料結構中,並掛起當前goroutine,當IO就緒後,通過epoll返回的檔案描述符和其中附帶的goroutine的資訊,重新恢複當前goroutine的執行。
// Integrated network poller (platform-independent part).// 網路輪詢者(平台獨立部分)// A particular implementation (epoll/kqueue) must define the following functions:// 實際的實現(epoll/kqueue)必須定義以下函數:// func netpollinit() // to initialize the poller,初始化輪詢者// func netpollopen(fd uintptr, pd *pollDesc) int32 // to arm edge-triggered notifications, 為fd和pd啟動邊緣觸發通知// and associate fd with pd.// 一個實現必須調用下面的函數,用來指示pd已經準備好// An implementation must call the following function to denote that the pd is ready.// func netpollready(gpp **g, pd *pollDesc, mode int32)// pollDesc contains 2 binary semaphores, rg and wg, to park reader and writer// goroutines respectively. The semaphore can be in the following states:// pollDesc包含了2個二進位的訊號,分別負責讀寫goroutine的暫停.// 訊號可能處於下面的狀態:// pdReady - IO就緒通知被掛起;// 一個goroutine將次狀態置為nil來消費一個通知。// pdReady - io readiness notification is pending;// a goroutine consumes the notification by changing the state to nil.// pdWait - 一個goroutine準備暫停在訊號上,但是還沒有完成暫停。// 這個goroutine通過把這個狀態改變為G指標去提交這個暫停動作。// 或者,替代性的,並行的其他通知將狀態改變為READY.// 或者,替代性的,並行的逾時/關閉會將次狀態變為nil// pdWait - a goroutine prepares to park on the semaphore, but not yet parked;// the goroutine commits to park by changing the state to G pointer,// or, alternatively, concurrent io notification changes the state to READY,// or, alternatively, concurrent timeout/close changes the state to nil.// G指標 - 阻塞在訊號上的goroutine// IO通知或者逾時/關閉會分別將此狀態置為READY或者nil.// G pointer - the goroutine is blocked on the semaphore;// io notification or timeout/close changes the state to READY or nil respectively// and unparks the goroutine.// nil - nothing of the above.const ( pdReady uintptr = 1 pdWait uintptr = 2)
網路輪詢者的資料結構如下:
// Network poller descriptor.// 網路輪詢者描述符type pollDesc struct { link *pollDesc // in pollcache, protected by pollcache.lock // The lock protects pollOpen, pollSetDeadline, pollUnblock and deadlineimpl operations. // This fully covers seq, rt and wt variables. fd is constant throughout the PollDesc lifetime. // pollReset, pollWait, pollWaitCanceled and runtime·netpollready (IO readiness notification) // proceed w/o taking the lock. So closing, rg, rd, wg and wd are manipulated // in a lock-free way by all operations. // NOTE(dvyukov): the following code uses uintptr to store *g (rg/wg), // that will blow up when GC starts moving objects. // // lock鎖對象保護了pollOpen, pollSetDeadline, pollUnblock和deadlineimpl操作。 // 而這些操作又完全包含了對seq, rt, tw變數。 // fd在PollDesc整個生命過程中都是一個常量。 // 處理pollReset, pollWait, pollWaitCanceled和runtime.netpollready(IO就緒通知)不需要用到鎖。 // 所以closing, rg, rd, wg和wd的所有操作都是一個無鎖的操作。 lock mutex // protectes the following fields fd uintptr closing bool seq uintptr // protects from stale timers and ready notifications rg uintptr // pdReady, pdWait, G waiting for read or nil rt timer // read deadline timer (set if rt.f != nil) rd int64 // read deadline wg uintptr // pdReady, pdWait, G waiting for write or nil wt timer // write deadline timer wd int64 // write deadline user unsafe.Pointer // user settable cookie}
將當前goroutine設定為阻塞在fd上:
pd.WaitRead():
func (pd *pollDesc) WaitRead() error { return pd.Wait('r')}func (pd *pollDesc) Wait(mode int) error { res := runtime_pollWait(pd.runtimeCtx, mode) return convertErr(res)}
res是runtime_pollWait函數返回的結果,由conevertErr函數封裝後返回:
func convertErr(res int) error { switch res { case 0: return nil case 1: return errClosing case 2: return errTimeout } println("unreachable: ", res) panic("unreachable")}
- 函數返回0,表示IO已經準備好,返回nil。
- 返回1,說明串連已關閉,應該放回errClosing。
- 返回2,說明對IO進行的操作發生逾時,應該返回errTimeout。
runtime_pollWait會調用runtime/thunk.s中的函數:
TEXT net·runtime_pollWait(SB),NOSPLIT,$0-0 JMP runtime·netpollWait(SB)
這是一個封裝函數,沒有參數,直接跳轉到runtime/netpoll.go中的函數netpollWait:
func netpollWait(pd *pollDesc, mode int) int { // 檢查pd的狀態是否異常 err := netpollcheckerr(pd, int32(mode)) if err != 0 { return err } // As for now only Solaris uses level-triggered IO. if GOOS == "solaris" { onM(func() { netpollarm(pd, mode) }) } // 迴圈中檢查pd的狀態是不是已經被設定為pdReady // 即檢查IO是不是已經就緒 for !netpollblock(pd, int32(mode), false) { err = netpollcheckerr(pd, int32(mode)) if err != 0 { return err } // Can happen if timeout has fired and unblocked us, // but before we had a chance to run, timeout has been reset. // Pretend it has not happened and retry. } return 0}
netpollcheckerr函數檢查pd是否出現異常:
// 檢查pd的異常func netpollcheckerr(pd *pollDesc, mode int32) int { // 是否已經關閉 if pd.closing { return 1 // errClosing } // 當讀寫狀態下,deadline小於0,表示pd已經過了逾時時間 if (mode == 'r' && pd.rd < 0) || (mode == 'w' && pd.wd < 0) { return 2 // errTimeout } // 正常情況返回0 return 0}
netpollblock():
// returns true if IO is ready, or false if timedout or closed// waitio - wait only for completed IO, ignore errors// 這個函數被netpollWait迴圈調用// 返回true說明IO已經準備好,返回false說明IO操作已經逾時或者已經關閉func netpollblock(pd *pollDesc, mode int32, waitio bool) bool { // 擷取pd的rg gpp := &pd.rg // 如果模式是w,則擷取pd的wg if mode == 'w' { gpp = &pd.wg } // set the gpp semaphore to WAIT // 在迴圈中設定pd的gpp為pdWait // 因為casuintptr是自旋鎖,所以需要在迴圈中調用 for { // 如果在迴圈中發現IO已經準備好(pg的rg或者wg為pdReady狀態) // 則設定rg/wg為0,返回true old := *gpp if old == pdReady { *gpp = 0 return true } // 每次netpollblock執行完畢之後,gpp重設為0 // 非0表示重複wait if old != 0 { gothrow("netpollblock: double wait") } // CAS操作改變gpp為pdWait if casuintptr(gpp, 0, pdWait) { break } } // need to recheck error states after setting gpp to WAIT // this is necessary because runtime_pollUnblock/runtime_pollSetDeadline/deadlineimpl // do the opposite: store to closing/rd/wd, membarrier, load of rg/wg // // 當設定gpp為pdWait狀態後,重新檢查gpp的狀態 // 這是必要的,因為runtime_pollUnblock/runtime_pollSetDeadline/deadlineimpl會做相反的操作 // 如果狀態正常則掛起當前的goroutine // // 當netpollcheckerr檢查io出現逾時或者錯誤,waitio為true可用於等待ioReady // 否則當waitio為false, 且io不出現錯誤或者逾時才會掛起當前goroutine if waitio || netpollcheckerr(pd, mode) == 0 { // 解鎖函數,設定gpp為pdWait,如果設定不成功 // 說明已經是發生其他事件,可以讓g繼續運行,而不是掛起當前g f := netpollblockcommit // 嘗試掛起當前g gopark(**(**unsafe.Pointer)(unsafe.Pointer(&f)), unsafe.Pointer(gpp), "IO wait") } // be careful to not lose concurrent READY notification old := xchguintptr(gpp, 0) if old > pdWait { gothrow("netpollblock: corrupted state") } return old == pdReady}
runtime/proc.go: gopark():
// Puts the current goroutine into a waiting state and calls unlockf.// If unlockf returns false, the goroutine is resumed.// 將當前goroutine置為waiting狀態,然後調用unlockffunc gopark(unlockf unsafe.Pointer, lock unsafe.Pointer, reason string) { // 擷取當前M mp := acquirem() // 擷取當前G gp := mp.curg // 擷取G的狀態 status := readgstatus(gp) // 如果不是_Grunning或者_Gscanrunning,則報錯 if status != _Grunning && status != _Gscanrunning { gothrow("gopark: bad g status") } // 設定lock和unlockf mp.waitlock = lock mp.waitunlockf = unlockf gp.waitreason = reason releasem(mp) // can't do anything that might move the G between Ms here. // 在m->g0這個棧上調用park_m,而不是當前g的棧 mcall(park_m)}
mcall函數是一段彙編,在m->g0的棧上調用park_m,而不是在當前goroutine的棧上。mcall的功能分兩部分,第一部分儲存當前G的PC/SP到G的gobuf的pc/sp欄位,第二部分調用park_m函數:
// func mcall(fn func(*g))// Switch to m->g0's stack, call fn(g).// Fn must never return. It should gogo(&g->sched)// to keep running g.TEXT runtime·mcall(SB), NOSPLIT, $0-8 // 將需要執行的函數儲存在DI MOVQ fn+0(FP), DI // 將M的TLS存放在CX get_tls(CX) // 將G對象存放在AX MOVQ g(CX), AX // save state in g->sched // 將調用者的PC存放在BX MOVQ 0(SP), BX // caller's PC // 將調用者的PC儲存到g->sched.pc MOVQ BX, (g_sched+gobuf_pc)(AX) // 第一個參數的地址,即棧頂的地址,儲存到BX LEAQ fn+0(FP), BX // caller's SP // 儲存SP的地址到g->sched.sp MOVQ BX, (g_sched+gobuf_sp)(AX) // 將g對象儲存到g->sched->g MOVQ AX, (g_sched+gobuf_g)(AX) // switch to m->g0 & its stack, call fn // 將g對象指標儲存到BX MOVQ g(CX), BX // 將g->m儲存到BX MOVQ g_m(BX), BX // 將m->g0儲存到SI MOVQ m_g0(BX), SI CMPQ SI, AX // if g == m->g0 call badmcall JNE 3(PC) MOVQ $runtime·badmcall(SB), AX JMP AX // 將m->g0儲存到g MOVQ SI, g(CX) // g = m->g0 // 將g->sched.sp恢複到SP寄存器 // 即使用g0的棧 MOVQ (g_sched+gobuf_sp)(SI), SP // sp = m->g0->sched.sp // AX進棧 PUSHQ AX MOVQ DI, DX // 將fn的地址複製到DI MOVQ 0(DI), DI // 調用函數 CALL DI // AX出棧 POPQ AX MOVQ $runtime·badmcall2(SB), AX JMP AX RET
park_m函數的功能分為三部分,第一部分讓當前G和當前M脫離關係,第二部分是調用解鎖函數,這裡是調用netpoll.go源檔案中的netpollblockcommit函數:
// runtime·park continuation on g0.voidruntime·park_m(G *gp){ bool ok; // 設定當前g為Gwaiting狀態 runtime·casgstatus(gp, Grunning, Gwaiting); // 讓當前g和m脫離關係 dropg(); if(g->m->waitunlockf) { ok = g->m->waitunlockf(gp, g->m->waitlock); g->m->waitunlockf = nil; g->m->waitlock = nil; // 返回0為false,非0為true // 0說明g->m->waitlock發生了變化,即不是在gopark是設定的(pdWait) // 說明了脫離了WAIT狀態,應該設定為Grunnable,並執行g if(!ok) { runtime·casgstatus(gp, Gwaiting, Grunnable); execute(gp); // Schedule it back, never returns. } } // 這裡是調度當前m繼續執行其他g // 而不是上面執行execute schedule();}
netpollblockcommit函數,設定gpp為pdWait,設定成功返回1,否則返回0。1為true,0為false:
func netpollblockcommit(gp *g, gpp unsafe.Pointer) bool { return casuintptr((*uintptr)(gpp), pdWait, uintptr(unsafe.Pointer(gp)))}
到這裡當前goroutine對socket檔案描述符的等待IO繼續的行為已經完成。過程中首先儘早嘗試判斷IO是否已經就緒,如果未就緒則掛起當前goroutine,掛起之後再次判斷IO是否就緒,如果還未就緒則調度當前M運行其他G。如果是在調度goroutine之前IO已經就緒,則不會使當前goroutine進入調度隊列,會直接運行剛才掛起的G。否則當前goroutine會進入調度隊列。
接下來是等待runtime將其喚醒。runtime在執行findrunnablequeue、starttheworld,sysmon函數時,都會調用netpoll_epoll.go中的netpoll函數,尋找到IO就緒的socket檔案描述符,並找到這些socket檔案描述符對應的輪詢者中附帶的資訊,根據這些資訊將之前等待這些socket檔案描述符就緒的goroutine狀態修改為Grunnable。在以上函數中,執行完netpoll之後,會找到一個就緒的goroutine列表,接下來將就緒的goroutine加入到調度隊列中,等待調度運行。
在netpoll_epoll.go中的netpoll函數中,epoll_wait函數返回N個發生事件的檔案描述符對應的epollevent,接著對於每個event使用其data屬性,將event.data轉換為*pollDesc類型,再調用netpoll.go中的netpollready函數,將*pollDesc類型中的G資料類型去除,並附加到netpoll函數的調用者傳遞的G鏈表中:
// 將ev.data轉換為*pollDesc類型pd := *(**pollDesc)(unsafe.Pointer(&ev.data))// 調用netpollready將取出pd中儲存的G,並添加到鏈表中netpollready((**g)(noescape(unsafe.Pointer(&gp))), pd, mode)
所以runtime在執行findrunnablequeue、starttheworld,sysmon函數中會執行netpoll函數,並返回N個goroutine。這些goroutine期待的網路事件已經發生,runtime會將這些goroutine放入到當前P的可運行隊列中,接下來調度它們並運行。