gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/debug.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The aquachain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package metrics 18 19 import ( 20 "runtime/debug" 21 "time" 22 ) 23 24 var ( 25 debugMetrics struct { 26 GCStats struct { 27 LastGC Gauge 28 NumGC Gauge 29 Pause Histogram 30 //PauseQuantiles Histogram 31 PauseTotal Gauge 32 } 33 ReadGCStats Timer 34 } 35 gcStats debug.GCStats 36 ) 37 38 // Capture new values for the Go garbage collector statistics exported in 39 // debug.GCStats. This is designed to be called as a goroutine. 40 func CaptureDebugGCStats(r Registry, d time.Duration) { 41 for range time.Tick(d) { 42 CaptureDebugGCStatsOnce(r) 43 } 44 } 45 46 // Capture new values for the Go garbage collector statistics exported in 47 // debug.GCStats. This is designed to be called in a background goroutine. 48 // Giving a registry which has not been given to RegisterDebugGCStats will 49 // panic. 50 // 51 // Be careful (but much less so) with this because debug.ReadGCStats calls 52 // the C function runtime·lock(runtime·mheap) which, while not a stop-the-world 53 // operation, isn't something you want to be doing all the time. 54 func CaptureDebugGCStatsOnce(r Registry) { 55 lastGC := gcStats.LastGC 56 t := time.Now() 57 debug.ReadGCStats(&gcStats) 58 debugMetrics.ReadGCStats.UpdateSince(t) 59 60 debugMetrics.GCStats.LastGC.Update(gcStats.LastGC.UnixNano()) 61 debugMetrics.GCStats.NumGC.Update(gcStats.NumGC) 62 if lastGC != gcStats.LastGC && 0 < len(gcStats.Pause) { 63 debugMetrics.GCStats.Pause.Update(int64(gcStats.Pause[0])) 64 } 65 //debugMetrics.GCStats.PauseQuantiles.Update(gcStats.PauseQuantiles) 66 debugMetrics.GCStats.PauseTotal.Update(int64(gcStats.PauseTotal)) 67 } 68 69 // Register metrics for the Go garbage collector statistics exported in 70 // debug.GCStats. The metrics are named by their fully-qualified Go symbols, 71 // i.e. debug.GCStats.PauseTotal. 72 func RegisterDebugGCStats(r Registry) { 73 debugMetrics.GCStats.LastGC = NewGauge() 74 debugMetrics.GCStats.NumGC = NewGauge() 75 debugMetrics.GCStats.Pause = NewHistogram(NewExpDecaySample(1028, 0.015)) 76 //debugMetrics.GCStats.PauseQuantiles = NewHistogram(NewExpDecaySample(1028, 0.015)) 77 debugMetrics.GCStats.PauseTotal = NewGauge() 78 debugMetrics.ReadGCStats = NewTimer() 79 80 r.Register("debug.GCStats.LastGC", debugMetrics.GCStats.LastGC) 81 r.Register("debug.GCStats.NumGC", debugMetrics.GCStats.NumGC) 82 r.Register("debug.GCStats.Pause", debugMetrics.GCStats.Pause) 83 //r.Register("debug.GCStats.PauseQuantiles", debugMetrics.GCStats.PauseQuantiles) 84 r.Register("debug.GCStats.PauseTotal", debugMetrics.GCStats.PauseTotal) 85 r.Register("debug.ReadGCStats", debugMetrics.ReadGCStats) 86 } 87 88 // Allocate an initial slice for gcStats.Pause to avoid allocations during 89 // normal operation. 90 func init() { 91 gcStats.Pause = make([]time.Duration, 11) 92 }