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