runtime: add MemStats.Mallocs and Frees

This commit is contained in:
Damian Gryski
2022-08-09 22:52:24 -07:00
committed by Ron Evans
parent a87e5cdbf0
commit 697e8c725b
6 changed files with 25 additions and 1 deletions
+5 -1
View File
@@ -55,7 +55,9 @@ var (
metadataStart unsafe.Pointer // pointer to the start of the heap metadata
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 // for runtime.MemStats
gcTotalAlloc uint64 // total number of bytes allocated
gcMallocs uint64 // total number of allocations
gcFrees uint64 // total number of objects freed
)
// zeroSizedAlloc is just a sentinel that gets returned when allocating 0 bytes.
@@ -268,6 +270,7 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
}
gcTotalAlloc += uint64(size)
gcMallocs++
neededBlocks := (size + (bytesPerBlock - 1)) / bytesPerBlock
@@ -573,6 +576,7 @@ func sweep() {
// Unmarked head. Free it, including all tail blocks following it.
block.markFree()
freeCurrentObject = true
gcFrees++
case blockStateTail:
if freeCurrentObject {
// This is a tail object following an unmarked head.
+7
View File
@@ -19,6 +19,12 @@ var heapptr = heapStart
// Total amount allocated for runtime.MemStats
var gcTotalAlloc uint64
// Total number of calls to alloc()
var gcMallocs uint64
// Total number of objected freed; for leaking collector this stays 0
const gcFrees = 0
// Inlining alloc() speeds things up slightly but bloats the executable by 50%,
// see https://github.com/tinygo-org/tinygo/issues/2674. So don't.
//
@@ -30,6 +36,7 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
size = align(size)
addr := heapptr
gcTotalAlloc += uint64(size)
gcMallocs++
heapptr += size
for heapptr >= heapEnd {
// Try to increase the heap and check again.
+2
View File
@@ -14,6 +14,8 @@ import (
const gcAsserts = false // perform sanity checks
var gcTotalAlloc uint64 // for runtime.MemStats
var gcMallocs uint64
var gcFrees uint64
func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer
+7
View File
@@ -42,6 +42,13 @@ type MemStats struct {
// objects are freed.
TotalAlloc uint64
// Mallocs is the cumulative count of heap objects allocated.
// The number of live objects is Mallocs - Frees.
Mallocs uint64
// Frees is the cumulative count of heap objects freed.
Frees uint64
// Off-heap memory statistics.
//
// The following statistics measure runtime-internal
+2
View File
@@ -22,5 +22,7 @@ func ReadMemStats(m *MemStats) {
m.HeapSys = m.HeapInuse + m.HeapIdle
m.GCSys = uint64(heapEnd - uintptr(metadataStart))
m.TotalAlloc = gcTotalAlloc
m.Mallocs = gcMallocs
m.Frees = gcFrees
m.Sys = uint64(heapEnd - heapStart)
}
+2
View File
@@ -15,5 +15,7 @@ func ReadMemStats(m *MemStats) {
m.HeapSys = m.HeapInuse + m.HeapIdle
m.GCSys = 0
m.TotalAlloc = gcTotalAlloc
m.Mallocs = gcMallocs
m.Frees = gcFrees
m.Sys = uint64(heapEnd - heapStart)
}