Source file src/runtime/mgcmark_nogreenteagc.go

     1  // Copyright 2025 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  //go:build !goexperiment.greenteagc
     6  
     7  package runtime
     8  
     9  import (
    10  	"internal/goarch"
    11  	"internal/runtime/gc"
    12  	"internal/runtime/sys"
    13  	"unsafe"
    14  )
    15  
    16  func (s *mspan) markBitsForIndex(objIndex uintptr) markBits {
    17  	bytep, mask := s.gcmarkBits.bitp(objIndex)
    18  	return markBits{bytep, mask, objIndex}
    19  }
    20  
    21  func (s *mspan) markBitsForBase() markBits {
    22  	return markBits{&s.gcmarkBits.x, uint8(1), 0}
    23  }
    24  
    25  func tryDeferToSpanScan(p uintptr, gcw *gcWork) bool {
    26  	return false
    27  }
    28  
    29  func (s *mspan) initInlineMarkBits() {
    30  }
    31  
    32  func (s *mspan) moveInlineMarks(to *gcBits) {
    33  	throw("unimplemented")
    34  }
    35  
    36  func gcUsesSpanInlineMarkBits(_ uintptr) bool {
    37  	return false
    38  }
    39  
    40  func (s *mspan) inlineMarkBits() *spanInlineMarkBits {
    41  	return nil
    42  }
    43  
    44  func (s *mspan) scannedBitsForIndex(objIndex uintptr) markBits {
    45  	throw("unimplemented")
    46  	return markBits{}
    47  }
    48  
    49  type spanInlineMarkBits struct {
    50  }
    51  
    52  func (q *spanInlineMarkBits) tryAcquire() bool {
    53  	return false
    54  }
    55  
    56  type spanQueue struct {
    57  }
    58  
    59  func (q *spanQueue) flush() {
    60  }
    61  
    62  func (q *spanQueue) empty() bool {
    63  	return true
    64  }
    65  
    66  func (q *spanQueue) destroy() {
    67  }
    68  
    69  type spanSPMC struct {
    70  	_ sys.NotInHeap
    71  }
    72  
    73  func freeDeadSpanSPMCs() {
    74  	return
    75  }
    76  
    77  type objptr uintptr
    78  
    79  func (w *gcWork) tryGetSpanFast() objptr {
    80  	return 0
    81  }
    82  
    83  func (w *gcWork) tryGetSpan() objptr {
    84  	return 0
    85  }
    86  
    87  func (w *gcWork) tryStealSpan() objptr {
    88  	return 0
    89  }
    90  
    91  func scanSpan(p objptr, gcw *gcWork) {
    92  	throw("unimplemented")
    93  }
    94  
    95  type sizeClassScanStats struct {
    96  	sparseObjsScanned uint64
    97  }
    98  
    99  func dumpScanStats() {
   100  	var sparseObjsScanned uint64
   101  	for _, stats := range memstats.lastScanStats {
   102  		sparseObjsScanned += stats.sparseObjsScanned
   103  	}
   104  	print("scan: total ", sparseObjsScanned, " objs\n")
   105  	for i, stats := range memstats.lastScanStats {
   106  		if stats == (sizeClassScanStats{}) {
   107  			continue
   108  		}
   109  		if i == 0 {
   110  			print("scan: class L ")
   111  		} else {
   112  			print("scan: class ", gc.SizeClassToSize[i], "B ")
   113  		}
   114  		print(stats.sparseObjsScanned, " objs\n")
   115  	}
   116  }
   117  
   118  func (w *gcWork) flushScanStats(dst *[gc.NumSizeClasses]sizeClassScanStats) {
   119  	for i := range w.stats {
   120  		dst[i].sparseObjsScanned += w.stats[i].sparseObjsScanned
   121  	}
   122  	clear(w.stats[:])
   123  }
   124  
   125  // gcMarkWorkAvailable reports whether there's any non-local work available to do.
   126  func gcMarkWorkAvailable() bool {
   127  	if !work.full.empty() {
   128  		return true // global work available
   129  	}
   130  	if work.markrootNext.Load() < work.markrootJobs.Load() {
   131  		return true // root scan work available
   132  	}
   133  	return false
   134  }
   135  
   136  // scanObject scans the object starting at b, adding pointers to gcw.
   137  // b must point to the beginning of a heap object or an oblet.
   138  // scanObject consults the GC bitmap for the pointer mask and the
   139  // spans for the size of the object.
   140  //
   141  //go:nowritebarrier
   142  func scanObject(b uintptr, gcw *gcWork) {
   143  	// Prefetch object before we scan it.
   144  	//
   145  	// This will overlap fetching the beginning of the object with initial
   146  	// setup before we start scanning the object.
   147  	sys.Prefetch(b)
   148  
   149  	// Find the bits for b and the size of the object at b.
   150  	//
   151  	// b is either the beginning of an object, in which case this
   152  	// is the size of the object to scan, or it points to an
   153  	// oblet, in which case we compute the size to scan below.
   154  	s := spanOfUnchecked(b)
   155  	n := s.elemsize
   156  	if n == 0 {
   157  		throw("scanObject n == 0")
   158  	}
   159  	if s.spanclass.noscan() {
   160  		// Correctness-wise this is ok, but it's inefficient
   161  		// if noscan objects reach here.
   162  		throw("scanObject of a noscan object")
   163  	}
   164  
   165  	var tp typePointers
   166  	if n > maxObletBytes {
   167  		// Large object. Break into oblets for better
   168  		// parallelism and lower latency.
   169  		if b == s.base() {
   170  			// Enqueue the other oblets to scan later.
   171  			// Some oblets may be in b's scalar tail, but
   172  			// these will be marked as "no more pointers",
   173  			// so we'll drop out immediately when we go to
   174  			// scan those.
   175  			for oblet := b + maxObletBytes; oblet < s.base()+s.elemsize; oblet += maxObletBytes {
   176  				if !gcw.putObjFast(oblet) {
   177  					gcw.putObj(oblet)
   178  				}
   179  			}
   180  		}
   181  
   182  		// Compute the size of the oblet. Since this object
   183  		// must be a large object, s.base() is the beginning
   184  		// of the object.
   185  		n = s.base() + s.elemsize - b
   186  		n = min(n, maxObletBytes)
   187  		tp = s.typePointersOfUnchecked(s.base())
   188  		tp = tp.fastForward(b-tp.addr, b+n)
   189  	} else {
   190  		tp = s.typePointersOfUnchecked(b)
   191  	}
   192  
   193  	var scanSize uintptr
   194  	for {
   195  		var addr uintptr
   196  		if tp, addr = tp.nextFast(); addr == 0 {
   197  			if tp, addr = tp.next(b + n); addr == 0 {
   198  				break
   199  			}
   200  		}
   201  
   202  		// Keep track of farthest pointer we found, so we can
   203  		// update heapScanWork. TODO: is there a better metric,
   204  		// now that we can skip scalar portions pretty efficiently?
   205  		scanSize = addr - b + goarch.PtrSize
   206  
   207  		// Work here is duplicated in scanblock and above.
   208  		// If you make changes here, make changes there too.
   209  		obj := *(*uintptr)(unsafe.Pointer(addr))
   210  
   211  		// At this point we have extracted the next potential pointer.
   212  		// Quickly filter out nil and pointers back to the current object.
   213  		if obj != 0 && obj-b >= n {
   214  			// Test if obj points into the Go heap and, if so,
   215  			// mark the object.
   216  			//
   217  			// Note that it's possible for findObject to
   218  			// fail if obj points to a just-allocated heap
   219  			// object because of a race with growing the
   220  			// heap. In this case, we know the object was
   221  			// just allocated and hence will be marked by
   222  			// allocation itself.
   223  			if !tryDeferToSpanScan(obj, gcw) {
   224  				if obj, span, objIndex := findObject(obj, b, addr-b); obj != 0 {
   225  					greyobject(obj, b, addr-b, span, gcw, objIndex)
   226  				}
   227  			}
   228  		}
   229  	}
   230  	gcw.bytesMarked += uint64(n)
   231  	gcw.heapScanWork += int64(scanSize)
   232  	if debug.gctrace > 1 {
   233  		gcw.stats[s.spanclass.sizeclass()].sparseObjsScanned++
   234  	}
   235  }
   236  

View as plain text