runtime/metrics: add /gc/gomemlimit:bytes

For #56857

Change-Id: I184d752cc615874ada3d0dbc6ed1bf72c8debd0f
Reviewed-on: https://go-review.googlesource.com/c/go/+/497316
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Felix Geisendörfer <felix.geisendoerfer@datadoghq.com>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Felix Geisendörfer 2023-05-23 11:16:28 +02:00 committed by Gopher Robot
parent cb6b45a993
commit 2544b1051e
4 changed files with 26 additions and 0 deletions

View file

@ -248,6 +248,12 @@ func initMetrics() {
out.scalar = in.sysStats.heapGoal
},
},
"/gc/gomemlimit:bytes": {
compute: func(in *statAggregate, out *metricValue) {
out.kind = metricKindUint64
out.scalar = uint64(gcController.memoryLimit.Load())
},
},
"/gc/heap/live:bytes": {
deps: makeStatDepSet(heapStatsDep),
compute: func(in *statAggregate, out *metricValue) {

View file

@ -193,6 +193,13 @@ var allDesc = []Description{
Kind: KindUint64,
Cumulative: true,
},
{
Name: "/gc/gomemlimit:bytes",
Description: "Go runtime memory limit configured by the user, otherwise " +
"math.MaxInt64. This value is set by the GOMEMLIMIT environment variable, and " +
"the runtime/debug.SetMemoryLimit function.",
Kind: KindUint64,
},
{
Name: "/gc/heap/allocs-by-size:bytes",
Description: "Distribution of heap allocations by approximate size. " +

View file

@ -147,6 +147,11 @@ Below is the full list of supported metrics, ordered lexicographically.
/gc/cycles/total:gc-cycles
Count of all completed GC cycles.
/gc/gomemlimit:bytes
Go runtime memory limit configured by the user, otherwise
math.MaxInt64. This value is set by the GOMEMLIMIT environment
variable, and the runtime/debug.SetMemoryLimit function.
/gc/heap/allocs-by-size:bytes
Distribution of heap allocations by approximate size.
Bucket counts increase monotonically. Note that this does not

View file

@ -7,6 +7,7 @@ package runtime_test
import (
"reflect"
"runtime"
"runtime/debug"
"runtime/metrics"
"sort"
"strings"
@ -31,6 +32,11 @@ func TestReadMetrics(t *testing.T) {
// Run a GC cycle to get some of the stats to be non-zero.
runtime.GC()
// Set an arbitrary memory limit to check the metric for it
limit := int64(512 * 1024 * 1024)
oldLimit := debug.SetMemoryLimit(limit)
defer debug.SetMemoryLimit(oldLimit)
// Tests whether readMetrics produces values aligning
// with ReadMemStats while the world is stopped.
var mstats runtime.MemStats
@ -138,6 +144,8 @@ func TestReadMetrics(t *testing.T) {
// Might happen if we don't call runtime.GC() above.
t.Error("live bytes is 0")
}
case "/gc/gomemlimit:bytes":
checkUint64(t, name, samples[i].Value.Uint64(), uint64(limit))
case "/gc/heap/objects:objects":
checkUint64(t, name, samples[i].Value.Uint64(), mstats.HeapObjects)
case "/gc/heap/goal:bytes":