From b56baa7aad93b4ead470b4207c45169fcc4c2c40 Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Tue, 9 Aug 2022 14:01:43 -0700 Subject: [PATCH] runtime: make MemStats available to leaking collector --- src/runtime/gc_none.go | 2 ++ src/runtime/mstats.go | 24 ------------------------ src/runtime/mstats_conservative.go | 25 +++++++++++++++++++++++++ src/runtime/mstats_leaking.go | 18 ++++++++++++++++++ 4 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 src/runtime/mstats_conservative.go create mode 100644 src/runtime/mstats_leaking.go diff --git a/src/runtime/gc_none.go b/src/runtime/gc_none.go index a27b57a2..789802ed 100644 --- a/src/runtime/gc_none.go +++ b/src/runtime/gc_none.go @@ -13,6 +13,8 @@ import ( const gcAsserts = false // perform sanity checks +var gcTotalAlloc uint64 // for runtime.MemStats + func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer func realloc(ptr unsafe.Pointer, size uintptr) unsafe.Pointer diff --git a/src/runtime/mstats.go b/src/runtime/mstats.go index 38596dce..5f42d6a5 100644 --- a/src/runtime/mstats.go +++ b/src/runtime/mstats.go @@ -1,6 +1,3 @@ -//go:build gc.conservative -// +build gc.conservative - package runtime // Memory statistics @@ -47,24 +44,3 @@ type MemStats struct { // GCSys is bytes of memory in garbage collection metadata. GCSys uint64 } - -// ReadMemStats populates m with memory statistics. -// -// The returned memory statistics are up to date as of the -// call to ReadMemStats. This would not do GC implicitly for you. -func ReadMemStats(m *MemStats) { - m.HeapIdle = 0 - m.HeapInuse = 0 - for block := gcBlock(0); block < endBlock; block++ { - bstate := block.state() - if bstate == blockStateFree { - m.HeapIdle += uint64(bytesPerBlock) - } else { - m.HeapInuse += uint64(bytesPerBlock) - } - } - 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.Sys = uint64(heapEnd - heapStart) -} diff --git a/src/runtime/mstats_conservative.go b/src/runtime/mstats_conservative.go new file mode 100644 index 00000000..a65c9da5 --- /dev/null +++ b/src/runtime/mstats_conservative.go @@ -0,0 +1,25 @@ +//go:build gc.conservative +// +build gc.conservative + +package runtime + +// ReadMemStats populates m with memory statistics. +// +// The returned memory statistics are up to date as of the +// call to ReadMemStats. This would not do GC implicitly for you. +func ReadMemStats(m *MemStats) { + m.HeapIdle = 0 + m.HeapInuse = 0 + for block := gcBlock(0); block < endBlock; block++ { + bstate := block.state() + if bstate == blockStateFree { + m.HeapIdle += uint64(bytesPerBlock) + } else { + m.HeapInuse += uint64(bytesPerBlock) + } + } + 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.Sys = uint64(heapEnd - heapStart) +} diff --git a/src/runtime/mstats_leaking.go b/src/runtime/mstats_leaking.go new file mode 100644 index 00000000..70e0503e --- /dev/null +++ b/src/runtime/mstats_leaking.go @@ -0,0 +1,18 @@ +//go:build gc.leaking +// +build gc.leaking + +package runtime + +// ReadMemStats populates m with memory statistics. +// +// The returned memory statistics are up to date as of the +// call to ReadMemStats. This would not do GC implicitly for you. +func ReadMemStats(m *MemStats) { + m.HeapIdle = 0 + m.HeapInuse = uint64(heapptr - heapStart) + 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.Sys = uint64(heapEnd - heapStart) +}