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

View as plain text