Skip to content

Commit 8836ce3

Browse files
committed
internal/plot: add alloc/free rates plot
1 parent bb6aa8e commit 8836ce3

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

internal/plot/layouts.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func sizeClassesLayout(samples []metrics.Sample) Heatmap {
203203
}
204204

205205
func gcPausesLayout(samples []metrics.Sample) Heatmap {
206-
idxgcpauses := metricIdx["/gc/pauses:seconds"]
206+
idxgcpauses := metricIdx["/sched/pauses/total/gc:seconds"]
207207

208208
gcpauses := samples[idxgcpauses].Value.Float64Histogram()
209209
histfactor := downsampleFactor(len(gcpauses.Buckets), maxBuckets)
@@ -439,7 +439,7 @@ var cpuScavengerLayout = Scatter{
439439
Layout: ScatterLayout{
440440
BarMode: "stack",
441441
Yaxis: ScatterYAxis{
442-
Title: "cpu-seconds per seconds",
442+
Title: "cpu-seconds / second",
443443
TickSuffix: "s",
444444
},
445445
},
@@ -470,7 +470,7 @@ var cpuOverallLayout = Scatter{
470470
Layout: ScatterLayout{
471471
BarMode: "stack",
472472
Yaxis: ScatterYAxis{
473-
Title: "cpu-seconds per seconds",
473+
Title: "cpu-seconds / second",
474474
TickSuffix: "s",
475475
},
476476
},
@@ -518,7 +518,7 @@ var mutexWaitLayout = Scatter{
518518
Events: "lastgc",
519519
Layout: ScatterLayout{
520520
Yaxis: ScatterYAxis{
521-
Title: "seconds per seconds",
521+
Title: "seconds / second",
522522
TickSuffix: "s",
523523
},
524524
},
@@ -572,3 +572,29 @@ This plot shows the amount of memory that is scannable by the GC.
572572
<i>scanned stack</i> is <b>/gc/scan/stack</b>, the number of bytes of stack that were scanned last GC cycle.
573573
`,
574574
}
575+
576+
var allocFreeRatesLayout = Scatter{
577+
Name: "heap alloc/free rates",
578+
Title: "Heap Allocation & Free Rates",
579+
Type: "scatter",
580+
Layout: ScatterLayout{
581+
Yaxis: ScatterYAxis{
582+
Title: "objects / second",
583+
},
584+
},
585+
Subplots: []Subplot{
586+
{
587+
Name: "allocs/sec",
588+
Unitfmt: "%{y:.4s}",
589+
Color: RGBString(66, 133, 244),
590+
},
591+
{
592+
Name: "frees/sec",
593+
Unitfmt: "%{y:.4s}",
594+
Color: RGBString(219, 68, 55),
595+
},
596+
},
597+
InfoText: `
598+
<i>Allocations per second</i> is derived by differencing the cumulative <b>/gc/heap/allocs:objects</b> metric.
599+
<i>Frees per second</i> is similarly derived from <b>/gc/heap/frees:objects</b>.`,
600+
}

internal/plot/plots.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ func init() {
237237
layout: gcScanLayout,
238238
make: makeGCScan,
239239
},
240+
{
241+
name: "alloc-free-rate",
242+
tags: []string{"gc"},
243+
metrics: []string{
244+
"/gc/heap/allocs:objects",
245+
"/gc/heap/frees:objects",
246+
},
247+
layout: allocFreeRatesLayout,
248+
make: makeAllocFreeRates,
249+
},
240250
}
241251
}
242252

@@ -892,6 +902,47 @@ func (p *gcScan) values(samples []metrics.Sample) any {
892902
}
893903
}
894904

905+
// alloc/free rates
906+
type allocFreeRates struct {
907+
idxallocs int
908+
idxfrees int
909+
910+
lasttime time.Time
911+
lastallocs uint64
912+
lastfrees uint64
913+
}
914+
915+
func makeAllocFreeRates(indices ...int) metricsGetter {
916+
return &allocFreeRates{
917+
idxallocs: indices[0],
918+
idxfrees: indices[1],
919+
}
920+
}
921+
922+
func (p *allocFreeRates) values(samples []metrics.Sample) any {
923+
if p.lasttime.IsZero() {
924+
p.lasttime = time.Now()
925+
p.lastallocs = samples[p.idxallocs].Value.Uint64()
926+
p.lastfrees = samples[p.idxfrees].Value.Uint64()
927+
928+
return []float64{0, 0}
929+
}
930+
931+
t := time.Since(p.lasttime).Seconds()
932+
933+
allocs := float64(samples[p.idxallocs].Value.Uint64()-p.lastallocs) / t
934+
frees := float64(samples[p.idxfrees].Value.Uint64()-p.lastfrees) / t
935+
936+
p.lastallocs = samples[p.idxallocs].Value.Uint64()
937+
p.lastfrees = samples[p.idxfrees].Value.Uint64()
938+
p.lasttime = time.Now()
939+
940+
return []float64{
941+
allocs,
942+
frees,
943+
}
944+
}
945+
895946
/*
896947
* helpers
897948
*/

0 commit comments

Comments
 (0)