...
Run Format

Source file src/runtime/runtime2.go

Documentation: runtime

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

View as plain text