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.
5 // Garbage collector (GC).
7 // The GC runs concurrently with mutator threads, is type accurate (aka precise), allows multiple
8 // GC thread to run in parallel. It is a concurrent mark and sweep that uses a write barrier. It is
9 // non-generational and non-compacting. Allocation is done using size segregated per P allocation
10 // areas to minimize fragmentation while eliminating locks in the common case.
12 // The algorithm decomposes into several steps.
13 // This is a high level description of the algorithm being used. For an overview of GC a good
14 // place to start is Richard Jones' gchandbook.org.
16 // The algorithm's intellectual heritage includes Dijkstra's on-the-fly algorithm, see
17 // Edsger W. Dijkstra, Leslie Lamport, A. J. Martin, C. S. Scholten, and E. F. M. Steffens. 1978.
18 // On-the-fly garbage collection: an exercise in cooperation. Commun. ACM 21, 11 (November 1978),
20 // For journal quality proofs that these steps are complete, correct, and terminate see
21 // Hudson, R., and Moss, J.E.B. Copying Garbage Collection without stopping the world.
22 // Concurrency and Computation: Practice and Experience 15(3-5), 2003.
24 // 1. GC performs sweep termination.
26 // a. Stop the world. This causes all Ps to reach a GC safe-point.
28 // b. Sweep any unswept spans. There will only be unswept spans if
29 // this GC cycle was forced before the expected time.
31 // 2. GC performs the mark phase.
33 // a. Prepare for the mark phase by setting gcphase to _GCmark
34 // (from _GCoff), enabling the write barrier, enabling mutator
35 // assists, and enqueueing root mark jobs. No objects may be
36 // scanned until all Ps have enabled the write barrier, which is
37 // accomplished using STW.
39 // b. Start the world. From this point, GC work is done by mark
40 // workers started by the scheduler and by assists performed as
41 // part of allocation. The write barrier shades both the
42 // overwritten pointer and the new pointer value for any pointer
43 // writes (see mbarrier.go for details). Newly allocated objects
44 // are immediately marked black.
46 // c. GC performs root marking jobs. This includes scanning all
47 // stacks, shading all globals, and shading any heap pointers in
48 // off-heap runtime data structures. Scanning a stack stops a
49 // goroutine, shades any pointers found on its stack, and then
50 // resumes the goroutine.
52 // d. GC drains the work queue of grey objects, scanning each grey
53 // object to black and shading all pointers found in the object
54 // (which in turn may add those pointers to the work queue).
56 // e. Because GC work is spread across local caches, GC uses a
57 // distributed termination algorithm to detect when there are no
58 // more root marking jobs or grey objects (see gcMarkDone). At this
59 // point, GC transitions to mark termination.
61 // 3. GC performs mark termination.
65 // b. Set gcphase to _GCmarktermination, and disable workers and
68 // c. Perform housekeeping like flushing mcaches.
70 // 4. GC performs the sweep phase.
72 // a. Prepare for the sweep phase by setting gcphase to _GCoff,
73 // setting up sweep state and disabling the write barrier.
75 // b. Start the world. From this point on, newly allocated objects
76 // are white, and allocating sweeps spans before use if necessary.
78 // c. GC does concurrent sweeping in the background and in response
79 // to allocation. See description below.
81 // 5. When sufficient allocation has taken place, replay the sequence
82 // starting with 1 above. See discussion of GC rate below.
86 // The sweep phase proceeds concurrently with normal program execution.
87 // The heap is swept span-by-span both lazily (when a goroutine needs another span)
88 // and concurrently in a background goroutine (this helps programs that are not CPU bound).
89 // At the end of STW mark termination all spans are marked as "needs sweeping".
91 // The background sweeper goroutine simply sweeps spans one-by-one.
93 // To avoid requesting more OS memory while there are unswept spans, when a
94 // goroutine needs another span, it first attempts to reclaim that much memory
95 // by sweeping. When a goroutine needs to allocate a new small-object span, it
96 // sweeps small-object spans for the same object size until it frees at least
97 // one object. When a goroutine needs to allocate large-object span from heap,
98 // it sweeps spans until it frees at least that many pages into heap. There is
99 // one case where this may not suffice: if a goroutine sweeps and frees two
100 // nonadjacent one-page spans to the heap, it will allocate a new two-page
101 // span, but there can still be other one-page unswept spans which could be
102 // combined into a two-page span.
104 // It's critical to ensure that no operations proceed on unswept spans (that would corrupt
105 // mark bits in GC bitmap). During GC all mcaches are flushed into the central cache,
106 // so they are empty. When a goroutine grabs a new span into mcache, it sweeps it.
107 // When a goroutine explicitly frees an object or sets a finalizer, it ensures that
108 // the span is swept (either by sweeping it, or by waiting for the concurrent sweep to finish).
109 // The finalizer goroutine is kicked off only when all spans are swept.
110 // When the next GC starts, it sweeps all not-yet-swept spans (if any).
113 // Next GC is after we've allocated an extra amount of memory proportional to
114 // the amount already in use. The proportion is controlled by GOGC environment variable
115 // (100 by default). If GOGC=100 and we're using 4M, we'll GC again when we get to 8M
116 // (this mark is computed by the gcController.heapGoal method). This keeps the GC cost in
117 // linear proportion to the allocation cost. Adjusting GOGC just changes the linear constant
118 // (and also the amount of extra memory used).
122 // In order to prevent long pauses while scanning large objects and to
123 // improve parallelism, the garbage collector breaks up scan jobs for
124 // objects larger than maxObletBytes into "oblets" of at most
125 // maxObletBytes. When scanning encounters the beginning of a large
126 // object, it scans only the first oblet and enqueues the remaining
127 // oblets as new scan jobs.
133 "runtime/internal/atomic"
139 _FinBlockSize = 4 * 1024
141 // concurrentSweep is a debug flag. Disabling this flag
142 // ensures all spans are swept while the world is stopped.
143 concurrentSweep = true
145 // debugScanConservative enables debug logging for stack
146 // frames that are scanned conservatively.
147 debugScanConservative = false
149 // sweepMinHeapDistance is a lower bound on the heap distance
150 // (in bytes) reserved for concurrent sweeping between GC
152 sweepMinHeapDistance = 1024 * 1024
155 // heapObjectsCanMove always returns false in the current garbage collector.
156 // It exists for go4.org/unsafe/assume-no-moving-gc, which is an
157 // unfortunate idea that had an even more unfortunate implementation.
158 // Every time a new Go release happened, the package stopped building,
159 // and the authors had to add a new file with a new //go:build line, and
160 // then the entire ecosystem of packages with that as a dependency had to
161 // explicitly update to the new version. Many packages depend on
162 // assume-no-moving-gc transitively, through paths like
163 // inet.af/netaddr -> go4.org/intern -> assume-no-moving-gc.
164 // This was causing a significant amount of friction around each new
165 // release, so we added this bool for the package to //go:linkname
166 // instead. The bool is still unfortunate, but it's not as bad as
167 // breaking the ecosystem on every new release.
169 // If the Go garbage collector ever does move heap objects, we can set
170 // this to true to break all the programs using assume-no-moving-gc.
172 //go:linkname heapObjectsCanMove
173 func heapObjectsCanMove() bool {
178 if unsafe.Sizeof(workbuf{}) != _WorkbufSize {
179 throw("size of Workbuf is suboptimal")
181 // No sweep on the first cycle.
182 sweep.active.state.Store(sweepDrainedMask)
184 // Initialize GC pacer state.
185 // Use the environment variable GOGC for the initial gcPercent value.
186 // Use the environment variable GOMEMLIMIT for the initial memoryLimit value.
187 gcController.init(readGOGC(), readGOMEMLIMIT())
190 work.markDoneSema = 1
191 lockInit(&work.sweepWaiters.lock, lockRankSweepWaiters)
192 lockInit(&work.assistQueue.lock, lockRankAssistQueue)
193 lockInit(&work.wbufSpans.lock, lockRankWbufSpans)
196 // gcenable is called after the bulk of the runtime initialization,
197 // just before we're about to start letting user code run.
198 // It kicks off the background sweeper goroutine, the background
199 // scavenger goroutine, and enables GC.
201 // Kick off sweeping and scavenging.
202 c := make(chan int, 2)
207 memstats.enablegc = true // now that runtime is initialized, GC is okay
210 // Garbage collector phase.
211 // Indicates to write barrier and synchronization task to perform.
214 // The compiler knows about this variable.
215 // If you change it, you must change builtin/runtime.go, too.
216 // If you change the first four bytes, you must also change the write
217 // barrier insertion code.
218 var writeBarrier struct {
219 enabled bool // compiler emits a check of this before calling write barrier
220 pad [3]byte // compiler uses 32-bit load for "enabled" field
221 needed bool // identical to enabled, for now (TODO: dedup)
222 alignme uint64 // guarantee alignment so that compiler can use a 32 or 64-bit load
225 // gcBlackenEnabled is 1 if mutator assists and background mark
226 // workers are allowed to blacken objects. This must only be set when
227 // gcphase == _GCmark.
228 var gcBlackenEnabled uint32
231 _GCoff = iota // GC not running; sweeping in background, write barrier disabled
232 _GCmark // GC marking roots and workbufs: allocate black, write barrier ENABLED
233 _GCmarktermination // GC mark termination: allocate black, P's help GC, write barrier ENABLED
237 func setGCPhase(x uint32) {
238 atomic.Store(&gcphase, x)
239 writeBarrier.needed = gcphase == _GCmark || gcphase == _GCmarktermination
240 writeBarrier.enabled = writeBarrier.needed
243 // gcMarkWorkerMode represents the mode that a concurrent mark worker
244 // should operate in.
246 // Concurrent marking happens through four different mechanisms. One
247 // is mutator assists, which happen in response to allocations and are
248 // not scheduled. The other three are variations in the per-P mark
249 // workers and are distinguished by gcMarkWorkerMode.
250 type gcMarkWorkerMode int
253 // gcMarkWorkerNotWorker indicates that the next scheduled G is not
254 // starting work and the mode should be ignored.
255 gcMarkWorkerNotWorker gcMarkWorkerMode = iota
257 // gcMarkWorkerDedicatedMode indicates that the P of a mark
258 // worker is dedicated to running that mark worker. The mark
259 // worker should run without preemption.
260 gcMarkWorkerDedicatedMode
262 // gcMarkWorkerFractionalMode indicates that a P is currently
263 // running the "fractional" mark worker. The fractional worker
264 // is necessary when GOMAXPROCS*gcBackgroundUtilization is not
265 // an integer and using only dedicated workers would result in
266 // utilization too far from the target of gcBackgroundUtilization.
267 // The fractional worker should run until it is preempted and
268 // will be scheduled to pick up the fractional part of
269 // GOMAXPROCS*gcBackgroundUtilization.
270 gcMarkWorkerFractionalMode
272 // gcMarkWorkerIdleMode indicates that a P is running the mark
273 // worker because it has nothing else to do. The idle worker
274 // should run until it is preempted and account its time
275 // against gcController.idleMarkTime.
279 // gcMarkWorkerModeStrings are the strings labels of gcMarkWorkerModes
280 // to use in execution traces.
281 var gcMarkWorkerModeStrings = [...]string{
288 // pollFractionalWorkerExit reports whether a fractional mark worker
289 // should self-preempt. It assumes it is called from the fractional
291 func pollFractionalWorkerExit() bool {
292 // This should be kept in sync with the fractional worker
293 // scheduler logic in findRunnableGCWorker.
295 delta := now - gcController.markStartTime
299 p := getg().m.p.ptr()
300 selfTime := p.gcFractionalMarkTime + (now - p.gcMarkWorkerStartTime)
301 // Add some slack to the utilization goal so that the
302 // fractional worker isn't behind again the instant it exits.
303 return float64(selfTime)/float64(delta) > 1.2*gcController.fractionalUtilizationGoal
308 type workType struct {
309 full lfstack // lock-free list of full blocks workbuf
310 _ cpu.CacheLinePad // prevents false-sharing between full and empty
311 empty lfstack // lock-free list of empty blocks workbuf
312 _ cpu.CacheLinePad // prevents false-sharing between empty and nproc/nwait
316 // free is a list of spans dedicated to workbufs, but
317 // that don't currently contain any workbufs.
319 // busy is a list of all spans containing workbufs on
320 // one of the workbuf lists.
324 // Restore 64-bit alignment on 32-bit.
327 // bytesMarked is the number of bytes marked this cycle. This
328 // includes bytes blackened in scanned objects, noscan objects
329 // that go straight to black, and permagrey objects scanned by
330 // markroot during the concurrent scan phase. This is updated
331 // atomically during the cycle. Updates may be batched
332 // arbitrarily, since the value is only read at the end of the
335 // Because of benign races during marking, this number may not
336 // be the exact number of marked bytes, but it should be very
339 // Put this field here because it needs 64-bit atomic access
340 // (and thus 8-byte alignment even on 32-bit architectures).
343 markrootNext uint32 // next markroot job
344 markrootJobs uint32 // number of markroot jobs
350 // Number of roots of various root types. Set by gcMarkRootPrepare.
352 // nStackRoots == len(stackRoots), but we have nStackRoots for
354 nDataRoots, nBSSRoots, nSpanRoots, nStackRoots int
356 // Base indexes of each root type. Set by gcMarkRootPrepare.
357 baseData, baseBSS, baseSpans, baseStacks, baseEnd uint32
359 // stackRoots is a snapshot of all of the Gs that existed
360 // before the beginning of concurrent marking. The backing
361 // store of this must not be modified because it might be
362 // shared with allgs.
365 // Each type of GC state transition is protected by a lock.
366 // Since multiple threads can simultaneously detect the state
367 // transition condition, any thread that detects a transition
368 // condition must acquire the appropriate transition lock,
369 // re-check the transition condition and return if it no
370 // longer holds or perform the transition if it does.
371 // Likewise, any transition must invalidate the transition
372 // condition before releasing the lock. This ensures that each
373 // transition is performed by exactly one thread and threads
374 // that need the transition to happen block until it has
377 // startSema protects the transition from "off" to mark or
380 // markDoneSema protects transitions from mark to mark termination.
383 bgMarkReady note // signal background mark worker has started
384 bgMarkDone uint32 // cas to 1 when at a background mark completion point
385 // Background mark completion signaling
387 // mode is the concurrency mode of the current GC cycle.
390 // userForced indicates the current GC cycle was forced by an
391 // explicit user call.
394 // initialHeapLive is the value of gcController.heapLive at the
395 // beginning of this GC cycle.
396 initialHeapLive uint64
398 // assistQueue is a queue of assists that are blocked because
399 // there was neither enough credit to steal or enough work to
406 // sweepWaiters is a list of blocked goroutines to wake when
407 // we transition from mark termination to sweep.
408 sweepWaiters struct {
413 // cycles is the number of completed GC cycles, where a GC
414 // cycle is sweep termination, mark, mark termination, and
415 // sweep. This differs from memstats.numgc, which is
416 // incremented at mark termination.
419 // Timing/utilization stats for this cycle.
420 stwprocs, maxprocs int32
421 tSweepTerm, tMark, tMarkTerm, tEnd int64 // nanotime() of phase start
423 pauseNS int64 // total STW time this cycle
424 pauseStart int64 // nanotime() of last STW
426 // debug.gctrace heap sizes for this cycle.
427 heap0, heap1, heap2 uint64
429 // Cumulative estimated CPU usage.
433 // GC runs a garbage collection and blocks the caller until the
434 // garbage collection is complete. It may also block the entire
437 // We consider a cycle to be: sweep termination, mark, mark
438 // termination, and sweep. This function shouldn't return
439 // until a full cycle has been completed, from beginning to
440 // end. Hence, we always want to finish up the current cycle
441 // and start a new one. That means:
443 // 1. In sweep termination, mark, or mark termination of cycle
444 // N, wait until mark termination N completes and transitions
447 // 2. In sweep N, help with sweep N.
449 // At this point we can begin a full cycle N+1.
451 // 3. Trigger cycle N+1 by starting sweep termination N+1.
453 // 4. Wait for mark termination N+1 to complete.
455 // 5. Help with sweep N+1 until it's done.
457 // This all has to be written to deal with the fact that the
458 // GC may move ahead on its own. For example, when we block
459 // until mark termination N, we may wake up in cycle N+2.
461 // Wait until the current sweep termination, mark, and mark
462 // termination complete.
463 n := work.cycles.Load()
466 // We're now in sweep N or later. Trigger GC cycle N+1, which
467 // will first finish sweep N if necessary and then enter sweep
469 gcStart(gcTrigger{kind: gcTriggerCycle, n: n + 1})
471 // Wait for mark termination N+1 to complete.
474 // Finish sweep N+1 before returning. We do this both to
475 // complete the cycle and because runtime.GC() is often used
476 // as part of tests and benchmarks to get the system into a
477 // relatively stable and isolated state.
478 for work.cycles.Load() == n+1 && sweepone() != ^uintptr(0) {
482 // Callers may assume that the heap profile reflects the
483 // just-completed cycle when this returns (historically this
484 // happened because this was a STW GC), but right now the
485 // profile still reflects mark termination N, not N+1.
487 // As soon as all of the sweep frees from cycle N+1 are done,
488 // we can go ahead and publish the heap profile.
490 // First, wait for sweeping to finish. (We know there are no
491 // more spans on the sweep queue, but we may be concurrently
492 // sweeping spans, so we have to wait.)
493 for work.cycles.Load() == n+1 && !isSweepDone() {
497 // Now we're really done with sweeping, so we can publish the
498 // stable heap profile. Only do this if we haven't already hit
499 // another mark termination.
501 cycle := work.cycles.Load()
502 if cycle == n+1 || (gcphase == _GCmark && cycle == n+2) {
508 // gcWaitOnMark blocks until GC finishes the Nth mark phase. If GC has
509 // already completed this mark phase, it returns immediately.
510 func gcWaitOnMark(n uint32) {
512 // Disable phase transitions.
513 lock(&work.sweepWaiters.lock)
514 nMarks := work.cycles.Load()
515 if gcphase != _GCmark {
516 // We've already completed this cycle's mark.
521 unlock(&work.sweepWaiters.lock)
525 // Wait until sweep termination, mark, and mark
526 // termination of cycle N complete.
527 work.sweepWaiters.list.push(getg())
528 goparkunlock(&work.sweepWaiters.lock, waitReasonWaitForGCCycle, traceBlockUntilGCEnds, 1)
532 // gcMode indicates how concurrent a GC cycle should be.
536 gcBackgroundMode gcMode = iota // concurrent GC and sweep
537 gcForceMode // stop-the-world GC now, concurrent sweep
538 gcForceBlockMode // stop-the-world GC now and STW sweep (forced by user)
541 // A gcTrigger is a predicate for starting a GC cycle. Specifically,
542 // it is an exit condition for the _GCoff phase.
543 type gcTrigger struct {
545 now int64 // gcTriggerTime: current time
546 n uint32 // gcTriggerCycle: cycle number to start
549 type gcTriggerKind int
552 // gcTriggerHeap indicates that a cycle should be started when
553 // the heap size reaches the trigger heap size computed by the
555 gcTriggerHeap gcTriggerKind = iota
557 // gcTriggerTime indicates that a cycle should be started when
558 // it's been more than forcegcperiod nanoseconds since the
559 // previous GC cycle.
562 // gcTriggerCycle indicates that a cycle should be started if
563 // we have not yet started cycle number gcTrigger.n (relative
568 // test reports whether the trigger condition is satisfied, meaning
569 // that the exit condition for the _GCoff phase has been met. The exit
570 // condition should be tested when allocating.
571 func (t gcTrigger) test() bool {
572 if !memstats.enablegc || panicking.Load() != 0 || gcphase != _GCoff {
577 trigger, _ := gcController.trigger()
578 return gcController.heapLive.Load() >= trigger
580 if gcController.gcPercent.Load() < 0 {
583 lastgc := int64(atomic.Load64(&memstats.last_gc_nanotime))
584 return lastgc != 0 && t.now-lastgc > forcegcperiod
586 // t.n > work.cycles, but accounting for wraparound.
587 return int32(t.n-work.cycles.Load()) > 0
592 // gcStart starts the GC. It transitions from _GCoff to _GCmark (if
593 // debug.gcstoptheworld == 0) or performs all of GC (if
594 // debug.gcstoptheworld != 0).
596 // This may return without performing this transition in some cases,
597 // such as when called on a system stack or with locks held.
598 func gcStart(trigger gcTrigger) {
599 // Since this is called from malloc and malloc is called in
600 // the guts of a number of libraries that might be holding
601 // locks, don't attempt to start GC in non-preemptible or
602 // potentially unstable situations.
604 if gp := getg(); gp == mp.g0 || mp.locks > 1 || mp.preemptoff != "" {
611 // Pick up the remaining unswept/not being swept spans concurrently
613 // This shouldn't happen if we're being invoked in background
614 // mode since proportional sweep should have just finished
615 // sweeping everything, but rounding errors, etc, may leave a
616 // few spans unswept. In forced mode, this is necessary since
617 // GC can be forced at any point in the sweeping cycle.
619 // We check the transition condition continuously here in case
620 // this G gets delayed in to the next GC cycle.
621 for trigger.test() && sweepone() != ^uintptr(0) {
624 // Perform GC initialization and the sweep termination
626 semacquire(&work.startSema)
627 // Re-check transition condition under transition lock.
629 semrelease(&work.startSema)
633 // In gcstoptheworld debug mode, upgrade the mode accordingly.
634 // We do this after re-checking the transition condition so
635 // that multiple goroutines that detect the heap trigger don't
636 // start multiple STW GCs.
637 mode := gcBackgroundMode
638 if debug.gcstoptheworld == 1 {
640 } else if debug.gcstoptheworld == 2 {
641 mode = gcForceBlockMode
644 // Ok, we're doing it! Stop everybody else
646 semacquire(&worldsema)
648 // For stats, check if this GC was forced by the user.
649 // Update it under gcsema to avoid gctrace getting wrong values.
650 work.userForced = trigger.kind == gcTriggerCycle
656 // Check that all Ps have finished deferred mcache flushes.
657 for _, p := range allp {
658 if fg := p.mcache.flushGen.Load(); fg != mheap_.sweepgen {
659 println("runtime: p", p.id, "flushGen", fg, "!= sweepgen", mheap_.sweepgen)
660 throw("p mcache not flushed")
664 gcBgMarkStartWorkers()
666 systemstack(gcResetMarkState)
668 work.stwprocs, work.maxprocs = gomaxprocs, gomaxprocs
669 if work.stwprocs > ncpu {
670 // This is used to compute CPU time of the STW phases,
671 // so it can't be more than ncpu, even if GOMAXPROCS is.
674 work.heap0 = gcController.heapLive.Load()
679 work.tSweepTerm = now
680 work.pauseStart = now
681 systemstack(func() { stopTheWorldWithSema(stwGCSweepTerm) })
682 // Finish sweep before we start concurrent scan.
687 // clearpools before we start the GC. If we wait the memory will not be
688 // reclaimed until the next GC cycle.
693 // Assists and workers can start the moment we start
695 gcController.startCycle(now, int(gomaxprocs), trigger)
697 // Notify the CPU limiter that assists may begin.
698 gcCPULimiter.startGCTransition(true, now)
700 // In STW mode, disable scheduling of user Gs. This may also
701 // disable scheduling of this goroutine, so it may block as
702 // soon as we start the world again.
703 if mode != gcBackgroundMode {
704 schedEnableUser(false)
707 // Enter concurrent mark phase and enable
710 // Because the world is stopped, all Ps will
711 // observe that write barriers are enabled by
712 // the time we start the world and begin
715 // Write barriers must be enabled before assists are
716 // enabled because they must be enabled before
717 // any non-leaf heap objects are marked. Since
718 // allocations are blocked until assists can
719 // happen, we want to enable assists as early as
723 gcBgMarkPrepare() // Must happen before assists are enabled.
726 // Mark all active tinyalloc blocks. Since we're
727 // allocating from these, they need to be black like
728 // other allocations. The alternative is to blacken
729 // the tiny block on every allocation from it, which
730 // would slow down the tiny allocator.
733 // At this point all Ps have enabled the write
734 // barrier, thus maintaining the no white to
735 // black invariant. Enable mutator assists to
736 // put back-pressure on fast allocating
738 atomic.Store(&gcBlackenEnabled, 1)
740 // In STW mode, we could block the instant systemstack
741 // returns, so make sure we're not preemptible.
746 now = startTheWorldWithSema()
747 work.pauseNS += now - work.pauseStart
749 memstats.gcPauseDist.record(now - work.pauseStart)
751 sweepTermCpu := int64(work.stwprocs) * (work.tMark - work.tSweepTerm)
752 work.cpuStats.gcPauseTime += sweepTermCpu
753 work.cpuStats.gcTotalTime += sweepTermCpu
755 // Release the CPU limiter.
756 gcCPULimiter.finishGCTransition(now)
759 // Release the world sema before Gosched() in STW mode
760 // because we will need to reacquire it later but before
761 // this goroutine becomes runnable again, and we could
762 // self-deadlock otherwise.
763 semrelease(&worldsema)
766 // Make sure we block instead of returning to user code
768 if mode != gcBackgroundMode {
772 semrelease(&work.startSema)
775 // gcMarkDoneFlushed counts the number of P's with flushed work.
777 // Ideally this would be a captured local in gcMarkDone, but forEachP
778 // escapes its callback closure, so it can't capture anything.
780 // This is protected by markDoneSema.
781 var gcMarkDoneFlushed uint32
783 // gcMarkDone transitions the GC from mark to mark termination if all
784 // reachable objects have been marked (that is, there are no grey
785 // objects and can be no more in the future). Otherwise, it flushes
786 // all local work to the global queues where it can be discovered by
789 // This should be called when all local mark work has been drained and
790 // there are no remaining workers. Specifically, when
792 // work.nwait == work.nproc && !gcMarkWorkAvailable(p)
794 // The calling context must be preemptible.
796 // Flushing local work is important because idle Ps may have local
797 // work queued. This is the only way to make that work visible and
798 // drive GC to completion.
800 // It is explicitly okay to have write barriers in this function. If
801 // it does transition to mark termination, then all reachable objects
802 // have been marked, so the write barrier cannot shade any more
805 // Ensure only one thread is running the ragged barrier at a
807 semacquire(&work.markDoneSema)
810 // Re-check transition condition under transition lock.
812 // It's critical that this checks the global work queues are
813 // empty before performing the ragged barrier. Otherwise,
814 // there could be global work that a P could take after the P
815 // has passed the ragged barrier.
816 if !(gcphase == _GCmark && work.nwait == work.nproc && !gcMarkWorkAvailable(nil)) {
817 semrelease(&work.markDoneSema)
821 // forEachP needs worldsema to execute, and we'll need it to
822 // stop the world later, so acquire worldsema now.
823 semacquire(&worldsema)
825 // Flush all local buffers and collect flushedWork flags.
826 gcMarkDoneFlushed = 0
829 // Mark the user stack as preemptible so that it may be scanned.
830 // Otherwise, our attempt to force all P's to a safepoint could
831 // result in a deadlock as we attempt to preempt a worker that's
832 // trying to preempt us (e.g. for a stack scan).
833 casGToWaiting(gp, _Grunning, waitReasonGCMarkTermination)
834 forEachP(func(pp *p) {
835 // Flush the write barrier buffer, since this may add
836 // work to the gcWork.
839 // Flush the gcWork, since this may create global work
840 // and set the flushedWork flag.
842 // TODO(austin): Break up these workbufs to
843 // better distribute work.
845 // Collect the flushedWork flag.
846 if pp.gcw.flushedWork {
847 atomic.Xadd(&gcMarkDoneFlushed, 1)
848 pp.gcw.flushedWork = false
851 casgstatus(gp, _Gwaiting, _Grunning)
854 if gcMarkDoneFlushed != 0 {
855 // More grey objects were discovered since the
856 // previous termination check, so there may be more
857 // work to do. Keep going. It's possible the
858 // transition condition became true again during the
859 // ragged barrier, so re-check it.
860 semrelease(&worldsema)
864 // There was no global work, no local work, and no Ps
865 // communicated work since we took markDoneSema. Therefore
866 // there are no grey objects and no more objects can be
867 // shaded. Transition to mark termination.
870 work.pauseStart = now
871 getg().m.preemptoff = "gcing"
872 systemstack(func() { stopTheWorldWithSema(stwGCMarkTerm) })
873 // The gcphase is _GCmark, it will transition to _GCmarktermination
874 // below. The important thing is that the wb remains active until
875 // all marking is complete. This includes writes made by the GC.
877 // There is sometimes work left over when we enter mark termination due
878 // to write barriers performed after the completion barrier above.
879 // Detect this and resume concurrent mark. This is obviously
882 // See issue #27993 for details.
884 // Switch to the system stack to call wbBufFlush1, though in this case
885 // it doesn't matter because we're non-preemptible anyway.
888 for _, p := range allp {
897 getg().m.preemptoff = ""
899 now := startTheWorldWithSema()
900 work.pauseNS += now - work.pauseStart
901 memstats.gcPauseDist.record(now - work.pauseStart)
903 semrelease(&worldsema)
907 gcComputeStartingStackSize()
909 // Disable assists and background workers. We must do
910 // this before waking blocked assists.
911 atomic.Store(&gcBlackenEnabled, 0)
913 // Notify the CPU limiter that GC assists will now cease.
914 gcCPULimiter.startGCTransition(false, now)
916 // Wake all blocked assists. These will run when we
917 // start the world again.
920 // Likewise, release the transition lock. Blocked
921 // workers and assists will run when we start the
923 semrelease(&work.markDoneSema)
925 // In STW mode, re-enable user goroutines. These will be
926 // queued to run after we start the world.
927 schedEnableUser(true)
929 // endCycle depends on all gcWork cache stats being flushed.
930 // The termination algorithm above ensured that up to
931 // allocations since the ragged barrier.
932 gcController.endCycle(now, int(gomaxprocs), work.userForced)
934 // Perform mark termination. This will restart the world.
938 // World must be stopped and mark assists and background workers must be
940 func gcMarkTermination() {
941 // Start marktermination (write barrier remains enabled for now).
942 setGCPhase(_GCmarktermination)
944 work.heap1 = gcController.heapLive.Load()
945 startTime := nanotime()
948 mp.preemptoff = "gcing"
951 casGToWaiting(curgp, _Grunning, waitReasonGarbageCollection)
953 // Run gc on the g0 stack. We do this so that the g stack
954 // we're currently running on will no longer change. Cuts
955 // the root set down a bit (g0 stacks are not scanned, and
956 // we don't need to scan gc's internal state). We also
957 // need to switch to g0 so we can shrink the stack.
960 // Must return immediately.
961 // The outer function's stack may have moved
962 // during gcMark (it shrinks stacks, including the
963 // outer function's stack), so we must not refer
964 // to any of its variables. Return back to the
965 // non-system stack to pick up the new addresses
966 // before continuing.
971 work.heap2 = work.bytesMarked
972 if debug.gccheckmark > 0 {
973 // Run a full non-parallel, stop-the-world
974 // mark using checkmark bits, to check that we
975 // didn't forget to mark anything during the
976 // concurrent mark process.
979 gcw := &getg().m.p.ptr().gcw
981 wbBufFlush1(getg().m.p.ptr())
986 // marking is complete so we can turn the write barrier off
988 stwSwept = gcSweep(work.mode)
992 casgstatus(curgp, _Gwaiting, _Grunning)
1001 if gcphase != _GCoff {
1002 throw("gc done but gcphase != _GCoff")
1005 // Record heapInUse for scavenger.
1006 memstats.lastHeapInUse = gcController.heapInUse.load()
1008 // Update GC trigger and pacing, as well as downstream consumers
1009 // of this pacing information, for the next cycle.
1010 systemstack(gcControllerCommit)
1012 // Update timing memstats
1014 sec, nsec, _ := time_now()
1015 unixNow := sec*1e9 + int64(nsec)
1016 work.pauseNS += now - work.pauseStart
1018 memstats.gcPauseDist.record(now - work.pauseStart)
1019 atomic.Store64(&memstats.last_gc_unix, uint64(unixNow)) // must be Unix time to make sense to user
1020 atomic.Store64(&memstats.last_gc_nanotime, uint64(now)) // monotonic time for us
1021 memstats.pause_ns[memstats.numgc%uint32(len(memstats.pause_ns))] = uint64(work.pauseNS)
1022 memstats.pause_end[memstats.numgc%uint32(len(memstats.pause_end))] = uint64(unixNow)
1023 memstats.pause_total_ns += uint64(work.pauseNS)
1025 markTermCpu := int64(work.stwprocs) * (work.tEnd - work.tMarkTerm)
1026 work.cpuStats.gcPauseTime += markTermCpu
1027 work.cpuStats.gcTotalTime += markTermCpu
1029 // Accumulate CPU stats.
1031 // Pass gcMarkPhase=true so we can get all the latest GC CPU stats in there too.
1032 work.cpuStats.accumulate(now, true)
1034 // Compute overall GC CPU utilization.
1035 // Omit idle marking time from the overall utilization here since it's "free".
1036 memstats.gc_cpu_fraction = float64(work.cpuStats.gcTotalTime-work.cpuStats.gcIdleTime) / float64(work.cpuStats.totalTime)
1038 // Reset assist time and background time stats.
1040 // Do this now, instead of at the start of the next GC cycle, because
1041 // these two may keep accumulating even if the GC is not active.
1042 scavenge.assistTime.Store(0)
1043 scavenge.backgroundTime.Store(0)
1045 // Reset idle time stat.
1046 sched.idleTime.Store(0)
1048 if work.userForced {
1049 memstats.numforcedgc++
1052 // Bump GC cycle count and wake goroutines waiting on sweep.
1053 lock(&work.sweepWaiters.lock)
1055 injectglist(&work.sweepWaiters.list)
1056 unlock(&work.sweepWaiters.lock)
1058 // Increment the scavenge generation now.
1060 // This moment represents peak heap in use because we're
1061 // about to start sweeping.
1062 mheap_.pages.scav.index.nextGen()
1064 // Release the CPU limiter.
1065 gcCPULimiter.finishGCTransition(now)
1067 // Finish the current heap profiling cycle and start a new
1068 // heap profiling cycle. We do this before starting the world
1069 // so events don't leak into the wrong cycle.
1072 // There may be stale spans in mcaches that need to be swept.
1073 // Those aren't tracked in any sweep lists, so we need to
1074 // count them against sweep completion until we ensure all
1075 // those spans have been forced out.
1077 // If gcSweep fully swept the heap (for example if the sweep
1078 // is not concurrent due to a GODEBUG setting), then we expect
1079 // the sweepLocker to be invalid, since sweeping is done.
1081 // N.B. Below we might duplicate some work from gcSweep; this is
1082 // fine as all that work is idempotent within a GC cycle, and
1083 // we're still holding worldsema so a new cycle can't start.
1084 sl := sweep.active.begin()
1085 if !stwSwept && !sl.valid {
1086 throw("failed to set sweep barrier")
1087 } else if stwSwept && sl.valid {
1088 throw("non-concurrent sweep failed to drain all sweep queues")
1091 systemstack(func() { startTheWorldWithSema() })
1093 // Flush the heap profile so we can start a new cycle next GC.
1094 // This is relatively expensive, so we don't do it with the
1098 // Prepare workbufs for freeing by the sweeper. We do this
1099 // asynchronously because it can take non-trivial time.
1100 prepareFreeWorkbufs()
1102 // Free stack spans. This must be done between GC cycles.
1103 systemstack(freeStackSpans)
1105 // Ensure all mcaches are flushed. Each P will flush its own
1106 // mcache before allocating, but idle Ps may not. Since this
1107 // is necessary to sweep all spans, we need to ensure all
1108 // mcaches are flushed before we start the next GC cycle.
1110 // While we're here, flush the page cache for idle Ps to avoid
1111 // having pages get stuck on them. These pages are hidden from
1112 // the scavenger, so in small idle heaps a significant amount
1113 // of additional memory might be held onto.
1115 // Also, flush the pinner cache, to avoid leaking that memory
1117 systemstack(func() {
1118 forEachP(func(pp *p) {
1119 pp.mcache.prepareForSweep()
1120 if pp.status == _Pidle {
1121 systemstack(func() {
1123 pp.pcache.flush(&mheap_.pages)
1124 unlock(&mheap_.lock)
1127 pp.pinnerCache = nil
1131 // Now that we've swept stale spans in mcaches, they don't
1132 // count against unswept spans.
1134 // Note: this sweepLocker may not be valid if sweeping had
1135 // already completed during the STW. See the corresponding
1136 // begin() call that produced sl.
1137 sweep.active.end(sl)
1140 // Print gctrace before dropping worldsema. As soon as we drop
1141 // worldsema another cycle could start and smash the stats
1142 // we're trying to print.
1143 if debug.gctrace > 0 {
1144 util := int(memstats.gc_cpu_fraction * 100)
1148 print("gc ", memstats.numgc,
1149 " @", string(itoaDiv(sbuf[:], uint64(work.tSweepTerm-runtimeInitTime)/1e6, 3)), "s ",
1151 prev := work.tSweepTerm
1152 for i, ns := range []int64{work.tMark, work.tMarkTerm, work.tEnd} {
1156 print(string(fmtNSAsMS(sbuf[:], uint64(ns-prev))))
1159 print(" ms clock, ")
1160 for i, ns := range []int64{
1161 int64(work.stwprocs) * (work.tMark - work.tSweepTerm),
1162 gcController.assistTime.Load(),
1163 gcController.dedicatedMarkTime.Load() + gcController.fractionalMarkTime.Load(),
1164 gcController.idleMarkTime.Load(),
1167 if i == 2 || i == 3 {
1168 // Separate mark time components with /.
1173 print(string(fmtNSAsMS(sbuf[:], uint64(ns))))
1176 work.heap0>>20, "->", work.heap1>>20, "->", work.heap2>>20, " MB, ",
1177 gcController.lastHeapGoal>>20, " MB goal, ",
1178 gcController.lastStackScan.Load()>>20, " MB stacks, ",
1179 gcController.globalsScan.Load()>>20, " MB globals, ",
1180 work.maxprocs, " P")
1181 if work.userForced {
1188 // Set any arena chunks that were deferred to fault.
1189 lock(&userArenaState.lock)
1190 faultList := userArenaState.fault
1191 userArenaState.fault = nil
1192 unlock(&userArenaState.lock)
1193 for _, lc := range faultList {
1194 lc.mspan.setUserArenaChunkToFault()
1197 // Enable huge pages on some metadata if we cross a heap threshold.
1198 if gcController.heapGoal() > minHeapForMetadataHugePages {
1199 mheap_.enableMetadataHugePages()
1202 semrelease(&worldsema)
1204 // Careful: another GC cycle may start now.
1209 // now that gc is done, kick off finalizer thread if needed
1210 if !concurrentSweep {
1211 // give the queued finalizers, if any, a chance to run
1216 // gcBgMarkStartWorkers prepares background mark worker goroutines. These
1217 // goroutines will not run until the mark phase, but they must be started while
1218 // the work is not stopped and from a regular G stack. The caller must hold
1220 func gcBgMarkStartWorkers() {
1221 // Background marking is performed by per-P G's. Ensure that each P has
1222 // a background GC G.
1224 // Worker Gs don't exit if gomaxprocs is reduced. If it is raised
1225 // again, we can reuse the old workers; no need to create new workers.
1226 for gcBgMarkWorkerCount < gomaxprocs {
1229 notetsleepg(&work.bgMarkReady, -1)
1230 noteclear(&work.bgMarkReady)
1231 // The worker is now guaranteed to be added to the pool before
1232 // its P's next findRunnableGCWorker.
1234 gcBgMarkWorkerCount++
1238 // gcBgMarkPrepare sets up state for background marking.
1239 // Mutator assists must not yet be enabled.
1240 func gcBgMarkPrepare() {
1241 // Background marking will stop when the work queues are empty
1242 // and there are no more workers (note that, since this is
1243 // concurrent, this may be a transient state, but mark
1244 // termination will clean it up). Between background workers
1245 // and assists, we don't really know how many workers there
1246 // will be, so we pretend to have an arbitrarily large number
1247 // of workers, almost all of which are "waiting". While a
1248 // worker is working it decrements nwait. If nproc == nwait,
1249 // there are no workers.
1250 work.nproc = ^uint32(0)
1251 work.nwait = ^uint32(0)
1254 // gcBgMarkWorkerNode is an entry in the gcBgMarkWorkerPool. It points to a single
1255 // gcBgMarkWorker goroutine.
1256 type gcBgMarkWorkerNode struct {
1257 // Unused workers are managed in a lock-free stack. This field must be first.
1260 // The g of this worker.
1263 // Release this m on park. This is used to communicate with the unlock
1264 // function, which cannot access the G's stack. It is unused outside of
1265 // gcBgMarkWorker().
1269 func gcBgMarkWorker() {
1272 // We pass node to a gopark unlock function, so it can't be on
1273 // the stack (see gopark). Prevent deadlock from recursively
1274 // starting GC by disabling preemption.
1275 gp.m.preemptoff = "GC worker init"
1276 node := new(gcBgMarkWorkerNode)
1277 gp.m.preemptoff = ""
1281 node.m.set(acquirem())
1282 notewakeup(&work.bgMarkReady)
1283 // After this point, the background mark worker is generally scheduled
1284 // cooperatively by gcController.findRunnableGCWorker. While performing
1285 // work on the P, preemption is disabled because we are working on
1286 // P-local work buffers. When the preempt flag is set, this puts itself
1287 // into _Gwaiting to be woken up by gcController.findRunnableGCWorker
1288 // at the appropriate time.
1290 // When preemption is enabled (e.g., while in gcMarkDone), this worker
1291 // may be preempted and schedule as a _Grunnable G from a runq. That is
1292 // fine; it will eventually gopark again for further scheduling via
1293 // findRunnableGCWorker.
1295 // Since we disable preemption before notifying bgMarkReady, we
1296 // guarantee that this G will be in the worker pool for the next
1297 // findRunnableGCWorker. This isn't strictly necessary, but it reduces
1298 // latency between _GCmark starting and the workers starting.
1301 // Go to sleep until woken by
1302 // gcController.findRunnableGCWorker.
1303 gopark(func(g *g, nodep unsafe.Pointer) bool {
1304 node := (*gcBgMarkWorkerNode)(nodep)
1306 if mp := node.m.ptr(); mp != nil {
1307 // The worker G is no longer running; release
1310 // N.B. it is _safe_ to release the M as soon
1311 // as we are no longer performing P-local mark
1314 // However, since we cooperatively stop work
1315 // when gp.preempt is set, if we releasem in
1316 // the loop then the following call to gopark
1317 // would immediately preempt the G. This is
1318 // also safe, but inefficient: the G must
1319 // schedule again only to enter gopark and park
1320 // again. Thus, we defer the release until
1321 // after parking the G.
1325 // Release this G to the pool.
1326 gcBgMarkWorkerPool.push(&node.node)
1327 // Note that at this point, the G may immediately be
1328 // rescheduled and may be running.
1330 }, unsafe.Pointer(node), waitReasonGCWorkerIdle, traceBlockSystemGoroutine, 0)
1332 // Preemption must not occur here, or another G might see
1333 // p.gcMarkWorkerMode.
1335 // Disable preemption so we can use the gcw. If the
1336 // scheduler wants to preempt us, we'll stop draining,
1337 // dispose the gcw, and then preempt.
1338 node.m.set(acquirem())
1339 pp := gp.m.p.ptr() // P can't change with preemption disabled.
1341 if gcBlackenEnabled == 0 {
1342 println("worker mode", pp.gcMarkWorkerMode)
1343 throw("gcBgMarkWorker: blackening not enabled")
1346 if pp.gcMarkWorkerMode == gcMarkWorkerNotWorker {
1347 throw("gcBgMarkWorker: mode not set")
1350 startTime := nanotime()
1351 pp.gcMarkWorkerStartTime = startTime
1352 var trackLimiterEvent bool
1353 if pp.gcMarkWorkerMode == gcMarkWorkerIdleMode {
1354 trackLimiterEvent = pp.limiterEvent.start(limiterEventIdleMarkWork, startTime)
1357 decnwait := atomic.Xadd(&work.nwait, -1)
1358 if decnwait == work.nproc {
1359 println("runtime: work.nwait=", decnwait, "work.nproc=", work.nproc)
1360 throw("work.nwait was > work.nproc")
1363 systemstack(func() {
1364 // Mark our goroutine preemptible so its stack
1365 // can be scanned. This lets two mark workers
1366 // scan each other (otherwise, they would
1367 // deadlock). We must not modify anything on
1368 // the G stack. However, stack shrinking is
1369 // disabled for mark workers, so it is safe to
1370 // read from the G stack.
1371 casGToWaiting(gp, _Grunning, waitReasonGCWorkerActive)
1372 switch pp.gcMarkWorkerMode {
1374 throw("gcBgMarkWorker: unexpected gcMarkWorkerMode")
1375 case gcMarkWorkerDedicatedMode:
1376 gcDrainMarkWorkerDedicated(&pp.gcw, true)
1378 // We were preempted. This is
1379 // a useful signal to kick
1380 // everything out of the run
1381 // queue so it can run
1383 if drainQ, n := runqdrain(pp); n > 0 {
1385 globrunqputbatch(&drainQ, int32(n))
1389 // Go back to draining, this time
1390 // without preemption.
1391 gcDrainMarkWorkerDedicated(&pp.gcw, false)
1392 case gcMarkWorkerFractionalMode:
1393 gcDrainMarkWorkerFractional(&pp.gcw)
1394 case gcMarkWorkerIdleMode:
1395 gcDrainMarkWorkerIdle(&pp.gcw)
1397 casgstatus(gp, _Gwaiting, _Grunning)
1400 // Account for time and mark us as stopped.
1402 duration := now - startTime
1403 gcController.markWorkerStop(pp.gcMarkWorkerMode, duration)
1404 if trackLimiterEvent {
1405 pp.limiterEvent.stop(limiterEventIdleMarkWork, now)
1407 if pp.gcMarkWorkerMode == gcMarkWorkerFractionalMode {
1408 atomic.Xaddint64(&pp.gcFractionalMarkTime, duration)
1411 // Was this the last worker and did we run out
1413 incnwait := atomic.Xadd(&work.nwait, +1)
1414 if incnwait > work.nproc {
1415 println("runtime: p.gcMarkWorkerMode=", pp.gcMarkWorkerMode,
1416 "work.nwait=", incnwait, "work.nproc=", work.nproc)
1417 throw("work.nwait > work.nproc")
1420 // We'll releasem after this point and thus this P may run
1421 // something else. We must clear the worker mode to avoid
1422 // attributing the mode to a different (non-worker) G in
1424 pp.gcMarkWorkerMode = gcMarkWorkerNotWorker
1426 // If this worker reached a background mark completion
1427 // point, signal the main GC goroutine.
1428 if incnwait == work.nproc && !gcMarkWorkAvailable(nil) {
1429 // We don't need the P-local buffers here, allow
1430 // preemption because we may schedule like a regular
1431 // goroutine in gcMarkDone (block on locks, etc).
1432 releasem(node.m.ptr())
1440 // gcMarkWorkAvailable reports whether executing a mark worker
1441 // on p is potentially useful. p may be nil, in which case it only
1442 // checks the global sources of work.
1443 func gcMarkWorkAvailable(p *p) bool {
1444 if p != nil && !p.gcw.empty() {
1447 if !work.full.empty() {
1448 return true // global work available
1450 if work.markrootNext < work.markrootJobs {
1451 return true // root scan work available
1456 // gcMark runs the mark (or, for concurrent GC, mark termination)
1457 // All gcWork caches must be empty.
1458 // STW is in effect at this point.
1459 func gcMark(startTime int64) {
1460 if debug.allocfreetrace > 0 {
1464 if gcphase != _GCmarktermination {
1465 throw("in gcMark expecting to see gcphase as _GCmarktermination")
1467 work.tstart = startTime
1469 // Check that there's no marking work remaining.
1470 if work.full != 0 || work.markrootNext < work.markrootJobs {
1471 print("runtime: full=", hex(work.full), " next=", work.markrootNext, " jobs=", work.markrootJobs, " nDataRoots=", work.nDataRoots, " nBSSRoots=", work.nBSSRoots, " nSpanRoots=", work.nSpanRoots, " nStackRoots=", work.nStackRoots, "\n")
1472 panic("non-empty mark queue after concurrent mark")
1475 if debug.gccheckmark > 0 {
1476 // This is expensive when there's a large number of
1477 // Gs, so only do it if checkmark is also enabled.
1481 // Drop allg snapshot. allgs may have grown, in which case
1482 // this is the only reference to the old backing store and
1483 // there's no need to keep it around.
1484 work.stackRoots = nil
1486 // Clear out buffers and double-check that all gcWork caches
1487 // are empty. This should be ensured by gcMarkDone before we
1488 // enter mark termination.
1490 // TODO: We could clear out buffers just before mark if this
1491 // has a non-negligible impact on STW time.
1492 for _, p := range allp {
1493 // The write barrier may have buffered pointers since
1494 // the gcMarkDone barrier. However, since the barrier
1495 // ensured all reachable objects were marked, all of
1496 // these must be pointers to black objects. Hence we
1497 // can just discard the write barrier buffer.
1498 if debug.gccheckmark > 0 {
1499 // For debugging, flush the buffer and make
1500 // sure it really was all marked.
1509 print("runtime: P ", p.id, " flushedWork ", gcw.flushedWork)
1510 if gcw.wbuf1 == nil {
1511 print(" wbuf1=<nil>")
1513 print(" wbuf1.n=", gcw.wbuf1.nobj)
1515 if gcw.wbuf2 == nil {
1516 print(" wbuf2=<nil>")
1518 print(" wbuf2.n=", gcw.wbuf2.nobj)
1521 throw("P has cached GC work at end of mark termination")
1523 // There may still be cached empty buffers, which we
1524 // need to flush since we're going to free them. Also,
1525 // there may be non-zero stats because we allocated
1526 // black after the gcMarkDone barrier.
1530 // Flush scanAlloc from each mcache since we're about to modify
1531 // heapScan directly. If we were to flush this later, then scanAlloc
1532 // might have incorrect information.
1534 // Note that it's not important to retain this information; we know
1535 // exactly what heapScan is at this point via scanWork.
1536 for _, p := range allp {
1544 // Reset controller state.
1545 gcController.resetLive(work.bytesMarked)
1548 // gcSweep must be called on the system stack because it acquires the heap
1549 // lock. See mheap for details.
1551 // Returns true if the heap was fully swept by this function.
1553 // The world must be stopped.
1556 func gcSweep(mode gcMode) bool {
1557 assertWorldStopped()
1559 if gcphase != _GCoff {
1560 throw("gcSweep being done but phase is not GCoff")
1564 mheap_.sweepgen += 2
1565 sweep.active.reset()
1566 mheap_.pagesSwept.Store(0)
1567 mheap_.sweepArenas = mheap_.allArenas
1568 mheap_.reclaimIndex.Store(0)
1569 mheap_.reclaimCredit.Store(0)
1570 unlock(&mheap_.lock)
1572 sweep.centralIndex.clear()
1574 if !concurrentSweep || mode == gcForceBlockMode {
1575 // Special case synchronous sweep.
1576 // Record that no proportional sweeping has to happen.
1578 mheap_.sweepPagesPerByte = 0
1579 unlock(&mheap_.lock)
1580 // Flush all mcaches.
1581 for _, pp := range allp {
1582 pp.mcache.prepareForSweep()
1584 // Sweep all spans eagerly.
1585 for sweepone() != ^uintptr(0) {
1587 // Free workbufs eagerly.
1588 prepareFreeWorkbufs()
1589 for freeSomeWbufs(false) {
1591 // All "free" events for this mark/sweep cycle have
1592 // now happened, so we can make this profile cycle
1593 // available immediately.
1599 // Background sweep.
1602 sweep.parked = false
1603 ready(sweep.g, 0, true)
1609 // gcResetMarkState resets global state prior to marking (concurrent
1610 // or STW) and resets the stack scan state of all Gs.
1612 // This is safe to do without the world stopped because any Gs created
1613 // during or after this will start out in the reset state.
1615 // gcResetMarkState must be called on the system stack because it acquires
1616 // the heap lock. See mheap for details.
1619 func gcResetMarkState() {
1620 // This may be called during a concurrent phase, so lock to make sure
1621 // allgs doesn't change.
1622 forEachG(func(gp *g) {
1623 gp.gcscandone = false // set to true in gcphasework
1624 gp.gcAssistBytes = 0
1627 // Clear page marks. This is just 1MB per 64GB of heap, so the
1628 // time here is pretty trivial.
1630 arenas := mheap_.allArenas
1631 unlock(&mheap_.lock)
1632 for _, ai := range arenas {
1633 ha := mheap_.arenas[ai.l1()][ai.l2()]
1634 for i := range ha.pageMarks {
1639 work.bytesMarked = 0
1640 work.initialHeapLive = gcController.heapLive.Load()
1643 // Hooks for other packages
1645 var poolcleanup func()
1646 var boringCaches []unsafe.Pointer // for crypto/internal/boring
1648 //go:linkname sync_runtime_registerPoolCleanup sync.runtime_registerPoolCleanup
1649 func sync_runtime_registerPoolCleanup(f func()) {
1653 //go:linkname boring_registerCache crypto/internal/boring/bcache.registerCache
1654 func boring_registerCache(p unsafe.Pointer) {
1655 boringCaches = append(boringCaches, p)
1660 if poolcleanup != nil {
1664 // clear boringcrypto caches
1665 for _, p := range boringCaches {
1666 atomicstorep(p, nil)
1669 // Clear central sudog cache.
1670 // Leave per-P caches alone, they have strictly bounded size.
1671 // Disconnect cached list before dropping it on the floor,
1672 // so that a dangling ref to one entry does not pin all of them.
1673 lock(&sched.sudoglock)
1674 var sg, sgnext *sudog
1675 for sg = sched.sudogcache; sg != nil; sg = sgnext {
1679 sched.sudogcache = nil
1680 unlock(&sched.sudoglock)
1682 // Clear central defer pool.
1683 // Leave per-P pools alone, they have strictly bounded size.
1684 lock(&sched.deferlock)
1685 // disconnect cached list before dropping it on the floor,
1686 // so that a dangling ref to one entry does not pin all of them.
1687 var d, dlink *_defer
1688 for d = sched.deferpool; d != nil; d = dlink {
1692 sched.deferpool = nil
1693 unlock(&sched.deferlock)
1698 // itoaDiv formats val/(10**dec) into buf.
1699 func itoaDiv(buf []byte, val uint64, dec int) []byte {
1702 for val >= 10 || i >= idec {
1703 buf[i] = byte(val%10 + '0')
1711 buf[i] = byte(val + '0')
1715 // fmtNSAsMS nicely formats ns nanoseconds as milliseconds.
1716 func fmtNSAsMS(buf []byte, ns uint64) []byte {
1718 // Format as whole milliseconds.
1719 return itoaDiv(buf, ns/1e6, 0)
1721 // Format two digits of precision, with at most three decimal places.
1732 return itoaDiv(buf, x, dec)
1735 // Helpers for testing GC.
1737 // gcTestMoveStackOnNextCall causes the stack to be moved on a call
1738 // immediately following the call to this. It may not work correctly
1739 // if any other work appears after this call (such as returning).
1740 // Typically the following call should be marked go:noinline so it
1741 // performs a stack check.
1743 // In rare cases this may not cause the stack to move, specifically if
1744 // there's a preemption between this call and the next.
1745 func gcTestMoveStackOnNextCall() {
1747 gp.stackguard0 = stackForceMove
1750 // gcTestIsReachable performs a GC and returns a bit set where bit i
1751 // is set if ptrs[i] is reachable.
1752 func gcTestIsReachable(ptrs ...unsafe.Pointer) (mask uint64) {
1753 // This takes the pointers as unsafe.Pointers in order to keep
1754 // them live long enough for us to attach specials. After
1755 // that, we drop our references to them.
1758 panic("too many pointers for uint64 mask")
1761 // Block GC while we attach specials and drop our references
1762 // to ptrs. Otherwise, if a GC is in progress, it could mark
1763 // them reachable via this function before we have a chance to
1767 // Create reachability specials for ptrs.
1768 specials := make([]*specialReachable, len(ptrs))
1769 for i, p := range ptrs {
1770 lock(&mheap_.speciallock)
1771 s := (*specialReachable)(mheap_.specialReachableAlloc.alloc())
1772 unlock(&mheap_.speciallock)
1773 s.special.kind = _KindSpecialReachable
1774 if !addspecial(p, &s.special) {
1775 throw("already have a reachable special (duplicate pointer?)")
1778 // Make sure we don't retain ptrs.
1784 // Force a full GC and sweep.
1787 // Process specials.
1788 for i, s := range specials {
1791 println("runtime: object", i, "was not swept")
1792 throw("IsReachable failed")
1797 lock(&mheap_.speciallock)
1798 mheap_.specialReachableAlloc.free(unsafe.Pointer(s))
1799 unlock(&mheap_.speciallock)
1805 // gcTestPointerClass returns the category of what p points to, one of:
1806 // "heap", "stack", "data", "bss", "other". This is useful for checking
1807 // that a test is doing what it's intended to do.
1809 // This is nosplit simply to avoid extra pointer shuffling that may
1810 // complicate a test.
1813 func gcTestPointerClass(p unsafe.Pointer) string {
1814 p2 := uintptr(noescape(p))
1816 if gp.stack.lo <= p2 && p2 < gp.stack.hi {
1819 if base, _, _ := findObject(p2, 0, 0); base != 0 {
1822 for _, datap := range activeModules() {
1823 if datap.data <= p2 && p2 < datap.edata || datap.noptrdata <= p2 && p2 < datap.enoptrdata {
1826 if datap.bss <= p2 && p2 < datap.ebss || datap.noptrbss <= p2 && p2 <= datap.enoptrbss {