mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 19:17:47 +00:00
runtime: add MemStats.TotalAlloc
This commit is contained in:
@@ -55,6 +55,7 @@ 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
|
||||
)
|
||||
|
||||
// zeroSizedAlloc is just a sentinel that gets returned when allocating 0 bytes.
|
||||
@@ -266,6 +267,8 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
|
||||
return unsafe.Pointer(&zeroSizedAlloc)
|
||||
}
|
||||
|
||||
gcTotalAlloc += uint64(size)
|
||||
|
||||
neededBlocks := (size + (bytesPerBlock - 1)) / bytesPerBlock
|
||||
|
||||
// Continue looping until a run of free blocks has been found that fits the
|
||||
|
||||
@@ -16,6 +16,9 @@ const gcAsserts = false // perform sanity checks
|
||||
// Ever-incrementing pointer: no memory is freed.
|
||||
var heapptr = heapStart
|
||||
|
||||
// Total amount allocated for runtime.MemStats
|
||||
var gcTotalAlloc uint64
|
||||
|
||||
// Inlining alloc() speeds things up slightly but bloats the executable by 50%,
|
||||
// see https://github.com/tinygo-org/tinygo/issues/2674. So don't.
|
||||
//
|
||||
@@ -26,6 +29,7 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
|
||||
// systems).
|
||||
size = align(size)
|
||||
addr := heapptr
|
||||
gcTotalAlloc += uint64(size)
|
||||
heapptr += size
|
||||
for heapptr >= heapEnd {
|
||||
// Try to increase the heap and check again.
|
||||
|
||||
@@ -35,6 +35,13 @@ type MemStats struct {
|
||||
// HeapReleased is bytes of physical memory returned to the OS.
|
||||
HeapReleased uint64
|
||||
|
||||
// TotalAlloc is cumulative bytes allocated for heap objects.
|
||||
//
|
||||
// TotalAlloc increases as heap objects are allocated, but
|
||||
// unlike Alloc and HeapAlloc, it does not decrease when
|
||||
// objects are freed.
|
||||
TotalAlloc uint64
|
||||
|
||||
// Off-heap memory statistics.
|
||||
//
|
||||
// The following statistics measure runtime-internal
|
||||
|
||||
@@ -21,5 +21,6 @@ func ReadMemStats(m *MemStats) {
|
||||
m.HeapReleased = 0 // always 0, we don't currently release memory back to the OS.
|
||||
m.HeapSys = m.HeapInuse + m.HeapIdle
|
||||
m.GCSys = uint64(heapEnd - uintptr(metadataStart))
|
||||
m.TotalAlloc = gcTotalAlloc
|
||||
m.Sys = uint64(heapEnd - heapStart)
|
||||
}
|
||||
|
||||
@@ -9,10 +9,11 @@ package runtime
|
||||
// call to ReadMemStats. This would not do GC implicitly for you.
|
||||
func ReadMemStats(m *MemStats) {
|
||||
m.HeapIdle = 0
|
||||
m.HeapInuse = uint64(heapptr - heapStart)
|
||||
m.HeapInuse = gcTotalAlloc
|
||||
m.HeapReleased = 0 // always 0, we don't currently release memory back to the OS.
|
||||
|
||||
m.HeapSys = m.HeapInuse + m.HeapIdle
|
||||
m.GCSys = 0
|
||||
m.TotalAlloc = gcTotalAlloc
|
||||
m.Sys = uint64(heapEnd - heapStart)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user