Skip to content

Commit 2613176

Browse files
committed
internal/plot: use sync.Once for initIndices() to ensure init() order
1 parent ffcb10b commit 2613176

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

internal/plot/plot_runnable_time.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@ var _ = register(description{
1616
counts := [maxBuckets]uint64{}
1717

1818
return func(_ time.Time, samples []metrics.Sample) any {
19+
hist := samples[idx_sched_latencies_seconds].Value.Float64Histogram()
1920
if histfactor == 0 {
20-
schedlat := samples[idx_sched_latencies_seconds].Value.Float64Histogram()
21-
histfactor = downsampleFactor(len(schedlat.Buckets), maxBuckets)
21+
histfactor = downsampleFactor(len(hist.Buckets), maxBuckets)
2222
}
2323

24-
schedlat := samples[idx_sched_latencies_seconds].Value.Float64Histogram()
25-
26-
return downsampleCounts(schedlat, histfactor, counts[:])
24+
return downsampleCounts(hist, histfactor, counts[:])
2725
}
2826
},
2927
layout: func(samples []metrics.Sample) Heatmap {
30-
schedlat := samples[idx_sched_latencies_seconds].Value.Float64Histogram()
31-
histfactor := downsampleFactor(len(schedlat.Buckets), maxBuckets)
32-
buckets := downsampleBuckets(schedlat, histfactor)
28+
hist := samples[idx_sched_latencies_seconds].Value.Float64Histogram()
29+
histfactor := downsampleFactor(len(hist.Buckets), maxBuckets)
30+
buckets := downsampleBuckets(hist, histfactor)
3331

3432
return Heatmap{
3533
Name: "TODO(set later)",

internal/plot/plot_stopping_pauses_gc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
var _ = register(description{
99
name: "stopping-pauses-gc",
10-
tags: []tag{tagScheduler},
10+
tags: []tag{tagScheduler, tagGC},
1111
metrics: []string{
1212
"/sched/pauses/stopping/gc:seconds",
1313
},

internal/plot/plot_total_pauses_gc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
var _ = register(description{
99
name: "total-pauses-gc",
10-
tags: []tag{tagScheduler},
10+
tags: []tag{tagScheduler, tagGC},
1111
metrics: []string{
1212
"/sched/pauses/total/gc:seconds",
1313
},
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package plot
44
import (
55
"runtime/debug"
66
"runtime/metrics"
7+
"sync"
78
"time"
89
)
910

@@ -32,7 +33,22 @@ var (
3233
metricIdx map[string]int
3334
)
3435

36+
var initIndices = sync.OnceValue(func() []metrics.Sample {
37+
all := metrics.All()
38+
39+
metricIdx = make(map[string]int, len(all))
40+
41+
samples := make([]metrics.Sample, len(all))
42+
for i := range samples {
43+
samples[i].Name = all[i].Name
44+
metricIdx[samples[i].Name] = i
45+
}
46+
metrics.Read(samples)
47+
return samples
48+
})
49+
3550
func mustidx(metric string) int {
51+
_ = initIndices()
3652
idx, ok := metricIdx[metric]
3753
if !ok {
3854
bnfo, ok := debug.ReadBuildInfo()
@@ -50,20 +66,9 @@ func register(desc description) struct{} {
5066
}
5167

5268
func init() {
53-
// We need a first set of sample in order to dimension and process the
54-
// heatmaps buckets.
55-
all := metrics.All()
56-
samples := make([]metrics.Sample, len(all))
57-
metricIdx = make(map[string]int)
58-
59-
for i := range samples {
60-
samples[i].Name = all[i].Name
61-
metricIdx[samples[i].Name] = i
62-
}
63-
metrics.Read(samples)
64-
6569
type heatmapLayoutFunc = func(samples []metrics.Sample) Heatmap
6670

71+
samples := initIndices()
6772
for i := range registry {
6873
desc := &registry[i]
6974
if hm, ok := desc.layout.(heatmapLayoutFunc); ok {

0 commit comments

Comments
 (0)