Source file src/runtime/runtime2.go

     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/abi"
     9  	"internal/chacha8rand"
    10  	"internal/goarch"
    11  	"internal/runtime/atomic"
    12  	"runtime/internal/sys"
    13  	"unsafe"
    14  )
    15  
    16  // defined constants
    17  const (
    18  	// G status
    19  	//
    20  	// Beyond indicating the general state of a G, the G status
    21  	// acts like a lock on the goroutine's stack (and hence its
    22  	// ability to execute user code).
    23  	//
    24  	// If you add to this list, add to the list
    25  	// of "okay during garbage collection" status
    26  	// in mgcmark.go too.
    27  	//
    28  	// TODO(austin): The _Gscan bit could be much lighter-weight.
    29  	// For example, we could choose not to run _Gscanrunnable
    30  	// goroutines found in the run queue, rather than CAS-looping
    31  	// until they become _Grunnable. And transitions like
    32  	// _Gscanwaiting -> _Gscanrunnable are actually okay because
    33  	// they don't affect stack ownership.
    34  
    35  	// _Gidle means this goroutine was just allocated and has not
    36  	// yet been initialized.
    37  	_Gidle = iota // 0
    38  
    39  	// _Grunnable means this goroutine is on a run queue. It is
    40  	// not currently executing user code. The stack is not owned.
    41  	_Grunnable // 1
    42  
    43  	// _Grunning means this goroutine may execute user code. The
    44  	// stack is owned by this goroutine. It is not on a run queue.
    45  	// It is assigned an M and a P (g.m and g.m.p are valid).
    46  	_Grunning // 2
    47  
    48  	// _Gsyscall means this goroutine is executing a system call.
    49  	// It is not executing user code. The stack is owned by this
    50  	// goroutine. It is not on a run queue. It is assigned an M.
    51  	_Gsyscall // 3
    52  
    53  	// _Gwaiting means this goroutine is blocked in the runtime.
    54  	// It is not executing user code. It is not on a run queue,
    55  	// but should be recorded somewhere (e.g., a channel wait
    56  	// queue) so it can be ready()d when necessary. The stack is
    57  	// not owned *except* that a channel operation may read or
    58  	// write parts of the stack under the appropriate channel
    59  	// lock. Otherwise, it is not safe to access the stack after a
    60  	// goroutine enters _Gwaiting (e.g., it may get moved).
    61  	_Gwaiting // 4
    62  
    63  	// _Gmoribund_unused is currently unused, but hardcoded in gdb
    64  	// scripts.
    65  	_Gmoribund_unused // 5
    66  
    67  	// _Gdead means this goroutine is currently unused. It may be
    68  	// just exited, on a free list, or just being initialized. It
    69  	// is not executing user code. It may or may not have a stack
    70  	// allocated. The G and its stack (if any) are owned by the M
    71  	// that is exiting the G or that obtained the G from the free
    72  	// list.
    73  	_Gdead // 6
    74  
    75  	// _Genqueue_unused is currently unused.
    76  	_Genqueue_unused // 7
    77  
    78  	// _Gcopystack means this goroutine's stack is being moved. It
    79  	// is not executing user code and is not on a run queue. The
    80  	// stack is owned by the goroutine that put it in _Gcopystack.
    81  	_Gcopystack // 8
    82  
    83  	// _Gpreempted means this goroutine stopped itself for a
    84  	// suspendG preemption. It is like _Gwaiting, but nothing is
    85  	// yet responsible for ready()ing it. Some suspendG must CAS
    86  	// the status to _Gwaiting to take responsibility for
    87  	// ready()ing this G.
    88  	_Gpreempted // 9
    89  
    90  	// _Gscan combined with one of the above states other than
    91  	// _Grunning indicates that GC is scanning the stack. The
    92  	// goroutine is not executing user code and the stack is owned
    93  	// by the goroutine that set the _Gscan bit.
    94  	//
    95  	// _Gscanrunning is different: it is used to briefly block
    96  	// state transitions while GC signals the G to scan its own
    97  	// stack. This is otherwise like _Grunning.
    98  	//
    99  	// atomicstatus&~Gscan gives the state the goroutine will
   100  	// return to when the scan completes.
   101  	_Gscan          = 0x1000
   102  	_Gscanrunnable  = _Gscan + _Grunnable  // 0x1001
   103  	_Gscanrunning   = _Gscan + _Grunning   // 0x1002
   104  	_Gscansyscall   = _Gscan + _Gsyscall   // 0x1003
   105  	_Gscanwaiting   = _Gscan + _Gwaiting   // 0x1004
   106  	_Gscanpreempted = _Gscan + _Gpreempted // 0x1009
   107  )
   108  
   109  const (
   110  	// P status
   111  
   112  	// _Pidle means a P is not being used to run user code or the
   113  	// scheduler. Typically, it's on the idle P list and available
   114  	// to the scheduler, but it may just be transitioning between
   115  	// other states.
   116  	//
   117  	// The P is owned by the idle list or by whatever is
   118  	// transitioning its state. Its run queue is empty.
   119  	_Pidle = iota
   120  
   121  	// _Prunning means a P is owned by an M and is being used to
   122  	// run user code or the scheduler. Only the M that owns this P
   123  	// is allowed to change the P's status from _Prunning. The M
   124  	// may transition the P to _Pidle (if it has no more work to
   125  	// do), _Psyscall (when entering a syscall), or _Pgcstop (to
   126  	// halt for the GC). The M may also hand ownership of the P
   127  	// off directly to another M (e.g., to schedule a locked G).
   128  	_Prunning
   129  
   130  	// _Psyscall means a P is not running user code. It has
   131  	// affinity to an M in a syscall but is not owned by it and
   132  	// may be stolen by another M. This is similar to _Pidle but
   133  	// uses lightweight transitions and maintains M affinity.
   134  	//
   135  	// Leaving _Psyscall must be done with a CAS, either to steal
   136  	// or retake the P. Note that there's an ABA hazard: even if
   137  	// an M successfully CASes its original P back to _Prunning
   138  	// after a syscall, it must understand the P may have been
   139  	// used by another M in the interim.
   140  	_Psyscall
   141  
   142  	// _Pgcstop means a P is halted for STW and owned by the M
   143  	// that stopped the world. The M that stopped the world
   144  	// continues to use its P, even in _Pgcstop. Transitioning
   145  	// from _Prunning to _Pgcstop causes an M to release its P and
   146  	// park.
   147  	//
   148  	// The P retains its run queue and startTheWorld will restart
   149  	// the scheduler on Ps with non-empty run queues.
   150  	_Pgcstop
   151  
   152  	// _Pdead means a P is no longer used (GOMAXPROCS shrank). We
   153  	// reuse Ps if GOMAXPROCS increases. A dead P is mostly
   154  	// stripped of its resources, though a few things remain
   155  	// (e.g., trace buffers).
   156  	_Pdead
   157  )
   158  
   159  // Mutual exclusion locks.  In the uncontended case,
   160  // as fast as spin locks (just a few user-level instructions),
   161  // but on the contention path they sleep in the kernel.
   162  // A zeroed Mutex is unlocked (no need to initialize each lock).
   163  // Initialization is helpful for static lock ranking, but not required.
   164  type mutex struct {
   165  	// Empty struct if lock ranking is disabled, otherwise includes the lock rank
   166  	lockRankStruct
   167  	// Futex-based impl treats it as uint32 key,
   168  	// while sema-based impl as M* waitm.
   169  	// Used to be a union, but unions break precise GC.
   170  	key uintptr
   171  }
   172  
   173  // sleep and wakeup on one-time events.
   174  // before any calls to notesleep or notewakeup,
   175  // must call noteclear to initialize the Note.
   176  // then, exactly one thread can call notesleep
   177  // and exactly one thread can call notewakeup (once).
   178  // once notewakeup has been called, the notesleep
   179  // will return.  future notesleep will return immediately.
   180  // subsequent noteclear must be called only after
   181  // previous notesleep has returned, e.g. it's disallowed
   182  // to call noteclear straight after notewakeup.
   183  //
   184  // notetsleep is like notesleep but wakes up after
   185  // a given number of nanoseconds even if the event
   186  // has not yet happened.  if a goroutine uses notetsleep to
   187  // wake up early, it must wait to call noteclear until it
   188  // can be sure that no other goroutine is calling
   189  // notewakeup.
   190  //
   191  // notesleep/notetsleep are generally called on g0,
   192  // notetsleepg is similar to notetsleep but is called on user g.
   193  type note struct {
   194  	// Futex-based impl treats it as uint32 key,
   195  	// while sema-based impl as M* waitm.
   196  	// Used to be a union, but unions break precise GC.
   197  	key uintptr
   198  }
   199  
   200  type funcval struct {
   201  	fn uintptr
   202  	// variable-size, fn-specific data here
   203  }
   204  
   205  type iface struct {
   206  	tab  *itab
   207  	data unsafe.Pointer
   208  }
   209  
   210  type eface struct {
   211  	_type *_type
   212  	data  unsafe.Pointer
   213  }
   214  
   215  func efaceOf(ep *any) *eface {
   216  	return (*eface)(unsafe.Pointer(ep))
   217  }
   218  
   219  // The guintptr, muintptr, and puintptr are all used to bypass write barriers.
   220  // It is particularly important to avoid write barriers when the current P has
   221  // been released, because the GC thinks the world is stopped, and an
   222  // unexpected write barrier would not be synchronized with the GC,
   223  // which can lead to a half-executed write barrier that has marked the object
   224  // but not queued it. If the GC skips the object and completes before the
   225  // queuing can occur, it will incorrectly free the object.
   226  //
   227  // We tried using special assignment functions invoked only when not
   228  // holding a running P, but then some updates to a particular memory
   229  // word went through write barriers and some did not. This breaks the
   230  // write barrier shadow checking mode, and it is also scary: better to have
   231  // a word that is completely ignored by the GC than to have one for which
   232  // only a few updates are ignored.
   233  //
   234  // Gs and Ps are always reachable via true pointers in the
   235  // allgs and allp lists or (during allocation before they reach those lists)
   236  // from stack variables.
   237  //
   238  // Ms are always reachable via true pointers either from allm or
   239  // freem. Unlike Gs and Ps we do free Ms, so it's important that
   240  // nothing ever hold an muintptr across a safe point.
   241  
   242  // A guintptr holds a goroutine pointer, but typed as a uintptr
   243  // to bypass write barriers. It is used in the Gobuf goroutine state
   244  // and in scheduling lists that are manipulated without a P.
   245  //
   246  // The Gobuf.g goroutine pointer is almost always updated by assembly code.
   247  // In one of the few places it is updated by Go code - func save - it must be
   248  // treated as a uintptr to avoid a write barrier being emitted at a bad time.
   249  // Instead of figuring out how to emit the write barriers missing in the
   250  // assembly manipulation, we change the type of the field to uintptr,
   251  // so that it does not require write barriers at all.
   252  //
   253  // Goroutine structs are published in the allg list and never freed.
   254  // That will keep the goroutine structs from being collected.
   255  // There is never a time that Gobuf.g's contain the only references
   256  // to a goroutine: the publishing of the goroutine in allg comes first.
   257  // Goroutine pointers are also kept in non-GC-visible places like TLS,
   258  // so I can't see them ever moving. If we did want to start moving data
   259  // in the GC, we'd need to allocate the goroutine structs from an
   260  // alternate arena. Using guintptr doesn't make that problem any worse.
   261  // Note that pollDesc.rg, pollDesc.wg also store g in uintptr form,
   262  // so they would need to be updated too if g's start moving.
   263  type guintptr uintptr
   264  
   265  //go:nosplit
   266  func (gp guintptr) ptr() *g { return (*g)(unsafe.Pointer(gp)) }
   267  
   268  //go:nosplit
   269  func (gp *guintptr) set(g *g) { *gp = guintptr(unsafe.Pointer(g)) }
   270  
   271  //go:nosplit
   272  func (gp *guintptr) cas(old, new guintptr) bool {
   273  	return atomic.Casuintptr((*uintptr)(unsafe.Pointer(gp)), uintptr(old), uintptr(new))
   274  }
   275  
   276  //go:nosplit
   277  func (gp *g) guintptr() guintptr {
   278  	return guintptr(unsafe.Pointer(gp))
   279  }
   280  
   281  // setGNoWB performs *gp = new without a write barrier.
   282  // For times when it's impractical to use a guintptr.
   283  //
   284  //go:nosplit
   285  //go:nowritebarrier
   286  func setGNoWB(gp **g, new *g) {
   287  	(*guintptr)(unsafe.Pointer(gp)).set(new)
   288  }
   289  
   290  type puintptr uintptr
   291  
   292  //go:nosplit
   293  func (pp puintptr) ptr() *p { return (*p)(unsafe.Pointer(pp)) }
   294  
   295  //go:nosplit
   296  func (pp *puintptr) set(p *p) { *pp = puintptr(unsafe.Pointer(p)) }
   297  
   298  // muintptr is a *m that is not tracked by the garbage collector.
   299  //
   300  // Because we do free Ms, there are some additional constrains on
   301  // muintptrs:
   302  //
   303  //  1. Never hold an muintptr locally across a safe point.
   304  //
   305  //  2. Any muintptr in the heap must be owned by the M itself so it can
   306  //     ensure it is not in use when the last true *m is released.
   307  type muintptr uintptr
   308  
   309  //go:nosplit
   310  func (mp muintptr) ptr() *m { return (*m)(unsafe.Pointer(mp)) }
   311  
   312  //go:nosplit
   313  func (mp *muintptr) set(m *m) { *mp = muintptr(unsafe.Pointer(m)) }
   314  
   315  // setMNoWB performs *mp = new without a write barrier.
   316  // For times when it's impractical to use an muintptr.
   317  //
   318  //go:nosplit
   319  //go:nowritebarrier
   320  func setMNoWB(mp **m, new *m) {
   321  	(*muintptr)(unsafe.Pointer(mp)).set(new)
   322  }
   323  
   324  type gobuf struct {
   325  	// The offsets of sp, pc, and g are known to (hard-coded in) libmach.
   326  	//
   327  	// ctxt is unusual with respect to GC: it may be a
   328  	// heap-allocated funcval, so GC needs to track it, but it
   329  	// needs to be set and cleared from assembly, where it's
   330  	// difficult to have write barriers. However, ctxt is really a
   331  	// saved, live register, and we only ever exchange it between
   332  	// the real register and the gobuf. Hence, we treat it as a
   333  	// root during stack scanning, which means assembly that saves
   334  	// and restores it doesn't need write barriers. It's still
   335  	// typed as a pointer so that any other writes from Go get
   336  	// write barriers.
   337  	sp   uintptr
   338  	pc   uintptr
   339  	g    guintptr
   340  	ctxt unsafe.Pointer
   341  	ret  uintptr
   342  	lr   uintptr
   343  	bp   uintptr // for framepointer-enabled architectures
   344  }
   345  
   346  // sudog (pseudo-g) represents a g in a wait list, such as for sending/receiving
   347  // on a channel.
   348  //
   349  // sudog is necessary because the g ↔ synchronization object relation
   350  // is many-to-many. A g can be on many wait lists, so there may be
   351  // many sudogs for one g; and many gs may be waiting on the same
   352  // synchronization object, so there may be many sudogs for one object.
   353  //
   354  // sudogs are allocated from a special pool. Use acquireSudog and
   355  // releaseSudog to allocate and free them.
   356  type sudog struct {
   357  	// The following fields are protected by the hchan.lock of the
   358  	// channel this sudog is blocking on. shrinkstack depends on
   359  	// this for sudogs involved in channel ops.
   360  
   361  	g *g
   362  
   363  	next *sudog
   364  	prev *sudog
   365  	elem unsafe.Pointer // data element (may point to stack)
   366  
   367  	// The following fields are never accessed concurrently.
   368  	// For channels, waitlink is only accessed by g.
   369  	// For semaphores, all fields (including the ones above)
   370  	// are only accessed when holding a semaRoot lock.
   371  
   372  	acquiretime int64
   373  	releasetime int64
   374  	ticket      uint32
   375  
   376  	// isSelect indicates g is participating in a select, so
   377  	// g.selectDone must be CAS'd to win the wake-up race.
   378  	isSelect bool
   379  
   380  	// success indicates whether communication over channel c
   381  	// succeeded. It is true if the goroutine was awoken because a
   382  	// value was delivered over channel c, and false if awoken
   383  	// because c was closed.
   384  	success bool
   385  
   386  	// waiters is a count of semaRoot waiting list other than head of list,
   387  	// clamped to a uint16 to fit in unused space.
   388  	// Only meaningful at the head of the list.
   389  	// (If we wanted to be overly clever, we could store a high 16 bits
   390  	// in the second entry in the list.)
   391  	waiters uint16
   392  
   393  	parent   *sudog // semaRoot binary tree
   394  	waitlink *sudog // g.waiting list or semaRoot
   395  	waittail *sudog // semaRoot
   396  	c        *hchan // channel
   397  }
   398  
   399  type libcall struct {
   400  	fn   uintptr
   401  	n    uintptr // number of parameters
   402  	args uintptr // parameters
   403  	r1   uintptr // return values
   404  	r2   uintptr
   405  	err  uintptr // error number
   406  }
   407  
   408  // Stack describes a Go execution stack.
   409  // The bounds of the stack are exactly [lo, hi),
   410  // with no implicit data structures on either side.
   411  type stack struct {
   412  	lo uintptr
   413  	hi uintptr
   414  }
   415  
   416  // heldLockInfo gives info on a held lock and the rank of that lock
   417  type heldLockInfo struct {
   418  	lockAddr uintptr
   419  	rank     lockRank
   420  }
   421  
   422  type g struct {
   423  	// Stack parameters.
   424  	// stack describes the actual stack memory: [stack.lo, stack.hi).
   425  	// stackguard0 is the stack pointer compared in the Go stack growth prologue.
   426  	// It is stack.lo+StackGuard normally, but can be StackPreempt to trigger a preemption.
   427  	// stackguard1 is the stack pointer compared in the //go:systemstack stack growth prologue.
   428  	// It is stack.lo+StackGuard on g0 and gsignal stacks.
   429  	// It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash).
   430  	stack       stack   // offset known to runtime/cgo
   431  	stackguard0 uintptr // offset known to liblink
   432  	stackguard1 uintptr // offset known to liblink
   433  
   434  	_panic    *_panic // innermost panic - offset known to liblink
   435  	_defer    *_defer // innermost defer
   436  	m         *m      // current m; offset known to arm liblink
   437  	sched     gobuf
   438  	syscallsp uintptr // if status==Gsyscall, syscallsp = sched.sp to use during gc
   439  	syscallpc uintptr // if status==Gsyscall, syscallpc = sched.pc to use during gc
   440  	syscallbp uintptr // if status==Gsyscall, syscallbp = sched.bp to use in fpTraceback
   441  	stktopsp  uintptr // expected sp at top of stack, to check in traceback
   442  	// param is a generic pointer parameter field used to pass
   443  	// values in particular contexts where other storage for the
   444  	// parameter would be difficult to find. It is currently used
   445  	// in four ways:
   446  	// 1. When a channel operation wakes up a blocked goroutine, it sets param to
   447  	//    point to the sudog of the completed blocking operation.
   448  	// 2. By gcAssistAlloc1 to signal back to its caller that the goroutine completed
   449  	//    the GC cycle. It is unsafe to do so in any other way, because the goroutine's
   450  	//    stack may have moved in the meantime.
   451  	// 3. By debugCallWrap to pass parameters to a new goroutine because allocating a
   452  	//    closure in the runtime is forbidden.
   453  	// 4. When a panic is recovered and control returns to the respective frame,
   454  	//    param may point to a savedOpenDeferState.
   455  	param        unsafe.Pointer
   456  	atomicstatus atomic.Uint32
   457  	stackLock    uint32 // sigprof/scang lock; TODO: fold in to atomicstatus
   458  	goid         uint64
   459  	schedlink    guintptr
   460  	waitsince    int64      // approx time when the g become blocked
   461  	waitreason   waitReason // if status==Gwaiting
   462  
   463  	preempt       bool // preemption signal, duplicates stackguard0 = stackpreempt
   464  	preemptStop   bool // transition to _Gpreempted on preemption; otherwise, just deschedule
   465  	preemptShrink bool // shrink stack at synchronous safe point
   466  
   467  	// asyncSafePoint is set if g is stopped at an asynchronous
   468  	// safe point. This means there are frames on the stack
   469  	// without precise pointer information.
   470  	asyncSafePoint bool
   471  
   472  	paniconfault bool // panic (instead of crash) on unexpected fault address
   473  	gcscandone   bool // g has scanned stack; protected by _Gscan bit in status
   474  	throwsplit   bool // must not split stack
   475  	// activeStackChans indicates that there are unlocked channels
   476  	// pointing into this goroutine's stack. If true, stack
   477  	// copying needs to acquire channel locks to protect these
   478  	// areas of the stack.
   479  	activeStackChans bool
   480  	// parkingOnChan indicates that the goroutine is about to
   481  	// park on a chansend or chanrecv. Used to signal an unsafe point
   482  	// for stack shrinking.
   483  	parkingOnChan atomic.Bool
   484  	// inMarkAssist indicates whether the goroutine is in mark assist.
   485  	// Used by the execution tracer.
   486  	inMarkAssist bool
   487  	coroexit     bool // argument to coroswitch_m
   488  
   489  	raceignore    int8  // ignore race detection events
   490  	nocgocallback bool  // whether disable callback from C
   491  	tracking      bool  // whether we're tracking this G for sched latency statistics
   492  	trackingSeq   uint8 // used to decide whether to track this G
   493  	trackingStamp int64 // timestamp of when the G last started being tracked
   494  	runnableTime  int64 // the amount of time spent runnable, cleared when running, only used when tracking
   495  	lockedm       muintptr
   496  	sig           uint32
   497  	writebuf      []byte
   498  	sigcode0      uintptr
   499  	sigcode1      uintptr
   500  	sigpc         uintptr
   501  	parentGoid    uint64          // goid of goroutine that created this goroutine
   502  	gopc          uintptr         // pc of go statement that created this goroutine
   503  	ancestors     *[]ancestorInfo // ancestor information goroutine(s) that created this goroutine (only used if debug.tracebackancestors)
   504  	startpc       uintptr         // pc of goroutine function
   505  	racectx       uintptr
   506  	waiting       *sudog         // sudog structures this g is waiting on (that have a valid elem ptr); in lock order
   507  	cgoCtxt       []uintptr      // cgo traceback context
   508  	labels        unsafe.Pointer // profiler labels
   509  	timer         *timer         // cached timer for time.Sleep
   510  	sleepWhen     int64          // when to sleep until
   511  	selectDone    atomic.Uint32  // are we participating in a select and did someone win the race?
   512  
   513  	// goroutineProfiled indicates the status of this goroutine's stack for the
   514  	// current in-progress goroutine profile
   515  	goroutineProfiled goroutineProfileStateHolder
   516  
   517  	coroarg *coro // argument during coroutine transfers
   518  
   519  	// Per-G tracer state.
   520  	trace gTraceState
   521  
   522  	// Per-G GC state
   523  
   524  	// gcAssistBytes is this G's GC assist credit in terms of
   525  	// bytes allocated. If this is positive, then the G has credit
   526  	// to allocate gcAssistBytes bytes without assisting. If this
   527  	// is negative, then the G must correct this by performing
   528  	// scan work. We track this in bytes to make it fast to update
   529  	// and check for debt in the malloc hot path. The assist ratio
   530  	// determines how this corresponds to scan work debt.
   531  	gcAssistBytes int64
   532  }
   533  
   534  // gTrackingPeriod is the number of transitions out of _Grunning between
   535  // latency tracking runs.
   536  const gTrackingPeriod = 8
   537  
   538  const (
   539  	// tlsSlots is the number of pointer-sized slots reserved for TLS on some platforms,
   540  	// like Windows.
   541  	tlsSlots = 6
   542  	tlsSize  = tlsSlots * goarch.PtrSize
   543  )
   544  
   545  // Values for m.freeWait.
   546  const (
   547  	freeMStack = 0 // M done, free stack and reference.
   548  	freeMRef   = 1 // M done, free reference.
   549  	freeMWait  = 2 // M still in use.
   550  )
   551  
   552  type m struct {
   553  	g0      *g     // goroutine with scheduling stack
   554  	morebuf gobuf  // gobuf arg to morestack
   555  	divmod  uint32 // div/mod denominator for arm - known to liblink
   556  	_       uint32 // align next field to 8 bytes
   557  
   558  	// Fields not known to debuggers.
   559  	procid        uint64            // for debuggers, but offset not hard-coded
   560  	gsignal       *g                // signal-handling g
   561  	goSigStack    gsignalStack      // Go-allocated signal handling stack
   562  	sigmask       sigset            // storage for saved signal mask
   563  	tls           [tlsSlots]uintptr // thread-local storage (for x86 extern register)
   564  	mstartfn      func()
   565  	curg          *g       // current running goroutine
   566  	caughtsig     guintptr // goroutine running during fatal signal
   567  	p             puintptr // attached p for executing go code (nil if not executing go code)
   568  	nextp         puintptr
   569  	oldp          puintptr // the p that was attached before executing a syscall
   570  	id            int64
   571  	mallocing     int32
   572  	throwing      throwType
   573  	preemptoff    string // if != "", keep curg running on this m
   574  	locks         int32
   575  	dying         int32
   576  	profilehz     int32
   577  	spinning      bool // m is out of work and is actively looking for work
   578  	blocked       bool // m is blocked on a note
   579  	newSigstack   bool // minit on C thread called sigaltstack
   580  	printlock     int8
   581  	incgo         bool          // m is executing a cgo call
   582  	isextra       bool          // m is an extra m
   583  	isExtraInC    bool          // m is an extra m that is not executing Go code
   584  	isExtraInSig  bool          // m is an extra m in a signal handler
   585  	freeWait      atomic.Uint32 // Whether it is safe to free g0 and delete m (one of freeMRef, freeMStack, freeMWait)
   586  	needextram    bool
   587  	traceback     uint8
   588  	ncgocall      uint64        // number of cgo calls in total
   589  	ncgo          int32         // number of cgo calls currently in progress
   590  	cgoCallersUse atomic.Uint32 // if non-zero, cgoCallers in use temporarily
   591  	cgoCallers    *cgoCallers   // cgo traceback if crashing in cgo call
   592  	park          note
   593  	alllink       *m // on allm
   594  	schedlink     muintptr
   595  	lockedg       guintptr
   596  	createstack   [32]uintptr // stack that created this thread, it's used for StackRecord.Stack0, so it must align with it.
   597  	lockedExt     uint32      // tracking for external LockOSThread
   598  	lockedInt     uint32      // tracking for internal lockOSThread
   599  	nextwaitm     muintptr    // next m waiting for lock
   600  
   601  	mLockProfile mLockProfile // fields relating to runtime.lock contention
   602  	profStack    []uintptr    // used for memory/block/mutex stack traces
   603  
   604  	// wait* are used to carry arguments from gopark into park_m, because
   605  	// there's no stack to put them on. That is their sole purpose.
   606  	waitunlockf          func(*g, unsafe.Pointer) bool
   607  	waitlock             unsafe.Pointer
   608  	waitTraceSkip        int
   609  	waitTraceBlockReason traceBlockReason
   610  
   611  	syscalltick uint32
   612  	freelink    *m // on sched.freem
   613  	trace       mTraceState
   614  
   615  	// these are here because they are too large to be on the stack
   616  	// of low-level NOSPLIT functions.
   617  	libcall    libcall
   618  	libcallpc  uintptr // for cpu profiler
   619  	libcallsp  uintptr
   620  	libcallg   guintptr
   621  	winsyscall winlibcall // stores syscall parameters on windows
   622  
   623  	vdsoSP uintptr // SP for traceback while in VDSO call (0 if not in call)
   624  	vdsoPC uintptr // PC for traceback while in VDSO call
   625  
   626  	// preemptGen counts the number of completed preemption
   627  	// signals. This is used to detect when a preemption is
   628  	// requested, but fails.
   629  	preemptGen atomic.Uint32
   630  
   631  	// Whether this is a pending preemption signal on this M.
   632  	signalPending atomic.Uint32
   633  
   634  	// pcvalue lookup cache
   635  	pcvalueCache pcvalueCache
   636  
   637  	dlogPerM
   638  
   639  	mOS
   640  
   641  	chacha8   chacha8rand.State
   642  	cheaprand uint64
   643  
   644  	// Up to 10 locks held by this m, maintained by the lock ranking code.
   645  	locksHeldLen int
   646  	locksHeld    [10]heldLockInfo
   647  }
   648  
   649  type p struct {
   650  	id          int32
   651  	status      uint32 // one of pidle/prunning/...
   652  	link        puintptr
   653  	schedtick   uint32     // incremented on every scheduler call
   654  	syscalltick uint32     // incremented on every system call
   655  	sysmontick  sysmontick // last tick observed by sysmon
   656  	m           muintptr   // back-link to associated m (nil if idle)
   657  	mcache      *mcache
   658  	pcache      pageCache
   659  	raceprocctx uintptr
   660  
   661  	deferpool    []*_defer // pool of available defer structs (see panic.go)
   662  	deferpoolbuf [32]*_defer
   663  
   664  	// Cache of goroutine ids, amortizes accesses to runtime·sched.goidgen.
   665  	goidcache    uint64
   666  	goidcacheend uint64
   667  
   668  	// Queue of runnable goroutines. Accessed without lock.
   669  	runqhead uint32
   670  	runqtail uint32
   671  	runq     [256]guintptr
   672  	// runnext, if non-nil, is a runnable G that was ready'd by
   673  	// the current G and should be run next instead of what's in
   674  	// runq if there's time remaining in the running G's time
   675  	// slice. It will inherit the time left in the current time
   676  	// slice. If a set of goroutines is locked in a
   677  	// communicate-and-wait pattern, this schedules that set as a
   678  	// unit and eliminates the (potentially large) scheduling
   679  	// latency that otherwise arises from adding the ready'd
   680  	// goroutines to the end of the run queue.
   681  	//
   682  	// Note that while other P's may atomically CAS this to zero,
   683  	// only the owner P can CAS it to a valid G.
   684  	runnext guintptr
   685  
   686  	// Available G's (status == Gdead)
   687  	gFree struct {
   688  		gList
   689  		n int32
   690  	}
   691  
   692  	sudogcache []*sudog
   693  	sudogbuf   [128]*sudog
   694  
   695  	// Cache of mspan objects from the heap.
   696  	mspancache struct {
   697  		// We need an explicit length here because this field is used
   698  		// in allocation codepaths where write barriers are not allowed,
   699  		// and eliminating the write barrier/keeping it eliminated from
   700  		// slice updates is tricky, more so than just managing the length
   701  		// ourselves.
   702  		len int
   703  		buf [128]*mspan
   704  	}
   705  
   706  	// Cache of a single pinner object to reduce allocations from repeated
   707  	// pinner creation.
   708  	pinnerCache *pinner
   709  
   710  	trace pTraceState
   711  
   712  	palloc persistentAlloc // per-P to avoid mutex
   713  
   714  	// Per-P GC state
   715  	gcAssistTime         int64 // Nanoseconds in assistAlloc
   716  	gcFractionalMarkTime int64 // Nanoseconds in fractional mark worker (atomic)
   717  
   718  	// limiterEvent tracks events for the GC CPU limiter.
   719  	limiterEvent limiterEvent
   720  
   721  	// gcMarkWorkerMode is the mode for the next mark worker to run in.
   722  	// That is, this is used to communicate with the worker goroutine
   723  	// selected for immediate execution by
   724  	// gcController.findRunnableGCWorker. When scheduling other goroutines,
   725  	// this field must be set to gcMarkWorkerNotWorker.
   726  	gcMarkWorkerMode gcMarkWorkerMode
   727  	// gcMarkWorkerStartTime is the nanotime() at which the most recent
   728  	// mark worker started.
   729  	gcMarkWorkerStartTime int64
   730  
   731  	// gcw is this P's GC work buffer cache. The work buffer is
   732  	// filled by write barriers, drained by mutator assists, and
   733  	// disposed on certain GC state transitions.
   734  	gcw gcWork
   735  
   736  	// wbBuf is this P's GC write barrier buffer.
   737  	//
   738  	// TODO: Consider caching this in the running G.
   739  	wbBuf wbBuf
   740  
   741  	runSafePointFn uint32 // if 1, run sched.safePointFn at next safe point
   742  
   743  	// statsSeq is a counter indicating whether this P is currently
   744  	// writing any stats. Its value is even when not, odd when it is.
   745  	statsSeq atomic.Uint32
   746  
   747  	// Timer heap.
   748  	timers timers
   749  
   750  	// maxStackScanDelta accumulates the amount of stack space held by
   751  	// live goroutines (i.e. those eligible for stack scanning).
   752  	// Flushed to gcController.maxStackScan once maxStackScanSlack
   753  	// or -maxStackScanSlack is reached.
   754  	maxStackScanDelta int64
   755  
   756  	// gc-time statistics about current goroutines
   757  	// Note that this differs from maxStackScan in that this
   758  	// accumulates the actual stack observed to be used at GC time (hi - sp),
   759  	// not an instantaneous measure of the total stack size that might need
   760  	// to be scanned (hi - lo).
   761  	scannedStackSize uint64 // stack size of goroutines scanned by this P
   762  	scannedStacks    uint64 // number of goroutines scanned by this P
   763  
   764  	// preempt is set to indicate that this P should be enter the
   765  	// scheduler ASAP (regardless of what G is running on it).
   766  	preempt bool
   767  
   768  	// gcStopTime is the nanotime timestamp that this P last entered _Pgcstop.
   769  	gcStopTime int64
   770  
   771  	// Padding is no longer needed. False sharing is now not a worry because p is large enough
   772  	// that its size class is an integer multiple of the cache line size (for any of our architectures).
   773  }
   774  
   775  type schedt struct {
   776  	goidgen   atomic.Uint64
   777  	lastpoll  atomic.Int64 // time of last network poll, 0 if currently polling
   778  	pollUntil atomic.Int64 // time to which current poll is sleeping
   779  
   780  	lock mutex
   781  
   782  	// When increasing nmidle, nmidlelocked, nmsys, or nmfreed, be
   783  	// sure to call checkdead().
   784  
   785  	midle        muintptr // idle m's waiting for work
   786  	nmidle       int32    // number of idle m's waiting for work
   787  	nmidlelocked int32    // number of locked m's waiting for work
   788  	mnext        int64    // number of m's that have been created and next M ID
   789  	maxmcount    int32    // maximum number of m's allowed (or die)
   790  	nmsys        int32    // number of system m's not counted for deadlock
   791  	nmfreed      int64    // cumulative number of freed m's
   792  
   793  	ngsys atomic.Int32 // number of system goroutines
   794  
   795  	pidle        puintptr // idle p's
   796  	npidle       atomic.Int32
   797  	nmspinning   atomic.Int32  // See "Worker thread parking/unparking" comment in proc.go.
   798  	needspinning atomic.Uint32 // See "Delicate dance" comment in proc.go. Boolean. Must hold sched.lock to set to 1.
   799  
   800  	// Global runnable queue.
   801  	runq     gQueue
   802  	runqsize int32
   803  
   804  	// disable controls selective disabling of the scheduler.
   805  	//
   806  	// Use schedEnableUser to control this.
   807  	//
   808  	// disable is protected by sched.lock.
   809  	disable struct {
   810  		// user disables scheduling of user goroutines.
   811  		user     bool
   812  		runnable gQueue // pending runnable Gs
   813  		n        int32  // length of runnable
   814  	}
   815  
   816  	// Global cache of dead G's.
   817  	gFree struct {
   818  		lock    mutex
   819  		stack   gList // Gs with stacks
   820  		noStack gList // Gs without stacks
   821  		n       int32
   822  	}
   823  
   824  	// Central cache of sudog structs.
   825  	sudoglock  mutex
   826  	sudogcache *sudog
   827  
   828  	// Central pool of available defer structs.
   829  	deferlock mutex
   830  	deferpool *_defer
   831  
   832  	// freem is the list of m's waiting to be freed when their
   833  	// m.exited is set. Linked through m.freelink.
   834  	freem *m
   835  
   836  	gcwaiting  atomic.Bool // gc is waiting to run
   837  	stopwait   int32
   838  	stopnote   note
   839  	sysmonwait atomic.Bool
   840  	sysmonnote note
   841  
   842  	// safePointFn should be called on each P at the next GC
   843  	// safepoint if p.runSafePointFn is set.
   844  	safePointFn   func(*p)
   845  	safePointWait int32
   846  	safePointNote note
   847  
   848  	profilehz int32 // cpu profiling rate
   849  
   850  	procresizetime int64 // nanotime() of last change to gomaxprocs
   851  	totaltime      int64 // ∫gomaxprocs dt up to procresizetime
   852  
   853  	// sysmonlock protects sysmon's actions on the runtime.
   854  	//
   855  	// Acquire and hold this mutex to block sysmon from interacting
   856  	// with the rest of the runtime.
   857  	sysmonlock mutex
   858  
   859  	// timeToRun is a distribution of scheduling latencies, defined
   860  	// as the sum of time a G spends in the _Grunnable state before
   861  	// it transitions to _Grunning.
   862  	timeToRun timeHistogram
   863  
   864  	// idleTime is the total CPU time Ps have "spent" idle.
   865  	//
   866  	// Reset on each GC cycle.
   867  	idleTime atomic.Int64
   868  
   869  	// totalMutexWaitTime is the sum of time goroutines have spent in _Gwaiting
   870  	// with a waitreason of the form waitReasonSync{RW,}Mutex{R,}Lock.
   871  	totalMutexWaitTime atomic.Int64
   872  
   873  	// stwStoppingTimeGC/Other are distributions of stop-the-world stopping
   874  	// latencies, defined as the time taken by stopTheWorldWithSema to get
   875  	// all Ps to stop. stwStoppingTimeGC covers all GC-related STWs,
   876  	// stwStoppingTimeOther covers the others.
   877  	stwStoppingTimeGC    timeHistogram
   878  	stwStoppingTimeOther timeHistogram
   879  
   880  	// stwTotalTimeGC/Other are distributions of stop-the-world total
   881  	// latencies, defined as the total time from stopTheWorldWithSema to
   882  	// startTheWorldWithSema. This is a superset of
   883  	// stwStoppingTimeGC/Other. stwTotalTimeGC covers all GC-related STWs,
   884  	// stwTotalTimeOther covers the others.
   885  	stwTotalTimeGC    timeHistogram
   886  	stwTotalTimeOther timeHistogram
   887  
   888  	// totalRuntimeLockWaitTime (plus the value of lockWaitTime on each M in
   889  	// allm) is the sum of time goroutines have spent in _Grunnable and with an
   890  	// M, but waiting for locks within the runtime. This field stores the value
   891  	// for Ms that have exited.
   892  	totalRuntimeLockWaitTime atomic.Int64
   893  }
   894  
   895  // Values for the flags field of a sigTabT.
   896  const (
   897  	_SigNotify   = 1 << iota // let signal.Notify have signal, even if from kernel
   898  	_SigKill                 // if signal.Notify doesn't take it, exit quietly
   899  	_SigThrow                // if signal.Notify doesn't take it, exit loudly
   900  	_SigPanic                // if the signal is from the kernel, panic
   901  	_SigDefault              // if the signal isn't explicitly requested, don't monitor it
   902  	_SigGoExit               // cause all runtime procs to exit (only used on Plan 9).
   903  	_SigSetStack             // Don't explicitly install handler, but add SA_ONSTACK to existing libc handler
   904  	_SigUnblock              // always unblock; see blockableSig
   905  	_SigIgn                  // _SIG_DFL action is to ignore the signal
   906  )
   907  
   908  // Layout of in-memory per-function information prepared by linker
   909  // See https://golang.org/s/go12symtab.
   910  // Keep in sync with linker (../cmd/link/internal/ld/pcln.go:/pclntab)
   911  // and with package debug/gosym and with symtab.go in package runtime.
   912  type _func struct {
   913  	sys.NotInHeap // Only in static data
   914  
   915  	entryOff uint32 // start pc, as offset from moduledata.text/pcHeader.textStart
   916  	nameOff  int32  // function name, as index into moduledata.funcnametab.
   917  
   918  	args        int32  // in/out args size
   919  	deferreturn uint32 // offset of start of a deferreturn call instruction from entry, if any.
   920  
   921  	pcsp      uint32
   922  	pcfile    uint32
   923  	pcln      uint32
   924  	npcdata   uint32
   925  	cuOffset  uint32     // runtime.cutab offset of this function's CU
   926  	startLine int32      // line number of start of function (func keyword/TEXT directive)
   927  	funcID    abi.FuncID // set for certain special runtime functions
   928  	flag      abi.FuncFlag
   929  	_         [1]byte // pad
   930  	nfuncdata uint8   // must be last, must end on a uint32-aligned boundary
   931  
   932  	// The end of the struct is followed immediately by two variable-length
   933  	// arrays that reference the pcdata and funcdata locations for this
   934  	// function.
   935  
   936  	// pcdata contains the offset into moduledata.pctab for the start of
   937  	// that index's table. e.g.,
   938  	// &moduledata.pctab[_func.pcdata[_PCDATA_UnsafePoint]] is the start of
   939  	// the unsafe point table.
   940  	//
   941  	// An offset of 0 indicates that there is no table.
   942  	//
   943  	// pcdata [npcdata]uint32
   944  
   945  	// funcdata contains the offset past moduledata.gofunc which contains a
   946  	// pointer to that index's funcdata. e.g.,
   947  	// *(moduledata.gofunc +  _func.funcdata[_FUNCDATA_ArgsPointerMaps]) is
   948  	// the argument pointer map.
   949  	//
   950  	// An offset of ^uint32(0) indicates that there is no entry.
   951  	//
   952  	// funcdata [nfuncdata]uint32
   953  }
   954  
   955  // Pseudo-Func that is returned for PCs that occur in inlined code.
   956  // A *Func can be either a *_func or a *funcinl, and they are distinguished
   957  // by the first uintptr.
   958  //
   959  // TODO(austin): Can we merge this with inlinedCall?
   960  type funcinl struct {
   961  	ones      uint32  // set to ^0 to distinguish from _func
   962  	entry     uintptr // entry of the real (the "outermost") frame
   963  	name      string
   964  	file      string
   965  	line      int32
   966  	startLine int32
   967  }
   968  
   969  type itab = abi.ITab
   970  
   971  // Lock-free stack node.
   972  // Also known to export_test.go.
   973  type lfnode struct {
   974  	next    uint64
   975  	pushcnt uintptr
   976  }
   977  
   978  type forcegcstate struct {
   979  	lock mutex
   980  	g    *g
   981  	idle atomic.Bool
   982  }
   983  
   984  // A _defer holds an entry on the list of deferred calls.
   985  // If you add a field here, add code to clear it in deferProcStack.
   986  // This struct must match the code in cmd/compile/internal/ssagen/ssa.go:deferstruct
   987  // and cmd/compile/internal/ssagen/ssa.go:(*state).call.
   988  // Some defers will be allocated on the stack and some on the heap.
   989  // All defers are logically part of the stack, so write barriers to
   990  // initialize them are not required. All defers must be manually scanned,
   991  // and for heap defers, marked.
   992  type _defer struct {
   993  	heap      bool
   994  	rangefunc bool    // true for rangefunc list
   995  	sp        uintptr // sp at time of defer
   996  	pc        uintptr // pc at time of defer
   997  	fn        func()  // can be nil for open-coded defers
   998  	link      *_defer // next defer on G; can point to either heap or stack!
   999  
  1000  	// If rangefunc is true, *head is the head of the atomic linked list
  1001  	// during a range-over-func execution.
  1002  	head *atomic.Pointer[_defer]
  1003  }
  1004  
  1005  // A _panic holds information about an active panic.
  1006  //
  1007  // A _panic value must only ever live on the stack.
  1008  //
  1009  // The argp and link fields are stack pointers, but don't need special
  1010  // handling during stack growth: because they are pointer-typed and
  1011  // _panic values only live on the stack, regular stack pointer
  1012  // adjustment takes care of them.
  1013  type _panic struct {
  1014  	argp unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblink
  1015  	arg  any            // argument to panic
  1016  	link *_panic        // link to earlier panic
  1017  
  1018  	// startPC and startSP track where _panic.start was called.
  1019  	startPC uintptr
  1020  	startSP unsafe.Pointer
  1021  
  1022  	// The current stack frame that we're running deferred calls for.
  1023  	sp unsafe.Pointer
  1024  	lr uintptr
  1025  	fp unsafe.Pointer
  1026  
  1027  	// retpc stores the PC where the panic should jump back to, if the
  1028  	// function last returned by _panic.next() recovers the panic.
  1029  	retpc uintptr
  1030  
  1031  	// Extra state for handling open-coded defers.
  1032  	deferBitsPtr *uint8
  1033  	slotsPtr     unsafe.Pointer
  1034  
  1035  	recovered   bool // whether this panic has been recovered
  1036  	goexit      bool
  1037  	deferreturn bool
  1038  }
  1039  
  1040  // savedOpenDeferState tracks the extra state from _panic that's
  1041  // necessary for deferreturn to pick up where gopanic left off,
  1042  // without needing to unwind the stack.
  1043  type savedOpenDeferState struct {
  1044  	retpc           uintptr
  1045  	deferBitsOffset uintptr
  1046  	slotsOffset     uintptr
  1047  }
  1048  
  1049  // ancestorInfo records details of where a goroutine was started.
  1050  type ancestorInfo struct {
  1051  	pcs  []uintptr // pcs from the stack of this goroutine
  1052  	goid uint64    // goroutine id of this goroutine; original goroutine possibly dead
  1053  	gopc uintptr   // pc of go statement that created this goroutine
  1054  }
  1055  
  1056  // A waitReason explains why a goroutine has been stopped.
  1057  // See gopark. Do not re-use waitReasons, add new ones.
  1058  type waitReason uint8
  1059  
  1060  const (
  1061  	waitReasonZero                  waitReason = iota // ""
  1062  	waitReasonGCAssistMarking                         // "GC assist marking"
  1063  	waitReasonIOWait                                  // "IO wait"
  1064  	waitReasonChanReceiveNilChan                      // "chan receive (nil chan)"
  1065  	waitReasonChanSendNilChan                         // "chan send (nil chan)"
  1066  	waitReasonDumpingHeap                             // "dumping heap"
  1067  	waitReasonGarbageCollection                       // "garbage collection"
  1068  	waitReasonGarbageCollectionScan                   // "garbage collection scan"
  1069  	waitReasonPanicWait                               // "panicwait"
  1070  	waitReasonSelect                                  // "select"
  1071  	waitReasonSelectNoCases                           // "select (no cases)"
  1072  	waitReasonGCAssistWait                            // "GC assist wait"
  1073  	waitReasonGCSweepWait                             // "GC sweep wait"
  1074  	waitReasonGCScavengeWait                          // "GC scavenge wait"
  1075  	waitReasonChanReceive                             // "chan receive"
  1076  	waitReasonChanSend                                // "chan send"
  1077  	waitReasonFinalizerWait                           // "finalizer wait"
  1078  	waitReasonForceGCIdle                             // "force gc (idle)"
  1079  	waitReasonSemacquire                              // "semacquire"
  1080  	waitReasonSleep                                   // "sleep"
  1081  	waitReasonSyncCondWait                            // "sync.Cond.Wait"
  1082  	waitReasonSyncMutexLock                           // "sync.Mutex.Lock"
  1083  	waitReasonSyncRWMutexRLock                        // "sync.RWMutex.RLock"
  1084  	waitReasonSyncRWMutexLock                         // "sync.RWMutex.Lock"
  1085  	waitReasonTraceReaderBlocked                      // "trace reader (blocked)"
  1086  	waitReasonWaitForGCCycle                          // "wait for GC cycle"
  1087  	waitReasonGCWorkerIdle                            // "GC worker (idle)"
  1088  	waitReasonGCWorkerActive                          // "GC worker (active)"
  1089  	waitReasonPreempted                               // "preempted"
  1090  	waitReasonDebugCall                               // "debug call"
  1091  	waitReasonGCMarkTermination                       // "GC mark termination"
  1092  	waitReasonStoppingTheWorld                        // "stopping the world"
  1093  	waitReasonFlushProcCaches                         // "flushing proc caches"
  1094  	waitReasonTraceGoroutineStatus                    // "trace goroutine status"
  1095  	waitReasonTraceProcStatus                         // "trace proc status"
  1096  	waitReasonPageTraceFlush                          // "page trace flush"
  1097  	waitReasonCoroutine                               // "coroutine"
  1098  )
  1099  
  1100  var waitReasonStrings = [...]string{
  1101  	waitReasonZero:                  "",
  1102  	waitReasonGCAssistMarking:       "GC assist marking",
  1103  	waitReasonIOWait:                "IO wait",
  1104  	waitReasonChanReceiveNilChan:    "chan receive (nil chan)",
  1105  	waitReasonChanSendNilChan:       "chan send (nil chan)",
  1106  	waitReasonDumpingHeap:           "dumping heap",
  1107  	waitReasonGarbageCollection:     "garbage collection",
  1108  	waitReasonGarbageCollectionScan: "garbage collection scan",
  1109  	waitReasonPanicWait:             "panicwait",
  1110  	waitReasonSelect:                "select",
  1111  	waitReasonSelectNoCases:         "select (no cases)",
  1112  	waitReasonGCAssistWait:          "GC assist wait",
  1113  	waitReasonGCSweepWait:           "GC sweep wait",
  1114  	waitReasonGCScavengeWait:        "GC scavenge wait",
  1115  	waitReasonChanReceive:           "chan receive",
  1116  	waitReasonChanSend:              "chan send",
  1117  	waitReasonFinalizerWait:         "finalizer wait",
  1118  	waitReasonForceGCIdle:           "force gc (idle)",
  1119  	waitReasonSemacquire:            "semacquire",
  1120  	waitReasonSleep:                 "sleep",
  1121  	waitReasonSyncCondWait:          "sync.Cond.Wait",
  1122  	waitReasonSyncMutexLock:         "sync.Mutex.Lock",
  1123  	waitReasonSyncRWMutexRLock:      "sync.RWMutex.RLock",
  1124  	waitReasonSyncRWMutexLock:       "sync.RWMutex.Lock",
  1125  	waitReasonTraceReaderBlocked:    "trace reader (blocked)",
  1126  	waitReasonWaitForGCCycle:        "wait for GC cycle",
  1127  	waitReasonGCWorkerIdle:          "GC worker (idle)",
  1128  	waitReasonGCWorkerActive:        "GC worker (active)",
  1129  	waitReasonPreempted:             "preempted",
  1130  	waitReasonDebugCall:             "debug call",
  1131  	waitReasonGCMarkTermination:     "GC mark termination",
  1132  	waitReasonStoppingTheWorld:      "stopping the world",
  1133  	waitReasonFlushProcCaches:       "flushing proc caches",
  1134  	waitReasonTraceGoroutineStatus:  "trace goroutine status",
  1135  	waitReasonTraceProcStatus:       "trace proc status",
  1136  	waitReasonPageTraceFlush:        "page trace flush",
  1137  	waitReasonCoroutine:             "coroutine",
  1138  }
  1139  
  1140  func (w waitReason) String() string {
  1141  	if w < 0 || w >= waitReason(len(waitReasonStrings)) {
  1142  		return "unknown wait reason"
  1143  	}
  1144  	return waitReasonStrings[w]
  1145  }
  1146  
  1147  func (w waitReason) isMutexWait() bool {
  1148  	return w == waitReasonSyncMutexLock ||
  1149  		w == waitReasonSyncRWMutexRLock ||
  1150  		w == waitReasonSyncRWMutexLock
  1151  }
  1152  
  1153  func (w waitReason) isWaitingForGC() bool {
  1154  	return isWaitingForGC[w]
  1155  }
  1156  
  1157  // isWaitingForGC indicates that a goroutine is only entering _Gwaiting and
  1158  // setting a waitReason because it needs to be able to let the GC take ownership
  1159  // of its stack. The G is always actually executing on the system stack, in
  1160  // these cases.
  1161  //
  1162  // TODO(mknyszek): Consider replacing this with a new dedicated G status.
  1163  var isWaitingForGC = [len(waitReasonStrings)]bool{
  1164  	waitReasonStoppingTheWorld:      true,
  1165  	waitReasonGCMarkTermination:     true,
  1166  	waitReasonGarbageCollection:     true,
  1167  	waitReasonGarbageCollectionScan: true,
  1168  	waitReasonTraceGoroutineStatus:  true,
  1169  	waitReasonTraceProcStatus:       true,
  1170  	waitReasonPageTraceFlush:        true,
  1171  	waitReasonGCAssistMarking:       true,
  1172  	waitReasonGCWorkerActive:        true,
  1173  	waitReasonFlushProcCaches:       true,
  1174  }
  1175  
  1176  var (
  1177  	allm       *m
  1178  	gomaxprocs int32
  1179  	ncpu       int32
  1180  	forcegc    forcegcstate
  1181  	sched      schedt
  1182  	newprocs   int32
  1183  )
  1184  
  1185  var (
  1186  	// allpLock protects P-less reads and size changes of allp, idlepMask,
  1187  	// and timerpMask, and all writes to allp.
  1188  	allpLock mutex
  1189  
  1190  	// len(allp) == gomaxprocs; may change at safe points, otherwise
  1191  	// immutable.
  1192  	allp []*p
  1193  
  1194  	// Bitmask of Ps in _Pidle list, one bit per P. Reads and writes must
  1195  	// be atomic. Length may change at safe points.
  1196  	//
  1197  	// Each P must update only its own bit. In order to maintain
  1198  	// consistency, a P going idle must the idle mask simultaneously with
  1199  	// updates to the idle P list under the sched.lock, otherwise a racing
  1200  	// pidleget may clear the mask before pidleput sets the mask,
  1201  	// corrupting the bitmap.
  1202  	//
  1203  	// N.B., procresize takes ownership of all Ps in stopTheWorldWithSema.
  1204  	idlepMask pMask
  1205  
  1206  	// Bitmask of Ps that may have a timer, one bit per P. Reads and writes
  1207  	// must be atomic. Length may change at safe points.
  1208  	//
  1209  	// Ideally, the timer mask would be kept immediately consistent on any timer
  1210  	// operations. Unfortunately, updating a shared global data structure in the
  1211  	// timer hot path adds too much overhead in applications frequently switching
  1212  	// between no timers and some timers.
  1213  	//
  1214  	// As a compromise, the timer mask is updated only on pidleget / pidleput. A
  1215  	// running P (returned by pidleget) may add a timer at any time, so its mask
  1216  	// must be set. An idle P (passed to pidleput) cannot add new timers while
  1217  	// idle, so if it has no timers at that time, its mask may be cleared.
  1218  	//
  1219  	// Thus, we get the following effects on timer-stealing in findrunnable:
  1220  	//
  1221  	//   - Idle Ps with no timers when they go idle are never checked in findrunnable
  1222  	//     (for work- or timer-stealing; this is the ideal case).
  1223  	//   - Running Ps must always be checked.
  1224  	//   - Idle Ps whose timers are stolen must continue to be checked until they run
  1225  	//     again, even after timer expiration.
  1226  	//
  1227  	// When the P starts running again, the mask should be set, as a timer may be
  1228  	// added at any time.
  1229  	//
  1230  	// TODO(prattmic): Additional targeted updates may improve the above cases.
  1231  	// e.g., updating the mask when stealing a timer.
  1232  	timerpMask pMask
  1233  )
  1234  
  1235  // goarmsoftfp is used by runtime/cgo assembly.
  1236  //
  1237  //go:linkname goarmsoftfp
  1238  
  1239  var (
  1240  	// Pool of GC parked background workers. Entries are type
  1241  	// *gcBgMarkWorkerNode.
  1242  	gcBgMarkWorkerPool lfstack
  1243  
  1244  	// Total number of gcBgMarkWorker goroutines. Protected by worldsema.
  1245  	gcBgMarkWorkerCount int32
  1246  
  1247  	// Information about what cpu features are available.
  1248  	// Packages outside the runtime should not use these
  1249  	// as they are not an external api.
  1250  	// Set on startup in asm_{386,amd64}.s
  1251  	processorVersionInfo uint32
  1252  	isIntel              bool
  1253  )
  1254  
  1255  // set by cmd/link on arm systems
  1256  // accessed using linkname by internal/runtime/atomic.
  1257  //
  1258  // goarm should be an internal detail,
  1259  // but widely used packages access it using linkname.
  1260  // Notable members of the hall of shame include:
  1261  //   - github.com/creativeprojects/go-selfupdate
  1262  //
  1263  // Do not remove or change the type signature.
  1264  // See go.dev/issue/67401.
  1265  //
  1266  //go:linkname goarm
  1267  var (
  1268  	goarm       uint8
  1269  	goarmsoftfp uint8
  1270  )
  1271  
  1272  // Set by the linker so the runtime can determine the buildmode.
  1273  var (
  1274  	islibrary bool // -buildmode=c-shared
  1275  	isarchive bool // -buildmode=c-archive
  1276  )
  1277  
  1278  // Must agree with internal/buildcfg.FramePointerEnabled.
  1279  const framepointer_enabled = GOARCH == "amd64" || GOARCH == "arm64"
  1280  
  1281  // getcallerfp returns the frame pointer of the caller of the caller
  1282  // of this function.
  1283  //
  1284  //go:nosplit
  1285  //go:noinline
  1286  func getcallerfp() uintptr {
  1287  	fp := getfp() // This frame's FP.
  1288  	if fp != 0 {
  1289  		fp = *(*uintptr)(unsafe.Pointer(fp)) // The caller's FP.
  1290  		fp = *(*uintptr)(unsafe.Pointer(fp)) // The caller's caller's FP.
  1291  	}
  1292  	return fp
  1293  }
  1294  

View as plain text