runtime: add MemStats.Mallocs and Frees

Этот коммит содержится в:
Damian Gryski 2022-08-09 22:52:24 -07:00 коммит произвёл Ron Evans
родитель a87e5cdbf0
коммит 697e8c725b
6 изменённых файлов: 25 добавлений и 1 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -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.

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -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)
}