metrics.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Go port of Coda Hale's Metrics library
  2. //
  3. // <https://github.com/rcrowley/go-metrics>
  4. //
  5. // Coda Hale's original work: <https://github.com/codahale/metrics>
  6. package metrics
  7. import (
  8. "os"
  9. "runtime"
  10. "strings"
  11. "time"
  12. "github.com/ethereum/go-ethereum/log"
  13. )
  14. // Enabled is checked by the constructor functions for all of the
  15. // standard metrics. If it is true, the metric returned is a stub.
  16. //
  17. // This global kill-switch helps quantify the observer effect and makes
  18. // for less cluttered pprof profiles.
  19. var Enabled = false
  20. // EnabledExpensive is a soft-flag meant for external packages to check if costly
  21. // metrics gathering is allowed or not. The goal is to separate standard metrics
  22. // for health monitoring and debug metrics that might impact runtime performance.
  23. var EnabledExpensive = false
  24. // enablerFlags is the CLI flag names to use to enable metrics collections.
  25. var enablerFlags = []string{"metrics"}
  26. // expensiveEnablerFlags is the CLI flag names to use to enable metrics collections.
  27. var expensiveEnablerFlags = []string{"metrics.expensive"}
  28. // Init enables or disables the metrics system. Since we need this to run before
  29. // any other code gets to create meters and timers, we'll actually do an ugly hack
  30. // and peek into the command line args for the metrics flag.
  31. func init() {
  32. for _, arg := range os.Args {
  33. flag := strings.TrimLeft(arg, "-")
  34. for _, enabler := range enablerFlags {
  35. if !Enabled && flag == enabler {
  36. log.Info("Enabling metrics collection")
  37. Enabled = true
  38. }
  39. }
  40. for _, enabler := range expensiveEnablerFlags {
  41. if !EnabledExpensive && flag == enabler {
  42. log.Info("Enabling expensive metrics collection")
  43. EnabledExpensive = true
  44. }
  45. }
  46. }
  47. }
  48. // CollectProcessMetrics periodically collects various metrics about the running
  49. // process.
  50. func CollectProcessMetrics(refresh time.Duration) {
  51. // Short circuit if the metrics system is disabled
  52. if !Enabled {
  53. return
  54. }
  55. refreshFreq := int64(refresh / time.Second)
  56. // Create the various data collectors
  57. cpuStats := make([]*CPUStats, 2)
  58. memstats := make([]*runtime.MemStats, 2)
  59. diskstats := make([]*DiskStats, 2)
  60. for i := 0; i < len(memstats); i++ {
  61. cpuStats[i] = new(CPUStats)
  62. memstats[i] = new(runtime.MemStats)
  63. diskstats[i] = new(DiskStats)
  64. }
  65. // Define the various metrics to collect
  66. var (
  67. cpuSysLoad = GetOrRegisterGauge("system/cpu/sysload", DefaultRegistry)
  68. cpuSysWait = GetOrRegisterGauge("system/cpu/syswait", DefaultRegistry)
  69. cpuProcLoad = GetOrRegisterGauge("system/cpu/procload", DefaultRegistry)
  70. cpuThreads = GetOrRegisterGauge("system/cpu/threads", DefaultRegistry)
  71. cpuGoroutines = GetOrRegisterGauge("system/cpu/goroutines", DefaultRegistry)
  72. memPauses = GetOrRegisterMeter("system/memory/pauses", DefaultRegistry)
  73. memAllocs = GetOrRegisterMeter("system/memory/allocs", DefaultRegistry)
  74. memFrees = GetOrRegisterMeter("system/memory/frees", DefaultRegistry)
  75. memHeld = GetOrRegisterGauge("system/memory/held", DefaultRegistry)
  76. memUsed = GetOrRegisterGauge("system/memory/used", DefaultRegistry)
  77. diskReads = GetOrRegisterMeter("system/disk/readcount", DefaultRegistry)
  78. diskReadBytes = GetOrRegisterMeter("system/disk/readdata", DefaultRegistry)
  79. diskReadBytesCounter = GetOrRegisterCounter("system/disk/readbytes", DefaultRegistry)
  80. diskWrites = GetOrRegisterMeter("system/disk/writecount", DefaultRegistry)
  81. diskWriteBytes = GetOrRegisterMeter("system/disk/writedata", DefaultRegistry)
  82. diskWriteBytesCounter = GetOrRegisterCounter("system/disk/writebytes", DefaultRegistry)
  83. )
  84. // Iterate loading the different stats and updating the meters
  85. for i := 1; ; i++ {
  86. location1 := i % 2
  87. location2 := (i - 1) % 2
  88. ReadCPUStats(cpuStats[location1])
  89. cpuSysLoad.Update((cpuStats[location1].GlobalTime - cpuStats[location2].GlobalTime) / refreshFreq)
  90. cpuSysWait.Update((cpuStats[location1].GlobalWait - cpuStats[location2].GlobalWait) / refreshFreq)
  91. cpuProcLoad.Update((cpuStats[location1].LocalTime - cpuStats[location2].LocalTime) / refreshFreq)
  92. cpuThreads.Update(int64(threadCreateProfile.Count()))
  93. cpuGoroutines.Update(int64(runtime.NumGoroutine()))
  94. runtime.ReadMemStats(memstats[location1])
  95. memPauses.Mark(int64(memstats[location1].PauseTotalNs - memstats[location2].PauseTotalNs))
  96. memAllocs.Mark(int64(memstats[location1].Mallocs - memstats[location2].Mallocs))
  97. memFrees.Mark(int64(memstats[location1].Frees - memstats[location2].Frees))
  98. memHeld.Update(int64(memstats[location1].HeapSys - memstats[location1].HeapReleased))
  99. memUsed.Update(int64(memstats[location1].Alloc))
  100. if ReadDiskStats(diskstats[location1]) == nil {
  101. diskReads.Mark(diskstats[location1].ReadCount - diskstats[location2].ReadCount)
  102. diskReadBytes.Mark(diskstats[location1].ReadBytes - diskstats[location2].ReadBytes)
  103. diskWrites.Mark(diskstats[location1].WriteCount - diskstats[location2].WriteCount)
  104. diskWriteBytes.Mark(diskstats[location1].WriteBytes - diskstats[location2].WriteBytes)
  105. diskReadBytesCounter.Inc(diskstats[location1].ReadBytes - diskstats[location2].ReadBytes)
  106. diskWriteBytesCounter.Inc(diskstats[location1].WriteBytes - diskstats[location2].WriteBytes)
  107. }
  108. time.Sleep(refresh)
  109. }
  110. }