github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/metrics/debug.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:41</date> 10 //</624342647804268544> 11 12 package metrics 13 14 import ( 15 "runtime/debug" 16 "time" 17 ) 18 19 var ( 20 debugMetrics struct { 21 GCStats struct { 22 LastGC Gauge 23 NumGC Gauge 24 Pause Histogram 25 //暂停序列柱状图 26 PauseTotal Gauge 27 } 28 ReadGCStats Timer 29 } 30 gcStats debug.GCStats 31 ) 32 33 //捕获在中导出的Go垃圾收集器统计信息的新值 34 //调试.gcstats。这被称为Goroutine。 35 func CaptureDebugGCStats(r Registry, d time.Duration) { 36 for range time.Tick(d) { 37 CaptureDebugGCStatsOnce(r) 38 } 39 } 40 41 //捕获在中导出的Go垃圾收集器统计信息的新值 42 //调试.gcstats。这被设计为在后台goroutine中调用。 43 //提供尚未提供给RegisterDebuggCStats的注册表将 44 //恐慌。 45 // 46 //注意(但更不用说),因为debug.readgcstats调用 47 //C函数runtime·lock(runtime·mheap),它虽然不能阻止世界 48 //手术,不是你一直想做的事情。 49 func CaptureDebugGCStatsOnce(r Registry) { 50 lastGC := gcStats.LastGC 51 t := time.Now() 52 debug.ReadGCStats(&gcStats) 53 debugMetrics.ReadGCStats.UpdateSince(t) 54 55 debugMetrics.GCStats.LastGC.Update(gcStats.LastGC.UnixNano()) 56 debugMetrics.GCStats.NumGC.Update(gcStats.NumGC) 57 if lastGC != gcStats.LastGC && 0 < len(gcStats.Pause) { 58 debugMetrics.GCStats.Pause.Update(int64(gcStats.Pause[0])) 59 } 60 //debugmetrics.gcstats.pauseQuantiles.update(gcstats.pauseQuantiles) 61 debugMetrics.GCStats.PauseTotal.Update(int64(gcStats.PauseTotal)) 62 } 63 64 //注册中导出的Go垃圾收集器统计信息的度量 65 //调试.gcstats。这些指标是通过其完全限定的GO符号命名的, 66 //即debug.gcstats.pausetotal。 67 func RegisterDebugGCStats(r Registry) { 68 debugMetrics.GCStats.LastGC = NewGauge() 69 debugMetrics.GCStats.NumGC = NewGauge() 70 debugMetrics.GCStats.Pause = NewHistogram(NewExpDecaySample(1028, 0.015)) 71 //debugmetrics.gcstats.pausequeantiles=newhistogram(newexpdecaysample(1028,0.015))。 72 debugMetrics.GCStats.PauseTotal = NewGauge() 73 debugMetrics.ReadGCStats = NewTimer() 74 75 r.Register("debug.GCStats.LastGC", debugMetrics.GCStats.LastGC) 76 r.Register("debug.GCStats.NumGC", debugMetrics.GCStats.NumGC) 77 r.Register("debug.GCStats.Pause", debugMetrics.GCStats.Pause) 78 //r.register(“debug.gcstats.pausequeantiles”,debugmetrics.gcstats.pausequeantiles) 79 r.Register("debug.GCStats.PauseTotal", debugMetrics.GCStats.PauseTotal) 80 r.Register("debug.ReadGCStats", debugMetrics.ReadGCStats) 81 } 82 83 //为gcstats分配初始切片。暂停以避免在 84 //正常运行。 85 func init() { 86 gcStats.Pause = make([]time.Duration, 11) 87 } 88