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// Garbage collector: marking and scanning
6
7package runtime
8
9import (
10	"runtime/internal/atomic"
11	"runtime/internal/sys"
12	"unsafe"
13)
14
15const (
16	fixedRootFinalizers = iota
17	fixedRootFreeGStacks
18	fixedRootCount
19
20	// rootBlockBytes is the number of bytes to scan per data or
21	// BSS root.
22	rootBlockBytes = 256 << 10
23
24	// rootBlockSpans is the number of spans to scan per span
25	// root.
26	rootBlockSpans = 8 * 1024 // 64MB worth of spans
27
28	// maxObletBytes is the maximum bytes of an object to scan at
29	// once. Larger objects will be split up into "oblets" of at
30	// most this size. Since we can scan 1–2 MB/ms, 128 KB bounds
31	// scan preemption at ~100 µs.
32	//
33	// This must be > _MaxSmallSize so that the object base is the
34	// span base.
35	maxObletBytes = 128 << 10
36
37	// drainCheckThreshold specifies how many units of work to do
38	// between self-preemption checks in gcDrain. Assuming a scan
39	// rate of 1 MB/ms, this is ~100 µs. Lower values have higher
40	// overhead in the scan loop (the scheduler check may perform
41	// a syscall, so its overhead is nontrivial). Higher values
42	// make the system less responsive to incoming work.
43	drainCheckThreshold = 100000
44)
45
46// gcMarkRootPrepare queues root scanning jobs (stacks, globals, and
47// some miscellany) and initializes scanning-related state.
48//
49// The caller must have call gcCopySpans().
50//
51// The world must be stopped.
52//
53//go:nowritebarrier
54func gcMarkRootPrepare() {
55	work.nFlushCacheRoots = 0
56
57	work.nDataRoots = 0
58
59	// Only scan globals once per cycle; preferably concurrently.
60	roots := gcRoots
61	for roots != nil {
62		work.nDataRoots++
63		roots = roots.next
64	}
65
66	// Scan span roots for finalizer specials.
67	//
68	// We depend on addfinalizer to mark objects that get
69	// finalizers after root marking.
70	//
71	// We're only interested in scanning the in-use spans,
72	// which will all be swept at this point. More spans
73	// may be added to this list during concurrent GC, but
74	// we only care about spans that were allocated before
75	// this mark phase.
76	work.nSpanRoots = mheap_.sweepSpans[mheap_.sweepgen/2%2].numBlocks()
77
78	// Scan stacks.
79	//
80	// Gs may be created after this point, but it's okay that we
81	// ignore them because they begin life without any roots, so
82	// there's nothing to scan, and any roots they create during
83	// the concurrent phase will be scanned during mark
84	// termination.
85	work.nStackRoots = int(atomic.Loaduintptr(&allglen))
86
87	work.markrootNext = 0
88	work.markrootJobs = uint32(fixedRootCount + work.nFlushCacheRoots + work.nDataRoots + work.nSpanRoots + work.nStackRoots)
89}
90
91// gcMarkRootCheck checks that all roots have been scanned. It is
92// purely for debugging.
93func gcMarkRootCheck() {
94	if work.markrootNext < work.markrootJobs {
95		print(work.markrootNext, " of ", work.markrootJobs, " markroot jobs done\n")
96		throw("left over markroot jobs")
97	}
98
99	lock(&allglock)
100	// Check that stacks have been scanned.
101	var gp *g
102	for i := 0; i < work.nStackRoots; i++ {
103		gp = allgs[i]
104		if !gp.gcscandone {
105			goto fail
106		}
107	}
108	unlock(&allglock)
109	return
110
111fail:
112	println("gp", gp, "goid", gp.goid,
113		"status", readgstatus(gp),
114		"gcscandone", gp.gcscandone,
115		"gcscanvalid", gp.gcscanvalid)
116	unlock(&allglock) // Avoid self-deadlock with traceback.
117	throw("scan missed a g")
118}
119
120// ptrmask for an allocation containing a single pointer.
121var oneptrmask = [...]uint8{1}
122
123// markroot scans the i'th root.
124//
125// Preemption must be disabled (because this uses a gcWork).
126//
127// nowritebarrier is only advisory here.
128//
129//go:nowritebarrier
130func markroot(gcw *gcWork, i uint32) {
131	// TODO(austin): This is a bit ridiculous. Compute and store
132	// the bases in gcMarkRootPrepare instead of the counts.
133	baseFlushCache := uint32(fixedRootCount)
134	baseData := baseFlushCache + uint32(work.nFlushCacheRoots)
135	baseSpans := baseData + uint32(work.nDataRoots)
136	baseStacks := baseSpans + uint32(work.nSpanRoots)
137	end := baseStacks + uint32(work.nStackRoots)
138
139	// Note: if you add a case here, please also update heapdump.go:dumproots.
140	switch {
141	case baseFlushCache <= i && i < baseData:
142		flushmcache(int(i - baseFlushCache))
143
144	case baseData <= i && i < baseSpans:
145		roots := gcRoots
146		c := baseData
147		for roots != nil {
148			if i == c {
149				markrootBlock(roots, gcw)
150				break
151			}
152			roots = roots.next
153			c++
154		}
155
156	case i == fixedRootFinalizers:
157		for fb := allfin; fb != nil; fb = fb.alllink {
158			cnt := uintptr(atomic.Load(&fb.cnt))
159			scanblock(uintptr(unsafe.Pointer(&fb.fin[0])), cnt*unsafe.Sizeof(fb.fin[0]), &finptrmask[0], gcw)
160		}
161
162	case i == fixedRootFreeGStacks:
163		// FIXME: We don't do this for gccgo.
164
165	case baseSpans <= i && i < baseStacks:
166		// mark mspan.specials
167		markrootSpans(gcw, int(i-baseSpans))
168
169	default:
170		// the rest is scanning goroutine stacks
171		var gp *g
172		if baseStacks <= i && i < end {
173			gp = allgs[i-baseStacks]
174		} else {
175			throw("markroot: bad index")
176		}
177
178		// remember when we've first observed the G blocked
179		// needed only to output in traceback
180		status := readgstatus(gp) // We are not in a scan state
181		if (status == _Gwaiting || status == _Gsyscall) && gp.waitsince == 0 {
182			gp.waitsince = work.tstart
183		}
184
185		// scang must be done on the system stack in case
186		// we're trying to scan our own stack.
187		systemstack(func() {
188			// If this is a self-scan, put the user G in
189			// _Gwaiting to prevent self-deadlock. It may
190			// already be in _Gwaiting if this is a mark
191			// worker or we're in mark termination.
192			userG := getg().m.curg
193			selfScan := gp == userG && readgstatus(userG) == _Grunning
194			if selfScan {
195				casgstatus(userG, _Grunning, _Gwaiting)
196				userG.waitreason = waitReasonGarbageCollectionScan
197			}
198
199			// TODO: scang blocks until gp's stack has
200			// been scanned, which may take a while for
201			// running goroutines. Consider doing this in
202			// two phases where the first is non-blocking:
203			// we scan the stacks we can and ask running
204			// goroutines to scan themselves; and the
205			// second blocks.
206			scang(gp, gcw)
207
208			if selfScan {
209				casgstatus(userG, _Gwaiting, _Grunning)
210			}
211		})
212	}
213}
214
215// markrootBlock scans one element of the list of GC roots.
216//
217//go:nowritebarrier
218func markrootBlock(roots *gcRootList, gcw *gcWork) {
219	for i := 0; i < roots.count; i++ {
220		r := &roots.roots[i]
221		scanblock(uintptr(r.decl), r.ptrdata, r.gcdata, gcw)
222	}
223}
224
225// markrootSpans marks roots for one shard of work.spans.
226//
227//go:nowritebarrier
228func markrootSpans(gcw *gcWork, shard int) {
229	// Objects with finalizers have two GC-related invariants:
230	//
231	// 1) Everything reachable from the object must be marked.
232	// This ensures that when we pass the object to its finalizer,
233	// everything the finalizer can reach will be retained.
234	//
235	// 2) Finalizer specials (which are not in the garbage
236	// collected heap) are roots. In practice, this means the fn
237	// field must be scanned.
238	//
239	// TODO(austin): There are several ideas for making this more
240	// efficient in issue #11485.
241
242	sg := mheap_.sweepgen
243	spans := mheap_.sweepSpans[mheap_.sweepgen/2%2].block(shard)
244	// Note that work.spans may not include spans that were
245	// allocated between entering the scan phase and now. This is
246	// okay because any objects with finalizers in those spans
247	// must have been allocated and given finalizers after we
248	// entered the scan phase, so addfinalizer will have ensured
249	// the above invariants for them.
250	for _, s := range spans {
251		if s.state != mSpanInUse {
252			continue
253		}
254		// Check that this span was swept (it may be cached or uncached).
255		if !useCheckmark && !(s.sweepgen == sg || s.sweepgen == sg+3) {
256			// sweepgen was updated (+2) during non-checkmark GC pass
257			print("sweep ", s.sweepgen, " ", sg, "\n")
258			throw("gc: unswept span")
259		}
260
261		// Speculatively check if there are any specials
262		// without acquiring the span lock. This may race with
263		// adding the first special to a span, but in that
264		// case addfinalizer will observe that the GC is
265		// active (which is globally synchronized) and ensure
266		// the above invariants. We may also ensure the
267		// invariants, but it's okay to scan an object twice.
268		if s.specials == nil {
269			continue
270		}
271
272		// Lock the specials to prevent a special from being
273		// removed from the list while we're traversing it.
274		lock(&s.speciallock)
275
276		for sp := s.specials; sp != nil; sp = sp.next {
277			if sp.kind != _KindSpecialFinalizer {
278				continue
279			}
280			// don't mark finalized object, but scan it so we
281			// retain everything it points to.
282			spf := (*specialfinalizer)(unsafe.Pointer(sp))
283			// A finalizer can be set for an inner byte of an object, find object beginning.
284			p := s.base() + uintptr(spf.special.offset)/s.elemsize*s.elemsize
285
286			// Mark everything that can be reached from
287			// the object (but *not* the object itself or
288			// we'll never collect it).
289			scanobject(p, gcw)
290
291			// The special itself is a root.
292			scanblock(uintptr(unsafe.Pointer(&spf.fn)), sys.PtrSize, &oneptrmask[0], gcw)
293		}
294
295		unlock(&s.speciallock)
296	}
297}
298
299// gcAssistAlloc performs GC work to make gp's assist debt positive.
300// gp must be the calling user gorountine.
301//
302// This must be called with preemption enabled.
303func gcAssistAlloc(gp *g) {
304	// Don't assist in non-preemptible contexts. These are
305	// generally fragile and won't allow the assist to block.
306	if getg() == gp.m.g0 {
307		return
308	}
309	if mp := getg().m; mp.locks > 0 || mp.preemptoff != "" {
310		return
311	}
312
313	traced := false
314retry:
315	// Compute the amount of scan work we need to do to make the
316	// balance positive. When the required amount of work is low,
317	// we over-assist to build up credit for future allocations
318	// and amortize the cost of assisting.
319	debtBytes := -gp.gcAssistBytes
320	scanWork := int64(gcController.assistWorkPerByte * float64(debtBytes))
321	if scanWork < gcOverAssistWork {
322		scanWork = gcOverAssistWork
323		debtBytes = int64(gcController.assistBytesPerWork * float64(scanWork))
324	}
325
326	// Steal as much credit as we can from the background GC's
327	// scan credit. This is racy and may drop the background
328	// credit below 0 if two mutators steal at the same time. This
329	// will just cause steals to fail until credit is accumulated
330	// again, so in the long run it doesn't really matter, but we
331	// do have to handle the negative credit case.
332	bgScanCredit := atomic.Loadint64(&gcController.bgScanCredit)
333	stolen := int64(0)
334	if bgScanCredit > 0 {
335		if bgScanCredit < scanWork {
336			stolen = bgScanCredit
337			gp.gcAssistBytes += 1 + int64(gcController.assistBytesPerWork*float64(stolen))
338		} else {
339			stolen = scanWork
340			gp.gcAssistBytes += debtBytes
341		}
342		atomic.Xaddint64(&gcController.bgScanCredit, -stolen)
343
344		scanWork -= stolen
345
346		if scanWork == 0 {
347			// We were able to steal all of the credit we
348			// needed.
349			if traced {
350				traceGCMarkAssistDone()
351			}
352			return
353		}
354	}
355
356	if trace.enabled && !traced {
357		traced = true
358		traceGCMarkAssistStart()
359	}
360
361	// Perform assist work
362	systemstack(func() {
363		gcAssistAlloc1(gp, scanWork)
364		// The user stack may have moved, so this can't touch
365		// anything on it until it returns from systemstack.
366	})
367
368	completed := gp.param != nil
369	gp.param = nil
370	if completed {
371		gcMarkDone()
372	}
373
374	if gp.gcAssistBytes < 0 {
375		// We were unable steal enough credit or perform
376		// enough work to pay off the assist debt. We need to
377		// do one of these before letting the mutator allocate
378		// more to prevent over-allocation.
379		//
380		// If this is because we were preempted, reschedule
381		// and try some more.
382		if gp.preempt {
383			Gosched()
384			goto retry
385		}
386
387		// Add this G to an assist queue and park. When the GC
388		// has more background credit, it will satisfy queued
389		// assists before flushing to the global credit pool.
390		//
391		// Note that this does *not* get woken up when more
392		// work is added to the work list. The theory is that
393		// there wasn't enough work to do anyway, so we might
394		// as well let background marking take care of the
395		// work that is available.
396		if !gcParkAssist() {
397			goto retry
398		}
399
400		// At this point either background GC has satisfied
401		// this G's assist debt, or the GC cycle is over.
402	}
403	if traced {
404		traceGCMarkAssistDone()
405	}
406}
407
408// gcAssistAlloc1 is the part of gcAssistAlloc that runs on the system
409// stack. This is a separate function to make it easier to see that
410// we're not capturing anything from the user stack, since the user
411// stack may move while we're in this function.
412//
413// gcAssistAlloc1 indicates whether this assist completed the mark
414// phase by setting gp.param to non-nil. This can't be communicated on
415// the stack since it may move.
416//
417//go:systemstack
418func gcAssistAlloc1(gp *g, scanWork int64) {
419	// Clear the flag indicating that this assist completed the
420	// mark phase.
421	gp.param = nil
422
423	if atomic.Load(&gcBlackenEnabled) == 0 {
424		// The gcBlackenEnabled check in malloc races with the
425		// store that clears it but an atomic check in every malloc
426		// would be a performance hit.
427		// Instead we recheck it here on the non-preemptable system
428		// stack to determine if we should perform an assist.
429
430		// GC is done, so ignore any remaining debt.
431		gp.gcAssistBytes = 0
432		return
433	}
434	// Track time spent in this assist. Since we're on the
435	// system stack, this is non-preemptible, so we can
436	// just measure start and end time.
437	startTime := nanotime()
438
439	decnwait := atomic.Xadd(&work.nwait, -1)
440	if decnwait == work.nproc {
441		println("runtime: work.nwait =", decnwait, "work.nproc=", work.nproc)
442		throw("nwait > work.nprocs")
443	}
444
445	// gcDrainN requires the caller to be preemptible.
446	casgstatus(gp, _Grunning, _Gwaiting)
447	gp.waitreason = waitReasonGCAssistMarking
448
449	// drain own cached work first in the hopes that it
450	// will be more cache friendly.
451	gcw := &getg().m.p.ptr().gcw
452	workDone := gcDrainN(gcw, scanWork)
453
454	casgstatus(gp, _Gwaiting, _Grunning)
455
456	// Record that we did this much scan work.
457	//
458	// Back out the number of bytes of assist credit that
459	// this scan work counts for. The "1+" is a poor man's
460	// round-up, to ensure this adds credit even if
461	// assistBytesPerWork is very low.
462	gp.gcAssistBytes += 1 + int64(gcController.assistBytesPerWork*float64(workDone))
463
464	// If this is the last worker and we ran out of work,
465	// signal a completion point.
466	incnwait := atomic.Xadd(&work.nwait, +1)
467	if incnwait > work.nproc {
468		println("runtime: work.nwait=", incnwait,
469			"work.nproc=", work.nproc)
470		throw("work.nwait > work.nproc")
471	}
472
473	if incnwait == work.nproc && !gcMarkWorkAvailable(nil) {
474		// This has reached a background completion point. Set
475		// gp.param to a non-nil value to indicate this. It
476		// doesn't matter what we set it to (it just has to be
477		// a valid pointer).
478		gp.param = unsafe.Pointer(gp)
479	}
480	duration := nanotime() - startTime
481	_p_ := gp.m.p.ptr()
482	_p_.gcAssistTime += duration
483	if _p_.gcAssistTime > gcAssistTimeSlack {
484		atomic.Xaddint64(&gcController.assistTime, _p_.gcAssistTime)
485		_p_.gcAssistTime = 0
486	}
487}
488
489// gcWakeAllAssists wakes all currently blocked assists. This is used
490// at the end of a GC cycle. gcBlackenEnabled must be false to prevent
491// new assists from going to sleep after this point.
492func gcWakeAllAssists() {
493	lock(&work.assistQueue.lock)
494	list := work.assistQueue.q.popList()
495	injectglist(&list)
496	unlock(&work.assistQueue.lock)
497}
498
499// gcParkAssist puts the current goroutine on the assist queue and parks.
500//
501// gcParkAssist reports whether the assist is now satisfied. If it
502// returns false, the caller must retry the assist.
503//
504//go:nowritebarrier
505func gcParkAssist() bool {
506	lock(&work.assistQueue.lock)
507	// If the GC cycle finished while we were getting the lock,
508	// exit the assist. The cycle can't finish while we hold the
509	// lock.
510	if atomic.Load(&gcBlackenEnabled) == 0 {
511		unlock(&work.assistQueue.lock)
512		return true
513	}
514
515	gp := getg()
516	oldList := work.assistQueue.q
517	work.assistQueue.q.pushBack(gp)
518
519	// Recheck for background credit now that this G is in
520	// the queue, but can still back out. This avoids a
521	// race in case background marking has flushed more
522	// credit since we checked above.
523	if atomic.Loadint64(&gcController.bgScanCredit) > 0 {
524		work.assistQueue.q = oldList
525		if oldList.tail != 0 {
526			oldList.tail.ptr().schedlink.set(nil)
527		}
528		unlock(&work.assistQueue.lock)
529		return false
530	}
531	// Park.
532	goparkunlock(&work.assistQueue.lock, waitReasonGCAssistWait, traceEvGoBlockGC, 2)
533	return true
534}
535
536// gcFlushBgCredit flushes scanWork units of background scan work
537// credit. This first satisfies blocked assists on the
538// work.assistQueue and then flushes any remaining credit to
539// gcController.bgScanCredit.
540//
541// Write barriers are disallowed because this is used by gcDrain after
542// it has ensured that all work is drained and this must preserve that
543// condition.
544//
545//go:nowritebarrierrec
546func gcFlushBgCredit(scanWork int64) {
547	if work.assistQueue.q.empty() {
548		// Fast path; there are no blocked assists. There's a
549		// small window here where an assist may add itself to
550		// the blocked queue and park. If that happens, we'll
551		// just get it on the next flush.
552		atomic.Xaddint64(&gcController.bgScanCredit, scanWork)
553		return
554	}
555
556	scanBytes := int64(float64(scanWork) * gcController.assistBytesPerWork)
557
558	lock(&work.assistQueue.lock)
559	for !work.assistQueue.q.empty() && scanBytes > 0 {
560		gp := work.assistQueue.q.pop()
561		// Note that gp.gcAssistBytes is negative because gp
562		// is in debt. Think carefully about the signs below.
563		if scanBytes+gp.gcAssistBytes >= 0 {
564			// Satisfy this entire assist debt.
565			scanBytes += gp.gcAssistBytes
566			gp.gcAssistBytes = 0
567			// It's important that we *not* put gp in
568			// runnext. Otherwise, it's possible for user
569			// code to exploit the GC worker's high
570			// scheduler priority to get itself always run
571			// before other goroutines and always in the
572			// fresh quantum started by GC.
573			ready(gp, 0, false)
574		} else {
575			// Partially satisfy this assist.
576			gp.gcAssistBytes += scanBytes
577			scanBytes = 0
578			// As a heuristic, we move this assist to the
579			// back of the queue so that large assists
580			// can't clog up the assist queue and
581			// substantially delay small assists.
582			work.assistQueue.q.pushBack(gp)
583			break
584		}
585	}
586
587	if scanBytes > 0 {
588		// Convert from scan bytes back to work.
589		scanWork = int64(float64(scanBytes) * gcController.assistWorkPerByte)
590		atomic.Xaddint64(&gcController.bgScanCredit, scanWork)
591	}
592	unlock(&work.assistQueue.lock)
593}
594
595// We use a C function to find the stack.
596// Returns whether we succesfully scanned the stack.
597func doscanstack(*g, *gcWork) bool
598
599func doscanstackswitch(*g, *g)
600
601// scanstack scans gp's stack, greying all pointers found on the stack.
602//
603// scanstack is marked go:systemstack because it must not be preempted
604// while using a workbuf.
605//
606//go:nowritebarrier
607//go:systemstack
608func scanstack(gp *g, gcw *gcWork) {
609	if gp.gcscanvalid {
610		return
611	}
612
613	if readgstatus(gp)&_Gscan == 0 {
614		print("runtime:scanstack: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", hex(readgstatus(gp)), "\n")
615		throw("scanstack - bad status")
616	}
617
618	switch readgstatus(gp) &^ _Gscan {
619	default:
620		print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
621		throw("mark - bad status")
622	case _Gdead:
623		return
624	case _Grunning:
625		// ok for gccgo, though not for gc.
626		if usestackmaps {
627			print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
628			throw("scanstack: goroutine not stopped")
629		}
630	case _Gsyscall:
631		if usestackmaps {
632			print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
633			throw("scanstack: goroutine in syscall")
634		}
635	case _Grunnable, _Gwaiting:
636		// ok
637	}
638
639	// Scan the stack.
640	if usestackmaps {
641		g := getg()
642		if g == gp {
643			// Scan its own stack.
644			doscanstack(gp, gcw)
645		} else if gp.entry != nil {
646			// This is a newly created g that hasn't run. No stack to scan.
647		} else {
648			// Scanning another g's stack. We need to switch to that g
649			// to unwind its stack. And switch back after scan.
650			scanstackswitch(gp, gcw)
651		}
652	} else {
653		doscanstack(gp, gcw)
654
655		// Conservatively scan the saved register values.
656		scanstackblock(uintptr(unsafe.Pointer(&gp.gcregs)), unsafe.Sizeof(gp.gcregs), gcw)
657		scanstackblock(uintptr(unsafe.Pointer(&gp.context)), unsafe.Sizeof(gp.context), gcw)
658	}
659
660	gp.gcscanvalid = true
661}
662
663// scanstackswitch scans gp's stack by switching (gogo) to gp and
664// letting it scan its own stack, and switching back upon finish.
665//
666//go:nowritebarrier
667func scanstackswitch(gp *g, gcw *gcWork) {
668	g := getg()
669
670	// We are on the system stack which prevents preemption. But
671	// we are going to switch to g stack. Lock m to block preemption.
672	mp := acquirem()
673
674	// The doscanstackswitch function will modify the current g's
675	// context. Preserve it.
676	// The stack scan code may call systemstack, which will modify
677	// gp's context. Preserve it as well so we can resume gp.
678	context := g.context
679	stackcontext := g.stackcontext
680	context2 := gp.context
681	stackcontext2 := gp.stackcontext
682
683	gp.scangcw = uintptr(unsafe.Pointer(gcw))
684	gp.scang = uintptr(unsafe.Pointer(g))
685	doscanstackswitch(g, gp)
686
687	// Restore the contexts.
688	g.context = context
689	g.stackcontext = stackcontext
690	gp.context = context2
691	gp.stackcontext = stackcontext2
692	gp.scangcw = 0
693	// gp.scang is already cleared in C code.
694
695	releasem(mp)
696}
697
698type gcDrainFlags int
699
700const (
701	gcDrainUntilPreempt gcDrainFlags = 1 << iota
702	gcDrainFlushBgCredit
703	gcDrainIdle
704	gcDrainFractional
705)
706
707// gcDrain scans roots and objects in work buffers, blackening grey
708// objects until it is unable to get more work. It may return before
709// GC is done; it's the caller's responsibility to balance work from
710// other Ps.
711//
712// If flags&gcDrainUntilPreempt != 0, gcDrain returns when g.preempt
713// is set.
714//
715// If flags&gcDrainIdle != 0, gcDrain returns when there is other work
716// to do.
717//
718// If flags&gcDrainFractional != 0, gcDrain self-preempts when
719// pollFractionalWorkerExit() returns true. This implies
720// gcDrainNoBlock.
721//
722// If flags&gcDrainFlushBgCredit != 0, gcDrain flushes scan work
723// credit to gcController.bgScanCredit every gcCreditSlack units of
724// scan work.
725//
726//go:nowritebarrier
727func gcDrain(gcw *gcWork, flags gcDrainFlags) {
728	if !writeBarrier.needed {
729		throw("gcDrain phase incorrect")
730	}
731
732	gp := getg().m.curg
733	preemptible := flags&gcDrainUntilPreempt != 0
734	flushBgCredit := flags&gcDrainFlushBgCredit != 0
735	idle := flags&gcDrainIdle != 0
736
737	initScanWork := gcw.scanWork
738
739	// checkWork is the scan work before performing the next
740	// self-preempt check.
741	checkWork := int64(1<<63 - 1)
742	var check func() bool
743	if flags&(gcDrainIdle|gcDrainFractional) != 0 {
744		checkWork = initScanWork + drainCheckThreshold
745		if idle {
746			check = pollWork
747		} else if flags&gcDrainFractional != 0 {
748			check = pollFractionalWorkerExit
749		}
750	}
751
752	// Drain root marking jobs.
753	if work.markrootNext < work.markrootJobs {
754		for !(preemptible && gp.preempt) {
755			job := atomic.Xadd(&work.markrootNext, +1) - 1
756			if job >= work.markrootJobs {
757				break
758			}
759			markroot(gcw, job)
760			if check != nil && check() {
761				goto done
762			}
763		}
764	}
765
766	// Drain heap marking jobs.
767	for !(preemptible && gp.preempt) {
768		// Try to keep work available on the global queue. We used to
769		// check if there were waiting workers, but it's better to
770		// just keep work available than to make workers wait. In the
771		// worst case, we'll do O(log(_WorkbufSize)) unnecessary
772		// balances.
773		if work.full == 0 {
774			gcw.balance()
775		}
776
777		b := gcw.tryGetFast()
778		if b == 0 {
779			b = gcw.tryGet()
780			if b == 0 {
781				// Flush the write barrier
782				// buffer; this may create
783				// more work.
784				wbBufFlush(nil, 0)
785				b = gcw.tryGet()
786			}
787		}
788		if b == 0 {
789			// Unable to get work.
790			break
791		}
792		scanobject(b, gcw)
793
794		// Flush background scan work credit to the global
795		// account if we've accumulated enough locally so
796		// mutator assists can draw on it.
797		if gcw.scanWork >= gcCreditSlack {
798			atomic.Xaddint64(&gcController.scanWork, gcw.scanWork)
799			if flushBgCredit {
800				gcFlushBgCredit(gcw.scanWork - initScanWork)
801				initScanWork = 0
802			}
803			checkWork -= gcw.scanWork
804			gcw.scanWork = 0
805
806			if checkWork <= 0 {
807				checkWork += drainCheckThreshold
808				if check != nil && check() {
809					break
810				}
811			}
812		}
813	}
814
815done:
816	// Flush remaining scan work credit.
817	if gcw.scanWork > 0 {
818		atomic.Xaddint64(&gcController.scanWork, gcw.scanWork)
819		if flushBgCredit {
820			gcFlushBgCredit(gcw.scanWork - initScanWork)
821		}
822		gcw.scanWork = 0
823	}
824}
825
826// gcDrainN blackens grey objects until it has performed roughly
827// scanWork units of scan work or the G is preempted. This is
828// best-effort, so it may perform less work if it fails to get a work
829// buffer. Otherwise, it will perform at least n units of work, but
830// may perform more because scanning is always done in whole object
831// increments. It returns the amount of scan work performed.
832//
833// The caller goroutine must be in a preemptible state (e.g.,
834// _Gwaiting) to prevent deadlocks during stack scanning. As a
835// consequence, this must be called on the system stack.
836//
837//go:nowritebarrier
838//go:systemstack
839func gcDrainN(gcw *gcWork, scanWork int64) int64 {
840	if !writeBarrier.needed {
841		throw("gcDrainN phase incorrect")
842	}
843
844	// There may already be scan work on the gcw, which we don't
845	// want to claim was done by this call.
846	workFlushed := -gcw.scanWork
847
848	gp := getg().m.curg
849	for !gp.preempt && workFlushed+gcw.scanWork < scanWork {
850		// See gcDrain comment.
851		if work.full == 0 {
852			gcw.balance()
853		}
854
855		// This might be a good place to add prefetch code...
856		// if(wbuf.nobj > 4) {
857		//         PREFETCH(wbuf->obj[wbuf.nobj - 3];
858		//  }
859		//
860		b := gcw.tryGetFast()
861		if b == 0 {
862			b = gcw.tryGet()
863			if b == 0 {
864				// Flush the write barrier buffer;
865				// this may create more work.
866				wbBufFlush(nil, 0)
867				b = gcw.tryGet()
868			}
869		}
870
871		if b == 0 {
872			// Try to do a root job.
873			//
874			// TODO: Assists should get credit for this
875			// work.
876			if work.markrootNext < work.markrootJobs {
877				job := atomic.Xadd(&work.markrootNext, +1) - 1
878				if job < work.markrootJobs {
879					markroot(gcw, job)
880					continue
881				}
882			}
883			// No heap or root jobs.
884			break
885		}
886		scanobject(b, gcw)
887
888		// Flush background scan work credit.
889		if gcw.scanWork >= gcCreditSlack {
890			atomic.Xaddint64(&gcController.scanWork, gcw.scanWork)
891			workFlushed += gcw.scanWork
892			gcw.scanWork = 0
893		}
894	}
895
896	// Unlike gcDrain, there's no need to flush remaining work
897	// here because this never flushes to bgScanCredit and
898	// gcw.dispose will flush any remaining work to scanWork.
899
900	return workFlushed + gcw.scanWork
901}
902
903// scanblock scans b as scanobject would, but using an explicit
904// pointer bitmap instead of the heap bitmap.
905//
906// This is used to scan non-heap roots, so it does not update
907// gcw.bytesMarked or gcw.scanWork.
908//
909//go:nowritebarrier
910func scanblock(b0, n0 uintptr, ptrmask *uint8, gcw *gcWork) {
911	// Use local copies of original parameters, so that a stack trace
912	// due to one of the throws below shows the original block
913	// base and extent.
914	b := b0
915	n := n0
916
917	for i := uintptr(0); i < n; {
918		// Find bits for the next word.
919		bits := uint32(*addb(ptrmask, i/(sys.PtrSize*8)))
920		if bits == 0 {
921			i += sys.PtrSize * 8
922			continue
923		}
924		for j := 0; j < 8 && i < n; j++ {
925			if bits&1 != 0 {
926				// Same work as in scanobject; see comments there.
927				p := *(*uintptr)(unsafe.Pointer(b + i))
928				if p != 0 {
929					if obj, span, objIndex := findObject(p, b, i, false); obj != 0 {
930						greyobject(obj, b, i, span, gcw, objIndex, false)
931					}
932				}
933			}
934			bits >>= 1
935			i += sys.PtrSize
936		}
937	}
938}
939
940// scanobject scans the object starting at b, adding pointers to gcw.
941// b must point to the beginning of a heap object or an oblet.
942// scanobject consults the GC bitmap for the pointer mask and the
943// spans for the size of the object.
944//
945//go:nowritebarrier
946func scanobject(b uintptr, gcw *gcWork) {
947	// Find the bits for b and the size of the object at b.
948	//
949	// b is either the beginning of an object, in which case this
950	// is the size of the object to scan, or it points to an
951	// oblet, in which case we compute the size to scan below.
952	hbits := heapBitsForAddr(b)
953	s := spanOfUnchecked(b)
954	n := s.elemsize
955	if n == 0 {
956		throw("scanobject n == 0")
957	}
958
959	if n > maxObletBytes {
960		// Large object. Break into oblets for better
961		// parallelism and lower latency.
962		if b == s.base() {
963			// It's possible this is a noscan object (not
964			// from greyobject, but from other code
965			// paths), in which case we must *not* enqueue
966			// oblets since their bitmaps will be
967			// uninitialized.
968			if s.spanclass.noscan() {
969				// Bypass the whole scan.
970				gcw.bytesMarked += uint64(n)
971				return
972			}
973
974			// Enqueue the other oblets to scan later.
975			// Some oblets may be in b's scalar tail, but
976			// these will be marked as "no more pointers",
977			// so we'll drop out immediately when we go to
978			// scan those.
979			for oblet := b + maxObletBytes; oblet < s.base()+s.elemsize; oblet += maxObletBytes {
980				if !gcw.putFast(oblet) {
981					gcw.put(oblet)
982				}
983			}
984		}
985
986		// Compute the size of the oblet. Since this object
987		// must be a large object, s.base() is the beginning
988		// of the object.
989		n = s.base() + s.elemsize - b
990		if n > maxObletBytes {
991			n = maxObletBytes
992		}
993	}
994
995	var i uintptr
996	for i = 0; i < n; i += sys.PtrSize {
997		// Find bits for this word.
998		if i != 0 {
999			// Avoid needless hbits.next() on last iteration.
1000			hbits = hbits.next()
1001		}
1002		// Load bits once. See CL 22712 and issue 16973 for discussion.
1003		bits := hbits.bits()
1004		// During checkmarking, 1-word objects store the checkmark
1005		// in the type bit for the one word. The only one-word objects
1006		// are pointers, or else they'd be merged with other non-pointer
1007		// data into larger allocations.
1008		if i != 1*sys.PtrSize && bits&bitScan == 0 {
1009			break // no more pointers in this object
1010		}
1011		if bits&bitPointer == 0 {
1012			continue // not a pointer
1013		}
1014
1015		// Work here is duplicated in scanblock and above.
1016		// If you make changes here, make changes there too.
1017		obj := *(*uintptr)(unsafe.Pointer(b + i))
1018
1019		// At this point we have extracted the next potential pointer.
1020		// Quickly filter out nil and pointers back to the current object.
1021		if obj != 0 && obj-b >= n {
1022			// Test if obj points into the Go heap and, if so,
1023			// mark the object.
1024			//
1025			// Note that it's possible for findObject to
1026			// fail if obj points to a just-allocated heap
1027			// object because of a race with growing the
1028			// heap. In this case, we know the object was
1029			// just allocated and hence will be marked by
1030			// allocation itself.
1031			if obj, span, objIndex := findObject(obj, b, i, false); obj != 0 {
1032				greyobject(obj, b, i, span, gcw, objIndex, false)
1033			}
1034		}
1035	}
1036	gcw.bytesMarked += uint64(n)
1037	gcw.scanWork += int64(i)
1038}
1039
1040//go:linkname scanstackblock runtime.scanstackblock
1041
1042// scanstackblock is called by the stack scanning code in C to
1043// actually find and mark pointers in the stack block. This is like
1044// scanblock, but we scan the stack conservatively, so there is no
1045// bitmask of pointers.
1046func scanstackblock(b, n uintptr, gcw *gcWork) {
1047	if usestackmaps {
1048		throw("scanstackblock: conservative scan but stack map is used")
1049	}
1050
1051	for i := uintptr(0); i < n; i += sys.PtrSize {
1052		// Same work as in scanobject; see comments there.
1053		obj := *(*uintptr)(unsafe.Pointer(b + i))
1054		if obj, span, objIndex := findObject(obj, b, i, true); obj != 0 {
1055			greyobject(obj, b, i, span, gcw, objIndex, true)
1056		}
1057	}
1058}
1059
1060// scanstackblockwithmap is like scanstackblock, but with an explicit
1061// pointer bitmap. This is used only when precise stack scan is enabled.
1062//go:linkname scanstackblockwithmap runtime.scanstackblockwithmap
1063//go:nowritebarrier
1064func scanstackblockwithmap(pc, b0, n0 uintptr, ptrmask *uint8, gcw *gcWork) {
1065	// Use local copies of original parameters, so that a stack trace
1066	// due to one of the throws below shows the original block
1067	// base and extent.
1068	b := b0
1069	n := n0
1070
1071	for i := uintptr(0); i < n; {
1072		// Find bits for the next word.
1073		bits := uint32(*addb(ptrmask, i/(sys.PtrSize*8)))
1074		if bits == 0 {
1075			i += sys.PtrSize * 8
1076			continue
1077		}
1078		for j := 0; j < 8 && i < n; j++ {
1079			if bits&1 != 0 {
1080				// Same work as in scanobject; see comments there.
1081				obj := *(*uintptr)(unsafe.Pointer(b + i))
1082				if obj != 0 {
1083					o, span, objIndex := findObject(obj, b, i, false)
1084					if obj < minPhysPageSize ||
1085						span != nil && span.state != mSpanManual &&
1086							(obj < span.base() || obj >= span.limit || span.state != mSpanInUse) {
1087						print("runtime: found in object at *(", hex(b), "+", hex(i), ") = ", hex(obj), ", pc=", hex(pc), "\n")
1088						name, file, line := funcfileline(pc, -1)
1089						print(name, "\n", file, ":", line, "\n")
1090						//gcDumpObject("object", b, i)
1091						throw("found bad pointer in Go stack (incorrect use of unsafe or cgo?)")
1092					}
1093					if o != 0 {
1094						greyobject(o, b, i, span, gcw, objIndex, false)
1095					}
1096				}
1097			}
1098			bits >>= 1
1099			i += sys.PtrSize
1100		}
1101	}
1102}
1103
1104// Shade the object if it isn't already.
1105// The object is not nil and known to be in the heap.
1106// Preemption must be disabled.
1107//go:nowritebarrier
1108func shade(b uintptr) {
1109	if obj, span, objIndex := findObject(b, 0, 0, !usestackmaps); obj != 0 {
1110		gcw := &getg().m.p.ptr().gcw
1111		greyobject(obj, 0, 0, span, gcw, objIndex, !usestackmaps)
1112	}
1113}
1114
1115// obj is the start of an object with mark mbits.
1116// If it isn't already marked, mark it and enqueue into gcw.
1117// base and off are for debugging only and could be removed.
1118//
1119// See also wbBufFlush1, which partially duplicates this logic.
1120//
1121//go:nowritebarrierrec
1122func greyobject(obj, base, off uintptr, span *mspan, gcw *gcWork, objIndex uintptr, forStack bool) {
1123	// obj should be start of allocation, and so must be at least pointer-aligned.
1124	if obj&(sys.PtrSize-1) != 0 {
1125		throw("greyobject: obj not pointer-aligned")
1126	}
1127	mbits := span.markBitsForIndex(objIndex)
1128
1129	if useCheckmark {
1130		if !mbits.isMarked() {
1131			// Stack scanning is conservative, so we can
1132			// see a reference to an object not previously
1133			// found. Assume the object was correctly not
1134			// marked and ignore the pointer.
1135			if forStack {
1136				return
1137			}
1138			printlock()
1139			print("runtime:greyobject: checkmarks finds unexpected unmarked object obj=", hex(obj), "\n")
1140			print("runtime: found obj at *(", hex(base), "+", hex(off), ")\n")
1141
1142			// Dump the source (base) object
1143			gcDumpObject("base", base, off)
1144
1145			// Dump the object
1146			gcDumpObject("obj", obj, ^uintptr(0))
1147
1148			getg().m.traceback = 2
1149			throw("checkmark found unmarked object")
1150		}
1151		hbits := heapBitsForAddr(obj)
1152		if hbits.isCheckmarked(span.elemsize) {
1153			return
1154		}
1155		hbits.setCheckmarked(span.elemsize)
1156		if !hbits.isCheckmarked(span.elemsize) {
1157			throw("setCheckmarked and isCheckmarked disagree")
1158		}
1159	} else {
1160		// Stack scanning is conservative, so we can see a
1161		// pointer to a free object. Assume the object was
1162		// correctly freed and we must ignore the pointer.
1163		if forStack && span.isFree(objIndex) {
1164			return
1165		}
1166
1167		if debug.gccheckmark > 0 && span.isFree(objIndex) {
1168			print("runtime: marking free object ", hex(obj), " found at *(", hex(base), "+", hex(off), ")\n")
1169			gcDumpObject("base", base, off)
1170			gcDumpObject("obj", obj, ^uintptr(0))
1171			getg().m.traceback = 2
1172			throw("marking free object")
1173		}
1174
1175		// If marked we have nothing to do.
1176		if mbits.isMarked() {
1177			return
1178		}
1179		mbits.setMarked()
1180
1181		// Mark span.
1182		arena, pageIdx, pageMask := pageIndexOf(span.base())
1183		if arena.pageMarks[pageIdx]&pageMask == 0 {
1184			atomic.Or8(&arena.pageMarks[pageIdx], pageMask)
1185		}
1186
1187		// If this is a noscan object, fast-track it to black
1188		// instead of greying it.
1189		if span.spanclass.noscan() {
1190			gcw.bytesMarked += uint64(span.elemsize)
1191			return
1192		}
1193	}
1194
1195	// Queue the obj for scanning. The PREFETCH(obj) logic has been removed but
1196	// seems like a nice optimization that can be added back in.
1197	// There needs to be time between the PREFETCH and the use.
1198	// Previously we put the obj in an 8 element buffer that is drained at a rate
1199	// to give the PREFETCH time to do its work.
1200	// Use of PREFETCHNTA might be more appropriate than PREFETCH
1201	if !gcw.putFast(obj) {
1202		gcw.put(obj)
1203	}
1204}
1205
1206// gcDumpObject dumps the contents of obj for debugging and marks the
1207// field at byte offset off in obj.
1208func gcDumpObject(label string, obj, off uintptr) {
1209	s := spanOf(obj)
1210	print(label, "=", hex(obj))
1211	if s == nil {
1212		print(" s=nil\n")
1213		return
1214	}
1215	print(" s.base()=", hex(s.base()), " s.limit=", hex(s.limit), " s.spanclass=", s.spanclass, " s.elemsize=", s.elemsize, " s.state=")
1216	if 0 <= s.state && int(s.state) < len(mSpanStateNames) {
1217		print(mSpanStateNames[s.state], "\n")
1218	} else {
1219		print("unknown(", s.state, ")\n")
1220	}
1221
1222	skipped := false
1223	size := s.elemsize
1224	if s.state == mSpanManual && size == 0 {
1225		// We're printing something from a stack frame. We
1226		// don't know how big it is, so just show up to an
1227		// including off.
1228		size = off + sys.PtrSize
1229	}
1230	for i := uintptr(0); i < size; i += sys.PtrSize {
1231		// For big objects, just print the beginning (because
1232		// that usually hints at the object's type) and the
1233		// fields around off.
1234		if !(i < 128*sys.PtrSize || off-16*sys.PtrSize < i && i < off+16*sys.PtrSize) {
1235			skipped = true
1236			continue
1237		}
1238		if skipped {
1239			print(" ...\n")
1240			skipped = false
1241		}
1242		print(" *(", label, "+", i, ") = ", hex(*(*uintptr)(unsafe.Pointer(obj + i))))
1243		if i == off {
1244			print(" <==")
1245		}
1246		print("\n")
1247	}
1248	if skipped {
1249		print(" ...\n")
1250	}
1251}
1252
1253// gcmarknewobject marks a newly allocated object black. obj must
1254// not contain any non-nil pointers.
1255//
1256// This is nosplit so it can manipulate a gcWork without preemption.
1257//
1258//go:nowritebarrier
1259//go:nosplit
1260func gcmarknewobject(obj, size, scanSize uintptr) {
1261	if useCheckmark { // The world should be stopped so this should not happen.
1262		throw("gcmarknewobject called while doing checkmark")
1263	}
1264	markBitsForAddr(obj).setMarked()
1265	gcw := &getg().m.p.ptr().gcw
1266	gcw.bytesMarked += uint64(size)
1267	gcw.scanWork += int64(scanSize)
1268}
1269
1270// gcMarkTinyAllocs greys all active tiny alloc blocks.
1271//
1272// The world must be stopped.
1273func gcMarkTinyAllocs() {
1274	for _, p := range allp {
1275		c := p.mcache
1276		if c == nil || c.tiny == 0 {
1277			continue
1278		}
1279		_, span, objIndex := findObject(c.tiny, 0, 0, false)
1280		gcw := &p.gcw
1281		greyobject(c.tiny, 0, 0, span, gcw, objIndex, false)
1282	}
1283}
1284
1285// Checkmarking
1286
1287// To help debug the concurrent GC we remark with the world
1288// stopped ensuring that any object encountered has their normal
1289// mark bit set. To do this we use an orthogonal bit
1290// pattern to indicate the object is marked. The following pattern
1291// uses the upper two bits in the object's boundary nibble.
1292// 01: scalar  not marked
1293// 10: pointer not marked
1294// 11: pointer     marked
1295// 00: scalar      marked
1296// Xoring with 01 will flip the pattern from marked to unmarked and vica versa.
1297// The higher bit is 1 for pointers and 0 for scalars, whether the object
1298// is marked or not.
1299// The first nibble no longer holds the typeDead pattern indicating that the
1300// there are no more pointers in the object. This information is held
1301// in the second nibble.
1302
1303// If useCheckmark is true, marking of an object uses the
1304// checkmark bits (encoding above) instead of the standard
1305// mark bits.
1306var useCheckmark = false
1307
1308//go:nowritebarrier
1309func initCheckmarks() {
1310	useCheckmark = true
1311	for _, s := range mheap_.allspans {
1312		if s.state == mSpanInUse {
1313			heapBitsForAddr(s.base()).initCheckmarkSpan(s.layout())
1314		}
1315	}
1316}
1317
1318func clearCheckmarks() {
1319	useCheckmark = false
1320	for _, s := range mheap_.allspans {
1321		if s.state == mSpanInUse {
1322			heapBitsForAddr(s.base()).clearCheckmarkSpan(s.layout())
1323		}
1324	}
1325}
1326