Skip to content

Commit 07c3f65

Browse files
committed
runtime,runtime/metrics: add heap object count metric
For #37112. Change-Id: Idd3dd5c84215ddd1ab05c2e76e848aa0a4d40fb0 Reviewed-on: https://go-review.googlesource.com/c/go/+/247043 Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Michael Knyszek <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 74e566e commit 07c3f65

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

src/runtime/metrics.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ func initMetrics() {
3838
return
3939
}
4040
metrics = map[string]metricData{
41+
"/gc/heap/objects:objects": {
42+
deps: makeStatDepSet(heapStatsDep),
43+
compute: func(in *statAggregate, out *metricValue) {
44+
out.kind = metricKindUint64
45+
out.scalar = in.heapStats.numObjects
46+
},
47+
},
4148
"/memory/classes/heap/free:bytes": {
4249
deps: makeStatDepSet(heapStatsDep),
4350
compute: func(in *statAggregate, out *metricValue) {
@@ -210,9 +217,13 @@ func (s *statDepSet) has(d statDep) bool {
210217
type heapStatsAggregate struct {
211218
heapStatsDelta
212219

220+
// Derived from values in heapStatsDelta.
221+
213222
// inObjects is the bytes of memory occupied by objects,
214-
// derived from other values in heapStats.
215223
inObjects uint64
224+
225+
// numObjects is the number of live objects in the heap.
226+
numObjects uint64
216227
}
217228

218229
// compute populates the heapStatsAggregate with values from the runtime.
@@ -221,8 +232,11 @@ func (a *heapStatsAggregate) compute() {
221232

222233
// Calculate derived stats.
223234
a.inObjects = uint64(a.largeAlloc - a.largeFree)
235+
a.numObjects = uint64(a.largeAllocCount - a.largeFreeCount)
224236
for i := range a.smallAllocCount {
225-
a.inObjects += uint64(a.smallAllocCount[i]-a.smallFreeCount[i]) * uint64(class_to_size[i])
237+
n := uint64(a.smallAllocCount[i] - a.smallFreeCount[i])
238+
a.inObjects += n * uint64(class_to_size[i])
239+
a.numObjects += n
226240
}
227241
}
228242

src/runtime/metrics/description.go

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ type Description struct {
5050
// The English language descriptions below must be kept in sync with the
5151
// descriptions of each metric in doc.go.
5252
var allDesc = []Description{
53+
{
54+
Name: "/gc/heap/objects:objects",
55+
Description: "Number of objects, live or unswept, occupying heap memory.",
56+
Kind: KindUint64,
57+
},
5358
{
5459
Name: "/memory/classes/heap/free:bytes",
5560
Description: "Memory that is available for allocation, and may be returned to the underlying system.",

src/runtime/metrics/doc.go

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ the documentation of the Name field of the Description struct.
4444
4545
Supported metrics
4646
47+
/gc/heap/objects:objects
48+
Number of objects, live or unswept, occupying heap memory.
49+
4750
/memory/classes/heap/free:bytes
4851
Memory that is available for allocation, and may be returned
4952
to the underlying system.

src/runtime/metrics_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func TestReadMetrics(t *testing.T) {
7070
checkUint64(t, name, samples[i].Value.Uint64(), mstats.BuckHashSys)
7171
case "/memory/classes/total:bytes":
7272
checkUint64(t, name, samples[i].Value.Uint64(), mstats.Sys)
73+
case "/gc/heap/objects:objects":
74+
checkUint64(t, name, samples[i].Value.Uint64(), mstats.HeapObjects)
7375
}
7476
}
7577
}

0 commit comments

Comments
 (0)