@@ -192,19 +192,25 @@ func initMetrics() {
192
192
"/gc/scan/globals:bytes" : {
193
193
compute : func (in * statAggregate , out * metricValue ) {
194
194
out .kind = metricKindUint64
195
- out .scalar = gcController . globalsScan . Load ()
195
+ out .scalar = in . gcStats . globalsScan
196
196
},
197
197
},
198
198
"/gc/scan/heap:bytes" : {
199
199
compute : func (in * statAggregate , out * metricValue ) {
200
200
out .kind = metricKindUint64
201
- out .scalar = gcController .heapScan .Load ()
201
+ out .scalar = in .gcStats .heapScan
202
+ },
203
+ },
204
+ "/gc/scan/stack:bytes" : {
205
+ compute : func (in * statAggregate , out * metricValue ) {
206
+ out .kind = metricKindUint64
207
+ out .scalar = in .gcStats .stackScan
202
208
},
203
209
},
204
210
"/gc/scan/total:bytes" : {
205
211
compute : func (in * statAggregate , out * metricValue ) {
206
212
out .kind = metricKindUint64
207
- out .scalar = gcController . globalsScan . Load () + gcController . heapScan . Load () + gcController . lastStackScan . Load ()
213
+ out .scalar = in . gcStats . totalScan
208
214
},
209
215
},
210
216
"/gc/heap/allocs-by-size:bytes" : {
@@ -318,12 +324,6 @@ func initMetrics() {
318
324
hist .counts [len (hist .counts )- 1 ] = memstats .gcPauseDist .overflow .Load ()
319
325
},
320
326
},
321
- "/gc/scan/stack:bytes" : {
322
- compute : func (in * statAggregate , out * metricValue ) {
323
- out .kind = metricKindUint64
324
- out .scalar = uint64 (gcController .lastStackScan .Load ())
325
- },
326
- },
327
327
"/gc/stack/starting-size:bytes" : {
328
328
compute : func (in * statAggregate , out * metricValue ) {
329
329
out .kind = metricKindUint64
@@ -505,6 +505,7 @@ const (
505
505
heapStatsDep statDep = iota // corresponds to heapStatsAggregate
506
506
sysStatsDep // corresponds to sysStatsAggregate
507
507
cpuStatsDep // corresponds to cpuStatsAggregate
508
+ gcStatsDep // corresponds to gcStatsAggregate
508
509
numStatsDeps
509
510
)
510
511
@@ -666,6 +667,23 @@ func (a *cpuStatsAggregate) compute() {
666
667
// a.cpuStats.accumulate(nanotime(), gcphase == _GCmark)
667
668
}
668
669
670
+ // cpuStatsAggregate represents various GC stats obtained from the runtime
671
+ // acquired together to avoid skew and inconsistencies.
672
+ type gcStatsAggregate struct {
673
+ heapScan uint64
674
+ stackScan uint64
675
+ globalsScan uint64
676
+ totalScan uint64
677
+ }
678
+
679
+ // compute populates the gcStatsAggregate with values from the runtime.
680
+ func (a * gcStatsAggregate ) compute () {
681
+ a .heapScan = gcController .heapScan .Load ()
682
+ a .stackScan = uint64 (gcController .lastStackScan .Load ())
683
+ a .globalsScan = gcController .globalsScan .Load ()
684
+ a .totalScan = a .heapScan + a .stackScan + a .globalsScan
685
+ }
686
+
669
687
// nsToSec takes a duration in nanoseconds and converts it to seconds as
670
688
// a float64.
671
689
func nsToSec (ns int64 ) float64 {
@@ -682,6 +700,7 @@ type statAggregate struct {
682
700
heapStats heapStatsAggregate
683
701
sysStats sysStatsAggregate
684
702
cpuStats cpuStatsAggregate
703
+ gcStats gcStatsAggregate
685
704
}
686
705
687
706
// ensure populates statistics aggregates determined by deps if they
@@ -702,6 +721,8 @@ func (a *statAggregate) ensure(deps *statDepSet) {
702
721
a .sysStats .compute ()
703
722
case cpuStatsDep :
704
723
a .cpuStats .compute ()
724
+ case gcStatsDep :
725
+ a .gcStats .compute ()
705
726
}
706
727
}
707
728
a .ensured = a .ensured .union (missing )
0 commit comments