Skip to content

Commit

Permalink
Add support for fraction of CPU time used by GC
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac Saldana committed Nov 29, 2015
1 parent 1ce93ef commit 8f4775a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
56 changes: 30 additions & 26 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,33 @@ var (
memStats runtime.MemStats
runtimeMetrics struct {
MemStats struct {
Alloc Gauge
BuckHashSys Gauge
DebugGC Gauge
EnableGC Gauge
Frees Gauge
HeapAlloc Gauge
HeapIdle Gauge
HeapInuse Gauge
HeapObjects Gauge
HeapReleased Gauge
HeapSys Gauge
LastGC Gauge
Lookups Gauge
Mallocs Gauge
MCacheInuse Gauge
MCacheSys Gauge
MSpanInuse Gauge
MSpanSys Gauge
NextGC Gauge
NumGC Gauge
PauseNs Histogram
PauseTotalNs Gauge
StackInuse Gauge
StackSys Gauge
Sys Gauge
TotalAlloc Gauge
Alloc Gauge
BuckHashSys Gauge
DebugGC Gauge
EnableGC Gauge
Frees Gauge
HeapAlloc Gauge
HeapIdle Gauge
HeapInuse Gauge
HeapObjects Gauge
HeapReleased Gauge
HeapSys Gauge
LastGC Gauge
Lookups Gauge
Mallocs Gauge
MCacheInuse Gauge
MCacheSys Gauge
MSpanInuse Gauge
MSpanSys Gauge
NextGC Gauge
NumGC Gauge
GCCPUFraction GaugeFloat64
PauseNs Histogram
PauseTotalNs Gauge
StackInuse Gauge
StackSys Gauge
Sys Gauge
TotalAlloc Gauge
}
NumCgoCall Gauge
NumGoroutine Gauge
Expand Down Expand Up @@ -97,6 +98,7 @@ func CaptureRuntimeMemStatsOnce(r Registry) {
runtimeMetrics.MemStats.MSpanSys.Update(int64(memStats.MSpanSys))
runtimeMetrics.MemStats.NextGC.Update(int64(memStats.NextGC))
runtimeMetrics.MemStats.NumGC.Update(int64(memStats.NumGC - numGC))
runtimeMetrics.MemStats.GCCPUFraction.Update(gcCPUFraction(&memStats))

// <https://code.google.com/p/go/source/browse/src/pkg/runtime/mgc0.c>
i := numGC % uint32(len(memStats.PauseNs))
Expand Down Expand Up @@ -158,6 +160,7 @@ func RegisterRuntimeMemStats(r Registry) {
runtimeMetrics.MemStats.MSpanSys = NewGauge()
runtimeMetrics.MemStats.NextGC = NewGauge()
runtimeMetrics.MemStats.NumGC = NewGauge()
runtimeMetrics.MemStats.GCCPUFraction = NewGaugeFloat64()
runtimeMetrics.MemStats.PauseNs = NewHistogram(NewExpDecaySample(1028, 0.015))
runtimeMetrics.MemStats.PauseTotalNs = NewGauge()
runtimeMetrics.MemStats.StackInuse = NewGauge()
Expand Down Expand Up @@ -188,6 +191,7 @@ func RegisterRuntimeMemStats(r Registry) {
r.Register("runtime.MemStats.MSpanSys", runtimeMetrics.MemStats.MSpanSys)
r.Register("runtime.MemStats.NextGC", runtimeMetrics.MemStats.NextGC)
r.Register("runtime.MemStats.NumGC", runtimeMetrics.MemStats.NumGC)
r.Register("runtime.MemStats.GCCPUFraction", runtimeMetrics.MemStats.GCCPUFraction)
r.Register("runtime.MemStats.PauseNs", runtimeMetrics.MemStats.PauseNs)
r.Register("runtime.MemStats.PauseTotalNs", runtimeMetrics.MemStats.PauseTotalNs)
r.Register("runtime.MemStats.StackInuse", runtimeMetrics.MemStats.StackInuse)
Expand Down
9 changes: 9 additions & 0 deletions runtime_gccpufraction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build go1.5

package metrics

import "runtime"

func gcCPUFraction(memStats *runtime.MemStats) float64 {
return memStats.GCCPUFraction
}
9 changes: 9 additions & 0 deletions runtime_no_gccpufraction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build !go1.5

package metrics

import "runtime"

func gcCPUFraction(memStats *runtime.MemStats) float64 {
return 0
}

0 comments on commit 8f4775a

Please sign in to comment.