runtime: add MemStats.TotalAlloc
Этот коммит содержится в:
родитель
b56baa7aad
коммит
a87e5cdbf0
5 изменённых файлов: 17 добавлений и 1 удалений
|
@ -55,6 +55,7 @@ var (
|
||||||
metadataStart unsafe.Pointer // pointer to the start of the heap metadata
|
metadataStart unsafe.Pointer // pointer to the start of the heap metadata
|
||||||
nextAlloc gcBlock // the next block that should be tried by the allocator
|
nextAlloc gcBlock // the next block that should be tried by the allocator
|
||||||
endBlock gcBlock // the block just past the end of the available space
|
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.
|
// 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)
|
return unsafe.Pointer(&zeroSizedAlloc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gcTotalAlloc += uint64(size)
|
||||||
|
|
||||||
neededBlocks := (size + (bytesPerBlock - 1)) / bytesPerBlock
|
neededBlocks := (size + (bytesPerBlock - 1)) / bytesPerBlock
|
||||||
|
|
||||||
// Continue looping until a run of free blocks has been found that fits the
|
// 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.
|
// Ever-incrementing pointer: no memory is freed.
|
||||||
var heapptr = heapStart
|
var heapptr = heapStart
|
||||||
|
|
||||||
|
// Total amount allocated for runtime.MemStats
|
||||||
|
var gcTotalAlloc uint64
|
||||||
|
|
||||||
// Inlining alloc() speeds things up slightly but bloats the executable by 50%,
|
// Inlining alloc() speeds things up slightly but bloats the executable by 50%,
|
||||||
// see https://github.com/tinygo-org/tinygo/issues/2674. So don't.
|
// 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).
|
// systems).
|
||||||
size = align(size)
|
size = align(size)
|
||||||
addr := heapptr
|
addr := heapptr
|
||||||
|
gcTotalAlloc += uint64(size)
|
||||||
heapptr += size
|
heapptr += size
|
||||||
for heapptr >= heapEnd {
|
for heapptr >= heapEnd {
|
||||||
// Try to increase the heap and check again.
|
// 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 is bytes of physical memory returned to the OS.
|
||||||
HeapReleased uint64
|
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.
|
// Off-heap memory statistics.
|
||||||
//
|
//
|
||||||
// The following statistics measure runtime-internal
|
// 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.HeapReleased = 0 // always 0, we don't currently release memory back to the OS.
|
||||||
m.HeapSys = m.HeapInuse + m.HeapIdle
|
m.HeapSys = m.HeapInuse + m.HeapIdle
|
||||||
m.GCSys = uint64(heapEnd - uintptr(metadataStart))
|
m.GCSys = uint64(heapEnd - uintptr(metadataStart))
|
||||||
|
m.TotalAlloc = gcTotalAlloc
|
||||||
m.Sys = uint64(heapEnd - heapStart)
|
m.Sys = uint64(heapEnd - heapStart)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,11 @@ package runtime
|
||||||
// call to ReadMemStats. This would not do GC implicitly for you.
|
// call to ReadMemStats. This would not do GC implicitly for you.
|
||||||
func ReadMemStats(m *MemStats) {
|
func ReadMemStats(m *MemStats) {
|
||||||
m.HeapIdle = 0
|
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.HeapReleased = 0 // always 0, we don't currently release memory back to the OS.
|
||||||
|
|
||||||
m.HeapSys = m.HeapInuse + m.HeapIdle
|
m.HeapSys = m.HeapInuse + m.HeapIdle
|
||||||
m.GCSys = 0
|
m.GCSys = 0
|
||||||
|
m.TotalAlloc = gcTotalAlloc
|
||||||
m.Sys = uint64(heapEnd - heapStart)
|
m.Sys = uint64(heapEnd - heapStart)
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче