2018-02-23 09:56:08 +00:00
|
|
|
// Go port of Coda Hale's Metrics library
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2018-02-23 09:56:08 +00:00
|
|
|
// <https://github.com/rcrowley/go-metrics>
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2018-02-23 09:56:08 +00:00
|
|
|
// Coda Hale's original work: <https://github.com/codahale/metrics>
|
2015-06-27 15:12:58 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2015-06-29 13:11:01 +00:00
|
|
|
"os"
|
2015-06-27 15:12:58 +00:00
|
|
|
"runtime"
|
2015-06-29 13:11:01 +00:00
|
|
|
"strings"
|
2021-03-09 06:34:13 +00:00
|
|
|
"sync/atomic"
|
2015-06-27 15:12:58 +00:00
|
|
|
"time"
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
2021-02-28 04:11:28 +00:00
|
|
|
"github.com/shirou/gopsutil/v3/process"
|
2015-06-27 15:12:58 +00:00
|
|
|
)
|
|
|
|
|
2018-02-23 09:56:08 +00:00
|
|
|
// Enabled is checked by the constructor functions for all of the
|
2019-03-25 08:01:18 +00:00
|
|
|
// standard metrics. If it is true, the metric returned is a stub.
|
2018-02-23 09:56:08 +00:00
|
|
|
//
|
|
|
|
// This global kill-switch helps quantify the observer effect and makes
|
|
|
|
// for less cluttered pprof profiles.
|
2019-02-18 11:37:31 +00:00
|
|
|
var Enabled = false
|
2018-02-23 09:56:08 +00:00
|
|
|
|
2021-03-09 06:34:13 +00:00
|
|
|
// callbacks - storing list of callbacks as type []func()
|
|
|
|
// use metrics.AddCallback to add your function to metrics collection loop (to avoid multiple goroutines collecting metrics)
|
|
|
|
var callbacks atomic.Value
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
callbacks.Store([]func(){})
|
|
|
|
}
|
|
|
|
func AddCallback(collect func()) {
|
|
|
|
list := callbacks.Load().([]func())
|
|
|
|
list = append(list, collect)
|
|
|
|
callbacks.Store(list)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCallbacks() []func() {
|
|
|
|
return callbacks.Load().([]func())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calling Load method
|
|
|
|
|
2019-03-25 08:01:18 +00:00
|
|
|
// EnabledExpensive is a soft-flag meant for external packages to check if costly
|
|
|
|
// metrics gathering is allowed or not. The goal is to separate standard metrics
|
|
|
|
// for health monitoring and debug metrics that might impact runtime performance.
|
|
|
|
var EnabledExpensive = false
|
|
|
|
|
|
|
|
// enablerFlags is the CLI flag names to use to enable metrics collections.
|
2019-11-14 09:04:16 +00:00
|
|
|
var enablerFlags = []string{"metrics"}
|
2019-03-25 08:01:18 +00:00
|
|
|
|
|
|
|
// expensiveEnablerFlags is the CLI flag names to use to enable metrics collections.
|
|
|
|
var expensiveEnablerFlags = []string{"metrics.expensive"}
|
2015-06-29 13:11:01 +00:00
|
|
|
|
|
|
|
// Init enables or disables the metrics system. Since we need this to run before
|
|
|
|
// any other code gets to create meters and timers, we'll actually do an ugly hack
|
|
|
|
// and peek into the command line args for the metrics flag.
|
|
|
|
func init() {
|
|
|
|
for _, arg := range os.Args {
|
2019-03-25 08:01:18 +00:00
|
|
|
flag := strings.TrimLeft(arg, "-")
|
|
|
|
|
|
|
|
for _, enabler := range enablerFlags {
|
|
|
|
if !Enabled && flag == enabler {
|
|
|
|
log.Info("Enabling metrics collection")
|
|
|
|
Enabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, enabler := range expensiveEnablerFlags {
|
2019-03-25 08:40:46 +00:00
|
|
|
if !EnabledExpensive && flag == enabler {
|
2019-03-25 08:01:18 +00:00
|
|
|
log.Info("Enabling expensive metrics collection")
|
|
|
|
EnabledExpensive = true
|
|
|
|
}
|
2015-06-29 13:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-27 15:12:58 +00:00
|
|
|
// CollectProcessMetrics periodically collects various metrics about the running
|
|
|
|
// process.
|
|
|
|
func CollectProcessMetrics(refresh time.Duration) {
|
2015-06-29 13:11:01 +00:00
|
|
|
// Short circuit if the metrics system is disabled
|
2015-07-02 11:13:46 +00:00
|
|
|
if !Enabled {
|
2015-06-29 13:11:01 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-10 11:21:02 +00:00
|
|
|
refreshFreq := int64(refresh / time.Second)
|
|
|
|
|
2015-06-27 15:12:58 +00:00
|
|
|
// Create the various data collectors
|
2019-06-10 11:21:02 +00:00
|
|
|
cpuStats := make([]*CPUStats, 2)
|
2021-03-08 12:03:33 +00:00
|
|
|
//memstats := make([]*runtime.MemStats, 2)
|
2015-06-27 15:12:58 +00:00
|
|
|
diskstats := make([]*DiskStats, 2)
|
2021-03-08 12:03:33 +00:00
|
|
|
for i := 0; i < len(cpuStats); i++ {
|
2019-06-10 11:21:02 +00:00
|
|
|
cpuStats[i] = new(CPUStats)
|
2021-03-08 12:03:33 +00:00
|
|
|
//memstats[i] = new(runtime.MemStats)
|
2015-06-27 15:12:58 +00:00
|
|
|
diskstats[i] = new(DiskStats)
|
|
|
|
}
|
|
|
|
// Define the various metrics to collect
|
2019-06-17 07:53:17 +00:00
|
|
|
var (
|
|
|
|
cpuSysLoad = GetOrRegisterGauge("system/cpu/sysload", DefaultRegistry)
|
|
|
|
cpuSysWait = GetOrRegisterGauge("system/cpu/syswait", DefaultRegistry)
|
|
|
|
cpuThreads = GetOrRegisterGauge("system/cpu/threads", DefaultRegistry)
|
|
|
|
cpuGoroutines = GetOrRegisterGauge("system/cpu/goroutines", DefaultRegistry)
|
2019-06-10 11:21:02 +00:00
|
|
|
|
2021-03-08 12:03:33 +00:00
|
|
|
// disabled because of performance impact and because this info exists in logs
|
|
|
|
//memPauses = GetOrRegisterMeter("system/memory/pauses", DefaultRegistry)
|
|
|
|
//memAllocs = GetOrRegisterMeter("system/memory/allocs", DefaultRegistry)
|
|
|
|
//memFrees = GetOrRegisterMeter("system/memory/frees", DefaultRegistry)
|
|
|
|
//memHeld = GetOrRegisterGauge("system/memory/held", DefaultRegistry)
|
|
|
|
//memUsed = GetOrRegisterGauge("system/memory/used", DefaultRegistry)
|
2015-06-27 15:12:58 +00:00
|
|
|
|
2021-02-28 04:11:28 +00:00
|
|
|
diskReadBytes = GetOrRegisterMeter("system/disk/readbytes", DefaultRegistry)
|
|
|
|
diskWriteBytes = GetOrRegisterMeter("system/disk/writebytes", DefaultRegistry)
|
2020-05-04 22:30:51 +00:00
|
|
|
|
|
|
|
// copy from prometheus client
|
|
|
|
goGoroutines = GetOrRegisterGauge("go/goroutines", DefaultRegistry)
|
|
|
|
goThreads = GetOrRegisterGauge("go/threads", DefaultRegistry)
|
2021-02-28 04:11:28 +00:00
|
|
|
|
|
|
|
ruMinflt = GetOrRegisterGauge("ru/minflt", DefaultRegistry)
|
|
|
|
ruMajflt = GetOrRegisterGauge("ru/majflt", DefaultRegistry)
|
|
|
|
ruInblock = GetOrRegisterGauge("ru/inblock", DefaultRegistry)
|
|
|
|
ruOutblock = GetOrRegisterGauge("ru/outblock", DefaultRegistry)
|
|
|
|
ruNvcsw = GetOrRegisterGauge("ru/nvcsw", DefaultRegistry)
|
|
|
|
ruNivcsw = GetOrRegisterGauge("ru/nivcsw", DefaultRegistry)
|
|
|
|
|
2021-04-03 03:30:28 +00:00
|
|
|
/*
|
|
|
|
memRSS = GetOrRegisterGauge("mem/rss", DefaultRegistry)
|
|
|
|
memVMS = GetOrRegisterGauge("mem/vms", DefaultRegistry)
|
|
|
|
memHVM = GetOrRegisterGauge("mem/hvm", DefaultRegistry)
|
|
|
|
memData = GetOrRegisterGauge("mem/data", DefaultRegistry)
|
|
|
|
memStack = GetOrRegisterGauge("mem/stack", DefaultRegistry)
|
|
|
|
memLocked = GetOrRegisterGauge("mem/locked", DefaultRegistry)
|
|
|
|
memSwap = GetOrRegisterGauge("mem/swap", DefaultRegistry)
|
|
|
|
|
|
|
|
vmemTotal = GetOrRegisterGauge("vmem/total", DefaultRegistry)
|
|
|
|
vmemAvailable = GetOrRegisterGauge("vmem/available", DefaultRegistry)
|
|
|
|
vmemUsed = GetOrRegisterGauge("vmem/used", DefaultRegistry)
|
|
|
|
vmemBuffers = GetOrRegisterGauge("vmem/buffers", DefaultRegistry)
|
|
|
|
vmemCached = GetOrRegisterGauge("vmem/cached", DefaultRegistry)
|
|
|
|
vmemWriteBack = GetOrRegisterGauge("vmem/writeback", DefaultRegistry)
|
|
|
|
vmemDirty = GetOrRegisterGauge("vmem/dirty", DefaultRegistry)
|
|
|
|
vmemShared = GetOrRegisterGauge("vmem/shared", DefaultRegistry)
|
|
|
|
vmemMapped = GetOrRegisterGauge("vmem/mapped", DefaultRegistry)
|
|
|
|
*/
|
2019-06-17 07:53:17 +00:00
|
|
|
)
|
2020-05-04 22:30:51 +00:00
|
|
|
|
2021-02-28 04:11:28 +00:00
|
|
|
p, _ := process.NewProcess(int32(os.Getpid()))
|
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-27 15:12:58 +00:00
|
|
|
// Iterate loading the different stats and updating the meters
|
|
|
|
for i := 1; ; i++ {
|
2021-03-10 03:58:57 +00:00
|
|
|
time.Sleep(refresh)
|
2018-06-12 11:02:36 +00:00
|
|
|
location1 := i % 2
|
|
|
|
location2 := (i - 1) % 2
|
2015-06-27 15:12:58 +00:00
|
|
|
|
2021-02-28 04:11:28 +00:00
|
|
|
//ReadCPUStats(cpuStats[location1])
|
|
|
|
ReadCPUStats(p, cpuStats[location1])
|
2019-06-10 11:21:02 +00:00
|
|
|
cpuSysLoad.Update((cpuStats[location1].GlobalTime - cpuStats[location2].GlobalTime) / refreshFreq)
|
|
|
|
cpuSysWait.Update((cpuStats[location1].GlobalWait - cpuStats[location2].GlobalWait) / refreshFreq)
|
2021-02-28 04:11:28 +00:00
|
|
|
|
2021-03-03 15:12:23 +00:00
|
|
|
inblock, outblokc, nvcsw, nivcsw := getRUsage(p)
|
|
|
|
ruInblock.Update(inblock)
|
|
|
|
ruOutblock.Update(outblokc)
|
|
|
|
ruNvcsw.Update(nvcsw)
|
|
|
|
ruNivcsw.Update(nivcsw)
|
2021-02-28 04:11:28 +00:00
|
|
|
|
2019-06-17 07:53:17 +00:00
|
|
|
cpuThreads.Update(int64(threadCreateProfile.Count()))
|
|
|
|
cpuGoroutines.Update(int64(runtime.NumGoroutine()))
|
2019-06-10 11:21:02 +00:00
|
|
|
|
2021-04-03 03:30:28 +00:00
|
|
|
/*
|
|
|
|
if m, _ := mem.VirtualMemory(); m != nil {
|
|
|
|
vmemTotal.Update(int64(m.Total))
|
|
|
|
vmemAvailable.Update(int64(m.Available))
|
|
|
|
vmemUsed.Update(int64(m.Used))
|
|
|
|
vmemBuffers.Update(int64(m.Buffers))
|
|
|
|
vmemCached.Update(int64(m.Cached))
|
|
|
|
vmemWriteBack.Update(int64(m.WriteBack))
|
|
|
|
vmemDirty.Update(int64(m.Dirty))
|
|
|
|
vmemShared.Update(int64(m.Shared))
|
|
|
|
vmemMapped.Update(int64(m.Mapped))
|
|
|
|
|
|
|
|
//Slab uint64 `json:"slab"`
|
|
|
|
//Sreclaimable uint64 `json:"sreclaimable"`
|
|
|
|
//Sunreclaim uint64 `json:"sunreclaim"`
|
|
|
|
//PageTables uint64 `json:"pageTables"`
|
|
|
|
//SwapCached uint64 `json:"swapCached"`
|
|
|
|
//CommitLimit uint64 `json:"commitLimit"`
|
|
|
|
//CommittedAS uint64 `json:"committedAS"`
|
|
|
|
//HighTotal uint64 `json:"highTotal"`
|
|
|
|
//HighFree uint64 `json:"highFree"`
|
|
|
|
//LowTotal uint64 `json:"lowTotal"`
|
|
|
|
//LowFree uint64 `json:"lowFree"`
|
|
|
|
//SwapTotal uint64 `json:"swapTotal"`
|
|
|
|
//SwapFree uint64 `json:"swapFree"`
|
|
|
|
//VmallocTotal uint64 `json:"vmallocTotal"`
|
|
|
|
//VmallocUsed uint64 `json:"vmallocUsed"`
|
|
|
|
//VmallocChunk uint64 `json:"vmallocChunk"`
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
if m, _ := p.MemoryInfo(); m != nil {
|
|
|
|
memRSS.Update(int64(m.RSS))
|
|
|
|
memVMS.Update(int64(m.VMS))
|
|
|
|
memHVM.Update(int64(m.HWM))
|
|
|
|
memData.Update(int64(m.Data))
|
|
|
|
memStack.Update(int64(m.Stack))
|
|
|
|
memLocked.Update(int64(m.Locked))
|
|
|
|
memSwap.Update(int64(m.Swap))
|
|
|
|
}
|
|
|
|
*/
|
2021-02-28 04:11:28 +00:00
|
|
|
|
|
|
|
if pf, _ := p.PageFaults(); pf != nil {
|
|
|
|
ruMinflt.Update(int64(pf.MinorFaults))
|
|
|
|
ruMajflt.Update(int64(pf.MajorFaults))
|
|
|
|
}
|
|
|
|
|
2021-03-08 12:03:33 +00:00
|
|
|
//runtime.ReadMemStats(memstats[location1])
|
|
|
|
//memPauses.Mark(int64(memstats[location1].PauseTotalNs - memstats[location2].PauseTotalNs))
|
|
|
|
//memAllocs.Mark(int64(memstats[location1].Mallocs - memstats[location2].Mallocs))
|
|
|
|
//memFrees.Mark(int64(memstats[location1].Frees - memstats[location2].Frees))
|
|
|
|
//memHeld.Update(int64(memstats[location1].HeapSys - memstats[location1].HeapReleased))
|
|
|
|
//memUsed.Update(int64(memstats[location1].Alloc))
|
|
|
|
//
|
2021-02-28 04:11:28 +00:00
|
|
|
if io, _ := p.IOCounters(); io != nil {
|
|
|
|
diskstats[location1].ReadBytes = int64(io.ReadBytes)
|
|
|
|
diskstats[location1].WriteBytes = int64(io.WriteBytes)
|
2018-06-11 11:45:25 +00:00
|
|
|
diskReadBytes.Mark(diskstats[location1].ReadBytes - diskstats[location2].ReadBytes)
|
|
|
|
diskWriteBytes.Mark(diskstats[location1].WriteBytes - diskstats[location2].WriteBytes)
|
2015-06-27 15:12:58 +00:00
|
|
|
}
|
2020-05-04 22:30:51 +00:00
|
|
|
goGoroutines.Update(int64(runtime.NumGoroutine()))
|
|
|
|
n, _ := runtime.ThreadCreateProfile(nil)
|
|
|
|
goThreads.Update(int64(n))
|
|
|
|
|
2021-03-09 06:34:13 +00:00
|
|
|
for _, cb := range getCallbacks() {
|
|
|
|
cb()
|
|
|
|
}
|
2015-06-27 15:12:58 +00:00
|
|
|
}
|
|
|
|
}
|