github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/runtime/runtime2.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package runtime 6 7 import ( 8 "internal/cpu" 9 "runtime/internal/atomic" 10 "runtime/internal/sys" 11 "unsafe" 12 ) 13 14 // defined constants 15 const ( 16 // G status 17 // 18 // Beyond indicating the general state of a G, the G status 19 // acts like a lock on the goroutine's stack (and hence its 20 // ability to execute user code). 21 // 22 // If you add to this list, add to the list 23 // of "okay during garbage collection" status 24 // in mgcmark.go too. 25 // 26 // TODO(austin): The _Gscan bit could be much lighter-weight. 27 // For example, we could choose not to run _Gscanrunnable 28 // goroutines found in the run queue, rather than CAS-looping 29 // until they become _Grunnable. And transitions like 30 // _Gscanwaiting -> _Gscanrunnable are actually okay because 31 // they don't affect stack ownership. 32 33 // _Gidle means this goroutine was just allocated and has not 34 // yet been initialized. 35 _Gidle = iota // 0 36 37 // _Grunnable means this goroutine is on a run queue. It is 38 // not currently executing user code. The stack is not owned. 39 _Grunnable // 1 40 41 // _Grunning means this goroutine may execute user code. The 42 // stack is owned by this goroutine. It is not on a run queue. 43 // It is assigned an M and a P (g.m and g.m.p are valid). 44 _Grunning // 2 45 46 // _Gsyscall means this goroutine is executing a system call. 47 // It is not executing user code. The stack is owned by this 48 // goroutine. It is not on a run queue. It is assigned an M. 49 _Gsyscall // 3 50 51 // _Gwaiting means this goroutine is blocked in the runtime. 52 // It is not executing user code. It is not on a run queue, 53 // but should be recorded somewhere (e.g., a channel wait 54 // queue) so it can be ready()d when necessary. The stack is 55 // not owned *except* that a channel operation may read or 56 // write parts of the stack under the appropriate channel 57 // lock. Otherwise, it is not safe to access the stack after a 58 // goroutine enters _Gwaiting (e.g., it may get moved). 59 _Gwaiting // 4 60 61 // _Gmoribund_unused is currently unused, but hardcoded in gdb 62 // scripts. 63 _Gmoribund_unused // 5 64 65 // _Gdead means this goroutine is currently unused. It may be 66 // just exited, on a free list, or just being initialized. It 67 // is not executing user code. It may or may not have a stack 68 // allocated. The G and its stack (if any) are owned by the M 69 // that is exiting the G or that obtained the G from the free 70 // list. 71 _Gdead // 6 72 73 // _Genqueue_unused is currently unused. 74 _Genqueue_unused // 7 75 76 // _Gcopystack means this goroutine's stack is being moved. It 77 // is not executing user code and is not on a run queue. The 78 // stack is owned by the goroutine that put it in _Gcopystack. 79 _Gcopystack // 8 80 81 // _Gpreempted means this goroutine stopped itself for a 82 // suspendG preemption. It is like _Gwaiting, but nothing is 83 // yet responsible for ready()ing it. Some suspendG must CAS 84 // the status to _Gwaiting to take responsibility for 85 // ready()ing this G. 86 _Gpreempted // 9 87 88 // _Gscan combined with one of the above states other than 89 // _Grunning indicates that GC is scanning the stack. The 90 // goroutine is not executing user code and the stack is owned 91 // by the goroutine that set the _Gscan bit. 92 // 93 // _Gscanrunning is different: it is used to briefly block 94 // state transitions while GC signals the G to scan its own 95 // stack. This is otherwise like _Grunning. 96 // 97 // atomicstatus&~Gscan gives the state the goroutine will 98 // return to when the scan completes. 99 _Gscan = 0x1000 100 _Gscanrunnable = _Gscan + _Grunnable // 0x1001 101 _Gscanrunning = _Gscan + _Grunning // 0x1002 102 _Gscansyscall = _Gscan + _Gsyscall // 0x1003 103 _Gscanwaiting = _Gscan + _Gwaiting // 0x1004 104 _Gscanpreempted = _Gscan + _Gpreempted // 0x1009 105 ) 106 107 const ( 108 // P status 109 110 // _Pidle means a P is not being used to run user code or the 111 // scheduler. Typically, it's on the idle P list and available 112 // to the scheduler, but it may just be transitioning between 113 // other states. 114 // 115 // The P is owned by the idle list or by whatever is 116 // transitioning its state. Its run queue is empty. 117 _Pidle = iota 118 119 // _Prunning means a P is owned by an M and is being used to 120 // run user code or the scheduler. Only the M that owns this P 121 // is allowed to change the P's status from _Prunning. The M 122 // may transition the P to _Pidle (if it has no more work to 123 // do), _Psyscall (when entering a syscall), or _Pgcstop (to 124 // halt for the GC). The M may also hand ownership of the P 125 // off directly to another M (e.g., to schedule a locked G). 126 _Prunning 127 128 // _Psyscall means a P is not running user code. It has 129 // affinity to an M in a syscall but is not owned by it and 130 // may be stolen by another M. This is similar to _Pidle but 131 // uses lightweight transitions and maintains M affinity. 132 // 133 // Leaving _Psyscall must be done with a CAS, either to steal 134 // or retake the P. Note that there's an ABA hazard: even if 135 // an M successfully CASes its original P back to _Prunning 136 // after a syscall, it must understand the P may have been 137 // used by another M in the interim. 138 _Psyscall 139 140 // _Pgcstop means a P is halted for STW and owned by the M 141 // that stopped the world. The M that stopped the world 142 // continues to use its P, even in _Pgcstop. Transitioning 143 // from _Prunning to _Pgcstop causes an M to release its P and 144 // park. 145 // 146 // The P retains its run queue and startTheWorld will restart 147 // the scheduler on Ps with non-empty run queues. 148 _Pgcstop 149 150 // _Pdead means a P is no longer used (GOMAXPROCS shrank). We 151 // reuse Ps if GOMAXPROCS increases. A dead P is mostly 152 // stripped of its resources, though a few things remain 153 // (e.g., trace buffers). 154 _Pdead 155 ) 156 157 // Mutual exclusion locks. In the uncontended case, 158 // as fast as spin locks (just a few user-level instructions), 159 // but on the contention path they sleep in the kernel. 160 // A zeroed Mutex is unlocked (no need to initialize each lock). 161 // Initialization is helpful for static lock ranking, but not required. 162 type mutex struct { 163 // Empty struct if lock ranking is disabled, otherwise includes the lock rank 164 lockRankStruct 165 // Futex-based impl treats it as uint32 key, 166 // while sema-based impl as M* waitm. 167 // Used to be a union, but unions break precise GC. 168 key uintptr 169 } 170 171 // sleep and wakeup on one-time events. 172 // before any calls to notesleep or notewakeup, 173 // must call noteclear to initialize the Note. 174 // then, exactly one thread can call notesleep 175 // and exactly one thread can call notewakeup (once). 176 // once notewakeup has been called, the notesleep 177 // will return. future notesleep will return immediately. 178 // subsequent noteclear must be called only after 179 // previous notesleep has returned, e.g. it's disallowed 180 // to call noteclear straight after notewakeup. 181 // 182 // notetsleep is like notesleep but wakes up after 183 // a given number of nanoseconds even if the event 184 // has not yet happened. if a goroutine uses notetsleep to 185 // wake up early, it must wait to call noteclear until it 186 // can be sure that no other goroutine is calling 187 // notewakeup. 188 // 189 // notesleep/notetsleep are generally called on g0, 190 // notetsleepg is similar to notetsleep but is called on user g. 191 type note struct { 192 // Futex-based impl treats it as uint32 key, 193 // while sema-based impl as M* waitm. 194 // Used to be a union, but unions break precise GC. 195 key uintptr 196 } 197 198 type funcval struct { 199 fn uintptr 200 // variable-size, fn-specific data here 201 } 202 203 type iface struct { 204 tab *itab 205 data unsafe.Pointer 206 } 207 208 type eface struct { 209 _type *_type 210 data unsafe.Pointer 211 } 212 213 func efaceOf(ep *interface{}) *eface { 214 return (*eface)(unsafe.Pointer(ep)) 215 } 216 217 // The guintptr, muintptr, and puintptr are all used to bypass write barriers. 218 // It is particularly important to avoid write barriers when the current P has 219 // been released, because the GC thinks the world is stopped, and an 220 // unexpected write barrier would not be synchronized with the GC, 221 // which can lead to a half-executed write barrier that has marked the object 222 // but not queued it. If the GC skips the object and completes before the 223 // queuing can occur, it will incorrectly free the object. 224 // 225 // We tried using special assignment functions invoked only when not 226 // holding a running P, but then some updates to a particular memory 227 // word went through write barriers and some did not. This breaks the 228 // write barrier shadow checking mode, and it is also scary: better to have 229 // a word that is completely ignored by the GC than to have one for which 230 // only a few updates are ignored. 231 // 232 // Gs and Ps are always reachable via true pointers in the 233 // allgs and allp lists or (during allocation before they reach those lists) 234 // from stack variables. 235 // 236 // Ms are always reachable via true pointers either from allm or 237 // freem. Unlike Gs and Ps we do free Ms, so it's important that 238 // nothing ever hold an muintptr across a safe point. 239 240 // A guintptr holds a goroutine pointer, but typed as a uintptr 241 // to bypass write barriers. It is used in the Gobuf goroutine state 242 // and in scheduling lists that are manipulated without a P. 243 // 244 // The Gobuf.g goroutine pointer is almost always updated by assembly code. 245 // In one of the few places it is updated by Go code - func save - it must be 246 // treated as a uintptr to avoid a write barrier being emitted at a bad time. 247 // Instead of figuring out how to emit the write barriers missing in the 248 // assembly manipulation, we change the type of the field to uintptr, 249 // so that it does not require write barriers at all. 250 // 251 // Goroutine structs are published in the allg list and never freed. 252 // That will keep the goroutine structs from being collected. 253 // There is never a time that Gobuf.g's contain the only references 254 // to a goroutine: the publishing of the goroutine in allg comes first. 255 // Goroutine pointers are also kept in non-GC-visible places like TLS, 256 // so I can't see them ever moving. If we did want to start moving data 257 // in the GC, we'd need to allocate the goroutine structs from an 258 // alternate arena. Using guintptr doesn't make that problem any worse. 259 type guintptr uintptr 260 261 //go:nosplit 262 func (gp guintptr) ptr() *g { return (*g)(unsafe.Pointer(gp)) } 263 264 //go:nosplit 265 func (gp *guintptr) set(g *g) { *gp = guintptr(unsafe.Pointer(g)) } 266 267 //go:nosplit 268 func (gp *guintptr) cas(old, new guintptr) bool { 269 return atomic.Casuintptr((*uintptr)(unsafe.Pointer(gp)), uintptr(old), uintptr(new)) 270 } 271 272 // setGNoWB performs *gp = new without a write barrier. 273 // For times when it's impractical to use a guintptr. 274 //go:nosplit 275 //go:nowritebarrier 276 func setGNoWB(gp **g, new *g) { 277 (*guintptr)(unsafe.Pointer(gp)).set(new) 278 } 279 280 type puintptr uintptr 281 282 //go:nosplit 283 func (pp puintptr) ptr() *p { return (*p)(unsafe.Pointer(pp)) } 284 285 //go:nosplit 286 func (pp *puintptr) set(p *p) { *pp = puintptr(unsafe.Pointer(p)) } 287 288 // muintptr is a *m that is not tracked by the garbage collector. 289 // 290 // Because we do free Ms, there are some additional constrains on 291 // muintptrs: 292 // 293 // 1. Never hold an muintptr locally across a safe point. 294 // 295 // 2. Any muintptr in the heap must be owned by the M itself so it can 296 // ensure it is not in use when the last true *m is released. 297 type muintptr uintptr 298 299 //go:nosplit 300 func (mp muintptr) ptr() *m { return (*m)(unsafe.Pointer(mp)) } 301 302 //go:nosplit 303 func (mp *muintptr) set(m *m) { *mp = muintptr(unsafe.Pointer(m)) } 304 305 // setMNoWB performs *mp = new without a write barrier. 306 // For times when it's impractical to use an muintptr. 307 //go:nosplit 308 //go:nowritebarrier 309 func setMNoWB(mp **m, new *m) { 310 (*muintptr)(unsafe.Pointer(mp)).set(new) 311 } 312 313 type gobuf struct { 314 // The offsets of sp, pc, and g are known to (hard-coded in) libmach. 315 // 316 // ctxt is unusual with respect to GC: it may be a 317 // heap-allocated funcval, so GC needs to track it, but it 318 // needs to be set and cleared from assembly, where it's 319 // difficult to have write barriers. However, ctxt is really a 320 // saved, live register, and we only ever exchange it between 321 // the real register and the gobuf. Hence, we treat it as a 322 // root during stack scanning, which means assembly that saves 323 // and restores it doesn't need write barriers. It's still 324 // typed as a pointer so that any other writes from Go get 325 // write barriers. 326 sp uintptr 327 pc uintptr 328 g guintptr 329 ctxt unsafe.Pointer 330 ret sys.Uintreg 331 lr uintptr 332 bp uintptr // for framepointer-enabled architectures 333 } 334 335 // sudog represents a g in a wait list, such as for sending/receiving 336 // on a channel. 337 // 338 // sudog is necessary because the g ↔ synchronization object relation 339 // is many-to-many. A g can be on many wait lists, so there may be 340 // many sudogs for one g; and many gs may be waiting on the same 341 // synchronization object, so there may be many sudogs for one object. 342 // 343 // sudogs are allocated from a special pool. Use acquireSudog and 344 // releaseSudog to allocate and free them. 345 type sudog struct { 346 // The following fields are protected by the hchan.lock of the 347 // channel this sudog is blocking on. shrinkstack depends on 348 // this for sudogs involved in channel ops. 349 350 g *g 351 352 next *sudog 353 prev *sudog 354 elem unsafe.Pointer // data element (may point to stack) 355 356 // The following fields are never accessed concurrently. 357 // For channels, waitlink is only accessed by g. 358 // For semaphores, all fields (including the ones above) 359 // are only accessed when holding a semaRoot lock. 360 361 acquiretime int64 362 releasetime int64 363 ticket uint32 364 365 // isSelect indicates g is participating in a select, so 366 // g.selectDone must be CAS'd to win the wake-up race. 367 isSelect bool 368 369 // success indicates whether communication over channel c 370 // succeeded. It is true if the goroutine was awoken because a 371 // value was delivered over channel c, and false if awoken 372 // because c was closed. 373 success bool 374 375 parent *sudog // semaRoot binary tree 376 waitlink *sudog // g.waiting list or semaRoot 377 waittail *sudog // semaRoot 378 c *hchan // channel 379 } 380 381 type libcall struct { 382 fn uintptr 383 n uintptr // number of parameters 384 args uintptr // parameters 385 r1 uintptr // return values 386 r2 uintptr 387 err uintptr // error number 388 } 389 390 // Stack describes a Go execution stack. 391 // The bounds of the stack are exactly [lo, hi), 392 // with no implicit data structures on either side. 393 type stack struct { 394 lo uintptr 395 hi uintptr 396 } 397 398 // heldLockInfo gives info on a held lock and the rank of that lock 399 type heldLockInfo struct { 400 lockAddr uintptr 401 rank lockRank 402 } 403 404 type g struct { 405 // Stack parameters. 406 // stack describes the actual stack memory: [stack.lo, stack.hi). 407 // stackguard0 is the stack pointer compared in the Go stack growth prologue. 408 // It is stack.lo+StackGuard normally, but can be StackPreempt to trigger a preemption. 409 // stackguard1 is the stack pointer compared in the C stack growth prologue. 410 // It is stack.lo+StackGuard on g0 and gsignal stacks. 411 // It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash). 412 stack stack // offset known to runtime/cgo 413 stackguard0 uintptr // offset known to liblink 414 stackguard1 uintptr // offset known to liblink 415 416 _panic *_panic // innermost panic - offset known to liblink 417 _defer *_defer // innermost defer 418 m *m // current m; offset known to arm liblink 419 sched gobuf 420 syscallsp uintptr // if status==Gsyscall, syscallsp = sched.sp to use during gc 421 syscallpc uintptr // if status==Gsyscall, syscallpc = sched.pc to use during gc 422 stktopsp uintptr // expected sp at top of stack, to check in traceback 423 param unsafe.Pointer // passed parameter on wakeup 424 atomicstatus uint32 425 stackLock uint32 // sigprof/scang lock; TODO: fold in to atomicstatus 426 goid int64 427 schedlink guintptr 428 waitsince int64 // approx time when the g become blocked 429 waitreason waitReason // if status==Gwaiting 430 431 preempt bool // preemption signal, duplicates stackguard0 = stackpreempt 432 preemptStop bool // transition to _Gpreempted on preemption; otherwise, just deschedule 433 preemptShrink bool // shrink stack at synchronous safe point 434 435 // asyncSafePoint is set if g is stopped at an asynchronous 436 // safe point. This means there are frames on the stack 437 // without precise pointer information. 438 asyncSafePoint bool 439 440 paniconfault bool // panic (instead of crash) on unexpected fault address 441 gcscandone bool // g has scanned stack; protected by _Gscan bit in status 442 throwsplit bool // must not split stack 443 // activeStackChans indicates that there are unlocked channels 444 // pointing into this goroutine's stack. If true, stack 445 // copying needs to acquire channel locks to protect these 446 // areas of the stack. 447 activeStackChans bool 448 // parkingOnChan indicates that the goroutine is about to 449 // park on a chansend or chanrecv. Used to signal an unsafe point 450 // for stack shrinking. It's a boolean value, but is updated atomically. 451 parkingOnChan uint8 452 453 raceignore int8 // ignore race detection events 454 sysblocktraced bool // StartTrace has emitted EvGoInSyscall about this goroutine 455 sysexitticks int64 // cputicks when syscall has returned (for tracing) 456 traceseq uint64 // trace event sequencer 457 tracelastp puintptr // last P emitted an event for this goroutine 458 lockedm muintptr 459 sig uint32 460 writebuf []byte 461 sigcode0 uintptr 462 sigcode1 uintptr 463 sigpc uintptr 464 gopc uintptr // pc of go statement that created this goroutine 465 ancestors *[]ancestorInfo // ancestor information goroutine(s) that created this goroutine (only used if debug.tracebackancestors) 466 startpc uintptr // pc of goroutine function 467 racectx uintptr 468 waiting *sudog // sudog structures this g is waiting on (that have a valid elem ptr); in lock order 469 cgoCtxt []uintptr // cgo traceback context 470 labels unsafe.Pointer // profiler labels 471 timer *timer // cached timer for time.Sleep 472 selectDone uint32 // are we participating in a select and did someone win the race? 473 474 // Per-G GC state 475 476 // gcAssistBytes is this G's GC assist credit in terms of 477 // bytes allocated. If this is positive, then the G has credit 478 // to allocate gcAssistBytes bytes without assisting. If this 479 // is negative, then the G must correct this by performing 480 // scan work. We track this in bytes to make it fast to update 481 // and check for debt in the malloc hot path. The assist ratio 482 // determines how this corresponds to scan work debt. 483 gcAssistBytes int64 484 } 485 486 type m struct { 487 g0 *g // goroutine with scheduling stack 488 morebuf gobuf // gobuf arg to morestack 489 divmod uint32 // div/mod denominator for arm - known to liblink 490 491 // Fields not known to debuggers. 492 procid uint64 // for debuggers, but offset not hard-coded 493 gsignal *g // signal-handling g 494 goSigStack gsignalStack // Go-allocated signal handling stack 495 sigmask sigset // storage for saved signal mask 496 tls [6]uintptr // thread-local storage (for x86 extern register) 497 mstartfn func() 498 curg *g // current running goroutine 499 caughtsig guintptr // goroutine running during fatal signal 500 p puintptr // attached p for executing go code (nil if not executing go code) 501 nextp puintptr 502 oldp puintptr // the p that was attached before executing a syscall 503 id int64 504 mallocing int32 505 throwing int32 506 preemptoff string // if != "", keep curg running on this m 507 locks int32 508 dying int32 509 profilehz int32 510 spinning bool // m is out of work and is actively looking for work 511 blocked bool // m is blocked on a note 512 newSigstack bool // minit on C thread called sigaltstack 513 printlock int8 514 incgo bool // m is executing a cgo call 515 freeWait uint32 // if == 0, safe to free g0 and delete m (atomic) 516 fastrand [2]uint32 517 needextram bool 518 traceback uint8 519 ncgocall uint64 // number of cgo calls in total 520 ncgo int32 // number of cgo calls currently in progress 521 cgoCallersUse uint32 // if non-zero, cgoCallers in use temporarily 522 cgoCallers *cgoCallers // cgo traceback if crashing in cgo call 523 doesPark bool // non-P running threads: sysmon and newmHandoff never use .park 524 park note 525 alllink *m // on allm 526 schedlink muintptr 527 lockedg guintptr 528 createstack [32]uintptr // stack that created this thread. 529 lockedExt uint32 // tracking for external LockOSThread 530 lockedInt uint32 // tracking for internal lockOSThread 531 nextwaitm muintptr // next m waiting for lock 532 waitunlockf func(*g, unsafe.Pointer) bool 533 waitlock unsafe.Pointer 534 waittraceev byte 535 waittraceskip int 536 startingtrace bool 537 syscalltick uint32 538 freelink *m // on sched.freem 539 540 // mFixup is used to synchronize OS related m state (credentials etc) 541 // use mutex to access. 542 mFixup struct { 543 lock mutex 544 fn func(bool) bool 545 } 546 547 // these are here because they are too large to be on the stack 548 // of low-level NOSPLIT functions. 549 libcall libcall 550 libcallpc uintptr // for cpu profiler 551 libcallsp uintptr 552 libcallg guintptr 553 syscall libcall // stores syscall parameters on windows 554 555 vdsoSP uintptr // SP for traceback while in VDSO call (0 if not in call) 556 vdsoPC uintptr // PC for traceback while in VDSO call 557 558 // preemptGen counts the number of completed preemption 559 // signals. This is used to detect when a preemption is 560 // requested, but fails. Accessed atomically. 561 preemptGen uint32 562 563 // Whether this is a pending preemption signal on this M. 564 // Accessed atomically. 565 signalPending uint32 566 567 dlogPerM 568 569 mOS 570 571 // Up to 10 locks held by this m, maintained by the lock ranking code. 572 locksHeldLen int 573 locksHeld [10]heldLockInfo 574 } 575 576 type p struct { 577 id int32 578 status uint32 // one of pidle/prunning/... 579 link puintptr 580 schedtick uint32 // incremented on every scheduler call 581 syscalltick uint32 // incremented on every system call 582 sysmontick sysmontick // last tick observed by sysmon 583 m muintptr // back-link to associated m (nil if idle) 584 mcache *mcache 585 pcache pageCache 586 raceprocctx uintptr 587 588 deferpool [5][]*_defer // pool of available defer structs of different sizes (see panic.go) 589 deferpoolbuf [5][32]*_defer 590 591 // Cache of goroutine ids, amortizes accesses to runtime·sched.goidgen. 592 goidcache uint64 593 goidcacheend uint64 594 595 // Queue of runnable goroutines. Accessed without lock. 596 runqhead uint32 597 runqtail uint32 598 runq [256]guintptr 599 // runnext, if non-nil, is a runnable G that was ready'd by 600 // the current G and should be run next instead of what's in 601 // runq if there's time remaining in the running G's time 602 // slice. It will inherit the time left in the current time 603 // slice. If a set of goroutines is locked in a 604 // communicate-and-wait pattern, this schedules that set as a 605 // unit and eliminates the (potentially large) scheduling 606 // latency that otherwise arises from adding the ready'd 607 // goroutines to the end of the run queue. 608 runnext guintptr 609 610 // Available G's (status == Gdead) 611 gFree struct { 612 gList 613 n int32 614 } 615 616 sudogcache []*sudog 617 sudogbuf [128]*sudog 618 619 // Cache of mspan objects from the heap. 620 mspancache struct { 621 // We need an explicit length here because this field is used 622 // in allocation codepaths where write barriers are not allowed, 623 // and eliminating the write barrier/keeping it eliminated from 624 // slice updates is tricky, moreso than just managing the length 625 // ourselves. 626 len int 627 buf [128]*mspan 628 } 629 630 tracebuf traceBufPtr 631 632 // traceSweep indicates the sweep events should be traced. 633 // This is used to defer the sweep start event until a span 634 // has actually been swept. 635 traceSweep bool 636 // traceSwept and traceReclaimed track the number of bytes 637 // swept and reclaimed by sweeping in the current sweep loop. 638 traceSwept, traceReclaimed uintptr 639 640 palloc persistentAlloc // per-P to avoid mutex 641 642 _ uint32 // Alignment for atomic fields below 643 644 // The when field of the first entry on the timer heap. 645 // This is updated using atomic functions. 646 // This is 0 if the timer heap is empty. 647 timer0When uint64 648 649 // The earliest known nextwhen field of a timer with 650 // timerModifiedEarlier status. Because the timer may have been 651 // modified again, there need not be any timer with this value. 652 // This is updated using atomic functions. 653 // This is 0 if the value is unknown. 654 timerModifiedEarliest uint64 655 656 // Per-P GC state 657 gcAssistTime int64 // Nanoseconds in assistAlloc 658 gcFractionalMarkTime int64 // Nanoseconds in fractional mark worker (atomic) 659 660 // gcMarkWorkerMode is the mode for the next mark worker to run in. 661 // That is, this is used to communicate with the worker goroutine 662 // selected for immediate execution by 663 // gcController.findRunnableGCWorker. When scheduling other goroutines, 664 // this field must be set to gcMarkWorkerNotWorker. 665 gcMarkWorkerMode gcMarkWorkerMode 666 // gcMarkWorkerStartTime is the nanotime() at which the most recent 667 // mark worker started. 668 gcMarkWorkerStartTime int64 669 670 // gcw is this P's GC work buffer cache. The work buffer is 671 // filled by write barriers, drained by mutator assists, and 672 // disposed on certain GC state transitions. 673 gcw gcWork 674 675 // wbBuf is this P's GC write barrier buffer. 676 // 677 // TODO: Consider caching this in the running G. 678 wbBuf wbBuf 679 680 runSafePointFn uint32 // if 1, run sched.safePointFn at next safe point 681 682 // statsSeq is a counter indicating whether this P is currently 683 // writing any stats. Its value is even when not, odd when it is. 684 statsSeq uint32 685 686 // Lock for timers. We normally access the timers while running 687 // on this P, but the scheduler can also do it from a different P. 688 timersLock mutex 689 690 // Actions to take at some time. This is used to implement the 691 // standard library's time package. 692 // Must hold timersLock to access. 693 timers []*timer 694 695 // Number of timers in P's heap. 696 // Modified using atomic instructions. 697 numTimers uint32 698 699 // Number of timerModifiedEarlier timers on P's heap. 700 // This should only be modified while holding timersLock, 701 // or while the timer status is in a transient state 702 // such as timerModifying. 703 adjustTimers uint32 704 705 // Number of timerDeleted timers in P's heap. 706 // Modified using atomic instructions. 707 deletedTimers uint32 708 709 // Race context used while executing timer functions. 710 timerRaceCtx uintptr 711 712 // preempt is set to indicate that this P should be enter the 713 // scheduler ASAP (regardless of what G is running on it). 714 preempt bool 715 716 pad cpu.CacheLinePad 717 } 718 719 type schedt struct { 720 // accessed atomically. keep at top to ensure alignment on 32-bit systems. 721 goidgen uint64 722 lastpoll uint64 // time of last network poll, 0 if currently polling 723 pollUntil uint64 // time to which current poll is sleeping 724 725 lock mutex 726 727 // When increasing nmidle, nmidlelocked, nmsys, or nmfreed, be 728 // sure to call checkdead(). 729 730 midle muintptr // idle m's waiting for work 731 nmidle int32 // number of idle m's waiting for work 732 nmidlelocked int32 // number of locked m's waiting for work 733 mnext int64 // number of m's that have been created and next M ID 734 maxmcount int32 // maximum number of m's allowed (or die) 735 nmsys int32 // number of system m's not counted for deadlock 736 nmfreed int64 // cumulative number of freed m's 737 738 ngsys uint32 // number of system goroutines; updated atomically 739 740 pidle puintptr // idle p's 741 npidle uint32 742 nmspinning uint32 // See "Worker thread parking/unparking" comment in proc.go. 743 744 // Global runnable queue. 745 runq gQueue 746 runqsize int32 747 748 // disable controls selective disabling of the scheduler. 749 // 750 // Use schedEnableUser to control this. 751 // 752 // disable is protected by sched.lock. 753 disable struct { 754 // user disables scheduling of user goroutines. 755 user bool 756 runnable gQueue // pending runnable Gs 757 n int32 // length of runnable 758 } 759 760 // Global cache of dead G's. 761 gFree struct { 762 lock mutex 763 stack gList // Gs with stacks 764 noStack gList // Gs without stacks 765 n int32 766 } 767 768 // Central cache of sudog structs. 769 sudoglock mutex 770 sudogcache *sudog 771 772 // Central pool of available defer structs of different sizes. 773 deferlock mutex 774 deferpool [5]*_defer 775 776 // freem is the list of m's waiting to be freed when their 777 // m.exited is set. Linked through m.freelink. 778 freem *m 779 780 gcwaiting uint32 // gc is waiting to run 781 stopwait int32 782 stopnote note 783 sysmonwait uint32 784 sysmonnote note 785 786 // While true, sysmon not ready for mFixup calls. 787 // Accessed atomically. 788 sysmonStarting uint32 789 790 // safepointFn should be called on each P at the next GC 791 // safepoint if p.runSafePointFn is set. 792 safePointFn func(*p) 793 safePointWait int32 794 safePointNote note 795 796 profilehz int32 // cpu profiling rate 797 798 procresizetime int64 // nanotime() of last change to gomaxprocs 799 totaltime int64 // ∫gomaxprocs dt up to procresizetime 800 801 // sysmonlock protects sysmon's actions on the runtime. 802 // 803 // Acquire and hold this mutex to block sysmon from interacting 804 // with the rest of the runtime. 805 sysmonlock mutex 806 } 807 808 // Values for the flags field of a sigTabT. 809 const ( 810 _SigNotify = 1 << iota // let signal.Notify have signal, even if from kernel 811 _SigKill // if signal.Notify doesn't take it, exit quietly 812 _SigThrow // if signal.Notify doesn't take it, exit loudly 813 _SigPanic // if the signal is from the kernel, panic 814 _SigDefault // if the signal isn't explicitly requested, don't monitor it 815 _SigGoExit // cause all runtime procs to exit (only used on Plan 9). 816 _SigSetStack // add SA_ONSTACK to libc handler 817 _SigUnblock // always unblock; see blockableSig 818 _SigIgn // _SIG_DFL action is to ignore the signal 819 ) 820 821 // Layout of in-memory per-function information prepared by linker 822 // See https://golang.org/s/go12symtab. 823 // Keep in sync with linker (../cmd/link/internal/ld/pcln.go:/pclntab) 824 // and with package debug/gosym and with symtab.go in package runtime. 825 type _func struct { 826 entry uintptr // start pc 827 nameoff int32 // function name 828 829 args int32 // in/out args size 830 deferreturn uint32 // offset of start of a deferreturn call instruction from entry, if any. 831 832 pcsp uint32 833 pcfile uint32 834 pcln uint32 835 npcdata uint32 836 cuOffset uint32 // runtime.cutab offset of this function's CU 837 funcID funcID // set for certain special runtime functions 838 _ [2]byte // pad 839 nfuncdata uint8 // must be last 840 } 841 842 // Pseudo-Func that is returned for PCs that occur in inlined code. 843 // A *Func can be either a *_func or a *funcinl, and they are distinguished 844 // by the first uintptr. 845 type funcinl struct { 846 zero uintptr // set to 0 to distinguish from _func 847 entry uintptr // entry of the real (the "outermost") frame. 848 name string 849 file string 850 line int 851 } 852 853 // layout of Itab known to compilers 854 // allocated in non-garbage-collected memory 855 // Needs to be in sync with 856 // ../cmd/compile/internal/gc/reflect.go:/^func.dumptabs. 857 type itab struct { 858 inter *interfacetype 859 _type *_type 860 hash uint32 // copy of _type.hash. Used for type switches. 861 _ [4]byte 862 fun [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter. 863 } 864 865 // Lock-free stack node. 866 // Also known to export_test.go. 867 type lfnode struct { 868 next uint64 869 pushcnt uintptr 870 } 871 872 type forcegcstate struct { 873 lock mutex 874 g *g 875 idle uint32 876 } 877 878 // extendRandom extends the random numbers in r[:n] to the whole slice r. 879 // Treats n<0 as n==0. 880 func extendRandom(r []byte, n int) { 881 if n < 0 { 882 n = 0 883 } 884 for n < len(r) { 885 // Extend random bits using hash function & time seed 886 w := n 887 if w > 16 { 888 w = 16 889 } 890 h := memhash(unsafe.Pointer(&r[n-w]), uintptr(nanotime()), uintptr(w)) 891 for i := 0; i < sys.PtrSize && n < len(r); i++ { 892 r[n] = byte(h) 893 n++ 894 h >>= 8 895 } 896 } 897 } 898 899 // A _defer holds an entry on the list of deferred calls. 900 // If you add a field here, add code to clear it in freedefer and deferProcStack 901 // This struct must match the code in cmd/compile/internal/gc/reflect.go:deferstruct 902 // and cmd/compile/internal/gc/ssa.go:(*state).call. 903 // Some defers will be allocated on the stack and some on the heap. 904 // All defers are logically part of the stack, so write barriers to 905 // initialize them are not required. All defers must be manually scanned, 906 // and for heap defers, marked. 907 type _defer struct { 908 siz int32 // includes both arguments and results 909 started bool 910 heap bool 911 // openDefer indicates that this _defer is for a frame with open-coded 912 // defers. We have only one defer record for the entire frame (which may 913 // currently have 0, 1, or more defers active). 914 openDefer bool 915 sp uintptr // sp at time of defer 916 pc uintptr // pc at time of defer 917 fn *funcval // can be nil for open-coded defers 918 _panic *_panic // panic that is running defer 919 link *_defer 920 921 // If openDefer is true, the fields below record values about the stack 922 // frame and associated function that has the open-coded defer(s). sp 923 // above will be the sp for the frame, and pc will be address of the 924 // deferreturn call in the function. 925 fd unsafe.Pointer // funcdata for the function associated with the frame 926 varp uintptr // value of varp for the stack frame 927 // framepc is the current pc associated with the stack frame. Together, 928 // with sp above (which is the sp associated with the stack frame), 929 // framepc/sp can be used as pc/sp pair to continue a stack trace via 930 // gentraceback(). 931 framepc uintptr 932 } 933 934 // A _panic holds information about an active panic. 935 // 936 // A _panic value must only ever live on the stack. 937 // 938 // The argp and link fields are stack pointers, but don't need special 939 // handling during stack growth: because they are pointer-typed and 940 // _panic values only live on the stack, regular stack pointer 941 // adjustment takes care of them. 942 type _panic struct { 943 argp unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblink 944 arg interface{} // argument to panic 945 link *_panic // link to earlier panic 946 pc uintptr // where to return to in runtime if this panic is bypassed 947 sp unsafe.Pointer // where to return to in runtime if this panic is bypassed 948 recovered bool // whether this panic is over 949 aborted bool // the panic was aborted 950 goexit bool 951 } 952 953 // stack traces 954 type stkframe struct { 955 fn funcInfo // function being run 956 pc uintptr // program counter within fn 957 continpc uintptr // program counter where execution can continue, or 0 if not 958 lr uintptr // program counter at caller aka link register 959 sp uintptr // stack pointer at pc 960 fp uintptr // stack pointer at caller aka frame pointer 961 varp uintptr // top of local variables 962 argp uintptr // pointer to function arguments 963 arglen uintptr // number of bytes at argp 964 argmap *bitvector // force use of this argmap 965 } 966 967 // ancestorInfo records details of where a goroutine was started. 968 type ancestorInfo struct { 969 pcs []uintptr // pcs from the stack of this goroutine 970 goid int64 // goroutine id of this goroutine; original goroutine possibly dead 971 gopc uintptr // pc of go statement that created this goroutine 972 } 973 974 const ( 975 _TraceRuntimeFrames = 1 << iota // include frames for internal runtime functions. 976 _TraceTrap // the initial PC, SP are from a trap, not a return PC from a call 977 _TraceJumpStack // if traceback is on a systemstack, resume trace at g that called into it 978 ) 979 980 // The maximum number of frames we print for a traceback 981 const _TracebackMaxFrames = 100 982 983 // A waitReason explains why a goroutine has been stopped. 984 // See gopark. Do not re-use waitReasons, add new ones. 985 type waitReason uint8 986 987 const ( 988 waitReasonZero waitReason = iota // "" 989 waitReasonGCAssistMarking // "GC assist marking" 990 waitReasonIOWait // "IO wait" 991 waitReasonChanReceiveNilChan // "chan receive (nil chan)" 992 waitReasonChanSendNilChan // "chan send (nil chan)" 993 waitReasonDumpingHeap // "dumping heap" 994 waitReasonGarbageCollection // "garbage collection" 995 waitReasonGarbageCollectionScan // "garbage collection scan" 996 waitReasonPanicWait // "panicwait" 997 waitReasonSelect // "select" 998 waitReasonSelectNoCases // "select (no cases)" 999 waitReasonGCAssistWait // "GC assist wait" 1000 waitReasonGCSweepWait // "GC sweep wait" 1001 waitReasonGCScavengeWait // "GC scavenge wait" 1002 waitReasonChanReceive // "chan receive" 1003 waitReasonChanSend // "chan send" 1004 waitReasonFinalizerWait // "finalizer wait" 1005 waitReasonForceGCIdle // "force gc (idle)" 1006 waitReasonSemacquire // "semacquire" 1007 waitReasonSleep // "sleep" 1008 waitReasonSyncCondWait // "sync.Cond.Wait" 1009 waitReasonTimerGoroutineIdle // "timer goroutine (idle)" 1010 waitReasonTraceReaderBlocked // "trace reader (blocked)" 1011 waitReasonWaitForGCCycle // "wait for GC cycle" 1012 waitReasonGCWorkerIdle // "GC worker (idle)" 1013 waitReasonPreempted // "preempted" 1014 waitReasonDebugCall // "debug call" 1015 ) 1016 1017 var waitReasonStrings = [...]string{ 1018 waitReasonZero: "", 1019 waitReasonGCAssistMarking: "GC assist marking", 1020 waitReasonIOWait: "IO wait", 1021 waitReasonChanReceiveNilChan: "chan receive (nil chan)", 1022 waitReasonChanSendNilChan: "chan send (nil chan)", 1023 waitReasonDumpingHeap: "dumping heap", 1024 waitReasonGarbageCollection: "garbage collection", 1025 waitReasonGarbageCollectionScan: "garbage collection scan", 1026 waitReasonPanicWait: "panicwait", 1027 waitReasonSelect: "select", 1028 waitReasonSelectNoCases: "select (no cases)", 1029 waitReasonGCAssistWait: "GC assist wait", 1030 waitReasonGCSweepWait: "GC sweep wait", 1031 waitReasonGCScavengeWait: "GC scavenge wait", 1032 waitReasonChanReceive: "chan receive", 1033 waitReasonChanSend: "chan send", 1034 waitReasonFinalizerWait: "finalizer wait", 1035 waitReasonForceGCIdle: "force gc (idle)", 1036 waitReasonSemacquire: "semacquire", 1037 waitReasonSleep: "sleep", 1038 waitReasonSyncCondWait: "sync.Cond.Wait", 1039 waitReasonTimerGoroutineIdle: "timer goroutine (idle)", 1040 waitReasonTraceReaderBlocked: "trace reader (blocked)", 1041 waitReasonWaitForGCCycle: "wait for GC cycle", 1042 waitReasonGCWorkerIdle: "GC worker (idle)", 1043 waitReasonPreempted: "preempted", 1044 waitReasonDebugCall: "debug call", 1045 } 1046 1047 func (w waitReason) String() string { 1048 if w < 0 || w >= waitReason(len(waitReasonStrings)) { 1049 return "unknown wait reason" 1050 } 1051 return waitReasonStrings[w] 1052 } 1053 1054 var ( 1055 allm *m 1056 gomaxprocs int32 1057 ncpu int32 1058 forcegc forcegcstate 1059 sched schedt 1060 newprocs int32 1061 1062 // allpLock protects P-less reads and size changes of allp, idlepMask, 1063 // and timerpMask, and all writes to allp. 1064 allpLock mutex 1065 // len(allp) == gomaxprocs; may change at safe points, otherwise 1066 // immutable. 1067 allp []*p 1068 // Bitmask of Ps in _Pidle list, one bit per P. Reads and writes must 1069 // be atomic. Length may change at safe points. 1070 // 1071 // Each P must update only its own bit. In order to maintain 1072 // consistency, a P going idle must the idle mask simultaneously with 1073 // updates to the idle P list under the sched.lock, otherwise a racing 1074 // pidleget may clear the mask before pidleput sets the mask, 1075 // corrupting the bitmap. 1076 // 1077 // N.B., procresize takes ownership of all Ps in stopTheWorldWithSema. 1078 idlepMask pMask 1079 // Bitmask of Ps that may have a timer, one bit per P. Reads and writes 1080 // must be atomic. Length may change at safe points. 1081 timerpMask pMask 1082 1083 // Pool of GC parked background workers. Entries are type 1084 // *gcBgMarkWorkerNode. 1085 gcBgMarkWorkerPool lfstack 1086 1087 // Total number of gcBgMarkWorker goroutines. Protected by worldsema. 1088 gcBgMarkWorkerCount int32 1089 1090 // Information about what cpu features are available. 1091 // Packages outside the runtime should not use these 1092 // as they are not an external api. 1093 // Set on startup in asm_{386,amd64}.s 1094 processorVersionInfo uint32 1095 isIntel bool 1096 lfenceBeforeRdtsc bool 1097 1098 goarm uint8 // set by cmd/link on arm systems 1099 ) 1100 1101 // Set by the linker so the runtime can determine the buildmode. 1102 var ( 1103 islibrary bool // -buildmode=c-shared 1104 isarchive bool // -buildmode=c-archive 1105 ) 1106 1107 // Must agree with cmd/internal/objabi.Framepointer_enabled. 1108 const framepointer_enabled = GOARCH == "amd64" || GOARCH == "arm64" && (GOOS == "linux" || GOOS == "darwin" || GOOS == "ios")