Pārlūkot izejas kodu

Way more correct PauseNs.

Richard Crowley 13 gadi atpakaļ
vecāks
revīzija
88ddcecc4f
1 mainītis faili ar 39 papildinājumiem un 32 dzēšanām
  1. 39 32
      runtime.go

+ 39 - 32
runtime.go

@@ -5,6 +5,11 @@ import (
 	"time"
 )
 
+var (
+	numGC    uint32
+	memStats runtime.MemStats
+)
+
 // Capture new values for the Go runtime statistics exported in
 // runtime.MemStats.  This is designed to be called as a goroutine.
 func CaptureRuntimeMemStats(r Registry, interval int64) {
@@ -19,42 +24,44 @@ func CaptureRuntimeMemStats(r Registry, interval int64) {
 // goroutine.  Giving a registry which has not been given to
 // RegisterRuntimeMemStats will panic.
 func CaptureRuntimeMemStatsOnce(r Registry) {
-	var m runtime.MemStats
-	runtime.ReadMemStats(&m)
-
-	r.Get("runtime.MemStats.Alloc").(Gauge).Update(int64(m.Alloc))
-	r.Get("runtime.MemStats.TotalAlloc").(Gauge).Update(int64(m.TotalAlloc))
-	r.Get("runtime.MemStats.Sys").(Gauge).Update(int64(m.Sys))
-	r.Get("runtime.MemStats.Lookups").(Gauge).Update(int64(m.Lookups))
-	r.Get("runtime.MemStats.Mallocs").(Gauge).Update(int64(m.Mallocs))
-	r.Get("runtime.MemStats.Frees").(Gauge).Update(int64(m.Frees))
-
-	r.Get("runtime.MemStats.HeapAlloc").(Gauge).Update(int64(m.HeapAlloc))
-	r.Get("runtime.MemStats.HeapSys").(Gauge).Update(int64(m.HeapSys))
-	r.Get("runtime.MemStats.HeapIdle").(Gauge).Update(int64(m.HeapIdle))
-	r.Get("runtime.MemStats.HeapInuse").(Gauge).Update(int64(m.HeapInuse))
-	r.Get("runtime.MemStats.HeapReleased").(Gauge).Update(int64(m.HeapReleased))
-	r.Get("runtime.MemStats.HeapObjects").(Gauge).Update(int64(m.HeapObjects))
-
-	r.Get("runtime.MemStats.StackInuse").(Gauge).Update(int64(m.StackInuse))
-	r.Get("runtime.MemStats.StackSys").(Gauge).Update(int64(m.StackSys))
-	r.Get("runtime.MemStats.MSpanInuse").(Gauge).Update(int64(m.MSpanInuse))
-	r.Get("runtime.MemStats.MSpanSys").(Gauge).Update(int64(m.MSpanSys))
-	r.Get("runtime.MemStats.MCacheInuse").(Gauge).Update(int64(m.MCacheInuse))
-	r.Get("runtime.MemStats.MCacheSys").(Gauge).Update(int64(m.MCacheSys))
-	r.Get("runtime.MemStats.BuckHashSys").(Gauge).Update(int64(m.BuckHashSys))
-
-	r.Get("runtime.MemStats.NextGC").(Gauge).Update(int64(m.NextGC))
-	r.Get("runtime.MemStats.LastGC").(Gauge).Update(int64(m.LastGC))
-	r.Get("runtime.MemStats.PauseTotalNs").(Gauge).Update(int64(m.PauseTotalNs))
-	r.Get("runtime.MemStats.PauseNs").(Histogram).Update(int64(m.PauseNs[0]))
-	r.Get("runtime.MemStats.NumGC").(Gauge).Update(int64(m.NumGC))
-	if m.EnableGC {
+	runtime.ReadMemStats(&memStats)
+
+	r.Get("runtime.MemStats.Alloc").(Gauge).Update(int64(memStats.Alloc))
+	r.Get("runtime.MemStats.TotalAlloc").(Gauge).Update(int64(memStats.TotalAlloc))
+	r.Get("runtime.MemStats.Sys").(Gauge).Update(int64(memStats.Sys))
+	r.Get("runtime.MemStats.Lookups").(Gauge).Update(int64(memStats.Lookups))
+	r.Get("runtime.MemStats.Mallocs").(Gauge).Update(int64(memStats.Mallocs))
+	r.Get("runtime.MemStats.Frees").(Gauge).Update(int64(memStats.Frees))
+
+	r.Get("runtime.MemStats.HeapAlloc").(Gauge).Update(int64(memStats.HeapAlloc))
+	r.Get("runtime.MemStats.HeapSys").(Gauge).Update(int64(memStats.HeapSys))
+	r.Get("runtime.MemStats.HeapIdle").(Gauge).Update(int64(memStats.HeapIdle))
+	r.Get("runtime.MemStats.HeapInuse").(Gauge).Update(int64(memStats.HeapInuse))
+	r.Get("runtime.MemStats.HeapReleased").(Gauge).Update(int64(memStats.HeapReleased))
+	r.Get("runtime.MemStats.HeapObjects").(Gauge).Update(int64(memStats.HeapObjects))
+
+	r.Get("runtime.MemStats.StackInuse").(Gauge).Update(int64(memStats.StackInuse))
+	r.Get("runtime.MemStats.StackSys").(Gauge).Update(int64(memStats.StackSys))
+	r.Get("runtime.MemStats.MSpanInuse").(Gauge).Update(int64(memStats.MSpanInuse))
+	r.Get("runtime.MemStats.MSpanSys").(Gauge).Update(int64(memStats.MSpanSys))
+	r.Get("runtime.MemStats.MCacheInuse").(Gauge).Update(int64(memStats.MCacheInuse))
+	r.Get("runtime.MemStats.MCacheSys").(Gauge).Update(int64(memStats.MCacheSys))
+	r.Get("runtime.MemStats.BuckHashSys").(Gauge).Update(int64(memStats.BuckHashSys))
+
+	r.Get("runtime.MemStats.NextGC").(Gauge).Update(int64(memStats.NextGC))
+	r.Get("runtime.MemStats.LastGC").(Gauge).Update(int64(memStats.LastGC))
+	r.Get("runtime.MemStats.PauseTotalNs").(Gauge).Update(int64(memStats.PauseTotalNs))
+	// <https://code.google.com/p/go/source/browse/src/pkg/runtime/mgc0.c>
+	for i := uint32(1); i <= memStats.NumGC-numGC; i++ {
+		r.Get("runtime.MemStats.PauseNs").(Histogram).Update(int64(memStats.PauseNs[(memStats.NumGC%256-i)%256]))
+	}
+	r.Get("runtime.MemStats.NumGC").(Gauge).Update(int64(memStats.NumGC))
+	if memStats.EnableGC {
 		r.Get("runtime.MemStats.EnableGC").(Gauge).Update(1)
 	} else {
 		r.Get("runtime.MemStats.EnableGC").(Gauge).Update(0)
 	}
-	if m.EnableGC {
+	if memStats.EnableGC {
 		r.Get("runtime.MemStats.DebugGC").(Gauge).Update(1)
 	} else {
 		r.Get("runtime.MemStats.DebugGC").(Gauge).Update(0)