天天看點

golang排程器底層實作( G、M、P)

go的排程器隻要實作在 runtime 包中,路徑為: ./src/runtime/proc.go 檔案中。

go語言其實是在作業系統提供的核心線程之上搭建了一個特有得 【兩級線程】模型。下面再說兩級線程模型前,有三個必知的核心元素。(G、M、P)

G:Goroutine的縮寫,一個G代表了對一段需要被執行的Go語言代碼的封裝

M:Machine的縮寫,一個M代表了一個核心線程

P:Processor的縮寫,一個P代表了M所需的上下文環境

簡單的來說,一個G的執行需要M和P的支援。一個M在與一個P關聯之後形成了一個有效的G運作環境【核心線程 + 上下文環境】。每個P都會包含一個可運作的G的隊列 (runq )。

好了下面我們來具體的看看 G、M、P

M結構體的定義

type m struct {
  g0      *g     // goroutine with scheduling stack
  morebuf gobuf  // gobuf arg to morestack
  divmod  uint32 // div/mod denominator for arm - known to liblink

  // Fields not known to debuggers.
  procid        uint64       // for debuggers, but offset not hard-coded
  gsignal       *g           // signal-handling g
  goSigStack    gsignalStack // Go-allocated signal handling stack
  sigmask       sigset       // storage for saved signal mask
  tls           [6]uintptr   // thread-local storage (for x86 extern register)
  mstartfn      func()
  curg          *g       // current running goroutine
  caughtsig     guintptr // goroutine running during fatal signal
  p             puintptr // attached p for executing go code (nil if not executing go code)
  nextp         puintptr
  oldp          puintptr // the p that was attached before executing a syscall
  id            int64
  mallocing     int32
  throwing      int32
  preemptoff    string // if != "", keep curg running on this m
  locks         int32
  dying         int32
  profilehz     int32
  spinning      bool // m is out of work and is actively looking for work
  blocked       bool // m is blocked on a note
  inwb          bool // m is executing a write barrier
  newSigstack   bool // minit on C thread called sigaltstack
  printlock     int8
  incgo         bool   // m is executing a cgo call
  freeWait      uint32 // if == 0, safe to free g0 and delete m (atomic)
  fastrand      [2]uint32
  needextram    bool
  traceback     uint8
  ncgocall      uint64      // number of cgo calls in total
  ncgo          int32       // number of cgo calls currently in progress
  cgoCallersUse uint32      // if non-zero, cgoCallers in use temporarily
  cgoCallers    *cgoCallers // cgo traceback if crashing in cgo call
  park          note
  alllink       *m // on allm
  schedlink     muintptr
  mcache        *mcache
  lockedg       guintptr
  createstack   [32]uintptr    // stack that created this thread.
  lockedExt     uint32         // tracking for external LockOSThread
  lockedInt     uint32         // tracking for internal lockOSThread
  nextwaitm     muintptr       // next m waiting for lock
  waitunlockf   unsafe.Pointer // todo go func(*g, unsafe.pointer) bool
  waitlock      unsafe.Pointer
  waittraceev   byte
  waittraceskip int
  startingtrace bool
  syscalltick   uint32
  thread        uintptr // thread handle
  freelink      *m      // on sched.freem

  // these are here because they are too large to be on the stack
  // of low-level NOSPLIT functions.
  libcall   libcall
  libcallpc uintptr // for cpu profiler
  libcallsp uintptr
  libcallg  guintptr
  syscall   libcall // stores syscall parameters on windows

  vdsoSP uintptr // SP for traceback while in VDSO call (0 if not in call)
  vdsoPC uintptr // PC for traceback while in VDSO call

  mOS
}      

P結構體的定義

