github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/runtime/mgcmark.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Garbage collector: marking and scanning
     6  
     7  package runtime
     8  
     9  import (
    10  	"runtime/internal/atomic"
    11  	"runtime/internal/sys"
    12  	"unsafe"
    13  )
    14  
    15  const (
    16  	fixedRootFinalizers = iota
    17  	fixedRootFlushCaches
    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  
    29  // gcMarkRootPrepare queues root scanning jobs (stacks, globals, and
    30  // some miscellany) and initializes scanning-related state.
    31  //
    32  // The caller must have call gcCopySpans().
    33  //
    34  //go:nowritebarrier
    35  func gcMarkRootPrepare() {
    36  	// Compute how many data and BSS root blocks there are.
    37  	nBlocks := func(bytes uintptr) int {
    38  		return int((bytes + rootBlockBytes - 1) / rootBlockBytes)
    39  	}
    40  
    41  	work.nDataRoots = 0
    42  	for datap := &firstmoduledata; datap != nil; datap = datap.next {
    43  		nDataRoots := nBlocks(datap.edata - datap.data)
    44  		if nDataRoots > work.nDataRoots {
    45  			work.nDataRoots = nDataRoots
    46  		}
    47  	}
    48  
    49  	work.nBSSRoots = 0
    50  	for datap := &firstmoduledata; datap != nil; datap = datap.next {
    51  		nBSSRoots := nBlocks(datap.ebss - datap.bss)
    52  		if nBSSRoots > work.nBSSRoots {
    53  			work.nBSSRoots = nBSSRoots
    54  		}
    55  	}
    56  
    57  	// Compute number of span roots.
    58  	work.nSpanRoots = (len(work.spans) + rootBlockSpans - 1) / rootBlockSpans
    59  
    60  	// Snapshot of allglen. During concurrent scan, we just need
    61  	// to be consistent about how many markroot jobs we create and
    62  	// how many Gs we check. Gs may be created after this point,
    63  	// but it's okay that we ignore them because they begin life
    64  	// without any roots, so there's nothing to scan, and any
    65  	// roots they create during the concurrent phase will be
    66  	// scanned during mark termination. During mark termination,
    67  	// allglen isn't changing, so we'll scan all Gs.
    68  	work.nStackRoots = int(atomic.Loaduintptr(&allglen))
    69  
    70  	work.markrootNext = 0
    71  	work.markrootJobs = uint32(fixedRootCount + work.nDataRoots + work.nBSSRoots + work.nSpanRoots + work.nStackRoots)
    72  }
    73  
    74  // gcMarkRootCheck checks that all roots have been scanned. It is
    75  // purely for debugging.
    76  func gcMarkRootCheck() {
    77  	if work.markrootNext < work.markrootJobs {
    78  		print(work.markrootNext, " of ", work.markrootJobs, " markroot jobs done\n")
    79  		throw("left over markroot jobs")
    80  	}
    81  
    82  	lock(&allglock)
    83  	// Check that gc work is done.
    84  	for i := 0; i < work.nStackRoots; i++ {
    85  		gp := allgs[i]
    86  		if !gp.gcscandone {
    87  			throw("scan missed a g")
    88  		}
    89  	}
    90  	unlock(&allglock)
    91  }
    92  
    93  // ptrmask for an allocation containing a single pointer.
    94  var oneptrmask = [...]uint8{1}
    95  
    96  // markroot scans the i'th root.
    97  //
    98  // Preemption must be disabled (because this uses a gcWork).
    99  //
   100  //go:nowritebarrier
   101  func markroot(i uint32) {
   102  	// TODO: Consider using getg().m.p.ptr().gcw.
   103  	var gcw gcWork
   104  
   105  	baseData := uint32(fixedRootCount)
   106  	baseBSS := baseData + uint32(work.nDataRoots)
   107  	baseSpans := baseBSS + uint32(work.nBSSRoots)
   108  	baseStacks := baseSpans + uint32(work.nSpanRoots)
   109  
   110  	// Note: if you add a case here, please also update heapdump.go:dumproots.
   111  	switch {
   112  	case baseData <= i && i < baseBSS:
   113  		for datap := &firstmoduledata; datap != nil; datap = datap.next {
   114  			markrootBlock(datap.data, datap.edata-datap.data, datap.gcdatamask.bytedata, &gcw, int(i-baseData))
   115  		}
   116  
   117  	case baseBSS <= i && i < baseSpans:
   118  		for datap := &firstmoduledata; datap != nil; datap = datap.next {
   119  			markrootBlock(datap.bss, datap.ebss-datap.bss, datap.gcbssmask.bytedata, &gcw, int(i-baseBSS))
   120  		}
   121  
   122  	case i == fixedRootFinalizers:
   123  		for fb := allfin; fb != nil; fb = fb.alllink {
   124  			scanblock(uintptr(unsafe.Pointer(&fb.fin[0])), uintptr(fb.cnt)*unsafe.Sizeof(fb.fin[0]), &finptrmask[0], &gcw)
   125  		}
   126  
   127  	case i == fixedRootFlushCaches:
   128  		if gcphase == _GCmarktermination { // Do not flush mcaches during concurrent phase.
   129  			flushallmcaches()
   130  		}
   131  
   132  	case baseSpans <= i && i < baseStacks:
   133  		// mark MSpan.specials
   134  		markrootSpans(&gcw, int(i-baseSpans))
   135  
   136  	default:
   137  		// the rest is scanning goroutine stacks
   138  		if uintptr(i-baseStacks) >= allglen {
   139  			throw("markroot: bad index")
   140  		}
   141  		gp := allgs[i-baseStacks]
   142  
   143  		// remember when we've first observed the G blocked
   144  		// needed only to output in traceback
   145  		status := readgstatus(gp) // We are not in a scan state
   146  		if (status == _Gwaiting || status == _Gsyscall) && gp.waitsince == 0 {
   147  			gp.waitsince = work.tstart
   148  		}
   149  
   150  		// Shrink a stack if not much of it is being used but not in the scan phase.
   151  		if gcphase == _GCmarktermination {
   152  			// Shrink during STW GCmarktermination phase thus avoiding
   153  			// complications introduced by shrinking during
   154  			// non-STW phases.
   155  			shrinkstack(gp)
   156  		}
   157  
   158  		if gcphase != _GCmarktermination && gp.startpc == gcBgMarkWorkerPC {
   159  			// GC background workers may be
   160  			// non-preemptible, so we may deadlock if we
   161  			// try to scan them during a concurrent phase.
   162  			// They also have tiny stacks, so just ignore
   163  			// them until mark termination.
   164  			gp.gcscandone = true
   165  			break
   166  		}
   167  
   168  		// scang must be done on the system stack in case
   169  		// we're trying to scan our own stack.
   170  		systemstack(func() {
   171  			// If this is a self-scan, put the user G in
   172  			// _Gwaiting to prevent self-deadlock. It may
   173  			// already be in _Gwaiting if this is mark
   174  			// termination.
   175  			userG := getg().m.curg
   176  			selfScan := gp == userG && readgstatus(userG) == _Grunning
   177  			if selfScan {
   178  				casgstatus(userG, _Grunning, _Gwaiting)
   179  				userG.waitreason = "garbage collection scan"
   180  			}
   181  
   182  			// TODO: scang blocks until gp's stack has
   183  			// been scanned, which may take a while for
   184  			// running goroutines. Consider doing this in
   185  			// two phases where the first is non-blocking:
   186  			// we scan the stacks we can and ask running
   187  			// goroutines to scan themselves; and the
   188  			// second blocks.
   189  			scang(gp)
   190  
   191  			if selfScan {
   192  				casgstatus(userG, _Gwaiting, _Grunning)
   193  			}
   194  		})
   195  	}
   196  
   197  	gcw.dispose()
   198  }
   199  
   200  // markrootBlock scans the shard'th shard of the block of memory [b0,
   201  // b0+n0), with the given pointer mask.
   202  //
   203  //go:nowritebarrier
   204  func markrootBlock(b0, n0 uintptr, ptrmask0 *uint8, gcw *gcWork, shard int) {
   205  	if rootBlockBytes%(8*sys.PtrSize) != 0 {
   206  		// This is necessary to pick byte offsets in ptrmask0.
   207  		throw("rootBlockBytes must be a multiple of 8*ptrSize")
   208  	}
   209  
   210  	b := b0 + uintptr(shard)*rootBlockBytes
   211  	if b >= b0+n0 {
   212  		return
   213  	}
   214  	ptrmask := (*uint8)(add(unsafe.Pointer(ptrmask0), uintptr(shard)*(rootBlockBytes/(8*sys.PtrSize))))
   215  	n := uintptr(rootBlockBytes)
   216  	if b+n > b0+n0 {
   217  		n = b0 + n0 - b
   218  	}
   219  
   220  	// Scan this shard.
   221  	scanblock(b, n, ptrmask, gcw)
   222  }
   223  
   224  // markrootSpans marks roots for one shard of work.spans.
   225  //
   226  //go:nowritebarrier
   227  func markrootSpans(gcw *gcWork, shard int) {
   228  	// Objects with finalizers have two GC-related invariants:
   229  	//
   230  	// 1) Everything reachable from the object must be marked.
   231  	// This ensures that when we pass the object to its finalizer,
   232  	// everything the finalizer can reach will be retained.
   233  	//
   234  	// 2) Finalizer specials (which are not in the garbage
   235  	// collected heap) are roots. In practice, this means the fn
   236  	// field must be scanned.
   237  	//
   238  	// TODO(austin): There are several ideas for making this more
   239  	// efficient in issue #11485.
   240  
   241  	// We process objects with finalizers only during the first
   242  	// markroot pass. In concurrent GC, this happens during
   243  	// concurrent scan and we depend on addfinalizer to ensure the
   244  	// above invariants for objects that get finalizers after
   245  	// concurrent scan. In STW GC, this will happen during mark
   246  	// termination.
   247  	if work.finalizersDone {
   248  		return
   249  	}
   250  
   251  	sg := mheap_.sweepgen
   252  	startSpan := shard * rootBlockSpans
   253  	endSpan := (shard + 1) * rootBlockSpans
   254  	if endSpan > len(work.spans) {
   255  		endSpan = len(work.spans)
   256  	}
   257  	// Note that work.spans may not include spans that were
   258  	// allocated between entering the scan phase and now. This is
   259  	// okay because any objects with finalizers in those spans
   260  	// must have been allocated and given finalizers after we
   261  	// entered the scan phase, so addfinalizer will have ensured
   262  	// the above invariants for them.
   263  	for _, s := range work.spans[startSpan:endSpan] {
   264  		if s.state != mSpanInUse {
   265  			continue
   266  		}
   267  		if !useCheckmark && s.sweepgen != sg {
   268  			// sweepgen was updated (+2) during non-checkmark GC pass
   269  			print("sweep ", s.sweepgen, " ", sg, "\n")
   270  			throw("gc: unswept span")
   271  		}
   272  
   273  		// Speculatively check if there are any specials
   274  		// without acquiring the span lock. This may race with
   275  		// adding the first special to a span, but in that
   276  		// case addfinalizer will observe that the GC is
   277  		// active (which is globally synchronized) and ensure
   278  		// the above invariants. We may also ensure the
   279  		// invariants, but it's okay to scan an object twice.
   280  		if s.specials == nil {
   281  			continue
   282  		}
   283  
   284  		// Lock the specials to prevent a special from being
   285  		// removed from the list while we're traversing it.
   286  		lock(&s.speciallock)
   287  
   288  		for sp := s.specials; sp != nil; sp = sp.next {
   289  			if sp.kind != _KindSpecialFinalizer {
   290  				continue
   291  			}
   292  			// don't mark finalized object, but scan it so we
   293  			// retain everything it points to.
   294  			spf := (*specialfinalizer)(unsafe.Pointer(sp))
   295  			// A finalizer can be set for an inner byte of an object, find object beginning.
   296  			p := uintptr(s.start<<_PageShift) + uintptr(spf.special.offset)/s.elemsize*s.elemsize
   297  
   298  			// Mark everything that can be reached from
   299  			// the object (but *not* the object itself or
   300  			// we'll never collect it).
   301  			scanobject(p, gcw)
   302  
   303  			// The special itself is a root.
   304  			scanblock(uintptr(unsafe.Pointer(&spf.fn)), sys.PtrSize, &oneptrmask[0], gcw)
   305  		}
   306  
   307  		unlock(&s.speciallock)
   308  	}
   309  }
   310  
   311  // gcAssistAlloc performs GC work to make gp's assist debt positive.
   312  // gp must be the calling user gorountine.
   313  //
   314  // This must be called with preemption enabled.
   315  //go:nowritebarrier
   316  func gcAssistAlloc(gp *g) {
   317  	// Don't assist in non-preemptible contexts. These are
   318  	// generally fragile and won't allow the assist to block.
   319  	if getg() == gp.m.g0 {
   320  		return
   321  	}
   322  	if mp := getg().m; mp.locks > 0 || mp.preemptoff != "" {
   323  		return
   324  	}
   325  
   326  	// Compute the amount of scan work we need to do to make the
   327  	// balance positive. We over-assist to build up credit for
   328  	// future allocations and amortize the cost of assisting.
   329  	debtBytes := -gp.gcAssistBytes + gcOverAssistBytes
   330  	scanWork := int64(gcController.assistWorkPerByte * float64(debtBytes))
   331  
   332  retry:
   333  	// Steal as much credit as we can from the background GC's
   334  	// scan credit. This is racy and may drop the background
   335  	// credit below 0 if two mutators steal at the same time. This
   336  	// will just cause steals to fail until credit is accumulated
   337  	// again, so in the long run it doesn't really matter, but we
   338  	// do have to handle the negative credit case.
   339  	bgScanCredit := atomic.Loadint64(&gcController.bgScanCredit)
   340  	stolen := int64(0)
   341  	if bgScanCredit > 0 {
   342  		if bgScanCredit < scanWork {
   343  			stolen = bgScanCredit
   344  			gp.gcAssistBytes += 1 + int64(gcController.assistBytesPerWork*float64(stolen))
   345  		} else {
   346  			stolen = scanWork
   347  			gp.gcAssistBytes += debtBytes
   348  		}
   349  		atomic.Xaddint64(&gcController.bgScanCredit, -stolen)
   350  
   351  		scanWork -= stolen
   352  
   353  		if scanWork == 0 {
   354  			// We were able to steal all of the credit we
   355  			// needed.
   356  			return
   357  		}
   358  	}
   359  
   360  	// Perform assist work
   361  	completed := false
   362  	systemstack(func() {
   363  		if atomic.Load(&gcBlackenEnabled) == 0 {
   364  			// The gcBlackenEnabled check in malloc races with the
   365  			// store that clears it but an atomic check in every malloc
   366  			// would be a performance hit.
   367  			// Instead we recheck it here on the non-preemptable system
   368  			// stack to determine if we should preform an assist.
   369  
   370  			// GC is done, so ignore any remaining debt.
   371  			gp.gcAssistBytes = 0
   372  			return
   373  		}
   374  		// Track time spent in this assist. Since we're on the
   375  		// system stack, this is non-preemptible, so we can
   376  		// just measure start and end time.
   377  		startTime := nanotime()
   378  
   379  		decnwait := atomic.Xadd(&work.nwait, -1)
   380  		if decnwait == work.nproc {
   381  			println("runtime: work.nwait =", decnwait, "work.nproc=", work.nproc)
   382  			throw("nwait > work.nprocs")
   383  		}
   384  
   385  		// drain own cached work first in the hopes that it
   386  		// will be more cache friendly.
   387  		gcw := &getg().m.p.ptr().gcw
   388  		workDone := gcDrainN(gcw, scanWork)
   389  		// If we are near the end of the mark phase
   390  		// dispose of the gcw.
   391  		if gcBlackenPromptly {
   392  			gcw.dispose()
   393  		}
   394  
   395  		// Record that we did this much scan work.
   396  		//
   397  		// Back out the number of bytes of assist credit that
   398  		// this scan work counts for. The "1+" is a poor man's
   399  		// round-up, to ensure this adds credit even if
   400  		// assistBytesPerWork is very low.
   401  		gp.gcAssistBytes += 1 + int64(gcController.assistBytesPerWork*float64(workDone))
   402  
   403  		// If this is the last worker and we ran out of work,
   404  		// signal a completion point.
   405  		incnwait := atomic.Xadd(&work.nwait, +1)
   406  		if incnwait > work.nproc {
   407  			println("runtime: work.nwait=", incnwait,
   408  				"work.nproc=", work.nproc,
   409  				"gcBlackenPromptly=", gcBlackenPromptly)
   410  			throw("work.nwait > work.nproc")
   411  		}
   412  
   413  		if incnwait == work.nproc && !gcMarkWorkAvailable(nil) {
   414  			// This has reached a background completion
   415  			// point.
   416  			completed = true
   417  		}
   418  		duration := nanotime() - startTime
   419  		_p_ := gp.m.p.ptr()
   420  		_p_.gcAssistTime += duration
   421  		if _p_.gcAssistTime > gcAssistTimeSlack {
   422  			atomic.Xaddint64(&gcController.assistTime, _p_.gcAssistTime)
   423  			_p_.gcAssistTime = 0
   424  		}
   425  	})
   426  
   427  	if completed {
   428  		gcMarkDone()
   429  	}
   430  
   431  	if gp.gcAssistBytes < 0 {
   432  		// We were unable steal enough credit or perform
   433  		// enough work to pay off the assist debt. We need to
   434  		// do one of these before letting the mutator allocate
   435  		// more to prevent over-allocation.
   436  		//
   437  		// If this is because we were preempted, reschedule
   438  		// and try some more.
   439  		if gp.preempt {
   440  			Gosched()
   441  			goto retry
   442  		}
   443  
   444  		// Add this G to an assist queue and park. When the GC
   445  		// has more background credit, it will satisfy queued
   446  		// assists before flushing to the global credit pool.
   447  		//
   448  		// Note that this does *not* get woken up when more
   449  		// work is added to the work list. The theory is that
   450  		// there wasn't enough work to do anyway, so we might
   451  		// as well let background marking take care of the
   452  		// work that is available.
   453  		lock(&work.assistQueue.lock)
   454  
   455  		// If the GC cycle is over, just return. This is the
   456  		// likely path if we completed above. We do this
   457  		// under the lock to prevent a GC cycle from ending
   458  		// between this check and queuing the assist.
   459  		if atomic.Load(&gcBlackenEnabled) == 0 {
   460  			unlock(&work.assistQueue.lock)
   461  			return
   462  		}
   463  
   464  		oldHead, oldTail := work.assistQueue.head, work.assistQueue.tail
   465  		if oldHead == 0 {
   466  			work.assistQueue.head.set(gp)
   467  		} else {
   468  			oldTail.ptr().schedlink.set(gp)
   469  		}
   470  		work.assistQueue.tail.set(gp)
   471  		gp.schedlink.set(nil)
   472  		// Recheck for background credit now that this G is in
   473  		// the queue, but can still back out. This avoids a
   474  		// race in case background marking has flushed more
   475  		// credit since we checked above.
   476  		if atomic.Loadint64(&gcController.bgScanCredit) > 0 {
   477  			work.assistQueue.head = oldHead
   478  			work.assistQueue.tail = oldTail
   479  			if oldTail != 0 {
   480  				oldTail.ptr().schedlink.set(nil)
   481  			}
   482  			unlock(&work.assistQueue.lock)
   483  			goto retry
   484  		}
   485  		// Park for real.
   486  		goparkunlock(&work.assistQueue.lock, "GC assist wait", traceEvGoBlock, 2)
   487  
   488  		// At this point either background GC has satisfied
   489  		// this G's assist debt, or the GC cycle is over.
   490  	}
   491  }
   492  
   493  // gcWakeAllAssists wakes all currently blocked assists. This is used
   494  // at the end of a GC cycle.
   495  func gcWakeAllAssists() {
   496  	lock(&work.assistQueue.lock)
   497  	injectglist(work.assistQueue.head.ptr())
   498  	work.assistQueue.head.set(nil)
   499  	work.assistQueue.tail.set(nil)
   500  	unlock(&work.assistQueue.lock)
   501  }
   502  
   503  // gcFlushBgCredit flushes scanWork units of background scan work
   504  // credit. This first satisfies blocked assists on the
   505  // work.assistQueue and then flushes any remaining credit to
   506  // gcController.bgScanCredit.
   507  //
   508  // Write barriers are disallowed because this is used by gcDrain after
   509  // it has ensured that all work is drained and this must preserve that
   510  // condition.
   511  //
   512  //go:nowritebarrierrec
   513  func gcFlushBgCredit(scanWork int64) {
   514  	if work.assistQueue.head == 0 {
   515  		// Fast path; there are no blocked assists. There's a
   516  		// small window here where an assist may add itself to
   517  		// the blocked queue and park. If that happens, we'll
   518  		// just get it on the next flush.
   519  		atomic.Xaddint64(&gcController.bgScanCredit, scanWork)
   520  		return
   521  	}
   522  
   523  	scanBytes := int64(float64(scanWork) * gcController.assistBytesPerWork)
   524  
   525  	lock(&work.assistQueue.lock)
   526  	gp := work.assistQueue.head.ptr()
   527  	for gp != nil && scanBytes > 0 {
   528  		// Note that gp.gcAssistBytes is negative because gp
   529  		// is in debt. Think carefully about the signs below.
   530  		if scanBytes+gp.gcAssistBytes >= 0 {
   531  			// Satisfy this entire assist debt.
   532  			scanBytes += gp.gcAssistBytes
   533  			gp.gcAssistBytes = 0
   534  			xgp := gp
   535  			gp = gp.schedlink.ptr()
   536  			ready(xgp, 0)
   537  		} else {
   538  			// Partially satisfy this assist.
   539  			gp.gcAssistBytes += scanBytes
   540  			scanBytes = 0
   541  			// As a heuristic, we move this assist to the
   542  			// back of the queue so that large assists
   543  			// can't clog up the assist queue and
   544  			// substantially delay small assists.
   545  			xgp := gp
   546  			gp = gp.schedlink.ptr()
   547  			if gp == nil {
   548  				// gp is the only assist in the queue.
   549  				gp = xgp
   550  			} else {
   551  				xgp.schedlink = 0
   552  				work.assistQueue.tail.ptr().schedlink.set(xgp)
   553  				work.assistQueue.tail.set(xgp)
   554  			}
   555  			break
   556  		}
   557  	}
   558  	work.assistQueue.head.set(gp)
   559  	if gp == nil {
   560  		work.assistQueue.tail.set(nil)
   561  	}
   562  
   563  	if scanBytes > 0 {
   564  		// Convert from scan bytes back to work.
   565  		scanWork = int64(float64(scanBytes) * gcController.assistWorkPerByte)
   566  		atomic.Xaddint64(&gcController.bgScanCredit, scanWork)
   567  	}
   568  	unlock(&work.assistQueue.lock)
   569  }
   570  
   571  //go:nowritebarrier
   572  func scanstack(gp *g) {
   573  	if gp.gcscanvalid {
   574  		if gcphase == _GCmarktermination {
   575  			gcRemoveStackBarriers(gp)
   576  		}
   577  		return
   578  	}
   579  
   580  	if readgstatus(gp)&_Gscan == 0 {
   581  		print("runtime:scanstack: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", hex(readgstatus(gp)), "\n")
   582  		throw("scanstack - bad status")
   583  	}
   584  
   585  	switch readgstatus(gp) &^ _Gscan {
   586  	default:
   587  		print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
   588  		throw("mark - bad status")
   589  	case _Gdead:
   590  		return
   591  	case _Grunning:
   592  		print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->atomicstatus=", readgstatus(gp), "\n")
   593  		throw("scanstack: goroutine not stopped")
   594  	case _Grunnable, _Gsyscall, _Gwaiting:
   595  		// ok
   596  	}
   597  
   598  	if gp == getg() {
   599  		throw("can't scan our own stack")
   600  	}
   601  	mp := gp.m
   602  	if mp != nil && mp.helpgc != 0 {
   603  		throw("can't scan gchelper stack")
   604  	}
   605  
   606  	var sp, barrierOffset, nextBarrier uintptr
   607  	if gp.syscallsp != 0 {
   608  		sp = gp.syscallsp
   609  	} else {
   610  		sp = gp.sched.sp
   611  	}
   612  	switch gcphase {
   613  	case _GCmark:
   614  		// Install stack barriers during stack scan.
   615  		barrierOffset = uintptr(firstStackBarrierOffset)
   616  		nextBarrier = sp + barrierOffset
   617  
   618  		if debug.gcstackbarrieroff > 0 {
   619  			nextBarrier = ^uintptr(0)
   620  		}
   621  
   622  		if gp.stkbarPos != 0 || len(gp.stkbar) != 0 {
   623  			// If this happens, it's probably because we
   624  			// scanned a stack twice in the same phase.
   625  			print("stkbarPos=", gp.stkbarPos, " len(stkbar)=", len(gp.stkbar), " goid=", gp.goid, " gcphase=", gcphase, "\n")
   626  			throw("g already has stack barriers")
   627  		}
   628  
   629  		gcLockStackBarriers(gp)
   630  
   631  	case _GCmarktermination:
   632  		if int(gp.stkbarPos) == len(gp.stkbar) {
   633  			// gp hit all of the stack barriers (or there
   634  			// were none). Re-scan the whole stack.
   635  			nextBarrier = ^uintptr(0)
   636  		} else {
   637  			// Only re-scan up to the lowest un-hit
   638  			// barrier. Any frames above this have not
   639  			// executed since the concurrent scan of gp and
   640  			// any writes through up-pointers to above
   641  			// this barrier had write barriers.
   642  			nextBarrier = gp.stkbar[gp.stkbarPos].savedLRPtr
   643  			if debugStackBarrier {
   644  				print("rescan below ", hex(nextBarrier), " in [", hex(sp), ",", hex(gp.stack.hi), ") goid=", gp.goid, "\n")
   645  			}
   646  		}
   647  
   648  		gcRemoveStackBarriers(gp)
   649  
   650  	default:
   651  		throw("scanstack in wrong phase")
   652  	}
   653  
   654  	var cache pcvalueCache
   655  	gcw := &getg().m.p.ptr().gcw
   656  	n := 0
   657  	scanframe := func(frame *stkframe, unused unsafe.Pointer) bool {
   658  		scanframeworker(frame, &cache, gcw)
   659  
   660  		if frame.fp > nextBarrier {
   661  			// We skip installing a barrier on bottom-most
   662  			// frame because on LR machines this LR is not
   663  			// on the stack.
   664  			if gcphase == _GCmark && n != 0 {
   665  				if gcInstallStackBarrier(gp, frame) {
   666  					barrierOffset *= 2
   667  					nextBarrier = sp + barrierOffset
   668  				}
   669  			} else if gcphase == _GCmarktermination {
   670  				// We just scanned a frame containing
   671  				// a return to a stack barrier. Since
   672  				// this frame never returned, we can
   673  				// stop scanning.
   674  				return false
   675  			}
   676  		}
   677  		n++
   678  
   679  		return true
   680  	}
   681  	gentraceback(^uintptr(0), ^uintptr(0), 0, gp, 0, nil, 0x7fffffff, scanframe, nil, 0)
   682  	tracebackdefers(gp, scanframe, nil)
   683  	if gcphase == _GCmarktermination {
   684  		gcw.dispose()
   685  	}
   686  	if gcphase == _GCmark {
   687  		gcUnlockStackBarriers(gp)
   688  	}
   689  	gp.gcscanvalid = true
   690  }
   691  
   692  // Scan a stack frame: local variables and function arguments/results.
   693  //go:nowritebarrier
   694  func scanframeworker(frame *stkframe, cache *pcvalueCache, gcw *gcWork) {
   695  
   696  	f := frame.fn
   697  	targetpc := frame.continpc
   698  	if targetpc == 0 {
   699  		// Frame is dead.
   700  		return
   701  	}
   702  	if _DebugGC > 1 {
   703  		print("scanframe ", funcname(f), "\n")
   704  	}
   705  	if targetpc != f.entry {
   706  		targetpc--
   707  	}
   708  	pcdata := pcdatavalue(f, _PCDATA_StackMapIndex, targetpc, cache)
   709  	if pcdata == -1 {
   710  		// We do not have a valid pcdata value but there might be a
   711  		// stackmap for this function.  It is likely that we are looking
   712  		// at the function prologue, assume so and hope for the best.
   713  		pcdata = 0
   714  	}
   715  
   716  	// Scan local variables if stack frame has been allocated.
   717  	size := frame.varp - frame.sp
   718  	var minsize uintptr
   719  	switch sys.TheChar {
   720  	case '7':
   721  		minsize = sys.SpAlign
   722  	default:
   723  		minsize = sys.MinFrameSize
   724  	}
   725  	if size > minsize {
   726  		stkmap := (*stackmap)(funcdata(f, _FUNCDATA_LocalsPointerMaps))
   727  		if stkmap == nil || stkmap.n <= 0 {
   728  			print("runtime: frame ", funcname(f), " untyped locals ", hex(frame.varp-size), "+", hex(size), "\n")
   729  			throw("missing stackmap")
   730  		}
   731  
   732  		// Locals bitmap information, scan just the pointers in locals.
   733  		if pcdata < 0 || pcdata >= stkmap.n {
   734  			// don't know where we are
   735  			print("runtime: pcdata is ", pcdata, " and ", stkmap.n, " locals stack map entries for ", funcname(f), " (targetpc=", targetpc, ")\n")
   736  			throw("scanframe: bad symbol table")
   737  		}
   738  		bv := stackmapdata(stkmap, pcdata)
   739  		size = uintptr(bv.n) * sys.PtrSize
   740  		scanblock(frame.varp-size, size, bv.bytedata, gcw)
   741  	}
   742  
   743  	// Scan arguments.
   744  	if frame.arglen > 0 {
   745  		var bv bitvector
   746  		if frame.argmap != nil {
   747  			bv = *frame.argmap
   748  		} else {
   749  			stkmap := (*stackmap)(funcdata(f, _FUNCDATA_ArgsPointerMaps))
   750  			if stkmap == nil || stkmap.n <= 0 {
   751  				print("runtime: frame ", funcname(f), " untyped args ", hex(frame.argp), "+", hex(frame.arglen), "\n")
   752  				throw("missing stackmap")
   753  			}
   754  			if pcdata < 0 || pcdata >= stkmap.n {
   755  				// don't know where we are
   756  				print("runtime: pcdata is ", pcdata, " and ", stkmap.n, " args stack map entries for ", funcname(f), " (targetpc=", targetpc, ")\n")
   757  				throw("scanframe: bad symbol table")
   758  			}
   759  			bv = stackmapdata(stkmap, pcdata)
   760  		}
   761  		scanblock(frame.argp, uintptr(bv.n)*sys.PtrSize, bv.bytedata, gcw)
   762  	}
   763  }
   764  
   765  type gcDrainFlags int
   766  
   767  const (
   768  	gcDrainUntilPreempt gcDrainFlags = 1 << iota
   769  	gcDrainNoBlock
   770  	gcDrainFlushBgCredit
   771  
   772  	// gcDrainBlock means neither gcDrainUntilPreempt or
   773  	// gcDrainNoBlock. It is the default, but callers should use
   774  	// the constant for documentation purposes.
   775  	gcDrainBlock gcDrainFlags = 0
   776  )
   777  
   778  // gcDrain scans roots and objects in work buffers, blackening grey
   779  // objects until all roots and work buffers have been drained.
   780  //
   781  // If flags&gcDrainUntilPreempt != 0, gcDrain returns when g.preempt
   782  // is set. This implies gcDrainNoBlock.
   783  //
   784  // If flags&gcDrainNoBlock != 0, gcDrain returns as soon as it is
   785  // unable to get more work. Otherwise, it will block until all
   786  // blocking calls are blocked in gcDrain.
   787  //
   788  // If flags&gcDrainFlushBgCredit != 0, gcDrain flushes scan work
   789  // credit to gcController.bgScanCredit every gcCreditSlack units of
   790  // scan work.
   791  //
   792  //go:nowritebarrier
   793  func gcDrain(gcw *gcWork, flags gcDrainFlags) {
   794  	if !writeBarrier.needed {
   795  		throw("gcDrain phase incorrect")
   796  	}
   797  
   798  	gp := getg()
   799  	preemtible := flags&gcDrainUntilPreempt != 0
   800  	blocking := flags&(gcDrainUntilPreempt|gcDrainNoBlock) == 0
   801  	flushBgCredit := flags&gcDrainFlushBgCredit != 0
   802  
   803  	// Drain root marking jobs.
   804  	if work.markrootNext < work.markrootJobs {
   805  		for blocking || !gp.preempt {
   806  			job := atomic.Xadd(&work.markrootNext, +1) - 1
   807  			if job >= work.markrootJobs {
   808  				break
   809  			}
   810  			// TODO: Pass in gcw.
   811  			markroot(job)
   812  		}
   813  	}
   814  
   815  	initScanWork := gcw.scanWork
   816  
   817  	// Drain heap marking jobs.
   818  	for !(preemtible && gp.preempt) {
   819  		// If another proc wants a pointer, give it some.
   820  		if work.nwait > 0 && work.full == 0 {
   821  			gcw.balance()
   822  		}
   823  
   824  		var b uintptr
   825  		if blocking {
   826  			b = gcw.get()
   827  		} else {
   828  			b = gcw.tryGet()
   829  		}
   830  		if b == 0 {
   831  			// work barrier reached or tryGet failed.
   832  			break
   833  		}
   834  		// If the current wbuf is filled by the scan a new wbuf might be
   835  		// returned that could possibly hold only a single object. This
   836  		// could result in each iteration draining only a single object
   837  		// out of the wbuf passed in + a single object placed
   838  		// into an empty wbuf in scanobject so there could be
   839  		// a performance hit as we keep fetching fresh wbufs.
   840  		scanobject(b, gcw)
   841  
   842  		// Flush background scan work credit to the global
   843  		// account if we've accumulated enough locally so
   844  		// mutator assists can draw on it.
   845  		if gcw.scanWork >= gcCreditSlack {
   846  			atomic.Xaddint64(&gcController.scanWork, gcw.scanWork)
   847  			if flushBgCredit {
   848  				gcFlushBgCredit(gcw.scanWork - initScanWork)
   849  				initScanWork = 0
   850  			}
   851  			gcw.scanWork = 0
   852  		}
   853  	}
   854  
   855  	// In blocking mode, write barriers are not allowed after this
   856  	// point because we must preserve the condition that the work
   857  	// buffers are empty.
   858  
   859  	// Flush remaining scan work credit.
   860  	if gcw.scanWork > 0 {
   861  		atomic.Xaddint64(&gcController.scanWork, gcw.scanWork)
   862  		if flushBgCredit {
   863  			gcFlushBgCredit(gcw.scanWork - initScanWork)
   864  		}
   865  		gcw.scanWork = 0
   866  	}
   867  }
   868  
   869  // gcDrainN blackens grey objects until it has performed roughly
   870  // scanWork units of scan work or the G is preempted. This is
   871  // best-effort, so it may perform less work if it fails to get a work
   872  // buffer. Otherwise, it will perform at least n units of work, but
   873  // may perform more because scanning is always done in whole object
   874  // increments. It returns the amount of scan work performed.
   875  //go:nowritebarrier
   876  func gcDrainN(gcw *gcWork, scanWork int64) int64 {
   877  	if !writeBarrier.needed {
   878  		throw("gcDrainN phase incorrect")
   879  	}
   880  
   881  	// There may already be scan work on the gcw, which we don't
   882  	// want to claim was done by this call.
   883  	workFlushed := -gcw.scanWork
   884  
   885  	gp := getg().m.curg
   886  	for !gp.preempt && workFlushed+gcw.scanWork < scanWork {
   887  		// This might be a good place to add prefetch code...
   888  		// if(wbuf.nobj > 4) {
   889  		//         PREFETCH(wbuf->obj[wbuf.nobj - 3];
   890  		//  }
   891  		b := gcw.tryGet()
   892  		if b == 0 {
   893  			break
   894  		}
   895  		scanobject(b, gcw)
   896  
   897  		// Flush background scan work credit.
   898  		if gcw.scanWork >= gcCreditSlack {
   899  			atomic.Xaddint64(&gcController.scanWork, gcw.scanWork)
   900  			workFlushed += gcw.scanWork
   901  			gcw.scanWork = 0
   902  		}
   903  	}
   904  
   905  	// Unlike gcDrain, there's no need to flush remaining work
   906  	// here because this never flushes to bgScanCredit and
   907  	// gcw.dispose will flush any remaining work to scanWork.
   908  
   909  	return workFlushed + gcw.scanWork
   910  }
   911  
   912  // scanblock scans b as scanobject would, but using an explicit
   913  // pointer bitmap instead of the heap bitmap.
   914  //
   915  // This is used to scan non-heap roots, so it does not update
   916  // gcw.bytesMarked or gcw.scanWork.
   917  //
   918  //go:nowritebarrier
   919  func scanblock(b0, n0 uintptr, ptrmask *uint8, gcw *gcWork) {
   920  	// Use local copies of original parameters, so that a stack trace
   921  	// due to one of the throws below shows the original block
   922  	// base and extent.
   923  	b := b0
   924  	n := n0
   925  
   926  	arena_start := mheap_.arena_start
   927  	arena_used := mheap_.arena_used
   928  
   929  	for i := uintptr(0); i < n; {
   930  		// Find bits for the next word.
   931  		bits := uint32(*addb(ptrmask, i/(sys.PtrSize*8)))
   932  		if bits == 0 {
   933  			i += sys.PtrSize * 8
   934  			continue
   935  		}
   936  		for j := 0; j < 8 && i < n; j++ {
   937  			if bits&1 != 0 {
   938  				// Same work as in scanobject; see comments there.
   939  				obj := *(*uintptr)(unsafe.Pointer(b + i))
   940  				if obj != 0 && arena_start <= obj && obj < arena_used {
   941  					if obj, hbits, span := heapBitsForObject(obj, b, i); obj != 0 {
   942  						greyobject(obj, b, i, hbits, span, gcw)
   943  					}
   944  				}
   945  			}
   946  			bits >>= 1
   947  			i += sys.PtrSize
   948  		}
   949  	}
   950  }
   951  
   952  // scanobject scans the object starting at b, adding pointers to gcw.
   953  // b must point to the beginning of a heap object; scanobject consults
   954  // the GC bitmap for the pointer mask and the spans for the size of the
   955  // object (it ignores n).
   956  //go:nowritebarrier
   957  func scanobject(b uintptr, gcw *gcWork) {
   958  	// Note that arena_used may change concurrently during
   959  	// scanobject and hence scanobject may encounter a pointer to
   960  	// a newly allocated heap object that is *not* in
   961  	// [start,used). It will not mark this object; however, we
   962  	// know that it was just installed by a mutator, which means
   963  	// that mutator will execute a write barrier and take care of
   964  	// marking it. This is even more pronounced on relaxed memory
   965  	// architectures since we access arena_used without barriers
   966  	// or synchronization, but the same logic applies.
   967  	arena_start := mheap_.arena_start
   968  	arena_used := mheap_.arena_used
   969  
   970  	// Find bits of the beginning of the object.
   971  	// b must point to the beginning of a heap object, so
   972  	// we can get its bits and span directly.
   973  	hbits := heapBitsForAddr(b)
   974  	s := spanOfUnchecked(b)
   975  	n := s.elemsize
   976  	if n == 0 {
   977  		throw("scanobject n == 0")
   978  	}
   979  
   980  	var i uintptr
   981  	for i = 0; i < n; i += sys.PtrSize {
   982  		// Find bits for this word.
   983  		if i != 0 {
   984  			// Avoid needless hbits.next() on last iteration.
   985  			hbits = hbits.next()
   986  		}
   987  		// During checkmarking, 1-word objects store the checkmark
   988  		// in the type bit for the one word. The only one-word objects
   989  		// are pointers, or else they'd be merged with other non-pointer
   990  		// data into larger allocations.
   991  		bits := hbits.bits()
   992  		if i >= 2*sys.PtrSize && bits&bitMarked == 0 {
   993  			break // no more pointers in this object
   994  		}
   995  		if bits&bitPointer == 0 {
   996  			continue // not a pointer
   997  		}
   998  
   999  		// Work here is duplicated in scanblock and above.
  1000  		// If you make changes here, make changes there too.
  1001  		obj := *(*uintptr)(unsafe.Pointer(b + i))
  1002  
  1003  		// At this point we have extracted the next potential pointer.
  1004  		// Check if it points into heap and not back at the current object.
  1005  		if obj != 0 && arena_start <= obj && obj < arena_used && obj-b >= n {
  1006  			// Mark the object.
  1007  			if obj, hbits, span := heapBitsForObject(obj, b, i); obj != 0 {
  1008  				greyobject(obj, b, i, hbits, span, gcw)
  1009  			}
  1010  		}
  1011  	}
  1012  	gcw.bytesMarked += uint64(n)
  1013  	gcw.scanWork += int64(i)
  1014  }
  1015  
  1016  // Shade the object if it isn't already.
  1017  // The object is not nil and known to be in the heap.
  1018  // Preemption must be disabled.
  1019  //go:nowritebarrier
  1020  func shade(b uintptr) {
  1021  	if obj, hbits, span := heapBitsForObject(b, 0, 0); obj != 0 {
  1022  		gcw := &getg().m.p.ptr().gcw
  1023  		greyobject(obj, 0, 0, hbits, span, gcw)
  1024  		if gcphase == _GCmarktermination || gcBlackenPromptly {
  1025  			// Ps aren't allowed to cache work during mark
  1026  			// termination.
  1027  			gcw.dispose()
  1028  		}
  1029  	}
  1030  }
  1031  
  1032  // obj is the start of an object with mark mbits.
  1033  // If it isn't already marked, mark it and enqueue into gcw.
  1034  // base and off are for debugging only and could be removed.
  1035  //go:nowritebarrier
  1036  func greyobject(obj, base, off uintptr, hbits heapBits, span *mspan, gcw *gcWork) {
  1037  	// obj should be start of allocation, and so must be at least pointer-aligned.
  1038  	if obj&(sys.PtrSize-1) != 0 {
  1039  		throw("greyobject: obj not pointer-aligned")
  1040  	}
  1041  
  1042  	if useCheckmark {
  1043  		if !hbits.isMarked() {
  1044  			printlock()
  1045  			print("runtime:greyobject: checkmarks finds unexpected unmarked object obj=", hex(obj), "\n")
  1046  			print("runtime: found obj at *(", hex(base), "+", hex(off), ")\n")
  1047  
  1048  			// Dump the source (base) object
  1049  			gcDumpObject("base", base, off)
  1050  
  1051  			// Dump the object
  1052  			gcDumpObject("obj", obj, ^uintptr(0))
  1053  
  1054  			throw("checkmark found unmarked object")
  1055  		}
  1056  		if hbits.isCheckmarked(span.elemsize) {
  1057  			return
  1058  		}
  1059  		hbits.setCheckmarked(span.elemsize)
  1060  		if !hbits.isCheckmarked(span.elemsize) {
  1061  			throw("setCheckmarked and isCheckmarked disagree")
  1062  		}
  1063  	} else {
  1064  		// If marked we have nothing to do.
  1065  		if hbits.isMarked() {
  1066  			return
  1067  		}
  1068  		hbits.setMarked()
  1069  
  1070  		// If this is a noscan object, fast-track it to black
  1071  		// instead of greying it.
  1072  		if !hbits.hasPointers(span.elemsize) {
  1073  			gcw.bytesMarked += uint64(span.elemsize)
  1074  			return
  1075  		}
  1076  	}
  1077  
  1078  	// Queue the obj for scanning. The PREFETCH(obj) logic has been removed but
  1079  	// seems like a nice optimization that can be added back in.
  1080  	// There needs to be time between the PREFETCH and the use.
  1081  	// Previously we put the obj in an 8 element buffer that is drained at a rate
  1082  	// to give the PREFETCH time to do its work.
  1083  	// Use of PREFETCHNTA might be more appropriate than PREFETCH
  1084  
  1085  	gcw.put(obj)
  1086  }
  1087  
  1088  // gcDumpObject dumps the contents of obj for debugging and marks the
  1089  // field at byte offset off in obj.
  1090  func gcDumpObject(label string, obj, off uintptr) {
  1091  	if obj < mheap_.arena_start || obj >= mheap_.arena_used {
  1092  		print(label, "=", hex(obj), " is not in the Go heap\n")
  1093  		return
  1094  	}
  1095  	k := obj >> _PageShift
  1096  	x := k
  1097  	x -= mheap_.arena_start >> _PageShift
  1098  	s := h_spans[x]
  1099  	print(label, "=", hex(obj), " k=", hex(k))
  1100  	if s == nil {
  1101  		print(" s=nil\n")
  1102  		return
  1103  	}
  1104  	print(" s.start*_PageSize=", hex(s.start*_PageSize), " s.limit=", hex(s.limit), " s.sizeclass=", s.sizeclass, " s.elemsize=", s.elemsize, "\n")
  1105  	skipped := false
  1106  	for i := uintptr(0); i < s.elemsize; i += sys.PtrSize {
  1107  		// For big objects, just print the beginning (because
  1108  		// that usually hints at the object's type) and the
  1109  		// fields around off.
  1110  		if !(i < 128*sys.PtrSize || off-16*sys.PtrSize < i && i < off+16*sys.PtrSize) {
  1111  			skipped = true
  1112  			continue
  1113  		}
  1114  		if skipped {
  1115  			print(" ...\n")
  1116  			skipped = false
  1117  		}
  1118  		print(" *(", label, "+", i, ") = ", hex(*(*uintptr)(unsafe.Pointer(obj + uintptr(i)))))
  1119  		if i == off {
  1120  			print(" <==")
  1121  		}
  1122  		print("\n")
  1123  	}
  1124  	if skipped {
  1125  		print(" ...\n")
  1126  	}
  1127  }
  1128  
  1129  // If gcBlackenPromptly is true we are in the second mark phase phase so we allocate black.
  1130  //go:nowritebarrier
  1131  func gcmarknewobject_m(obj, size uintptr) {
  1132  	if useCheckmark && !gcBlackenPromptly { // The world should be stopped so this should not happen.
  1133  		throw("gcmarknewobject called while doing checkmark")
  1134  	}
  1135  	heapBitsForAddr(obj).setMarked()
  1136  	atomic.Xadd64(&work.bytesMarked, int64(size))
  1137  }
  1138  
  1139  // Checkmarking
  1140  
  1141  // To help debug the concurrent GC we remark with the world
  1142  // stopped ensuring that any object encountered has their normal
  1143  // mark bit set. To do this we use an orthogonal bit
  1144  // pattern to indicate the object is marked. The following pattern
  1145  // uses the upper two bits in the object's boundary nibble.
  1146  // 01: scalar  not marked
  1147  // 10: pointer not marked
  1148  // 11: pointer     marked
  1149  // 00: scalar      marked
  1150  // Xoring with 01 will flip the pattern from marked to unmarked and vica versa.
  1151  // The higher bit is 1 for pointers and 0 for scalars, whether the object
  1152  // is marked or not.
  1153  // The first nibble no longer holds the typeDead pattern indicating that the
  1154  // there are no more pointers in the object. This information is held
  1155  // in the second nibble.
  1156  
  1157  // If useCheckmark is true, marking of an object uses the
  1158  // checkmark bits (encoding above) instead of the standard
  1159  // mark bits.
  1160  var useCheckmark = false
  1161  
  1162  //go:nowritebarrier
  1163  func initCheckmarks() {
  1164  	useCheckmark = true
  1165  	for _, s := range work.spans {
  1166  		if s.state == _MSpanInUse {
  1167  			heapBitsForSpan(s.base()).initCheckmarkSpan(s.layout())
  1168  		}
  1169  	}
  1170  }
  1171  
  1172  func clearCheckmarks() {
  1173  	useCheckmark = false
  1174  	for _, s := range work.spans {
  1175  		if s.state == _MSpanInUse {
  1176  			heapBitsForSpan(s.base()).clearCheckmarkSpan(s.layout())
  1177  		}
  1178  	}
  1179  }