Skip to content

Commit

Permalink
runtime: Expose MemStat for last N GCs.
Browse files Browse the repository at this point in the history
Change-Id: I0c85c8dd8086a942293479effcbf3bc10d0f1edd
  • Loading branch information
JayNakrani committed Feb 28, 2018
1 parent 4b1d704 commit 78ec37e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/runtime/mgc.go
Expand Up @@ -1658,14 +1658,18 @@ func gcMarkTermination(nextTriggerRatio float64) {
// Free stack spans. This must be done between GC cycles.
systemstack(freeStackSpans)

// Print gctrace before dropping worldsema. As soon as we drop
// Remember and print gctrace before dropping worldsema. As soon as we drop
// worldsema another cycle could start and smash the stats
// we're trying to print.
var stats MemStats
getmemstats(&stats)
gcstats[memstats.numgc%uint32(len(gcstats))] = stats
if debug.gctrace > 0 {
util := int(memstats.gc_cpu_fraction * 100)

var sbuf [24]byte
printlock()

print("gc ", memstats.numgc,
" @", string(itoaDiv(sbuf[:], uint64(work.tSweepTerm-runtimeInitTime)/1e6, 3)), "s ",
util, "%: ")
Expand Down
20 changes: 20 additions & 0 deletions src/runtime/mstats.go
Expand Up @@ -428,6 +428,11 @@ type MemStats struct {
}
}

// Circular buffer of stats from past GC.
//
// TODO(dhananjayn): Take buffer size as input from GODEBUG?
var gcstats [256]MemStats

// Size of the trailing by_size array differs between mstats and MemStats,
// and all data after by_size is local to runtime, not exported.
// NumSizeClasses was changed, but we cannot change MemStats because of backward compatibility.
Expand Down Expand Up @@ -464,9 +469,24 @@ func ReadMemStats(m *MemStats) {
startTheWorld()
}

// ReadAllMemStats returns memstats for past GCs.
func ReadAllMemStats(stats *[]MemStats) {
stopTheWorld("read all mem stats")

systemstack(func() {
copy(*stats, gcstats[:])
})

startTheWorld()
}

func readmemstats_m(stats *MemStats) {
updatememstats()
getmemstats(stats)
}

// converts memstats to MemStats
func getmemstats(stats *MemStats) {
// The size of the trailing by_size array differs between
// mstats and MemStats. NumSizeClasses was changed, but we
// cannot change MemStats because of backward compatibility.
Expand Down

0 comments on commit 78ec37e

Please sign in to comment.