type p struct {
  lock mutex

  id          int32
  status      uint32 // one of pidle/prunning/...
  link        puintptr
  schedtick   uint32     // incremented on every scheduler call
  syscalltick uint32     // incremented on every system call
  sysmontick  sysmontick // last tick observed by sysmon
  m           muintptr   // back-link to associated m (nil if idle)
  mcache      *mcache
  racectx     uintptr

  deferpool    [5][]*_defer // pool of available defer structs of different sizes (see panic.go)
  deferpoolbuf [5][32]*_defer

  // Cache of goroutine ids, amortizes accesses to runtime·sched.goidgen.
  goidcache    uint64
  goidcacheend uint64

  // Queue of runnable goroutines. Accessed without lock.
  runqhead uint32
  runqtail uint32
  runq     [256]guintptr
  // runnext, if non-nil, is a runnable G that was ready'd by
  // the current G and should be run next instead of what's in
  // runq if there's time remaining in the running G's time
  // slice. It will inherit the time left in the current time
  // slice. If a set of goroutines is locked in a
  // communicate-and-wait pattern, this schedules that set as a
  // unit and eliminates the (potentially large) scheduling
  // latency that otherwise arises from adding the ready'd
  // goroutines to the end of the run queue.
  runnext guintptr

  // Available G's (status == Gdead)
  gFree struct {
    gList
    n int32
  }

  sudogcache []*sudog
  sudogbuf   [128]*sudog

  tracebuf traceBufPtr

  // traceSweep indicates the sweep events should be traced.
  // This is used to defer the sweep start event until a span
  // has actually been swept.
  traceSweep bool
  // traceSwept and traceReclaimed track the number of bytes
  // swept and reclaimed by sweeping in the current sweep loop.
  traceSwept, traceReclaimed uintptr

  palloc persistentAlloc // per-P to avoid mutex

  // Per-P GC state
  gcAssistTime         int64 // Nanoseconds in assistAlloc
  gcFractionalMarkTime int64 // Nanoseconds in fractional mark worker
  gcBgMarkWorker       guintptr
  gcMarkWorkerMode     gcMarkWorkerMode

  // gcMarkWorkerStartTime is the nanotime() at which this mark
  // worker started.
  gcMarkWorkerStartTime int64

  // gcw is this P's GC work buffer cache. The work buffer is
  // filled by write barriers, drained by mutator assists, and
  // disposed on certain GC state transitions.
  gcw gcWork

  // wbBuf is this P's GC write barrier buffer.
  //
  // TODO: Consider caching this in the running G.
  wbBuf wbBuf

  runSafePointFn uint32 // if 1, run sched.safePointFn at next safe point

  pad cpu.CacheLinePad
}      

G結構體的定義

type g struct {
  // Stack parameters.
  // stack describes the actual stack memory: [stack.lo, stack.hi).
  // stackguard0 is the stack pointer compared in the Go stack growth prologue.
  // It is stack.lo+StackGuard normally, but can be StackPreempt to trigger a preemption.
  // stackguard1 is the stack pointer compared in the C stack growth prologue.
  // It is stack.lo+StackGuard on g0 and gsignal stacks.
  // It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash).
  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 // passed parameter on wakeup
  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
  paniconfault   bool       // panic (instead of crash) on unexpected fault address
  preemptscan    bool       // preempted g does scan for gc
  gcscandone     bool       // g has scanned stack; protected by _Gscan bit in status
  gcscanvalid    bool       // false at start of gc cycle, true if G has not run since last scan; TODO: remove?
  throwsplit     bool       // must not split stack
  raceignore     int8       // ignore race detection events
  sysblocktraced bool       // StartTrace has emitted EvGoInSyscall about this goroutine
  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?

  // Per-G GC state

  // gcAssistBytes is this G's GC assist credit in terms of
  // bytes allocated. If this is positive, then the G has credit
  // to allocate gcAssistBytes bytes without assisting. If this
  // is negative, then the G must correct this by performing
  // scan work. We track this in bytes to make it fast to update
  // and check for debt in the malloc hot path. The assist ratio
  // determines how this corresponds to scan work debt.
  gcAssistBytes int64
}