github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/runtime/runtime2.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime
     6  
     7  import (
     8  	"internal/cpu"
     9  	"runtime/internal/atomic"
    10  	"runtime/internal/sys"
    11  	"unsafe"
    12  )
    13  
    14  // defined constants
    15  const (
    16  	// G status
    17  	//
    18  	// Beyond indicating the general state of a G, the G status
    19  	// acts like a lock on the goroutine's stack (and hence its
    20  	// ability to execute user code).
    21  	//
    22  	// If you add to this list, add to the list
    23  	// of "okay during garbage collection" status
    24  	// in mgcmark.go too.
    25  
    26  	// _Gidle means this goroutine was just allocated and has not
    27  	// yet been initialized.
    28  	_Gidle = iota // 0
    29  
    30  	// _Grunnable means this goroutine is on a run queue. It is
    31  	// not currently executing user code. The stack is not owned.
    32  	_Grunnable // 1
    33  
    34  	// _Grunning means this goroutine may execute user code. The
    35  	// stack is owned by this goroutine. It is not on a run queue.
    36  	// It is assigned an M and a P.
    37  	_Grunning // 2
    38  
    39  	// _Gsyscall means this goroutine is executing a system call.
    40  	// It is not executing user code. The stack is owned by this
    41  	// goroutine. It is not on a run queue. It is assigned an M.
    42  	_Gsyscall // 3
    43  
    44  	// _Gwaiting means this goroutine is blocked in the runtime.
    45  	// It is not executing user code. It is not on a run queue,
    46  	// but should be recorded somewhere (e.g., a channel wait
    47  	// queue) so it can be ready()d when necessary. The stack is
    48  	// not owned *except* that a channel operation may read or
    49  	// write parts of the stack under the appropriate channel
    50  	// lock. Otherwise, it is not safe to access the stack after a
    51  	// goroutine enters _Gwaiting (e.g., it may get moved).
    52  	_Gwaiting // 4
    53  
    54  	// _Gmoribund_unused is currently unused, but hardcoded in gdb
    55  	// scripts.
    56  	_Gmoribund_unused // 5
    57  
    58  	// _Gdead means this goroutine is currently unused. It may be
    59  	// just exited, on a free list, or just being initialized. It
    60  	// is not executing user code. It may or may not have a stack
    61  	// allocated. The G and its stack (if any) are owned by the M
    62  	// that is exiting the G or that obtained the G from the free
    63  	// list.
    64  	_Gdead // 6
    65  
    66  	// _Genqueue_unused is currently unused.
    67  	_Genqueue_unused // 7
    68  
    69  	// _Gcopystack means this goroutine's stack is being moved. It
    70  	// is not executing user code and is not on a run queue. The
    71  	// stack is owned by the goroutine that put it in _Gcopystack.
    72  	_Gcopystack // 8
    73  
    74  	// _Gscan combined with one of the above states other than
    75  	// _Grunning indicates that GC is scanning the stack. The
    76  	// goroutine is not executing user code and the stack is owned
    77  	// by the goroutine that set the _Gscan bit.
    78  	//
    79  	// _Gscanrunning is different: it is used to briefly block
    80  	// state transitions while GC signals the G to scan its own
    81  	// stack. This is otherwise like _Grunning.
    82  	//
    83  	// atomicstatus&~Gscan gives the state the goroutine will
    84  	// return to when the scan completes.
    85  	_Gscan         = 0x1000
    86  	_Gscanrunnable = _Gscan + _Grunnable // 0x1001
    87  	_Gscanrunning  = _Gscan + _Grunning  // 0x1002
    88  	_Gscansyscall  = _Gscan + _Gsyscall  // 0x1003
    89  	_Gscanwaiting  = _Gscan + _Gwaiting  // 0x1004
    90  )
    91  
    92  const (
    93  	// P status
    94  	_Pidle    = iota
    95  	_Prunning // Only this P is allowed to change from _Prunning.
    96  	_Psyscall
    97  	_Pgcstop
    98  	_Pdead
    99  )
   100  
   101  // Mutual exclusion locks.  In the uncontended case,
   102  // as fast as spin locks (just a few user-level instructions),
   103  // but on the contention path they sleep in the kernel.
   104  // A zeroed Mutex is unlocked (no need to initialize each lock).
   105  type mutex struct {
   106  	// Futex-based impl treats it as uint32 key,
   107  	// while sema-based impl as M* waitm.
   108  	// Used to be a union, but unions break precise GC.
   109  	key uintptr
   110  }
   111  
   112  // sleep and wakeup on one-time events.
   113  // before any calls to notesleep or notewakeup,
   114  // must call noteclear to initialize the Note.
   115  // then, exactly one thread can call notesleep
   116  // and exactly one thread can call notewakeup (once).
   117  // once notewakeup has been called, the notesleep
   118  // will return.  future notesleep will return immediately.
   119  // subsequent noteclear must be called only after
   120  // previous notesleep has returned, e.g. it's disallowed
   121  // to call noteclear straight after notewakeup.
   122  //
   123  // notetsleep is like notesleep but wakes up after
   124  // a given number of nanoseconds even if the event
   125  // has not yet happened.  if a goroutine uses notetsleep to
   126  // wake up early, it must wait to call noteclear until it
   127  // can be sure that no other goroutine is calling
   128  // notewakeup.
   129  //
   130  // notesleep/notetsleep are generally called on g0,
   131  // notetsleepg is similar to notetsleep but is called on user g.
   132  type note struct {
   133  	// Futex-based impl treats it as uint32 key,
   134  	// while sema-based impl as M* waitm.
   135  	// Used to be a union, but unions break precise GC.
   136  	key uintptr
   137  }
   138  
   139  type funcval struct {
   140  	fn uintptr
   141  	// variable-size, fn-specific data here
   142  }
   143  
   144  type iface struct {
   145  	tab  *itab
   146  	data unsafe.Pointer
   147  }
   148  
   149  type eface struct {
   150  	_type *_type
   151  	data  unsafe.Pointer
   152  }
   153  
   154  func efaceOf(ep *interface{}) *eface {
   155  	return (*eface)(unsafe.Pointer(ep))
   156  }
   157  
   158  // The guintptr, muintptr, and puintptr are all used to bypass write barriers.
   159  // It is particularly important to avoid write barriers when the current P has
   160  // been released, because the GC thinks the world is stopped, and an
   161  // unexpected write barrier would not be synchronized with the GC,
   162  // which can lead to a half-executed write barrier that has marked the object
   163  // but not queued it. If the GC skips the object and completes before the
   164  // queuing can occur, it will incorrectly free the object.
   165  //
   166  // We tried using special assignment functions invoked only when not
   167  // holding a running P, but then some updates to a particular memory
   168  // word went through write barriers and some did not. This breaks the
   169  // write barrier shadow checking mode, and it is also scary: better to have
   170  // a word that is completely ignored by the GC than to have one for which
   171  // only a few updates are ignored.
   172  //
   173  // Gs and Ps are always reachable via true pointers in the
   174  // allgs and allp lists or (during allocation before they reach those lists)
   175  // from stack variables.
   176  //
   177  // Ms are always reachable via true pointers either from allm or
   178  // freem. Unlike Gs and Ps we do free Ms, so it's important that
   179  // nothing ever hold an muintptr across a safe point.
   180  
   181  // A guintptr holds a goroutine pointer, but typed as a uintptr
   182  // to bypass write barriers. It is used in the Gobuf goroutine state
   183  // and in scheduling lists that are manipulated without a P.
   184  //
   185  // The Gobuf.g goroutine pointer is almost always updated by assembly code.
   186  // In one of the few places it is updated by Go code - func save - it must be
   187  // treated as a uintptr to avoid a write barrier being emitted at a bad time.
   188  // Instead of figuring out how to emit the write barriers missing in the
   189  // assembly manipulation, we change the type of the field to uintptr,
   190  // so that it does not require write barriers at all.
   191  //
   192  // Goroutine structs are published in the allg list and never freed.
   193  // That will keep the goroutine structs from being collected.
   194  // There is never a time that Gobuf.g's contain the only references
   195  // to a goroutine: the publishing of the goroutine in allg comes first.
   196  // Goroutine pointers are also kept in non-GC-visible places like TLS,
   197  // so I can't see them ever moving. If we did want to start moving data
   198  // in the GC, we'd need to allocate the goroutine structs from an
   199  // alternate arena. Using guintptr doesn't make that problem any worse.
   200  type guintptr uintptr
   201  
   202  //go:nosplit
   203  func (gp guintptr) ptr() *g { return (*g)(unsafe.Pointer(gp)) }
   204  
   205  //go:nosplit
   206  func (gp *guintptr) set(g *g) { *gp = guintptr(unsafe.Pointer(g)) }
   207  
   208  //go:nosplit
   209  func (gp *guintptr) cas(old, new guintptr) bool {
   210  	return atomic.Casuintptr((*uintptr)(unsafe.Pointer(gp)), uintptr(old), uintptr(new))
   211  }
   212  
   213  // setGNoWB performs *gp = new without a write barrier.
   214  // For times when it's impractical to use a guintptr.
   215  //go:nosplit
   216  //go:nowritebarrier
   217  func setGNoWB(gp **g, new *g) {
   218  	(*guintptr)(unsafe.Pointer(gp)).set(new)
   219  }
   220  
   221  type puintptr uintptr
   222  
   223  //go:nosplit
   224  func (pp puintptr) ptr() *p { return (*p)(unsafe.Pointer(pp)) }
   225  
   226  //go:nosplit
   227  func (pp *puintptr) set(p *p) { *pp = puintptr(unsafe.Pointer(p)) }
   228  
   229  // muintptr is a *m that is not tracked by the garbage collector.
   230  //
   231  // Because we do free Ms, there are some additional constrains on
   232  // muintptrs:
   233  //
   234  // 1. Never hold an muintptr locally across a safe point.
   235  //
   236  // 2. Any muintptr in the heap must be owned by the M itself so it can
   237  //    ensure it is not in use when the last true *m is released.
   238  type muintptr uintptr
   239  
   240  //go:nosplit
   241  func (mp muintptr) ptr() *m { return (*m)(unsafe.Pointer(mp)) }
   242  
   243  //go:nosplit
   244  func (mp *muintptr) set(m *m) { *mp = muintptr(unsafe.Pointer(m)) }
   245  
   246  // setMNoWB performs *mp = new without a write barrier.
   247  // For times when it's impractical to use an muintptr.
   248  //go:nosplit
   249  //go:nowritebarrier
   250  func setMNoWB(mp **m, new *m) {
   251  	(*muintptr)(unsafe.Pointer(mp)).set(new)
   252  }
   253  
   254  type gobuf struct {
   255  	// The offsets of sp, pc, and g are known to (hard-coded in) libmach.
   256  	//
   257  	// ctxt is unusual with respect to GC: it may be a
   258  	// heap-allocated funcval, so GC needs to track it, but it
   259  	// needs to be set and cleared from assembly, where it's
   260  	// difficult to have write barriers. However, ctxt is really a
   261  	// saved, live register, and we only ever exchange it between
   262  	// the real register and the gobuf. Hence, we treat it as a
   263  	// root during stack scanning, which means assembly that saves
   264  	// and restores it doesn't need write barriers. It's still
   265  	// typed as a pointer so that any other writes from Go get
   266  	// write barriers.
   267  	sp   uintptr
   268  	pc   uintptr
   269  	g    guintptr
   270  	ctxt unsafe.Pointer
   271  	ret  sys.Uintreg
   272  	lr   uintptr
   273  	bp   uintptr // for GOEXPERIMENT=framepointer
   274  }
   275  
   276  // sudog represents a g in a wait list, such as for sending/receiving
   277  // on a channel.
   278  //
   279  // sudog is necessary because the g ↔ synchronization object relation
   280  // is many-to-many. A g can be on many wait lists, so there may be
   281  // many sudogs for one g; and many gs may be waiting on the same
   282  // synchronization object, so there may be many sudogs for one object.
   283  //
   284  // sudogs are allocated from a special pool. Use acquireSudog and
   285  // releaseSudog to allocate and free them.
   286  type sudog struct {
   287  	// The following fields are protected by the hchan.lock of the
   288  	// channel this sudog is blocking on. shrinkstack depends on
   289  	// this for sudogs involved in channel ops.
   290  
   291  	g *g
   292  
   293  	// isSelect indicates g is participating in a select, so
   294  	// g.selectDone must be CAS'd to win the wake-up race.
   295  	isSelect bool
   296  	next     *sudog
   297  	prev     *sudog
   298  	elem     unsafe.Pointer // data element (may point to stack)
   299  
   300  	// The following fields are never accessed concurrently.
   301  	// For channels, waitlink is only accessed by g.
   302  	// For semaphores, all fields (including the ones above)
   303  	// are only accessed when holding a semaRoot lock.
   304  
   305  	acquiretime int64
   306  	releasetime int64
   307  	ticket      uint32
   308  	parent      *sudog // semaRoot binary tree
   309  	waitlink    *sudog // g.waiting list or semaRoot
   310  	waittail    *sudog // semaRoot
   311  	c           *hchan // channel
   312  }
   313  
   314  type libcall struct {
   315  	fn   uintptr
   316  	n    uintptr // number of parameters
   317  	args uintptr // parameters
   318  	r1   uintptr // return values
   319  	r2   uintptr
   320  	err  uintptr // error number
   321  }
   322  
   323  // describes how to handle callback
   324  type wincallbackcontext struct {
   325  	gobody       unsafe.Pointer // go function to call
   326  	argsize      uintptr        // callback arguments size (in bytes)
   327  	restorestack uintptr        // adjust stack on return by (in bytes) (386 only)
   328  	cleanstack   bool
   329  }
   330  
   331  // Stack describes a Go execution stack.
   332  // The bounds of the stack are exactly [lo, hi),
   333  // with no implicit data structures on either side.
   334  type stack struct {
   335  	lo uintptr
   336  	hi uintptr
   337  }
   338  
   339  type g struct {
   340  	// Stack parameters.
   341  	// stack describes the actual stack memory: [stack.lo, stack.hi).
   342  	// stackguard0 is the stack pointer compared in the Go stack growth prologue.
   343  	// It is stack.lo+StackGuard normally, but can be StackPreempt to trigger a preemption.
   344  	// stackguard1 is the stack pointer compared in the C stack growth prologue.
   345  	// It is stack.lo+StackGuard on g0 and gsignal stacks.
   346  	// It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash).
   347  	stack       stack   // offset known to runtime/cgo
   348  	stackguard0 uintptr // offset known to liblink
   349  	stackguard1 uintptr // offset known to liblink
   350  
   351  	_panic         *_panic // innermost panic - offset known to liblink
   352  	_defer         *_defer // innermost defer
   353  	m              *m      // current m; offset known to arm liblink
   354  	sched          gobuf
   355  	syscallsp      uintptr        // if status==Gsyscall, syscallsp = sched.sp to use during gc
   356  	syscallpc      uintptr        // if status==Gsyscall, syscallpc = sched.pc to use during gc
   357  	stktopsp       uintptr        // expected sp at top of stack, to check in traceback
   358  	param          unsafe.Pointer // passed parameter on wakeup
   359  	atomicstatus   uint32
   360  	stackLock      uint32 // sigprof/scang lock; TODO: fold in to atomicstatus
   361  	goid           int64
   362  	schedlink      guintptr
   363  	waitsince      int64      // approx time when the g become blocked
   364  	waitreason     waitReason // if status==Gwaiting
   365  	preempt        bool       // preemption signal, duplicates stackguard0 = stackpreempt
   366  	paniconfault   bool       // panic (instead of crash) on unexpected fault address
   367  	preemptscan    bool       // preempted g does scan for gc
   368  	gcscandone     bool       // g has scanned stack; protected by _Gscan bit in status
   369  	gcscanvalid    bool       // false at start of gc cycle, true if G has not run since last scan; TODO: remove?
   370  	throwsplit     bool       // must not split stack
   371  	raceignore     int8       // ignore race detection events
   372  	sysblocktraced bool       // StartTrace has emitted EvGoInSyscall about this goroutine
   373  	sysexitticks   int64      // cputicks when syscall has returned (for tracing)
   374  	traceseq       uint64     // trace event sequencer
   375  	tracelastp     puintptr   // last P emitted an event for this goroutine
   376  	lockedm        muintptr
   377  	sig            uint32
   378  	writebuf       []byte
   379  	sigcode0       uintptr
   380  	sigcode1       uintptr
   381  	sigpc          uintptr
   382  	gopc           uintptr         // pc of go statement that created this goroutine
   383  	ancestors      *[]ancestorInfo // ancestor information goroutine(s) that created this goroutine (only used if debug.tracebackancestors)
   384  	startpc        uintptr         // pc of goroutine function
   385  	racectx        uintptr
   386  	waiting        *sudog         // sudog structures this g is waiting on (that have a valid elem ptr); in lock order
   387  	cgoCtxt        []uintptr      // cgo traceback context
   388  	labels         unsafe.Pointer // profiler labels
   389  	timer          *timer         // cached timer for time.Sleep
   390  	selectDone     uint32         // are we participating in a select and did someone win the race?
   391  
   392  	// Per-G GC state
   393  
   394  	// gcAssistBytes is this G's GC assist credit in terms of
   395  	// bytes allocated. If this is positive, then the G has credit
   396  	// to allocate gcAssistBytes bytes without assisting. If this
   397  	// is negative, then the G must correct this by performing
   398  	// scan work. We track this in bytes to make it fast to update
   399  	// and check for debt in the malloc hot path. The assist ratio
   400  	// determines how this corresponds to scan work debt.
   401  	gcAssistBytes int64
   402  }
   403  
   404  type m struct {
   405  	g0      *g     // goroutine with scheduling stack
   406  	morebuf gobuf  // gobuf arg to morestack
   407  	divmod  uint32 // div/mod denominator for arm - known to liblink
   408  
   409  	// Fields not known to debuggers.
   410  	procid        uint64       // for debuggers, but offset not hard-coded
   411  	gsignal       *g           // signal-handling g
   412  	goSigStack    gsignalStack // Go-allocated signal handling stack
   413  	sigmask       sigset       // storage for saved signal mask
   414  	tls           [6]uintptr   // thread-local storage (for x86 extern register)
   415  	mstartfn      func()
   416  	curg          *g       // current running goroutine
   417  	caughtsig     guintptr // goroutine running during fatal signal
   418  	p             puintptr // attached p for executing go code (nil if not executing go code)
   419  	nextp         puintptr
   420  	oldp          puintptr // the p that was attached before executing a syscall
   421  	id            int64
   422  	mallocing     int32
   423  	throwing      int32
   424  	preemptoff    string // if != "", keep curg running on this m
   425  	locks         int32
   426  	dying         int32
   427  	profilehz     int32
   428  	spinning      bool // m is out of work and is actively looking for work
   429  	blocked       bool // m is blocked on a note
   430  	inwb          bool // m is executing a write barrier
   431  	newSigstack   bool // minit on C thread called sigaltstack
   432  	printlock     int8
   433  	incgo         bool   // m is executing a cgo call
   434  	freeWait      uint32 // if == 0, safe to free g0 and delete m (atomic)
   435  	fastrand      [2]uint32
   436  	needextram    bool
   437  	traceback     uint8
   438  	ncgocall      uint64      // number of cgo calls in total
   439  	ncgo          int32       // number of cgo calls currently in progress
   440  	cgoCallersUse uint32      // if non-zero, cgoCallers in use temporarily
   441  	cgoCallers    *cgoCallers // cgo traceback if crashing in cgo call
   442  	park          note
   443  	alllink       *m // on allm
   444  	schedlink     muintptr
   445  	mcache        *mcache
   446  	lockedg       guintptr
   447  	createstack   [32]uintptr    // stack that created this thread.
   448  	lockedExt     uint32         // tracking for external LockOSThread
   449  	lockedInt     uint32         // tracking for internal lockOSThread
   450  	nextwaitm     muintptr       // next m waiting for lock
   451  	waitunlockf   unsafe.Pointer // todo go func(*g, unsafe.pointer) bool
   452  	waitlock      unsafe.Pointer
   453  	waittraceev   byte
   454  	waittraceskip int
   455  	startingtrace bool
   456  	syscalltick   uint32
   457  	thread        uintptr // thread handle
   458  	freelink      *m      // on sched.freem
   459  
   460  	// these are here because they are too large to be on the stack
   461  	// of low-level NOSPLIT functions.
   462  	libcall   libcall
   463  	libcallpc uintptr // for cpu profiler
   464  	libcallsp uintptr
   465  	libcallg  guintptr
   466  	syscall   libcall // stores syscall parameters on windows
   467  
   468  	vdsoSP uintptr // SP for traceback while in VDSO call (0 if not in call)
   469  	vdsoPC uintptr // PC for traceback while in VDSO call
   470  
   471  	mOS
   472  }
   473  
   474  type p struct {
   475  	lock mutex
   476  
   477  	id          int32
   478  	status      uint32 // one of pidle/prunning/...
   479  	link        puintptr
   480  	schedtick   uint32     // incremented on every scheduler call
   481  	syscalltick uint32     // incremented on every system call
   482  	sysmontick  sysmontick // last tick observed by sysmon
   483  	m           muintptr   // back-link to associated m (nil if idle)
   484  	mcache      *mcache
   485  	racectx     uintptr
   486  
   487  	deferpool    [5][]*_defer // pool of available defer structs of different sizes (see panic.go)
   488  	deferpoolbuf [5][32]*_defer
   489  
   490  	// Cache of goroutine ids, amortizes accesses to runtime·sched.goidgen.
   491  	goidcache    uint64
   492  	goidcacheend uint64
   493  
   494  	// Queue of runnable goroutines. Accessed without lock.
   495  	runqhead uint32
   496  	runqtail uint32
   497  	runq     [256]guintptr
   498  	// runnext, if non-nil, is a runnable G that was ready'd by
   499  	// the current G and should be run next instead of what's in
   500  	// runq if there's time remaining in the running G's time
   501  	// slice. It will inherit the time left in the current time
   502  	// slice. If a set of goroutines is locked in a
   503  	// communicate-and-wait pattern, this schedules that set as a
   504  	// unit and eliminates the (potentially large) scheduling
   505  	// latency that otherwise arises from adding the ready'd
   506  	// goroutines to the end of the run queue.
   507  	runnext guintptr
   508  
   509  	// Available G's (status == Gdead)
   510  	gFree struct {
   511  		gList
   512  		n int32
   513  	}
   514  
   515  	sudogcache []*sudog
   516  	sudogbuf   [128]*sudog
   517  
   518  	tracebuf traceBufPtr
   519  
   520  	// traceSweep indicates the sweep events should be traced.
   521  	// This is used to defer the sweep start event until a span
   522  	// has actually been swept.
   523  	traceSweep bool
   524  	// traceSwept and traceReclaimed track the number of bytes
   525  	// swept and reclaimed by sweeping in the current sweep loop.
   526  	traceSwept, traceReclaimed uintptr
   527  
   528  	palloc persistentAlloc // per-P to avoid mutex
   529  
   530  	// Per-P GC state
   531  	gcAssistTime         int64 // Nanoseconds in assistAlloc
   532  	gcFractionalMarkTime int64 // Nanoseconds in fractional mark worker
   533  	gcBgMarkWorker       guintptr
   534  	gcMarkWorkerMode     gcMarkWorkerMode
   535  
   536  	// gcMarkWorkerStartTime is the nanotime() at which this mark
   537  	// worker started.
   538  	gcMarkWorkerStartTime int64
   539  
   540  	// gcw is this P's GC work buffer cache. The work buffer is
   541  	// filled by write barriers, drained by mutator assists, and
   542  	// disposed on certain GC state transitions.
   543  	gcw gcWork
   544  
   545  	// wbBuf is this P's GC write barrier buffer.
   546  	//
   547  	// TODO: Consider caching this in the running G.
   548  	wbBuf wbBuf
   549  
   550  	runSafePointFn uint32 // if 1, run sched.safePointFn at next safe point
   551  
   552  	pad cpu.CacheLinePad
   553  }
   554  
   555  type schedt struct {
   556  	// accessed atomically. keep at top to ensure alignment on 32-bit systems.
   557  	goidgen  uint64
   558  	lastpoll uint64
   559  
   560  	lock mutex
   561  
   562  	// When increasing nmidle, nmidlelocked, nmsys, or nmfreed, be
   563  	// sure to call checkdead().
   564  
   565  	midle        muintptr // idle m's waiting for work
   566  	nmidle       int32    // number of idle m's waiting for work
   567  	nmidlelocked int32    // number of locked m's waiting for work
   568  	mnext        int64    // number of m's that have been created and next M ID
   569  	maxmcount    int32    // maximum number of m's allowed (or die)
   570  	nmsys        int32    // number of system m's not counted for deadlock
   571  	nmfreed      int64    // cumulative number of freed m's
   572  
   573  	ngsys uint32 // number of system goroutines; updated atomically
   574  
   575  	pidle      puintptr // idle p's
   576  	npidle     uint32
   577  	nmspinning uint32 // See "Worker thread parking/unparking" comment in proc.go.
   578  
   579  	// Global runnable queue.
   580  	runq     gQueue
   581  	runqsize int32
   582  
   583  	// disable controls selective disabling of the scheduler.
   584  	//
   585  	// Use schedEnableUser to control this.
   586  	//
   587  	// disable is protected by sched.lock.
   588  	disable struct {
   589  		// user disables scheduling of user goroutines.
   590  		user     bool
   591  		runnable gQueue // pending runnable Gs
   592  		n        int32  // length of runnable
   593  	}
   594  
   595  	// Global cache of dead G's.
   596  	gFree struct {
   597  		lock    mutex
   598  		stack   gList // Gs with stacks
   599  		noStack gList // Gs without stacks
   600  		n       int32
   601  	}
   602  
   603  	// Central cache of sudog structs.
   604  	sudoglock  mutex
   605  	sudogcache *sudog
   606  
   607  	// Central pool of available defer structs of different sizes.
   608  	deferlock mutex
   609  	deferpool [5]*_defer
   610  
   611  	// freem is the list of m's waiting to be freed when their
   612  	// m.exited is set. Linked through m.freelink.
   613  	freem *m
   614  
   615  	gcwaiting  uint32 // gc is waiting to run
   616  	stopwait   int32
   617  	stopnote   note
   618  	sysmonwait uint32
   619  	sysmonnote note
   620  
   621  	// safepointFn should be called on each P at the next GC
   622  	// safepoint if p.runSafePointFn is set.
   623  	safePointFn   func(*p)
   624  	safePointWait int32
   625  	safePointNote note
   626  
   627  	profilehz int32 // cpu profiling rate
   628  
   629  	procresizetime int64 // nanotime() of last change to gomaxprocs
   630  	totaltime      int64 // ∫gomaxprocs dt up to procresizetime
   631  }
   632  
   633  // Values for the flags field of a sigTabT.
   634  const (
   635  	_SigNotify   = 1 << iota // let signal.Notify have signal, even if from kernel
   636  	_SigKill                 // if signal.Notify doesn't take it, exit quietly
   637  	_SigThrow                // if signal.Notify doesn't take it, exit loudly
   638  	_SigPanic                // if the signal is from the kernel, panic
   639  	_SigDefault              // if the signal isn't explicitly requested, don't monitor it
   640  	_SigGoExit               // cause all runtime procs to exit (only used on Plan 9).
   641  	_SigSetStack             // add SA_ONSTACK to libc handler
   642  	_SigUnblock              // always unblock; see blockableSig
   643  	_SigIgn                  // _SIG_DFL action is to ignore the signal
   644  )
   645  
   646  // Layout of in-memory per-function information prepared by linker
   647  // See https://golang.org/s/go12symtab.
   648  // Keep in sync with linker (../cmd/link/internal/ld/pcln.go:/pclntab)
   649  // and with package debug/gosym and with symtab.go in package runtime.
   650  type _func struct {
   651  	entry   uintptr // start pc
   652  	nameoff int32   // function name
   653  
   654  	args        int32  // in/out args size
   655  	deferreturn uint32 // offset of a deferreturn block from entry, if any.
   656  
   657  	pcsp      int32
   658  	pcfile    int32
   659  	pcln      int32
   660  	npcdata   int32
   661  	funcID    funcID  // set for certain special runtime functions
   662  	_         [2]int8 // unused
   663  	nfuncdata uint8   // must be last
   664  }
   665  
   666  // Pseudo-Func that is returned for PCs that occur in inlined code.
   667  // A *Func can be either a *_func or a *funcinl, and they are distinguished
   668  // by the first uintptr.
   669  type funcinl struct {
   670  	zero  uintptr // set to 0 to distinguish from _func
   671  	entry uintptr // entry of the real (the "outermost") frame.
   672  	name  string
   673  	file  string
   674  	line  int
   675  }
   676  
   677  // layout of Itab known to compilers
   678  // allocated in non-garbage-collected memory
   679  // Needs to be in sync with
   680  // ../cmd/compile/internal/gc/reflect.go:/^func.dumptypestructs.
   681  type itab struct {
   682  	inter *interfacetype
   683  	_type *_type
   684  	hash  uint32 // copy of _type.hash. Used for type switches.
   685  	_     [4]byte
   686  	fun   [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
   687  }
   688  
   689  // Lock-free stack node.
   690  // // Also known to export_test.go.
   691  type lfnode struct {
   692  	next    uint64
   693  	pushcnt uintptr
   694  }
   695  
   696  type forcegcstate struct {
   697  	lock mutex
   698  	g    *g
   699  	idle uint32
   700  }
   701  
   702  // startup_random_data holds random bytes initialized at startup. These come from
   703  // the ELF AT_RANDOM auxiliary vector (vdso_linux_amd64.go or os_linux_386.go).
   704  var startupRandomData []byte
   705  
   706  // extendRandom extends the random numbers in r[:n] to the whole slice r.
   707  // Treats n<0 as n==0.
   708  func extendRandom(r []byte, n int) {
   709  	if n < 0 {
   710  		n = 0
   711  	}
   712  	for n < len(r) {
   713  		// Extend random bits using hash function & time seed
   714  		w := n
   715  		if w > 16 {
   716  			w = 16
   717  		}
   718  		h := memhash(unsafe.Pointer(&r[n-w]), uintptr(nanotime()), uintptr(w))
   719  		for i := 0; i < sys.PtrSize && n < len(r); i++ {
   720  			r[n] = byte(h)
   721  			n++
   722  			h >>= 8
   723  		}
   724  	}
   725  }
   726  
   727  // A _defer holds an entry on the list of deferred calls.
   728  // If you add a field here, add code to clear it in freedefer.
   729  type _defer struct {
   730  	siz     int32
   731  	started bool
   732  	sp      uintptr // sp at time of defer
   733  	pc      uintptr
   734  	fn      *funcval
   735  	_panic  *_panic // panic that is running defer
   736  	link    *_defer
   737  }
   738  
   739  // A _panic holds information about an active panic.
   740  //
   741  // This is marked go:notinheap because _panic values must only ever
   742  // live on the stack.
   743  //
   744  // The argp and link fields are stack pointers, but don't need special
   745  // handling during stack growth: because they are pointer-typed and
   746  // _panic values only live on the stack, regular stack pointer
   747  // adjustment takes care of them.
   748  //
   749  //go:notinheap
   750  type _panic struct {
   751  	argp      unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblink
   752  	arg       interface{}    // argument to panic
   753  	link      *_panic        // link to earlier panic
   754  	recovered bool           // whether this panic is over
   755  	aborted   bool           // the panic was aborted
   756  }
   757  
   758  // stack traces
   759  type stkframe struct {
   760  	fn       funcInfo   // function being run
   761  	pc       uintptr    // program counter within fn
   762  	continpc uintptr    // program counter where execution can continue, or 0 if not
   763  	lr       uintptr    // program counter at caller aka link register
   764  	sp       uintptr    // stack pointer at pc
   765  	fp       uintptr    // stack pointer at caller aka frame pointer
   766  	varp     uintptr    // top of local variables
   767  	argp     uintptr    // pointer to function arguments
   768  	arglen   uintptr    // number of bytes at argp
   769  	argmap   *bitvector // force use of this argmap
   770  }
   771  
   772  // ancestorInfo records details of where a goroutine was started.
   773  type ancestorInfo struct {
   774  	pcs  []uintptr // pcs from the stack of this goroutine
   775  	goid int64     // goroutine id of this goroutine; original goroutine possibly dead
   776  	gopc uintptr   // pc of go statement that created this goroutine
   777  }
   778  
   779  const (
   780  	_TraceRuntimeFrames = 1 << iota // include frames for internal runtime functions.
   781  	_TraceTrap                      // the initial PC, SP are from a trap, not a return PC from a call
   782  	_TraceJumpStack                 // if traceback is on a systemstack, resume trace at g that called into it
   783  )
   784  
   785  // The maximum number of frames we print for a traceback
   786  const _TracebackMaxFrames = 100
   787  
   788  // A waitReason explains why a goroutine has been stopped.
   789  // See gopark. Do not re-use waitReasons, add new ones.
   790  type waitReason uint8
   791  
   792  const (
   793  	waitReasonZero                  waitReason = iota // ""
   794  	waitReasonGCAssistMarking                         // "GC assist marking"
   795  	waitReasonIOWait                                  // "IO wait"
   796  	waitReasonChanReceiveNilChan                      // "chan receive (nil chan)"
   797  	waitReasonChanSendNilChan                         // "chan send (nil chan)"
   798  	waitReasonDumpingHeap                             // "dumping heap"
   799  	waitReasonGarbageCollection                       // "garbage collection"
   800  	waitReasonGarbageCollectionScan                   // "garbage collection scan"
   801  	waitReasonPanicWait                               // "panicwait"
   802  	waitReasonSelect                                  // "select"
   803  	waitReasonSelectNoCases                           // "select (no cases)"
   804  	waitReasonGCAssistWait                            // "GC assist wait"
   805  	waitReasonGCSweepWait                             // "GC sweep wait"
   806  	waitReasonChanReceive                             // "chan receive"
   807  	waitReasonChanSend                                // "chan send"
   808  	waitReasonFinalizerWait                           // "finalizer wait"
   809  	waitReasonForceGGIdle                             // "force gc (idle)"
   810  	waitReasonSemacquire                              // "semacquire"
   811  	waitReasonSleep                                   // "sleep"
   812  	waitReasonSyncCondWait                            // "sync.Cond.Wait"
   813  	waitReasonTimerGoroutineIdle                      // "timer goroutine (idle)"
   814  	waitReasonTraceReaderBlocked                      // "trace reader (blocked)"
   815  	waitReasonWaitForGCCycle                          // "wait for GC cycle"
   816  	waitReasonGCWorkerIdle                            // "GC worker (idle)"
   817  )
   818  
   819  var waitReasonStrings = [...]string{
   820  	waitReasonZero:                  "",
   821  	waitReasonGCAssistMarking:       "GC assist marking",
   822  	waitReasonIOWait:                "IO wait",
   823  	waitReasonChanReceiveNilChan:    "chan receive (nil chan)",
   824  	waitReasonChanSendNilChan:       "chan send (nil chan)",
   825  	waitReasonDumpingHeap:           "dumping heap",
   826  	waitReasonGarbageCollection:     "garbage collection",
   827  	waitReasonGarbageCollectionScan: "garbage collection scan",
   828  	waitReasonPanicWait:             "panicwait",
   829  	waitReasonSelect:                "select",
   830  	waitReasonSelectNoCases:         "select (no cases)",
   831  	waitReasonGCAssistWait:          "GC assist wait",
   832  	waitReasonGCSweepWait:           "GC sweep wait",
   833  	waitReasonChanReceive:           "chan receive",
   834  	waitReasonChanSend:              "chan send",
   835  	waitReasonFinalizerWait:         "finalizer wait",
   836  	waitReasonForceGGIdle:           "force gc (idle)",
   837  	waitReasonSemacquire:            "semacquire",
   838  	waitReasonSleep:                 "sleep",
   839  	waitReasonSyncCondWait:          "sync.Cond.Wait",
   840  	waitReasonTimerGoroutineIdle:    "timer goroutine (idle)",
   841  	waitReasonTraceReaderBlocked:    "trace reader (blocked)",
   842  	waitReasonWaitForGCCycle:        "wait for GC cycle",
   843  	waitReasonGCWorkerIdle:          "GC worker (idle)",
   844  }
   845  
   846  func (w waitReason) String() string {
   847  	if w < 0 || w >= waitReason(len(waitReasonStrings)) {
   848  		return "unknown wait reason"
   849  	}
   850  	return waitReasonStrings[w]
   851  }
   852  
   853  var (
   854  	allglen    uintptr
   855  	allm       *m
   856  	allp       []*p  // len(allp) == gomaxprocs; may change at safe points, otherwise immutable
   857  	allpLock   mutex // Protects P-less reads of allp and all writes
   858  	gomaxprocs int32
   859  	ncpu       int32
   860  	forcegc    forcegcstate
   861  	sched      schedt
   862  	newprocs   int32
   863  
   864  	// Information about what cpu features are available.
   865  	// Packages outside the runtime should not use these
   866  	// as they are not an external api.
   867  	// Set on startup in asm_{386,amd64,amd64p32}.s
   868  	processorVersionInfo uint32
   869  	isIntel              bool
   870  	lfenceBeforeRdtsc    bool
   871  
   872  	goarm                uint8 // set by cmd/link on arm systems
   873  	framepointer_enabled bool  // set by cmd/link
   874  )
   875  
   876  // Set by the linker so the runtime can determine the buildmode.
   877  var (
   878  	islibrary bool // -buildmode=c-shared
   879  	isarchive bool // -buildmode=c-archive
   880  )