Skip to content

Commit

Permalink
runtime: track Memstats.HeapAlloc for gc_blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dgryski committed Oct 8, 2024
1 parent c77ed8e commit fbb1251
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/runtime/gc_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ var (
nextAlloc gcBlock // the next block that should be tried by the allocator
endBlock gcBlock // the block just past the end of the available space
gcTotalAlloc uint64 // total number of bytes allocated
gcTotalBlocks uint64 // total number of allocated blocks
gcMallocs uint64 // total number of allocations
gcFrees uint64 // total number of objects freed
gcFreedBlocks uint64 // total number of freed blocks
)

// zeroSizedAlloc is just a sentinel that gets returned when allocating 0 bytes.
Expand Down Expand Up @@ -285,6 +287,7 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
gcMallocs++

neededBlocks := (size + (bytesPerBlock - 1)) / bytesPerBlock
gcTotalBlocks += uint64(neededBlocks)

// Continue looping until a run of free blocks has been found that fits the
// requested size.
Expand Down Expand Up @@ -619,20 +622,21 @@ func markRoot(addr, root uintptr) {
// It returns how many bytes are free in the heap after the sweep.
func sweep() (freeBytes uintptr) {
freeCurrentObject := false
var freed uint64
for block := gcBlock(0); block < endBlock; block++ {
switch block.state() {
case blockStateHead:
// Unmarked head. Free it, including all tail blocks following it.
block.markFree()
freeCurrentObject = true
gcFrees++
freeBytes += bytesPerBlock
freed++
case blockStateTail:
if freeCurrentObject {
// This is a tail object following an unmarked head.
// Free it now.
block.markFree()
freeBytes += bytesPerBlock
freed++
}
case blockStateMark:
// This is a marked object. The next tail blocks must not be freed,
Expand All @@ -644,6 +648,8 @@ func sweep() (freeBytes uintptr) {
freeBytes += bytesPerBlock
}
}
gcFreedBlocks += freed
freeBytes += uintptr(freed) * bytesPerBlock
return
}

Expand Down Expand Up @@ -690,6 +696,8 @@ func ReadMemStats(m *MemStats) {
m.Mallocs = gcMallocs
m.Frees = gcFrees
m.Sys = uint64(heapEnd - heapStart)
m.HeapAlloc = (gcTotalBlocks - gcFreedBlocks) * uint64(bytesPerBlock)
m.Alloc = m.HeapAlloc
}

func SetFinalizer(obj interface{}, finalizer interface{}) {
Expand Down

0 comments on commit fbb1251

Please sign in to comment.