]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime/metrics: add /gc/gomemlimit:bytes
authorFelix Geisendörfer <felix.geisendoerfer@datadoghq.com>
Tue, 23 May 2023 09:16:28 +0000 (11:16 +0200)
committerGopher Robot <gobot@golang.org>
Tue, 23 May 2023 19:14:04 +0000 (19:14 +0000)
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>
src/runtime/metrics.go
src/runtime/metrics/description.go
src/runtime/metrics/doc.go
src/runtime/metrics_test.go

index ad6348136ee1b5e5093f262b57a97fe53707ef33..9308a784b706cb01956d55e622884044610f5e81 100644 (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) {
index f5b10202716b77a4c9bc80f07b77c875fa8d14f4..e9c8ccc0fe1e05f2da0d5e3b516586c56c81a841 100644 (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. " +
index fe0d1f58e9b34f979a3b50feb9bf01c3e254619d..61326ed1dfabaf816515afbdc5231c826989c221 100644 (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
index c1a991ead08ff7420801030b6442e66cf8001e1c..19888376a4f65b61521d96775fb94bc4785db31b 100644 (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":