From 7b325ba27d8a5a90d16793267523ebf52a18bf65 Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Thu, 8 Jun 2023 17:08:52 +0000 Subject: [PATCH] runtime: apply looser bound to /gc/heap/live:bytes in test /gc/heap/live:bytes may exceed MemStats.HeapAlloc, even when all data is flushed, becuase the GC may double-count objects when marking them. This is an intentional design choice that is largely inconsequential. The runtime is already robust to it, and the condition is rare. Fixes #60607. Change-Id: I4da402efc24327328d2d8780e4e49961b189f0ea Reviewed-on: https://go-review.googlesource.com/c/go/+/501858 Auto-Submit: Michael Knyszek Reviewed-by: Michael Pratt Run-TryBot: Michael Knyszek TryBot-Result: Gopher Robot --- src/runtime/metrics_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/runtime/metrics_test.go b/src/runtime/metrics_test.go index c138a2a1ba..a64e898739 100644 --- a/src/runtime/metrics_test.go +++ b/src/runtime/metrics_test.go @@ -143,8 +143,16 @@ func TestReadMetrics(t *testing.T) { case "/gc/heap/frees:objects": frees = samples[i].Value.Uint64() case "/gc/heap/live:bytes": - if live := samples[i].Value.Uint64(); live > mstats.HeapAlloc { - t.Errorf("live bytes: %d > heap alloc: %d", live, mstats.HeapAlloc) + // Check for "obviously wrong" values. We can't check a stronger invariant, + // such as live <= HeapAlloc, because live is not 100% accurate. It's computed + // under racy conditions, and some objects may be double-counted (this is + // intentional and necessary for GC performance). + // + // Instead, check against a much more reasonable upper-bound: the amount of + // mapped heap memory. We can't possibly overcount to the point of exceeding + // total mapped heap memory, except if there's an accounting bug. + if live := samples[i].Value.Uint64(); live > mstats.HeapSys { + t.Errorf("live bytes: %d > heap sys: %d", live, mstats.HeapSys) } else if live == 0 { // Might happen if we don't call runtime.GC() above. t.Error("live bytes is 0") -- 2.48.1