]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime,runtime/metrics: add heap object count metric
authorMichael Anthony Knyszek <mknyszek@google.com>
Thu, 6 Aug 2020 15:44:27 +0000 (15:44 +0000)
committerMichael Knyszek <mknyszek@google.com>
Mon, 26 Oct 2020 18:29:18 +0000 (18:29 +0000)
For #37112.

Change-Id: Idd3dd5c84215ddd1ab05c2e76e848aa0a4d40fb0
Reviewed-on: https://go-review.googlesource.com/c/go/+/247043
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/runtime/metrics.go
src/runtime/metrics/description.go
src/runtime/metrics/doc.go
src/runtime/metrics_test.go

index 44b5a2975164ca7e02b3295304f172dee9ff88ef..cf619cca4bcd2782b477f731edf675f32f0e7dee 100644 (file)
@@ -38,6 +38,13 @@ func initMetrics() {
                return
        }
        metrics = map[string]metricData{
+               "/gc/heap/objects:objects": {
+                       deps: makeStatDepSet(heapStatsDep),
+                       compute: func(in *statAggregate, out *metricValue) {
+                               out.kind = metricKindUint64
+                               out.scalar = in.heapStats.numObjects
+                       },
+               },
                "/memory/classes/heap/free:bytes": {
                        deps: makeStatDepSet(heapStatsDep),
                        compute: func(in *statAggregate, out *metricValue) {
@@ -210,9 +217,13 @@ func (s *statDepSet) has(d statDep) bool {
 type heapStatsAggregate struct {
        heapStatsDelta
 
+       // Derived from values in heapStatsDelta.
+
        // inObjects is the bytes of memory occupied by objects,
-       // derived from other values in heapStats.
        inObjects uint64
+
+       // numObjects is the number of live objects in the heap.
+       numObjects uint64
 }
 
 // compute populates the heapStatsAggregate with values from the runtime.
@@ -221,8 +232,11 @@ func (a *heapStatsAggregate) compute() {
 
        // Calculate derived stats.
        a.inObjects = uint64(a.largeAlloc - a.largeFree)
+       a.numObjects = uint64(a.largeAllocCount - a.largeFreeCount)
        for i := range a.smallAllocCount {
-               a.inObjects += uint64(a.smallAllocCount[i]-a.smallFreeCount[i]) * uint64(class_to_size[i])
+               n := uint64(a.smallAllocCount[i] - a.smallFreeCount[i])
+               a.inObjects += n * uint64(class_to_size[i])
+               a.numObjects += n
        }
 }
 
index 2e7df7e09f717100b86dc0d1428a14e61ffd8a85..47013e1451dd8a94a1bd7cd8cb85260596deaf9d 100644 (file)
@@ -50,6 +50,11 @@ type Description struct {
 // The English language descriptions below must be kept in sync with the
 // descriptions of each metric in doc.go.
 var allDesc = []Description{
+       {
+               Name:        "/gc/heap/objects:objects",
+               Description: "Number of objects, live or unswept, occupying heap memory.",
+               Kind:        KindUint64,
+       },
        {
                Name:        "/memory/classes/heap/free:bytes",
                Description: "Memory that is available for allocation, and may be returned to the underlying system.",
index fb4e23a2b5f33a0d439fd1f2b5683d6fd748a561..4ac44bb19c5d62f8cb3a835607160c57542777ea 100644 (file)
@@ -44,6 +44,9 @@ the documentation of the Name field of the Description struct.
 
 Supported metrics
 
+       /gc/heap/objects:objects
+               Number of objects, live or unswept, occupying heap memory.
+
        /memory/classes/heap/free:bytes
                Memory that is available for allocation, and may be returned
                to the underlying system.
index d925b057b0e381fa31f48e35e4129b504c4bcf07..6c0be7dc0bfaca77b7a9ee1a74fb8f1f12284016 100644 (file)
@@ -70,6 +70,8 @@ func TestReadMetrics(t *testing.T) {
                        checkUint64(t, name, samples[i].Value.Uint64(), mstats.BuckHashSys)
                case "/memory/classes/total:bytes":
                        checkUint64(t, name, samples[i].Value.Uint64(), mstats.Sys)
+               case "/gc/heap/objects:objects":
+                       checkUint64(t, name, samples[i].Value.Uint64(), mstats.HeapObjects)
                }
        }
 }