Skip to content

Commit b08dfba

Browse files
committed
runtime,runtime/metrics: add memory metrics
This change adds support for a variety of runtime memory metrics and contains the base implementation of Read for the runtime/metrics package, which lives in the runtime. It also adds testing infrastructure for the metrics package, and a bunch of format and documentation tests. For #37112. Change-Id: I16a2c4781eeeb2de0abcb045c15105f1210e2d8a Reviewed-on: https://go-review.googlesource.com/c/go/+/247041 Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Pratt <[email protected]> Trust: Michael Knyszek <[email protected]>
1 parent 79781e8 commit b08dfba

File tree

9 files changed

+781
-6
lines changed

9 files changed

+781
-6
lines changed

src/cmd/go/internal/work/gc.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ func (gcToolchain) gc(b *Builder, a *Action, archive string, importcfg []byte, s
8989
extFiles := len(p.CgoFiles) + len(p.CFiles) + len(p.CXXFiles) + len(p.MFiles) + len(p.FFiles) + len(p.SFiles) + len(p.SysoFiles) + len(p.SwigFiles) + len(p.SwigCXXFiles)
9090
if p.Standard {
9191
switch p.ImportPath {
92-
case "bytes", "internal/poll", "net", "os", "runtime/pprof", "runtime/trace", "sync", "syscall", "time":
92+
case "bytes", "internal/poll", "net", "os":
93+
fallthrough
94+
case "runtime/metrics", "runtime/pprof", "runtime/trace":
95+
fallthrough
96+
case "sync", "syscall", "time":
9397
extFiles++
9498
}
9599
}

src/runtime/export_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,32 @@ func (p *ProfBuf) Close() {
298298
(*profBuf)(p).close()
299299
}
300300

301+
func ReadMetricsSlow(memStats *MemStats, samplesp unsafe.Pointer, len, cap int) {
302+
stopTheWorld("ReadMetricsSlow")
303+
304+
// Initialize the metrics beforehand because this could
305+
// allocate and skew the stats.
306+
semacquire(&metricsSema)
307+
initMetrics()
308+
semrelease(&metricsSema)
309+
310+
systemstack(func() {
311+
// Read memstats first. It's going to flush
312+
// the mcaches which readMetrics does not do, so
313+
// going the other way around may result in
314+
// inconsistent statistics.
315+
readmemstats_m(memStats)
316+
})
317+
318+
// Read metrics off the system stack.
319+
//
320+
// The only part of readMetrics that could allocate
321+
// and skew the stats is initMetrics.
322+
readMetrics(samplesp, len, cap)
323+
324+
startTheWorld()
325+
}
326+
301327
// ReadMemStatsSlow returns both the runtime-computed MemStats and
302328
// MemStats accumulated by scanning the heap.
303329
func ReadMemStatsSlow() (base, slow MemStats) {

0 commit comments

Comments
 (0)