runtime.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package metrics
  2. import (
  3. "runtime"
  4. "time"
  5. )
  6. var (
  7. numGC uint32
  8. memStats runtime.MemStats
  9. )
  10. // Capture new values for the Go runtime statistics exported in
  11. // runtime.MemStats. This is designed to be called as a goroutine.
  12. func CaptureRuntimeMemStats(r Registry, d time.Duration) {
  13. for {
  14. CaptureRuntimeMemStatsOnce(r)
  15. time.Sleep(d)
  16. }
  17. }
  18. // Capture new values for the Go runtime statistics exported in
  19. // runtime.MemStats. This is designed to be called in a background
  20. // goroutine. Giving a registry which has not been given to
  21. // RegisterRuntimeMemStats will panic.
  22. func CaptureRuntimeMemStatsOnce(r Registry) {
  23. runtime.ReadMemStats(&memStats)
  24. r.Get("runtime.MemStats.Alloc").(Gauge).Update(int64(memStats.Alloc))
  25. r.Get("runtime.MemStats.TotalAlloc").(Gauge).Update(int64(memStats.TotalAlloc))
  26. r.Get("runtime.MemStats.Sys").(Gauge).Update(int64(memStats.Sys))
  27. r.Get("runtime.MemStats.Lookups").(Gauge).Update(int64(memStats.Lookups))
  28. r.Get("runtime.MemStats.Mallocs").(Gauge).Update(int64(memStats.Mallocs))
  29. r.Get("runtime.MemStats.Frees").(Gauge).Update(int64(memStats.Frees))
  30. r.Get("runtime.MemStats.HeapAlloc").(Gauge).Update(int64(memStats.HeapAlloc))
  31. r.Get("runtime.MemStats.HeapSys").(Gauge).Update(int64(memStats.HeapSys))
  32. r.Get("runtime.MemStats.HeapIdle").(Gauge).Update(int64(memStats.HeapIdle))
  33. r.Get("runtime.MemStats.HeapInuse").(Gauge).Update(int64(memStats.HeapInuse))
  34. r.Get("runtime.MemStats.HeapReleased").(Gauge).Update(int64(memStats.HeapReleased))
  35. r.Get("runtime.MemStats.HeapObjects").(Gauge).Update(int64(memStats.HeapObjects))
  36. r.Get("runtime.MemStats.StackInuse").(Gauge).Update(int64(memStats.StackInuse))
  37. r.Get("runtime.MemStats.StackSys").(Gauge).Update(int64(memStats.StackSys))
  38. r.Get("runtime.MemStats.MSpanInuse").(Gauge).Update(int64(memStats.MSpanInuse))
  39. r.Get("runtime.MemStats.MSpanSys").(Gauge).Update(int64(memStats.MSpanSys))
  40. r.Get("runtime.MemStats.MCacheInuse").(Gauge).Update(int64(memStats.MCacheInuse))
  41. r.Get("runtime.MemStats.MCacheSys").(Gauge).Update(int64(memStats.MCacheSys))
  42. r.Get("runtime.MemStats.BuckHashSys").(Gauge).Update(int64(memStats.BuckHashSys))
  43. r.Get("runtime.MemStats.NextGC").(Gauge).Update(int64(memStats.NextGC))
  44. r.Get("runtime.MemStats.LastGC").(Gauge).Update(int64(memStats.LastGC))
  45. r.Get("runtime.MemStats.PauseTotalNs").(Gauge).Update(int64(memStats.PauseTotalNs))
  46. // <https://code.google.com/p/go/source/browse/src/pkg/runtime/mgc0.c>
  47. for i := uint32(1); i <= memStats.NumGC-numGC; i++ {
  48. r.Get("runtime.MemStats.PauseNs").(Histogram).Update(int64(memStats.PauseNs[(memStats.NumGC%256-i)%256]))
  49. }
  50. r.Get("runtime.MemStats.NumGC").(Gauge).Update(int64(memStats.NumGC))
  51. if memStats.EnableGC {
  52. r.Get("runtime.MemStats.EnableGC").(Gauge).Update(1)
  53. } else {
  54. r.Get("runtime.MemStats.EnableGC").(Gauge).Update(0)
  55. }
  56. if memStats.EnableGC {
  57. r.Get("runtime.MemStats.DebugGC").(Gauge).Update(1)
  58. } else {
  59. r.Get("runtime.MemStats.DebugGC").(Gauge).Update(0)
  60. }
  61. r.Get("runtime.NumCgoCall").(Gauge).Update(int64(runtime.NumCgoCall()))
  62. r.Get("runtime.NumGoroutine").(Gauge).Update(int64(runtime.NumGoroutine()))
  63. }
  64. // Register metrics for the Go runtime statistics exported in
  65. // runtime.MemStats. The metrics are named by their fully-qualified
  66. // Go symbols, i.e. runtime.MemStatsAlloc. In addition to
  67. // runtime.MemStats, register the return value of runtime.Goroutines()
  68. // as runtime.Goroutines.
  69. func RegisterRuntimeMemStats(r Registry) {
  70. r.Register("runtime.MemStats.Alloc", NewGauge())
  71. r.Register("runtime.MemStats.TotalAlloc", NewGauge())
  72. r.Register("runtime.MemStats.Sys", NewGauge())
  73. r.Register("runtime.MemStats.Lookups", NewGauge())
  74. r.Register("runtime.MemStats.Mallocs", NewGauge())
  75. r.Register("runtime.MemStats.Frees", NewGauge())
  76. r.Register("runtime.MemStats.HeapAlloc", NewGauge())
  77. r.Register("runtime.MemStats.HeapSys", NewGauge())
  78. r.Register("runtime.MemStats.HeapIdle", NewGauge())
  79. r.Register("runtime.MemStats.HeapInuse", NewGauge())
  80. r.Register("runtime.MemStats.HeapReleased", NewGauge())
  81. r.Register("runtime.MemStats.HeapObjects", NewGauge())
  82. r.Register("runtime.MemStats.StackInuse", NewGauge())
  83. r.Register("runtime.MemStats.StackSys", NewGauge())
  84. r.Register("runtime.MemStats.MSpanInuse", NewGauge())
  85. r.Register("runtime.MemStats.MSpanSys", NewGauge())
  86. r.Register("runtime.MemStats.MCacheInuse", NewGauge())
  87. r.Register("runtime.MemStats.MCacheSys", NewGauge())
  88. r.Register("runtime.MemStats.BuckHashSys", NewGauge())
  89. r.Register("runtime.MemStats.NextGC", NewGauge())
  90. r.Register("runtime.MemStats.LastGC", NewGauge())
  91. r.Register("runtime.MemStats.PauseTotalNs", NewGauge())
  92. r.Register("runtime.MemStats.PauseNs", NewHistogram(NewExpDecaySample(1028, 0.015)))
  93. r.Register("runtime.MemStats.NumGC", NewGauge())
  94. r.Register("runtime.MemStats.EnableGC", NewGauge())
  95. r.Register("runtime.MemStats.DebugGC", NewGauge())
  96. r.Register("runtime.NumCgoCall", NewGauge())
  97. r.Register("runtime.NumGoroutine", NewGauge())
  98. }