github.com/liujq9674git/golang-src-1.7@v0.0.0-20230517174348-17f6ec47f3f8/src/runtime/mgc.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 // TODO(rsc): The code having to do with the heap bitmap needs very serious cleanup. 6 // It has gotten completely out of control. 7 8 // Garbage collector (GC). 9 // 10 // The GC runs concurrently with mutator threads, is type accurate (aka precise), allows multiple 11 // GC thread to run in parallel. It is a concurrent mark and sweep that uses a write barrier. It is 12 // non-generational and non-compacting. Allocation is done using size segregated per P allocation 13 // areas to minimize fragmentation while eliminating locks in the common case. 14 // 15 // The algorithm decomposes into several steps. 16 // This is a high level description of the algorithm being used. For an overview of GC a good 17 // place to start is Richard Jones' gchandbook.org. 18 // 19 // The algorithm's intellectual heritage includes Dijkstra's on-the-fly algorithm, see 20 // Edsger W. Dijkstra, Leslie Lamport, A. J. Martin, C. S. Scholten, and E. F. M. Steffens. 1978. 21 // On-the-fly garbage collection: an exercise in cooperation. Commun. ACM 21, 11 (November 1978), 22 // 966-975. 23 // For journal quality proofs that these steps are complete, correct, and terminate see 24 // Hudson, R., and Moss, J.E.B. Copying Garbage Collection without stopping the world. 25 // Concurrency and Computation: Practice and Experience 15(3-5), 2003. 26 // 27 // TODO(austin): The rest of this comment is woefully out of date and 28 // needs to be rewritten. There is no distinct scan phase any more and 29 // we allocate black during GC. 30 // 31 // 0. Set phase = GCscan from GCoff. 32 // 1. Wait for all P's to acknowledge phase change. 33 // At this point all goroutines have passed through a GC safepoint and 34 // know we are in the GCscan phase. 35 // 2. GC scans all goroutine stacks, mark and enqueues all encountered pointers 36 // (marking avoids most duplicate enqueuing but races may produce benign duplication). 37 // Preempted goroutines are scanned before P schedules next goroutine. 38 // 3. Set phase = GCmark. 39 // 4. Wait for all P's to acknowledge phase change. 40 // 5. Now write barrier marks and enqueues black, grey, or white to white pointers. 41 // Malloc still allocates white (non-marked) objects. 42 // 6. Meanwhile GC transitively walks the heap marking reachable objects. 43 // 7. When GC finishes marking heap, it preempts P's one-by-one and 44 // retakes partial wbufs (filled by write barrier or during a stack scan of the goroutine 45 // currently scheduled on the P). 46 // 8. Once the GC has exhausted all available marking work it sets phase = marktermination. 47 // 9. Wait for all P's to acknowledge phase change. 48 // 10. Malloc now allocates black objects, so number of unmarked reachable objects 49 // monotonically decreases. 50 // 11. GC preempts P's one-by-one taking partial wbufs and marks all unmarked yet 51 // reachable objects. 52 // 12. When GC completes a full cycle over P's and discovers no new grey 53 // objects, (which means all reachable objects are marked) set phase = GCoff. 54 // 13. Wait for all P's to acknowledge phase change. 55 // 14. Now malloc allocates white (but sweeps spans before use). 56 // Write barrier becomes nop. 57 // 15. GC does background sweeping, see description below. 58 // 16. When sufficient allocation has taken place replay the sequence starting at 0 above, 59 // see discussion of GC rate below. 60 61 // Changing phases. 62 // Phases are changed by setting the gcphase to the next phase and possibly calling ackgcphase. 63 // All phase action must be benign in the presence of a change. 64 // Starting with GCoff 65 // GCoff to GCscan 66 // GSscan scans stacks and globals greying them and never marks an object black. 67 // Once all the P's are aware of the new phase they will scan gs on preemption. 68 // This means that the scanning of preempted gs can't start until all the Ps 69 // have acknowledged. 70 // When a stack is scanned, this phase also installs stack barriers to 71 // track how much of the stack has been active. 72 // This transition enables write barriers because stack barriers 73 // assume that writes to higher frames will be tracked by write 74 // barriers. Technically this only needs write barriers for writes 75 // to stack slots, but we enable write barriers in general. 76 // GCscan to GCmark 77 // In GCmark, work buffers are drained until there are no more 78 // pointers to scan. 79 // No scanning of objects (making them black) can happen until all 80 // Ps have enabled the write barrier, but that already happened in 81 // the transition to GCscan. 82 // GCmark to GCmarktermination 83 // The only change here is that we start allocating black so the Ps must acknowledge 84 // the change before we begin the termination algorithm 85 // GCmarktermination to GSsweep 86 // Object currently on the freelist must be marked black for this to work. 87 // Are things on the free lists black or white? How does the sweep phase work? 88 89 // Concurrent sweep. 90 // 91 // The sweep phase proceeds concurrently with normal program execution. 92 // The heap is swept span-by-span both lazily (when a goroutine needs another span) 93 // and concurrently in a background goroutine (this helps programs that are not CPU bound). 94 // At the end of STW mark termination all spans are marked as "needs sweeping". 95 // 96 // The background sweeper goroutine simply sweeps spans one-by-one. 97 // 98 // To avoid requesting more OS memory while there are unswept spans, when a 99 // goroutine needs another span, it first attempts to reclaim that much memory 100 // by sweeping. When a goroutine needs to allocate a new small-object span, it 101 // sweeps small-object spans for the same object size until it frees at least 102 // one object. When a goroutine needs to allocate large-object span from heap, 103 // it sweeps spans until it frees at least that many pages into heap. There is 104 // one case where this may not suffice: if a goroutine sweeps and frees two 105 // nonadjacent one-page spans to the heap, it will allocate a new two-page 106 // span, but there can still be other one-page unswept spans which could be 107 // combined into a two-page span. 108 // 109 // It's critical to ensure that no operations proceed on unswept spans (that would corrupt 110 // mark bits in GC bitmap). During GC all mcaches are flushed into the central cache, 111 // so they are empty. When a goroutine grabs a new span into mcache, it sweeps it. 112 // When a goroutine explicitly frees an object or sets a finalizer, it ensures that 113 // the span is swept (either by sweeping it, or by waiting for the concurrent sweep to finish). 114 // The finalizer goroutine is kicked off only when all spans are swept. 115 // When the next GC starts, it sweeps all not-yet-swept spans (if any). 116 117 // GC rate. 118 // Next GC is after we've allocated an extra amount of memory proportional to 119 // the amount already in use. The proportion is controlled by GOGC environment variable 120 // (100 by default). If GOGC=100 and we're using 4M, we'll GC again when we get to 8M 121 // (this mark is tracked in next_gc variable). This keeps the GC cost in linear 122 // proportion to the allocation cost. Adjusting GOGC just changes the linear constant 123 // (and also the amount of extra memory used). 124 125 package runtime 126 127 import ( 128 "runtime/internal/atomic" 129 "runtime/internal/sys" 130 "unsafe" 131 ) 132 133 const ( 134 _DebugGC = 0 135 _ConcurrentSweep = true 136 _FinBlockSize = 4 * 1024 137 138 // sweepMinHeapDistance is a lower bound on the heap distance 139 // (in bytes) reserved for concurrent sweeping between GC 140 // cycles. This will be scaled by gcpercent/100. 141 sweepMinHeapDistance = 1024 * 1024 142 ) 143 144 // heapminimum is the minimum heap size at which to trigger GC. 145 // For small heaps, this overrides the usual GOGC*live set rule. 146 // 147 // When there is a very small live set but a lot of allocation, simply 148 // collecting when the heap reaches GOGC*live results in many GC 149 // cycles and high total per-GC overhead. This minimum amortizes this 150 // per-GC overhead while keeping the heap reasonably small. 151 // 152 // During initialization this is set to 4MB*GOGC/100. In the case of 153 // GOGC==0, this will set heapminimum to 0, resulting in constant 154 // collection even when the heap size is small, which is useful for 155 // debugging. 156 var heapminimum uint64 = defaultHeapMinimum 157 158 // defaultHeapMinimum is the value of heapminimum for GOGC==100. 159 const defaultHeapMinimum = 4 << 20 160 161 // Initialized from $GOGC. GOGC=off means no GC. 162 var gcpercent int32 163 164 func gcinit() { 165 if unsafe.Sizeof(workbuf{}) != _WorkbufSize { 166 throw("size of Workbuf is suboptimal") 167 } 168 169 _ = setGCPercent(readgogc()) 170 for datap := &firstmoduledata; datap != nil; datap = datap.next { 171 datap.gcdatamask = progToPointerMask((*byte)(unsafe.Pointer(datap.gcdata)), datap.edata-datap.data) 172 datap.gcbssmask = progToPointerMask((*byte)(unsafe.Pointer(datap.gcbss)), datap.ebss-datap.bss) 173 } 174 memstats.next_gc = heapminimum 175 work.startSema = 1 176 work.markDoneSema = 1 177 } 178 179 func readgogc() int32 { 180 p := gogetenv("GOGC") 181 if p == "" { 182 return 100 183 } 184 if p == "off" { 185 return -1 186 } 187 return int32(atoi(p)) 188 } 189 190 // gcenable is called after the bulk of the runtime initialization, 191 // just before we're about to start letting user code run. 192 // It kicks off the background sweeper goroutine and enables GC. 193 func gcenable() { 194 c := make(chan int, 1) 195 go bgsweep(c) 196 <-c 197 memstats.enablegc = true // now that runtime is initialized, GC is okay 198 } 199 200 //go:linkname setGCPercent runtime/debug.setGCPercent 201 func setGCPercent(in int32) (out int32) { 202 lock(&mheap_.lock) 203 out = gcpercent 204 if in < 0 { 205 in = -1 206 } 207 gcpercent = in 208 heapminimum = defaultHeapMinimum * uint64(gcpercent) / 100 209 if gcController.triggerRatio > float64(gcpercent)/100 { 210 gcController.triggerRatio = float64(gcpercent) / 100 211 } 212 unlock(&mheap_.lock) 213 return out 214 } 215 216 // Garbage collector phase. 217 // Indicates to write barrier and sychronization task to preform. 218 var gcphase uint32 219 220 // The compiler knows about this variable. 221 // If you change it, you must change the compiler too. 222 var writeBarrier struct { 223 enabled bool // compiler emits a check of this before calling write barrier 224 needed bool // whether we need a write barrier for current GC phase 225 cgo bool // whether we need a write barrier for a cgo check 226 alignme uint64 // guarantee alignment so that compiler can use a 32 or 64-bit load 227 } 228 229 // gcBlackenEnabled is 1 if mutator assists and background mark 230 // workers are allowed to blacken objects. This must only be set when 231 // gcphase == _GCmark. 232 var gcBlackenEnabled uint32 233 234 // gcBlackenPromptly indicates that optimizations that may 235 // hide work from the global work queue should be disabled. 236 // 237 // If gcBlackenPromptly is true, per-P gcWork caches should 238 // be flushed immediately and new objects should be allocated black. 239 // 240 // There is a tension between allocating objects white and 241 // allocating them black. If white and the objects die before being 242 // marked they can be collected during this GC cycle. On the other 243 // hand allocating them black will reduce _GCmarktermination latency 244 // since more work is done in the mark phase. This tension is resolved 245 // by allocating white until the mark phase is approaching its end and 246 // then allocating black for the remainder of the mark phase. 247 var gcBlackenPromptly bool 248 249 const ( 250 _GCoff = iota // GC not running; sweeping in background, write barrier disabled 251 _GCmark // GC marking roots and workbufs: allocate black, write barrier ENABLED 252 _GCmarktermination // GC mark termination: allocate black, P's help GC, write barrier ENABLED 253 ) 254 255 //go:nosplit 256 func setGCPhase(x uint32) { 257 atomic.Store(&gcphase, x) 258 writeBarrier.needed = gcphase == _GCmark || gcphase == _GCmarktermination 259 writeBarrier.enabled = writeBarrier.needed || writeBarrier.cgo 260 } 261 262 // gcMarkWorkerMode represents the mode that a concurrent mark worker 263 // should operate in. 264 // 265 // Concurrent marking happens through four different mechanisms. One 266 // is mutator assists, which happen in response to allocations and are 267 // not scheduled. The other three are variations in the per-P mark 268 // workers and are distinguished by gcMarkWorkerMode. 269 type gcMarkWorkerMode int 270 271 const ( 272 // gcMarkWorkerDedicatedMode indicates that the P of a mark 273 // worker is dedicated to running that mark worker. The mark 274 // worker should run without preemption. 275 gcMarkWorkerDedicatedMode gcMarkWorkerMode = iota 276 277 // gcMarkWorkerFractionalMode indicates that a P is currently 278 // running the "fractional" mark worker. The fractional worker 279 // is necessary when GOMAXPROCS*gcGoalUtilization is not an 280 // integer. The fractional worker should run until it is 281 // preempted and will be scheduled to pick up the fractional 282 // part of GOMAXPROCS*gcGoalUtilization. 283 gcMarkWorkerFractionalMode 284 285 // gcMarkWorkerIdleMode indicates that a P is running the mark 286 // worker because it has nothing else to do. The idle worker 287 // should run until it is preempted and account its time 288 // against gcController.idleMarkTime. 289 gcMarkWorkerIdleMode 290 ) 291 292 // gcController implements the GC pacing controller that determines 293 // when to trigger concurrent garbage collection and how much marking 294 // work to do in mutator assists and background marking. 295 // 296 // It uses a feedback control algorithm to adjust the memstats.next_gc 297 // trigger based on the heap growth and GC CPU utilization each cycle. 298 // This algorithm optimizes for heap growth to match GOGC and for CPU 299 // utilization between assist and background marking to be 25% of 300 // GOMAXPROCS. The high-level design of this algorithm is documented 301 // at https://golang.org/s/go15gcpacing. 302 var gcController = gcControllerState{ 303 // Initial trigger ratio guess. 304 triggerRatio: 7 / 8.0, 305 } 306 307 type gcControllerState struct { 308 // scanWork is the total scan work performed this cycle. This 309 // is updated atomically during the cycle. Updates occur in 310 // bounded batches, since it is both written and read 311 // throughout the cycle. At the end of the cycle, this is how 312 // much of the retained heap is scannable. 313 // 314 // Currently this is the bytes of heap scanned. For most uses, 315 // this is an opaque unit of work, but for estimation the 316 // definition is important. 317 scanWork int64 318 319 // bgScanCredit is the scan work credit accumulated by the 320 // concurrent background scan. This credit is accumulated by 321 // the background scan and stolen by mutator assists. This is 322 // updated atomically. Updates occur in bounded batches, since 323 // it is both written and read throughout the cycle. 324 bgScanCredit int64 325 326 // assistTime is the nanoseconds spent in mutator assists 327 // during this cycle. This is updated atomically. Updates 328 // occur in bounded batches, since it is both written and read 329 // throughout the cycle. 330 assistTime int64 331 332 // dedicatedMarkTime is the nanoseconds spent in dedicated 333 // mark workers during this cycle. This is updated atomically 334 // at the end of the concurrent mark phase. 335 dedicatedMarkTime int64 336 337 // fractionalMarkTime is the nanoseconds spent in the 338 // fractional mark worker during this cycle. This is updated 339 // atomically throughout the cycle and will be up-to-date if 340 // the fractional mark worker is not currently running. 341 fractionalMarkTime int64 342 343 // idleMarkTime is the nanoseconds spent in idle marking 344 // during this cycle. This is updated atomically throughout 345 // the cycle. 346 idleMarkTime int64 347 348 // markStartTime is the absolute start time in nanoseconds 349 // that assists and background mark workers started. 350 markStartTime int64 351 352 // heapGoal is the goal memstats.heap_live for when this cycle 353 // ends. This is computed at the beginning of each cycle. 354 heapGoal uint64 355 356 // dedicatedMarkWorkersNeeded is the number of dedicated mark 357 // workers that need to be started. This is computed at the 358 // beginning of each cycle and decremented atomically as 359 // dedicated mark workers get started. 360 dedicatedMarkWorkersNeeded int64 361 362 // assistWorkPerByte is the ratio of scan work to allocated 363 // bytes that should be performed by mutator assists. This is 364 // computed at the beginning of each cycle and updated every 365 // time heap_scan is updated. 366 assistWorkPerByte float64 367 368 // assistBytesPerWork is 1/assistWorkPerByte. 369 assistBytesPerWork float64 370 371 // fractionalUtilizationGoal is the fraction of wall clock 372 // time that should be spent in the fractional mark worker. 373 // For example, if the overall mark utilization goal is 25% 374 // and GOMAXPROCS is 6, one P will be a dedicated mark worker 375 // and this will be set to 0.5 so that 50% of the time some P 376 // is in a fractional mark worker. This is computed at the 377 // beginning of each cycle. 378 fractionalUtilizationGoal float64 379 380 // triggerRatio is the heap growth ratio at which the garbage 381 // collection cycle should start. E.g., if this is 0.6, then 382 // GC should start when the live heap has reached 1.6 times 383 // the heap size marked by the previous cycle. This is updated 384 // at the end of of each cycle. 385 triggerRatio float64 386 387 _ [sys.CacheLineSize]byte 388 389 // fractionalMarkWorkersNeeded is the number of fractional 390 // mark workers that need to be started. This is either 0 or 391 // 1. This is potentially updated atomically at every 392 // scheduling point (hence it gets its own cache line). 393 fractionalMarkWorkersNeeded int64 394 395 _ [sys.CacheLineSize]byte 396 } 397 398 // startCycle resets the GC controller's state and computes estimates 399 // for a new GC cycle. The caller must hold worldsema. 400 func (c *gcControllerState) startCycle() { 401 c.scanWork = 0 402 c.bgScanCredit = 0 403 c.assistTime = 0 404 c.dedicatedMarkTime = 0 405 c.fractionalMarkTime = 0 406 c.idleMarkTime = 0 407 408 // If this is the first GC cycle or we're operating on a very 409 // small heap, fake heap_marked so it looks like next_gc is 410 // the appropriate growth from heap_marked, even though the 411 // real heap_marked may not have a meaningful value (on the 412 // first cycle) or may be much smaller (resulting in a large 413 // error response). 414 if memstats.next_gc <= heapminimum { 415 memstats.heap_marked = uint64(float64(memstats.next_gc) / (1 + c.triggerRatio)) 416 memstats.heap_reachable = memstats.heap_marked 417 } 418 419 // Compute the heap goal for this cycle 420 c.heapGoal = memstats.heap_reachable + memstats.heap_reachable*uint64(gcpercent)/100 421 422 // Ensure that the heap goal is at least a little larger than 423 // the current live heap size. This may not be the case if GC 424 // start is delayed or if the allocation that pushed heap_live 425 // over next_gc is large or if the trigger is really close to 426 // GOGC. Assist is proportional to this distance, so enforce a 427 // minimum distance, even if it means going over the GOGC goal 428 // by a tiny bit. 429 if c.heapGoal < memstats.heap_live+1024*1024 { 430 c.heapGoal = memstats.heap_live + 1024*1024 431 } 432 433 // Compute the total mark utilization goal and divide it among 434 // dedicated and fractional workers. 435 totalUtilizationGoal := float64(gomaxprocs) * gcGoalUtilization 436 c.dedicatedMarkWorkersNeeded = int64(totalUtilizationGoal) 437 c.fractionalUtilizationGoal = totalUtilizationGoal - float64(c.dedicatedMarkWorkersNeeded) 438 if c.fractionalUtilizationGoal > 0 { 439 c.fractionalMarkWorkersNeeded = 1 440 } else { 441 c.fractionalMarkWorkersNeeded = 0 442 } 443 444 // Clear per-P state 445 for _, p := range &allp { 446 if p == nil { 447 break 448 } 449 p.gcAssistTime = 0 450 } 451 452 // Compute initial values for controls that are updated 453 // throughout the cycle. 454 c.revise() 455 456 if debug.gcpacertrace > 0 { 457 print("pacer: assist ratio=", c.assistWorkPerByte, 458 " (scan ", memstats.heap_scan>>20, " MB in ", 459 work.initialHeapLive>>20, "->", 460 c.heapGoal>>20, " MB)", 461 " workers=", c.dedicatedMarkWorkersNeeded, 462 "+", c.fractionalMarkWorkersNeeded, "\n") 463 } 464 } 465 466 // revise updates the assist ratio during the GC cycle to account for 467 // improved estimates. This should be called either under STW or 468 // whenever memstats.heap_scan or memstats.heap_live is updated (with 469 // mheap_.lock held). 470 // 471 // It should only be called when gcBlackenEnabled != 0 (because this 472 // is when assists are enabled and the necessary statistics are 473 // available). 474 // 475 // TODO: Consider removing the periodic controller update altogether. 476 // Since we switched to allocating black, in theory we shouldn't have 477 // to change the assist ratio. However, this is still a useful hook 478 // that we've found many uses for when experimenting. 479 func (c *gcControllerState) revise() { 480 // Compute the expected scan work remaining. 481 // 482 // Note that we currently count allocations during GC as both 483 // scannable heap (heap_scan) and scan work completed 484 // (scanWork), so this difference won't be changed by 485 // allocations during GC. 486 // 487 // This particular estimate is a strict upper bound on the 488 // possible remaining scan work for the current heap. 489 // You might consider dividing this by 2 (or by 490 // (100+GOGC)/100) to counter this over-estimation, but 491 // benchmarks show that this has almost no effect on mean 492 // mutator utilization, heap size, or assist time and it 493 // introduces the danger of under-estimating and letting the 494 // mutator outpace the garbage collector. 495 scanWorkExpected := int64(memstats.heap_scan) - c.scanWork 496 if scanWorkExpected < 1000 { 497 // We set a somewhat arbitrary lower bound on 498 // remaining scan work since if we aim a little high, 499 // we can miss by a little. 500 // 501 // We *do* need to enforce that this is at least 1, 502 // since marking is racy and double-scanning objects 503 // may legitimately make the expected scan work 504 // negative. 505 scanWorkExpected = 1000 506 } 507 508 // Compute the heap distance remaining. 509 heapDistance := int64(c.heapGoal) - int64(memstats.heap_live) 510 if heapDistance <= 0 { 511 // This shouldn't happen, but if it does, avoid 512 // dividing by zero or setting the assist negative. 513 heapDistance = 1 514 } 515 516 // Compute the mutator assist ratio so by the time the mutator 517 // allocates the remaining heap bytes up to next_gc, it will 518 // have done (or stolen) the remaining amount of scan work. 519 c.assistWorkPerByte = float64(scanWorkExpected) / float64(heapDistance) 520 c.assistBytesPerWork = float64(heapDistance) / float64(scanWorkExpected) 521 } 522 523 // endCycle updates the GC controller state at the end of the 524 // concurrent part of the GC cycle. 525 func (c *gcControllerState) endCycle() { 526 h_t := c.triggerRatio // For debugging 527 528 // Proportional response gain for the trigger controller. Must 529 // be in [0, 1]. Lower values smooth out transient effects but 530 // take longer to respond to phase changes. Higher values 531 // react to phase changes quickly, but are more affected by 532 // transient changes. Values near 1 may be unstable. 533 const triggerGain = 0.5 534 535 // Compute next cycle trigger ratio. First, this computes the 536 // "error" for this cycle; that is, how far off the trigger 537 // was from what it should have been, accounting for both heap 538 // growth and GC CPU utilization. We compute the actual heap 539 // growth during this cycle and scale that by how far off from 540 // the goal CPU utilization we were (to estimate the heap 541 // growth if we had the desired CPU utilization). The 542 // difference between this estimate and the GOGC-based goal 543 // heap growth is the error. 544 // 545 // TODO(austin): next_gc is based on heap_reachable, not 546 // heap_marked, which means the actual growth ratio 547 // technically isn't comparable to the trigger ratio. 548 goalGrowthRatio := float64(gcpercent) / 100 549 actualGrowthRatio := float64(memstats.heap_live)/float64(memstats.heap_marked) - 1 550 assistDuration := nanotime() - c.markStartTime 551 552 // Assume background mark hit its utilization goal. 553 utilization := gcGoalUtilization 554 // Add assist utilization; avoid divide by zero. 555 if assistDuration > 0 { 556 utilization += float64(c.assistTime) / float64(assistDuration*int64(gomaxprocs)) 557 } 558 559 triggerError := goalGrowthRatio - c.triggerRatio - utilization/gcGoalUtilization*(actualGrowthRatio-c.triggerRatio) 560 561 // Finally, we adjust the trigger for next time by this error, 562 // damped by the proportional gain. 563 c.triggerRatio += triggerGain * triggerError 564 if c.triggerRatio < 0 { 565 // This can happen if the mutator is allocating very 566 // quickly or the GC is scanning very slowly. 567 c.triggerRatio = 0 568 } else if c.triggerRatio > goalGrowthRatio*0.95 { 569 // Ensure there's always a little margin so that the 570 // mutator assist ratio isn't infinity. 571 c.triggerRatio = goalGrowthRatio * 0.95 572 } 573 574 if debug.gcpacertrace > 0 { 575 // Print controller state in terms of the design 576 // document. 577 H_m_prev := memstats.heap_marked 578 H_T := memstats.next_gc 579 h_a := actualGrowthRatio 580 H_a := memstats.heap_live 581 h_g := goalGrowthRatio 582 H_g := int64(float64(H_m_prev) * (1 + h_g)) 583 u_a := utilization 584 u_g := gcGoalUtilization 585 W_a := c.scanWork 586 print("pacer: H_m_prev=", H_m_prev, 587 " h_t=", h_t, " H_T=", H_T, 588 " h_a=", h_a, " H_a=", H_a, 589 " h_g=", h_g, " H_g=", H_g, 590 " u_a=", u_a, " u_g=", u_g, 591 " W_a=", W_a, 592 " goalΔ=", goalGrowthRatio-h_t, 593 " actualΔ=", h_a-h_t, 594 " u_a/u_g=", u_a/u_g, 595 "\n") 596 } 597 } 598 599 // enlistWorker encourages another dedicated mark worker to start on 600 // another P if there are spare worker slots. It is used by putfull 601 // when more work is made available. 602 // 603 //go:nowritebarrier 604 func (c *gcControllerState) enlistWorker() { 605 if c.dedicatedMarkWorkersNeeded <= 0 { 606 return 607 } 608 // Pick a random other P to preempt. 609 if gomaxprocs <= 1 { 610 return 611 } 612 gp := getg() 613 if gp == nil || gp.m == nil || gp.m.p == 0 { 614 return 615 } 616 myID := gp.m.p.ptr().id 617 for tries := 0; tries < 5; tries++ { 618 id := int32(fastrand1() % uint32(gomaxprocs-1)) 619 if id >= myID { 620 id++ 621 } 622 p := allp[id] 623 if p.status != _Prunning { 624 continue 625 } 626 if preemptone(p) { 627 return 628 } 629 } 630 } 631 632 // findRunnableGCWorker returns the background mark worker for _p_ if it 633 // should be run. This must only be called when gcBlackenEnabled != 0. 634 func (c *gcControllerState) findRunnableGCWorker(_p_ *p) *g { 635 if gcBlackenEnabled == 0 { 636 throw("gcControllerState.findRunnable: blackening not enabled") 637 } 638 if _p_.gcBgMarkWorker == 0 { 639 // The mark worker associated with this P is blocked 640 // performing a mark transition. We can't run it 641 // because it may be on some other run or wait queue. 642 return nil 643 } 644 645 if !gcMarkWorkAvailable(_p_) { 646 // No work to be done right now. This can happen at 647 // the end of the mark phase when there are still 648 // assists tapering off. Don't bother running a worker 649 // now because it'll just return immediately. 650 return nil 651 } 652 653 decIfPositive := func(ptr *int64) bool { 654 if *ptr > 0 { 655 if atomic.Xaddint64(ptr, -1) >= 0 { 656 return true 657 } 658 // We lost a race 659 atomic.Xaddint64(ptr, +1) 660 } 661 return false 662 } 663 664 if decIfPositive(&c.dedicatedMarkWorkersNeeded) { 665 // This P is now dedicated to marking until the end of 666 // the concurrent mark phase. 667 _p_.gcMarkWorkerMode = gcMarkWorkerDedicatedMode 668 // TODO(austin): This P isn't going to run anything 669 // else for a while, so kick everything out of its run 670 // queue. 671 } else { 672 if !decIfPositive(&c.fractionalMarkWorkersNeeded) { 673 // No more workers are need right now. 674 return nil 675 } 676 677 // This P has picked the token for the fractional worker. 678 // Is the GC currently under or at the utilization goal? 679 // If so, do more work. 680 // 681 // We used to check whether doing one time slice of work 682 // would remain under the utilization goal, but that has the 683 // effect of delaying work until the mutator has run for 684 // enough time slices to pay for the work. During those time 685 // slices, write barriers are enabled, so the mutator is running slower. 686 // Now instead we do the work whenever we're under or at the 687 // utilization work and pay for it by letting the mutator run later. 688 // This doesn't change the overall utilization averages, but it 689 // front loads the GC work so that the GC finishes earlier and 690 // write barriers can be turned off sooner, effectively giving 691 // the mutator a faster machine. 692 // 693 // The old, slower behavior can be restored by setting 694 // gcForcePreemptNS = forcePreemptNS. 695 const gcForcePreemptNS = 0 696 697 // TODO(austin): We could fast path this and basically 698 // eliminate contention on c.fractionalMarkWorkersNeeded by 699 // precomputing the minimum time at which it's worth 700 // next scheduling the fractional worker. Then Ps 701 // don't have to fight in the window where we've 702 // passed that deadline and no one has started the 703 // worker yet. 704 // 705 // TODO(austin): Shorter preemption interval for mark 706 // worker to improve fairness and give this 707 // finer-grained control over schedule? 708 now := nanotime() - gcController.markStartTime 709 then := now + gcForcePreemptNS 710 timeUsed := c.fractionalMarkTime + gcForcePreemptNS 711 if then > 0 && float64(timeUsed)/float64(then) > c.fractionalUtilizationGoal { 712 // Nope, we'd overshoot the utilization goal 713 atomic.Xaddint64(&c.fractionalMarkWorkersNeeded, +1) 714 return nil 715 } 716 _p_.gcMarkWorkerMode = gcMarkWorkerFractionalMode 717 } 718 719 // Run the background mark worker 720 gp := _p_.gcBgMarkWorker.ptr() 721 casgstatus(gp, _Gwaiting, _Grunnable) 722 if trace.enabled { 723 traceGoUnpark(gp, 0) 724 } 725 return gp 726 } 727 728 // gcGoalUtilization is the goal CPU utilization for background 729 // marking as a fraction of GOMAXPROCS. 730 const gcGoalUtilization = 0.25 731 732 // gcCreditSlack is the amount of scan work credit that can can 733 // accumulate locally before updating gcController.scanWork and, 734 // optionally, gcController.bgScanCredit. Lower values give a more 735 // accurate assist ratio and make it more likely that assists will 736 // successfully steal background credit. Higher values reduce memory 737 // contention. 738 const gcCreditSlack = 2000 739 740 // gcAssistTimeSlack is the nanoseconds of mutator assist time that 741 // can accumulate on a P before updating gcController.assistTime. 742 const gcAssistTimeSlack = 5000 743 744 // gcOverAssistWork determines how many extra units of scan work a GC 745 // assist does when an assist happens. This amortizes the cost of an 746 // assist by pre-paying for this many bytes of future allocations. 747 const gcOverAssistWork = 64 << 10 748 749 var work struct { 750 full uint64 // lock-free list of full blocks workbuf 751 empty uint64 // lock-free list of empty blocks workbuf 752 pad0 [sys.CacheLineSize]uint8 // prevents false-sharing between full/empty and nproc/nwait 753 754 markrootNext uint32 // next markroot job 755 markrootJobs uint32 // number of markroot jobs 756 757 nproc uint32 758 tstart int64 759 nwait uint32 760 ndone uint32 761 alldone note 762 763 // Number of roots of various root types. Set by gcMarkRootPrepare. 764 nDataRoots, nBSSRoots, nSpanRoots, nStackRoots, nRescanRoots int 765 766 // markrootDone indicates that roots have been marked at least 767 // once during the current GC cycle. This is checked by root 768 // marking operations that have to happen only during the 769 // first root marking pass, whether that's during the 770 // concurrent mark phase in current GC or mark termination in 771 // STW GC. 772 markrootDone bool 773 774 // Each type of GC state transition is protected by a lock. 775 // Since multiple threads can simultaneously detect the state 776 // transition condition, any thread that detects a transition 777 // condition must acquire the appropriate transition lock, 778 // re-check the transition condition and return if it no 779 // longer holds or perform the transition if it does. 780 // Likewise, any transition must invalidate the transition 781 // condition before releasing the lock. This ensures that each 782 // transition is performed by exactly one thread and threads 783 // that need the transition to happen block until it has 784 // happened. 785 // 786 // startSema protects the transition from "off" to mark or 787 // mark termination. 788 startSema uint32 789 // markDoneSema protects transitions from mark 1 to mark 2 and 790 // from mark 2 to mark termination. 791 markDoneSema uint32 792 793 bgMarkReady note // signal background mark worker has started 794 bgMarkDone uint32 // cas to 1 when at a background mark completion point 795 // Background mark completion signaling 796 797 // mode is the concurrency mode of the current GC cycle. 798 mode gcMode 799 800 // Copy of mheap.allspans for marker or sweeper. 801 spans []*mspan 802 803 // totaltime is the CPU nanoseconds spent in GC since the 804 // program started if debug.gctrace > 0. 805 totaltime int64 806 807 // bytesMarked is the number of bytes marked this cycle. This 808 // includes bytes blackened in scanned objects, noscan objects 809 // that go straight to black, and permagrey objects scanned by 810 // markroot during the concurrent scan phase. This is updated 811 // atomically during the cycle. Updates may be batched 812 // arbitrarily, since the value is only read at the end of the 813 // cycle. 814 // 815 // Because of benign races during marking, this number may not 816 // be the exact number of marked bytes, but it should be very 817 // close. 818 bytesMarked uint64 819 820 // initialHeapLive is the value of memstats.heap_live at the 821 // beginning of this GC cycle. 822 initialHeapLive uint64 823 824 // assistQueue is a queue of assists that are blocked because 825 // there was neither enough credit to steal or enough work to 826 // do. 827 assistQueue struct { 828 lock mutex 829 head, tail guintptr 830 } 831 832 // rescan is a list of G's that need to be rescanned during 833 // mark termination. A G adds itself to this list when it 834 // first invalidates its stack scan. 835 rescan struct { 836 lock mutex 837 list []guintptr 838 } 839 840 // Timing/utilization stats for this cycle. 841 stwprocs, maxprocs int32 842 tSweepTerm, tMark, tMarkTerm, tEnd int64 // nanotime() of phase start 843 844 pauseNS int64 // total STW time this cycle 845 pauseStart int64 // nanotime() of last STW 846 847 // debug.gctrace heap sizes for this cycle. 848 heap0, heap1, heap2, heapGoal uint64 849 } 850 851 // GC runs a garbage collection and blocks the caller until the 852 // garbage collection is complete. It may also block the entire 853 // program. 854 func GC() { 855 gcStart(gcForceBlockMode, false) 856 } 857 858 // gcMode indicates how concurrent a GC cycle should be. 859 type gcMode int 860 861 const ( 862 gcBackgroundMode gcMode = iota // concurrent GC and sweep 863 gcForceMode // stop-the-world GC now, concurrent sweep 864 gcForceBlockMode // stop-the-world GC now and STW sweep 865 ) 866 867 // gcShouldStart returns true if the exit condition for the _GCoff 868 // phase has been met. The exit condition should be tested when 869 // allocating. 870 // 871 // If forceTrigger is true, it ignores the current heap size, but 872 // checks all other conditions. In general this should be false. 873 func gcShouldStart(forceTrigger bool) bool { 874 return gcphase == _GCoff && (forceTrigger || memstats.heap_live >= memstats.next_gc) && memstats.enablegc && panicking == 0 && gcpercent >= 0 875 } 876 877 // gcStart transitions the GC from _GCoff to _GCmark (if mode == 878 // gcBackgroundMode) or _GCmarktermination (if mode != 879 // gcBackgroundMode) by performing sweep termination and GC 880 // initialization. 881 // 882 // This may return without performing this transition in some cases, 883 // such as when called on a system stack or with locks held. 884 func gcStart(mode gcMode, forceTrigger bool) { 885 // Since this is called from malloc and malloc is called in 886 // the guts of a number of libraries that might be holding 887 // locks, don't attempt to start GC in non-preemptible or 888 // potentially unstable situations. 889 mp := acquirem() 890 if gp := getg(); gp == mp.g0 || mp.locks > 1 || mp.preemptoff != "" { 891 releasem(mp) 892 return 893 } 894 releasem(mp) 895 mp = nil 896 897 // Pick up the remaining unswept/not being swept spans concurrently 898 // 899 // This shouldn't happen if we're being invoked in background 900 // mode since proportional sweep should have just finished 901 // sweeping everything, but rounding errors, etc, may leave a 902 // few spans unswept. In forced mode, this is necessary since 903 // GC can be forced at any point in the sweeping cycle. 904 // 905 // We check the transition condition continuously here in case 906 // this G gets delayed in to the next GC cycle. 907 for (mode != gcBackgroundMode || gcShouldStart(forceTrigger)) && gosweepone() != ^uintptr(0) { 908 sweep.nbgsweep++ 909 } 910 911 // Perform GC initialization and the sweep termination 912 // transition. 913 // 914 // If this is a forced GC, don't acquire the transition lock 915 // or re-check the transition condition because we 916 // specifically *don't* want to share the transition with 917 // another thread. 918 useStartSema := mode == gcBackgroundMode 919 if useStartSema { 920 semacquire(&work.startSema, false) 921 // Re-check transition condition under transition lock. 922 if !gcShouldStart(forceTrigger) { 923 semrelease(&work.startSema) 924 return 925 } 926 } 927 928 // In gcstoptheworld debug mode, upgrade the mode accordingly. 929 // We do this after re-checking the transition condition so 930 // that multiple goroutines that detect the heap trigger don't 931 // start multiple STW GCs. 932 if mode == gcBackgroundMode { 933 if debug.gcstoptheworld == 1 { 934 mode = gcForceMode 935 } else if debug.gcstoptheworld == 2 { 936 mode = gcForceBlockMode 937 } 938 } 939 940 // Ok, we're doing it! Stop everybody else 941 semacquire(&worldsema, false) 942 943 if trace.enabled { 944 traceGCStart() 945 } 946 947 if mode == gcBackgroundMode { 948 gcBgMarkStartWorkers() 949 } 950 951 gcResetMarkState() 952 953 now := nanotime() 954 work.stwprocs, work.maxprocs = gcprocs(), gomaxprocs 955 work.tSweepTerm = now 956 work.heap0 = memstats.heap_live 957 work.pauseNS = 0 958 work.mode = mode 959 960 work.pauseStart = now 961 systemstack(stopTheWorldWithSema) 962 // Finish sweep before we start concurrent scan. 963 systemstack(func() { 964 finishsweep_m(true) 965 }) 966 // clearpools before we start the GC. If we wait they memory will not be 967 // reclaimed until the next GC cycle. 968 clearpools() 969 970 if mode == gcBackgroundMode { // Do as much work concurrently as possible 971 gcController.startCycle() 972 work.heapGoal = gcController.heapGoal 973 974 // Enter concurrent mark phase and enable 975 // write barriers. 976 // 977 // Because the world is stopped, all Ps will 978 // observe that write barriers are enabled by 979 // the time we start the world and begin 980 // scanning. 981 // 982 // It's necessary to enable write barriers 983 // during the scan phase for several reasons: 984 // 985 // They must be enabled for writes to higher 986 // stack frames before we scan stacks and 987 // install stack barriers because this is how 988 // we track writes to inactive stack frames. 989 // (Alternatively, we could not install stack 990 // barriers over frame boundaries with 991 // up-pointers). 992 // 993 // They must be enabled before assists are 994 // enabled because they must be enabled before 995 // any non-leaf heap objects are marked. Since 996 // allocations are blocked until assists can 997 // happen, we want enable assists as early as 998 // possible. 999 setGCPhase(_GCmark) 1000 1001 // markrootSpans uses work.spans, so make sure 1002 // it is up to date. 1003 gcCopySpans() 1004 1005 gcBgMarkPrepare() // Must happen before assist enable. 1006 gcMarkRootPrepare() 1007 1008 // At this point all Ps have enabled the write 1009 // barrier, thus maintaining the no white to 1010 // black invariant. Enable mutator assists to 1011 // put back-pressure on fast allocating 1012 // mutators. 1013 atomic.Store(&gcBlackenEnabled, 1) 1014 1015 // Assists and workers can start the moment we start 1016 // the world. 1017 gcController.markStartTime = now 1018 1019 // Concurrent mark. 1020 systemstack(startTheWorldWithSema) 1021 now = nanotime() 1022 work.pauseNS += now - work.pauseStart 1023 work.tMark = now 1024 } else { 1025 t := nanotime() 1026 work.tMark, work.tMarkTerm = t, t 1027 work.heapGoal = work.heap0 1028 1029 // Perform mark termination. This will restart the world. 1030 gcMarkTermination() 1031 } 1032 1033 if useStartSema { 1034 semrelease(&work.startSema) 1035 } 1036 } 1037 1038 // gcMarkDone transitions the GC from mark 1 to mark 2 and from mark 2 1039 // to mark termination. 1040 // 1041 // This should be called when all mark work has been drained. In mark 1042 // 1, this includes all root marking jobs, global work buffers, and 1043 // active work buffers in assists and background workers; however, 1044 // work may still be cached in per-P work buffers. In mark 2, per-P 1045 // caches are disabled. 1046 // 1047 // The calling context must be preemptible. 1048 // 1049 // Note that it is explicitly okay to have write barriers in this 1050 // function because completion of concurrent mark is best-effort 1051 // anyway. Any work created by write barriers here will be cleaned up 1052 // by mark termination. 1053 func gcMarkDone() { 1054 top: 1055 semacquire(&work.markDoneSema, false) 1056 1057 // Re-check transition condition under transition lock. 1058 if !(gcphase == _GCmark && work.nwait == work.nproc && !gcMarkWorkAvailable(nil)) { 1059 semrelease(&work.markDoneSema) 1060 return 1061 } 1062 1063 // Disallow starting new workers so that any remaining workers 1064 // in the current mark phase will drain out. 1065 // 1066 // TODO(austin): Should dedicated workers keep an eye on this 1067 // and exit gcDrain promptly? 1068 atomic.Xaddint64(&gcController.dedicatedMarkWorkersNeeded, -0xffffffff) 1069 atomic.Xaddint64(&gcController.fractionalMarkWorkersNeeded, -0xffffffff) 1070 1071 if !gcBlackenPromptly { 1072 // Transition from mark 1 to mark 2. 1073 // 1074 // The global work list is empty, but there can still be work 1075 // sitting in the per-P work caches and there can be more 1076 // objects reachable from global roots since they don't have write 1077 // barriers. Rescan some roots and flush work caches. 1078 1079 // Disallow caching workbufs and indicate that we're in mark 2. 1080 gcBlackenPromptly = true 1081 1082 // Prevent completion of mark 2 until we've flushed 1083 // cached workbufs. 1084 atomic.Xadd(&work.nwait, -1) 1085 1086 // GC is set up for mark 2. Let Gs blocked on the 1087 // transition lock go while we flush caches. 1088 semrelease(&work.markDoneSema) 1089 1090 systemstack(func() { 1091 // Flush all currently cached workbufs and 1092 // ensure all Ps see gcBlackenPromptly. This 1093 // also blocks until any remaining mark 1 1094 // workers have exited their loop so we can 1095 // start new mark 2 workers that will observe 1096 // the new root marking jobs. 1097 forEachP(func(_p_ *p) { 1098 _p_.gcw.dispose() 1099 }) 1100 }) 1101 1102 // Check that roots are marked. We should be able to 1103 // do this before the forEachP, but based on issue 1104 // #16083 there may be a (harmless) race where we can 1105 // enter mark 2 while some workers are still scanning 1106 // stacks. The forEachP ensures these scans are done. 1107 // 1108 // TODO(austin): Figure out the race and fix this 1109 // properly. 1110 gcMarkRootCheck() 1111 1112 // Now we can start up mark 2 workers. 1113 atomic.Xaddint64(&gcController.dedicatedMarkWorkersNeeded, 0xffffffff) 1114 atomic.Xaddint64(&gcController.fractionalMarkWorkersNeeded, 0xffffffff) 1115 1116 incnwait := atomic.Xadd(&work.nwait, +1) 1117 if incnwait == work.nproc && !gcMarkWorkAvailable(nil) { 1118 // This loop will make progress because 1119 // gcBlackenPromptly is now true, so it won't 1120 // take this same "if" branch. 1121 goto top 1122 } 1123 } else { 1124 // Transition to mark termination. 1125 now := nanotime() 1126 work.tMarkTerm = now 1127 work.pauseStart = now 1128 getg().m.preemptoff = "gcing" 1129 systemstack(stopTheWorldWithSema) 1130 // The gcphase is _GCmark, it will transition to _GCmarktermination 1131 // below. The important thing is that the wb remains active until 1132 // all marking is complete. This includes writes made by the GC. 1133 1134 // Record that one root marking pass has completed. 1135 work.markrootDone = true 1136 1137 // Disable assists and background workers. We must do 1138 // this before waking blocked assists. 1139 atomic.Store(&gcBlackenEnabled, 0) 1140 1141 // Flush the gcWork caches. This must be done before 1142 // endCycle since endCycle depends on statistics kept 1143 // in these caches. 1144 gcFlushGCWork() 1145 1146 // Wake all blocked assists. These will run when we 1147 // start the world again. 1148 gcWakeAllAssists() 1149 1150 // Likewise, release the transition lock. Blocked 1151 // workers and assists will run when we start the 1152 // world again. 1153 semrelease(&work.markDoneSema) 1154 1155 gcController.endCycle() 1156 1157 // Perform mark termination. This will restart the world. 1158 gcMarkTermination() 1159 } 1160 } 1161 1162 func gcMarkTermination() { 1163 // World is stopped. 1164 // Start marktermination which includes enabling the write barrier. 1165 atomic.Store(&gcBlackenEnabled, 0) 1166 gcBlackenPromptly = false 1167 setGCPhase(_GCmarktermination) 1168 1169 work.heap1 = memstats.heap_live 1170 startTime := nanotime() 1171 1172 mp := acquirem() 1173 mp.preemptoff = "gcing" 1174 _g_ := getg() 1175 _g_.m.traceback = 2 1176 gp := _g_.m.curg 1177 casgstatus(gp, _Grunning, _Gwaiting) 1178 gp.waitreason = "garbage collection" 1179 1180 // Run gc on the g0 stack. We do this so that the g stack 1181 // we're currently running on will no longer change. Cuts 1182 // the root set down a bit (g0 stacks are not scanned, and 1183 // we don't need to scan gc's internal state). We also 1184 // need to switch to g0 so we can shrink the stack. 1185 systemstack(func() { 1186 gcMark(startTime) 1187 // Must return immediately. 1188 // The outer function's stack may have moved 1189 // during gcMark (it shrinks stacks, including the 1190 // outer function's stack), so we must not refer 1191 // to any of its variables. Return back to the 1192 // non-system stack to pick up the new addresses 1193 // before continuing. 1194 }) 1195 1196 systemstack(func() { 1197 work.heap2 = work.bytesMarked 1198 if debug.gccheckmark > 0 { 1199 // Run a full stop-the-world mark using checkmark bits, 1200 // to check that we didn't forget to mark anything during 1201 // the concurrent mark process. 1202 gcResetMarkState() 1203 initCheckmarks() 1204 gcMark(startTime) 1205 clearCheckmarks() 1206 } 1207 1208 // marking is complete so we can turn the write barrier off 1209 setGCPhase(_GCoff) 1210 gcSweep(work.mode) 1211 1212 if debug.gctrace > 1 { 1213 startTime = nanotime() 1214 // The g stacks have been scanned so 1215 // they have gcscanvalid==true and gcworkdone==true. 1216 // Reset these so that all stacks will be rescanned. 1217 gcResetMarkState() 1218 finishsweep_m(true) 1219 1220 // Still in STW but gcphase is _GCoff, reset to _GCmarktermination 1221 // At this point all objects will be found during the gcMark which 1222 // does a complete STW mark and object scan. 1223 setGCPhase(_GCmarktermination) 1224 gcMark(startTime) 1225 setGCPhase(_GCoff) // marking is done, turn off wb. 1226 gcSweep(work.mode) 1227 } 1228 }) 1229 1230 _g_.m.traceback = 0 1231 casgstatus(gp, _Gwaiting, _Grunning) 1232 1233 if trace.enabled { 1234 traceGCDone() 1235 } 1236 1237 // all done 1238 mp.preemptoff = "" 1239 1240 if gcphase != _GCoff { 1241 throw("gc done but gcphase != _GCoff") 1242 } 1243 1244 // Update timing memstats 1245 now, unixNow := nanotime(), unixnanotime() 1246 work.pauseNS += now - work.pauseStart 1247 work.tEnd = now 1248 atomic.Store64(&memstats.last_gc, uint64(unixNow)) // must be Unix time to make sense to user 1249 memstats.pause_ns[memstats.numgc%uint32(len(memstats.pause_ns))] = uint64(work.pauseNS) 1250 memstats.pause_end[memstats.numgc%uint32(len(memstats.pause_end))] = uint64(unixNow) 1251 memstats.pause_total_ns += uint64(work.pauseNS) 1252 1253 // Update work.totaltime. 1254 sweepTermCpu := int64(work.stwprocs) * (work.tMark - work.tSweepTerm) 1255 // We report idle marking time below, but omit it from the 1256 // overall utilization here since it's "free". 1257 markCpu := gcController.assistTime + gcController.dedicatedMarkTime + gcController.fractionalMarkTime 1258 markTermCpu := int64(work.stwprocs) * (work.tEnd - work.tMarkTerm) 1259 cycleCpu := sweepTermCpu + markCpu + markTermCpu 1260 work.totaltime += cycleCpu 1261 1262 // Compute overall GC CPU utilization. 1263 totalCpu := sched.totaltime + (now-sched.procresizetime)*int64(gomaxprocs) 1264 memstats.gc_cpu_fraction = float64(work.totaltime) / float64(totalCpu) 1265 1266 memstats.numgc++ 1267 1268 // Reset sweep state. 1269 sweep.nbgsweep = 0 1270 sweep.npausesweep = 0 1271 1272 systemstack(startTheWorldWithSema) 1273 1274 // Free stack spans. This must be done between GC cycles. 1275 systemstack(freeStackSpans) 1276 1277 // Best-effort remove stack barriers so they don't get in the 1278 // way of things like GDB and perf. 1279 lock(&allglock) 1280 myallgs := allgs 1281 unlock(&allglock) 1282 gcTryRemoveAllStackBarriers(myallgs) 1283 1284 // Print gctrace before dropping worldsema. As soon as we drop 1285 // worldsema another cycle could start and smash the stats 1286 // we're trying to print. 1287 if debug.gctrace > 0 { 1288 util := int(memstats.gc_cpu_fraction * 100) 1289 1290 var sbuf [24]byte 1291 printlock() 1292 print("gc ", memstats.numgc, 1293 " @", string(itoaDiv(sbuf[:], uint64(work.tSweepTerm-runtimeInitTime)/1e6, 3)), "s ", 1294 util, "%: ") 1295 prev := work.tSweepTerm 1296 for i, ns := range []int64{work.tMark, work.tMarkTerm, work.tEnd} { 1297 if i != 0 { 1298 print("+") 1299 } 1300 print(string(fmtNSAsMS(sbuf[:], uint64(ns-prev)))) 1301 prev = ns 1302 } 1303 print(" ms clock, ") 1304 for i, ns := range []int64{sweepTermCpu, gcController.assistTime, gcController.dedicatedMarkTime + gcController.fractionalMarkTime, gcController.idleMarkTime, markTermCpu} { 1305 if i == 2 || i == 3 { 1306 // Separate mark time components with /. 1307 print("/") 1308 } else if i != 0 { 1309 print("+") 1310 } 1311 print(string(fmtNSAsMS(sbuf[:], uint64(ns)))) 1312 } 1313 print(" ms cpu, ", 1314 work.heap0>>20, "->", work.heap1>>20, "->", work.heap2>>20, " MB, ", 1315 work.heapGoal>>20, " MB goal, ", 1316 work.maxprocs, " P") 1317 if work.mode != gcBackgroundMode { 1318 print(" (forced)") 1319 } 1320 print("\n") 1321 printunlock() 1322 } 1323 1324 semrelease(&worldsema) 1325 // Careful: another GC cycle may start now. 1326 1327 releasem(mp) 1328 mp = nil 1329 1330 // now that gc is done, kick off finalizer thread if needed 1331 if !concurrentSweep { 1332 // give the queued finalizers, if any, a chance to run 1333 Gosched() 1334 } 1335 } 1336 1337 // gcBgMarkStartWorkers prepares background mark worker goroutines. 1338 // These goroutines will not run until the mark phase, but they must 1339 // be started while the work is not stopped and from a regular G 1340 // stack. The caller must hold worldsema. 1341 func gcBgMarkStartWorkers() { 1342 // Background marking is performed by per-P G's. Ensure that 1343 // each P has a background GC G. 1344 for _, p := range &allp { 1345 if p == nil || p.status == _Pdead { 1346 break 1347 } 1348 if p.gcBgMarkWorker == 0 { 1349 go gcBgMarkWorker(p) 1350 notetsleepg(&work.bgMarkReady, -1) 1351 noteclear(&work.bgMarkReady) 1352 } 1353 } 1354 } 1355 1356 // gcBgMarkPrepare sets up state for background marking. 1357 // Mutator assists must not yet be enabled. 1358 func gcBgMarkPrepare() { 1359 // Background marking will stop when the work queues are empty 1360 // and there are no more workers (note that, since this is 1361 // concurrent, this may be a transient state, but mark 1362 // termination will clean it up). Between background workers 1363 // and assists, we don't really know how many workers there 1364 // will be, so we pretend to have an arbitrarily large number 1365 // of workers, almost all of which are "waiting". While a 1366 // worker is working it decrements nwait. If nproc == nwait, 1367 // there are no workers. 1368 work.nproc = ^uint32(0) 1369 work.nwait = ^uint32(0) 1370 } 1371 1372 func gcBgMarkWorker(_p_ *p) { 1373 gp := getg() 1374 1375 type parkInfo struct { 1376 m muintptr // Release this m on park. 1377 attach puintptr // If non-nil, attach to this p on park. 1378 } 1379 // We pass park to a gopark unlock function, so it can't be on 1380 // the stack (see gopark). Prevent deadlock from recursively 1381 // starting GC by disabling preemption. 1382 gp.m.preemptoff = "GC worker init" 1383 park := new(parkInfo) 1384 gp.m.preemptoff = "" 1385 1386 park.m.set(acquirem()) 1387 park.attach.set(_p_) 1388 // Inform gcBgMarkStartWorkers that this worker is ready. 1389 // After this point, the background mark worker is scheduled 1390 // cooperatively by gcController.findRunnable. Hence, it must 1391 // never be preempted, as this would put it into _Grunnable 1392 // and put it on a run queue. Instead, when the preempt flag 1393 // is set, this puts itself into _Gwaiting to be woken up by 1394 // gcController.findRunnable at the appropriate time. 1395 notewakeup(&work.bgMarkReady) 1396 1397 for { 1398 // Go to sleep until woken by gcController.findRunnable. 1399 // We can't releasem yet since even the call to gopark 1400 // may be preempted. 1401 gopark(func(g *g, parkp unsafe.Pointer) bool { 1402 park := (*parkInfo)(parkp) 1403 1404 // The worker G is no longer running, so it's 1405 // now safe to allow preemption. 1406 releasem(park.m.ptr()) 1407 1408 // If the worker isn't attached to its P, 1409 // attach now. During initialization and after 1410 // a phase change, the worker may have been 1411 // running on a different P. As soon as we 1412 // attach, the owner P may schedule the 1413 // worker, so this must be done after the G is 1414 // stopped. 1415 if park.attach != 0 { 1416 p := park.attach.ptr() 1417 park.attach.set(nil) 1418 // cas the worker because we may be 1419 // racing with a new worker starting 1420 // on this P. 1421 if !p.gcBgMarkWorker.cas(0, guintptr(unsafe.Pointer(g))) { 1422 // The P got a new worker. 1423 // Exit this worker. 1424 return false 1425 } 1426 } 1427 return true 1428 }, unsafe.Pointer(park), "GC worker (idle)", traceEvGoBlock, 0) 1429 1430 // Loop until the P dies and disassociates this 1431 // worker (the P may later be reused, in which case 1432 // it will get a new worker) or we failed to associate. 1433 if _p_.gcBgMarkWorker.ptr() != gp { 1434 break 1435 } 1436 1437 // Disable preemption so we can use the gcw. If the 1438 // scheduler wants to preempt us, we'll stop draining, 1439 // dispose the gcw, and then preempt. 1440 park.m.set(acquirem()) 1441 1442 if gcBlackenEnabled == 0 { 1443 throw("gcBgMarkWorker: blackening not enabled") 1444 } 1445 1446 startTime := nanotime() 1447 1448 decnwait := atomic.Xadd(&work.nwait, -1) 1449 if decnwait == work.nproc { 1450 println("runtime: work.nwait=", decnwait, "work.nproc=", work.nproc) 1451 throw("work.nwait was > work.nproc") 1452 } 1453 1454 switch _p_.gcMarkWorkerMode { 1455 default: 1456 throw("gcBgMarkWorker: unexpected gcMarkWorkerMode") 1457 case gcMarkWorkerDedicatedMode: 1458 gcDrain(&_p_.gcw, gcDrainNoBlock|gcDrainFlushBgCredit) 1459 case gcMarkWorkerFractionalMode, gcMarkWorkerIdleMode: 1460 gcDrain(&_p_.gcw, gcDrainUntilPreempt|gcDrainFlushBgCredit) 1461 } 1462 1463 // If we are nearing the end of mark, dispose 1464 // of the cache promptly. We must do this 1465 // before signaling that we're no longer 1466 // working so that other workers can't observe 1467 // no workers and no work while we have this 1468 // cached, and before we compute done. 1469 if gcBlackenPromptly { 1470 _p_.gcw.dispose() 1471 } 1472 1473 // Account for time. 1474 duration := nanotime() - startTime 1475 switch _p_.gcMarkWorkerMode { 1476 case gcMarkWorkerDedicatedMode: 1477 atomic.Xaddint64(&gcController.dedicatedMarkTime, duration) 1478 atomic.Xaddint64(&gcController.dedicatedMarkWorkersNeeded, 1) 1479 case gcMarkWorkerFractionalMode: 1480 atomic.Xaddint64(&gcController.fractionalMarkTime, duration) 1481 atomic.Xaddint64(&gcController.fractionalMarkWorkersNeeded, 1) 1482 case gcMarkWorkerIdleMode: 1483 atomic.Xaddint64(&gcController.idleMarkTime, duration) 1484 } 1485 1486 // Was this the last worker and did we run out 1487 // of work? 1488 incnwait := atomic.Xadd(&work.nwait, +1) 1489 if incnwait > work.nproc { 1490 println("runtime: p.gcMarkWorkerMode=", _p_.gcMarkWorkerMode, 1491 "work.nwait=", incnwait, "work.nproc=", work.nproc) 1492 throw("work.nwait > work.nproc") 1493 } 1494 1495 // If this worker reached a background mark completion 1496 // point, signal the main GC goroutine. 1497 if incnwait == work.nproc && !gcMarkWorkAvailable(nil) { 1498 // Make this G preemptible and disassociate it 1499 // as the worker for this P so 1500 // findRunnableGCWorker doesn't try to 1501 // schedule it. 1502 _p_.gcBgMarkWorker.set(nil) 1503 releasem(park.m.ptr()) 1504 1505 gcMarkDone() 1506 1507 // Disable preemption and prepare to reattach 1508 // to the P. 1509 // 1510 // We may be running on a different P at this 1511 // point, so we can't reattach until this G is 1512 // parked. 1513 park.m.set(acquirem()) 1514 park.attach.set(_p_) 1515 } 1516 } 1517 } 1518 1519 // gcMarkWorkAvailable returns true if executing a mark worker 1520 // on p is potentially useful. p may be nil, in which case it only 1521 // checks the global sources of work. 1522 func gcMarkWorkAvailable(p *p) bool { 1523 if p != nil && !p.gcw.empty() { 1524 return true 1525 } 1526 if atomic.Load64(&work.full) != 0 { 1527 return true // global work available 1528 } 1529 if work.markrootNext < work.markrootJobs { 1530 return true // root scan work available 1531 } 1532 return false 1533 } 1534 1535 // gcFlushGCWork disposes the gcWork caches of all Ps. The world must 1536 // be stopped. 1537 //go:nowritebarrier 1538 func gcFlushGCWork() { 1539 // Gather all cached GC work. All other Ps are stopped, so 1540 // it's safe to manipulate their GC work caches. 1541 for i := 0; i < int(gomaxprocs); i++ { 1542 allp[i].gcw.dispose() 1543 } 1544 } 1545 1546 // gcMark runs the mark (or, for concurrent GC, mark termination) 1547 // STW is in effect at this point. 1548 //TODO go:nowritebarrier 1549 func gcMark(start_time int64) { 1550 if debug.allocfreetrace > 0 { 1551 tracegc() 1552 } 1553 1554 if gcphase != _GCmarktermination { 1555 throw("in gcMark expecting to see gcphase as _GCmarktermination") 1556 } 1557 work.tstart = start_time 1558 1559 gcCopySpans() // TODO(rlh): should this be hoisted and done only once? Right now it is done for normal marking and also for checkmarking. 1560 1561 // Make sure the per-P gcWork caches are empty. During mark 1562 // termination, these caches can still be used temporarily, 1563 // but must be disposed to the global lists immediately. 1564 gcFlushGCWork() 1565 1566 // Queue root marking jobs. 1567 gcMarkRootPrepare() 1568 1569 work.nwait = 0 1570 work.ndone = 0 1571 work.nproc = uint32(gcprocs()) 1572 1573 if trace.enabled { 1574 traceGCScanStart() 1575 } 1576 1577 if work.nproc > 1 { 1578 noteclear(&work.alldone) 1579 helpgc(int32(work.nproc)) 1580 } 1581 1582 gchelperstart() 1583 1584 gcw := &getg().m.p.ptr().gcw 1585 gcDrain(gcw, gcDrainBlock) 1586 gcw.dispose() 1587 1588 if debug.gccheckmark > 0 { 1589 // This is expensive when there's a large number of 1590 // Gs, so only do it if checkmark is also enabled. 1591 gcMarkRootCheck() 1592 } 1593 if work.full != 0 { 1594 throw("work.full != 0") 1595 } 1596 1597 if work.nproc > 1 { 1598 notesleep(&work.alldone) 1599 } 1600 1601 // Record that at least one root marking pass has completed. 1602 work.markrootDone = true 1603 1604 for i := 0; i < int(gomaxprocs); i++ { 1605 gcw := &allp[i].gcw 1606 if !gcw.empty() { 1607 throw("P has cached GC work at end of mark termination") 1608 } 1609 if gcw.scanWork != 0 || gcw.bytesMarked != 0 { 1610 throw("P has unflushed stats at end of mark termination") 1611 } 1612 } 1613 1614 if trace.enabled { 1615 traceGCScanDone() 1616 } 1617 1618 cachestats() 1619 1620 // Update the reachable heap stat. 1621 memstats.heap_reachable = work.bytesMarked 1622 1623 // Trigger the next GC cycle when the allocated heap has grown 1624 // by triggerRatio over the reachable heap size. Assume that 1625 // we're in steady state, so the reachable heap size is the 1626 // same now as it was at the beginning of the GC cycle. 1627 memstats.next_gc = uint64(float64(memstats.heap_reachable) * (1 + gcController.triggerRatio)) 1628 if memstats.next_gc < heapminimum { 1629 memstats.next_gc = heapminimum 1630 } 1631 if int64(memstats.next_gc) < 0 { 1632 print("next_gc=", memstats.next_gc, " bytesMarked=", work.bytesMarked, " heap_live=", memstats.heap_live, " initialHeapLive=", work.initialHeapLive, "\n") 1633 throw("next_gc underflow") 1634 } 1635 1636 // Update other GC heap size stats. This must happen after 1637 // cachestats (which flushes local statistics to these) and 1638 // flushallmcaches (which modifies heap_live). 1639 memstats.heap_live = work.bytesMarked 1640 memstats.heap_marked = work.bytesMarked 1641 memstats.heap_scan = uint64(gcController.scanWork) 1642 1643 minNextGC := memstats.heap_live + sweepMinHeapDistance*uint64(gcpercent)/100 1644 if memstats.next_gc < minNextGC { 1645 // The allocated heap is already past the trigger. 1646 // This can happen if the triggerRatio is very low and 1647 // the reachable heap estimate is less than the live 1648 // heap size. 1649 // 1650 // Concurrent sweep happens in the heap growth from 1651 // heap_live to next_gc, so bump next_gc up to ensure 1652 // that concurrent sweep has some heap growth in which 1653 // to perform sweeping before we start the next GC 1654 // cycle. 1655 memstats.next_gc = minNextGC 1656 } 1657 1658 if trace.enabled { 1659 traceHeapAlloc() 1660 traceNextGC() 1661 } 1662 } 1663 1664 func gcSweep(mode gcMode) { 1665 if gcphase != _GCoff { 1666 throw("gcSweep being done but phase is not GCoff") 1667 } 1668 gcCopySpans() 1669 1670 lock(&mheap_.lock) 1671 mheap_.sweepgen += 2 1672 mheap_.sweepdone = 0 1673 sweep.spanidx = 0 1674 unlock(&mheap_.lock) 1675 1676 if !_ConcurrentSweep || mode == gcForceBlockMode { 1677 // Special case synchronous sweep. 1678 // Record that no proportional sweeping has to happen. 1679 lock(&mheap_.lock) 1680 mheap_.sweepPagesPerByte = 0 1681 mheap_.pagesSwept = 0 1682 unlock(&mheap_.lock) 1683 // Sweep all spans eagerly. 1684 for sweepone() != ^uintptr(0) { 1685 sweep.npausesweep++ 1686 } 1687 // Do an additional mProf_GC, because all 'free' events are now real as well. 1688 mProf_GC() 1689 mProf_GC() 1690 return 1691 } 1692 1693 // Concurrent sweep needs to sweep all of the in-use pages by 1694 // the time the allocated heap reaches the GC trigger. Compute 1695 // the ratio of in-use pages to sweep per byte allocated. 1696 heapDistance := int64(memstats.next_gc) - int64(memstats.heap_live) 1697 // Add a little margin so rounding errors and concurrent 1698 // sweep are less likely to leave pages unswept when GC starts. 1699 heapDistance -= 1024 * 1024 1700 if heapDistance < _PageSize { 1701 // Avoid setting the sweep ratio extremely high 1702 heapDistance = _PageSize 1703 } 1704 lock(&mheap_.lock) 1705 mheap_.sweepPagesPerByte = float64(mheap_.pagesInUse) / float64(heapDistance) 1706 mheap_.pagesSwept = 0 1707 mheap_.spanBytesAlloc = 0 1708 unlock(&mheap_.lock) 1709 1710 // Background sweep. 1711 lock(&sweep.lock) 1712 if sweep.parked { 1713 sweep.parked = false 1714 ready(sweep.g, 0, true) 1715 } 1716 unlock(&sweep.lock) 1717 mProf_GC() 1718 } 1719 1720 func gcCopySpans() { 1721 // Cache runtime.mheap_.allspans in work.spans to avoid conflicts with 1722 // resizing/freeing allspans. 1723 // New spans can be created while GC progresses, but they are not garbage for 1724 // this round: 1725 // - new stack spans can be created even while the world is stopped. 1726 // - new malloc spans can be created during the concurrent sweep 1727 // Even if this is stop-the-world, a concurrent exitsyscall can allocate a stack from heap. 1728 lock(&mheap_.lock) 1729 // Free the old cached mark array if necessary. 1730 if work.spans != nil && &work.spans[0] != &h_allspans[0] { 1731 sysFree(unsafe.Pointer(&work.spans[0]), uintptr(len(work.spans))*unsafe.Sizeof(work.spans[0]), &memstats.other_sys) 1732 } 1733 // Cache the current array for sweeping. 1734 mheap_.gcspans = mheap_.allspans 1735 work.spans = h_allspans 1736 unlock(&mheap_.lock) 1737 } 1738 1739 // gcResetMarkState resets global state prior to marking (concurrent 1740 // or STW) and resets the stack scan state of all Gs. 1741 // 1742 // This is safe to do without the world stopped because any Gs created 1743 // during or after this will start out in the reset state. 1744 func gcResetMarkState() { 1745 // This may be called during a concurrent phase, so make sure 1746 // allgs doesn't change. 1747 if !(gcphase == _GCoff || gcphase == _GCmarktermination) { 1748 // Accessing gcRescan is unsafe. 1749 throw("bad GC phase") 1750 } 1751 lock(&allglock) 1752 for _, gp := range allgs { 1753 gp.gcscandone = false // set to true in gcphasework 1754 gp.gcscanvalid = false // stack has not been scanned 1755 gp.gcRescan = -1 1756 gp.gcAssistBytes = 0 1757 } 1758 unlock(&allglock) 1759 1760 // Clear rescan list. 1761 work.rescan.list = work.rescan.list[:0] 1762 1763 work.bytesMarked = 0 1764 work.initialHeapLive = memstats.heap_live 1765 work.markrootDone = false 1766 } 1767 1768 // Hooks for other packages 1769 1770 var poolcleanup func() 1771 1772 //go:linkname sync_runtime_registerPoolCleanup sync.runtime_registerPoolCleanup 1773 func sync_runtime_registerPoolCleanup(f func()) { 1774 poolcleanup = f 1775 } 1776 1777 func clearpools() { 1778 // clear sync.Pools 1779 if poolcleanup != nil { 1780 poolcleanup() 1781 } 1782 1783 // Clear central sudog cache. 1784 // Leave per-P caches alone, they have strictly bounded size. 1785 // Disconnect cached list before dropping it on the floor, 1786 // so that a dangling ref to one entry does not pin all of them. 1787 lock(&sched.sudoglock) 1788 var sg, sgnext *sudog 1789 for sg = sched.sudogcache; sg != nil; sg = sgnext { 1790 sgnext = sg.next 1791 sg.next = nil 1792 } 1793 sched.sudogcache = nil 1794 unlock(&sched.sudoglock) 1795 1796 // Clear central defer pools. 1797 // Leave per-P pools alone, they have strictly bounded size. 1798 lock(&sched.deferlock) 1799 for i := range sched.deferpool { 1800 // disconnect cached list before dropping it on the floor, 1801 // so that a dangling ref to one entry does not pin all of them. 1802 var d, dlink *_defer 1803 for d = sched.deferpool[i]; d != nil; d = dlink { 1804 dlink = d.link 1805 d.link = nil 1806 } 1807 sched.deferpool[i] = nil 1808 } 1809 unlock(&sched.deferlock) 1810 } 1811 1812 // Timing 1813 1814 //go:nowritebarrier 1815 func gchelper() { 1816 _g_ := getg() 1817 _g_.m.traceback = 2 1818 gchelperstart() 1819 1820 if trace.enabled { 1821 traceGCScanStart() 1822 } 1823 1824 // Parallel mark over GC roots and heap 1825 if gcphase == _GCmarktermination { 1826 gcw := &_g_.m.p.ptr().gcw 1827 gcDrain(gcw, gcDrainBlock) // blocks in getfull 1828 gcw.dispose() 1829 } 1830 1831 if trace.enabled { 1832 traceGCScanDone() 1833 } 1834 1835 nproc := work.nproc // work.nproc can change right after we increment work.ndone 1836 if atomic.Xadd(&work.ndone, +1) == nproc-1 { 1837 notewakeup(&work.alldone) 1838 } 1839 _g_.m.traceback = 0 1840 } 1841 1842 func gchelperstart() { 1843 _g_ := getg() 1844 1845 if _g_.m.helpgc < 0 || _g_.m.helpgc >= _MaxGcproc { 1846 throw("gchelperstart: bad m->helpgc") 1847 } 1848 if _g_ != _g_.m.g0 { 1849 throw("gchelper not running on g0 stack") 1850 } 1851 } 1852 1853 // itoaDiv formats val/(10**dec) into buf. 1854 func itoaDiv(buf []byte, val uint64, dec int) []byte { 1855 i := len(buf) - 1 1856 idec := i - dec 1857 for val >= 10 || i >= idec { 1858 buf[i] = byte(val%10 + '0') 1859 i-- 1860 if i == idec { 1861 buf[i] = '.' 1862 i-- 1863 } 1864 val /= 10 1865 } 1866 buf[i] = byte(val + '0') 1867 return buf[i:] 1868 } 1869 1870 // fmtNSAsMS nicely formats ns nanoseconds as milliseconds. 1871 func fmtNSAsMS(buf []byte, ns uint64) []byte { 1872 if ns >= 10e6 { 1873 // Format as whole milliseconds. 1874 return itoaDiv(buf, ns/1e6, 0) 1875 } 1876 // Format two digits of precision, with at most three decimal places. 1877 x := ns / 1e3 1878 if x == 0 { 1879 buf[0] = '0' 1880 return buf[:1] 1881 } 1882 dec := 3 1883 for x >= 100 { 1884 x /= 10 1885 dec-- 1886 } 1887 return itoaDiv(buf, x, dec) 1888 }