From 6a5f86423c29ddacf9b446741f86e901c116ba63 Mon Sep 17 00:00:00 2001 From: Alex Sharov <AskAlexSharov@gmail.com> Date: Sat, 3 Jul 2021 14:44:23 +0700 Subject: [PATCH] Remove "numgc" from logs (#2275) --- .github/workflows/ci.yml | 2 +- Makefile | 2 +- cmd/hack/hack.go | 12 +- common/etl/collector.go | 4 +- common/etl/dataprovider.go | 2 +- common/etl/etl.go | 2 +- common/etl/heap.go | 5 +- eth/stagedsync/stage_bodies.go | 3 +- eth/stagedsync/stage_call_traces.go | 9 +- eth/stagedsync/stage_execute.go | 3 +- eth/stagedsync/stage_headers.go | 3 +- metrics/debug.go | 76 ---------- metrics/debug_test.go | 9 -- metrics/metrics.go | 12 +- metrics/metrics_test.go | 95 ------------- metrics/runtime.go | 212 ---------------------------- metrics/runtime_gccpufraction.go | 9 -- metrics/runtime_no_gccpufraction.go | 9 -- metrics/runtime_test.go | 51 ------- 19 files changed, 25 insertions(+), 495 deletions(-) delete mode 100644 metrics/debug.go delete mode 100644 metrics/runtime.go delete mode 100644 metrics/runtime_gccpufraction.go delete mode 100644 metrics/runtime_no_gccpufraction.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8b77fe8a50..c69428160b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,7 +42,7 @@ jobs: if: matrix.os == 'ubuntu-20.04' uses: golangci/golangci-lint-action@v2 with: - version: v1.40 + version: v1.41 skip-go-installation: true skip-pkg-cache: true skip-build-cache: true diff --git a/Makefile b/Makefile index 78945ad8b5..32af0af515 100644 --- a/Makefile +++ b/Makefile @@ -123,7 +123,7 @@ lintci: lintci-deps: rm -f ./build/bin/golangci-lint - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b ./build/bin v1.40.1 + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b ./build/bin v1.41.1 clean: env GO111MODULE=on go clean -cache diff --git a/cmd/hack/hack.go b/cmd/hack/hack.go index b4bc603154..10f7f0c523 100644 --- a/cmd/hack/hack.go +++ b/cmd/hack/hack.go @@ -1027,7 +1027,7 @@ func testGetProof(chaindata string, address common.Address, rewind int, regen bo headNumber := rawdb.ReadHeaderNumber(tx, headHash) block := *headNumber - uint64(rewind) log.Info("GetProof", "address", address, "storage keys", len(storageKeys), "head", *headNumber, "block", block, - "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC)) + "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys)) ts := dbutils.EncodeBlockNumber(block + 1) accountMap := make(map[string]*accounts.Account) @@ -1060,7 +1060,7 @@ func testGetProof(chaindata string, address common.Address, rewind int, regen bo } runtime.ReadMemStats(&m) log.Info("Constructed account map", "size", len(accountMap), - "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC)) + "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys)) storageMap := make(map[string][]byte) if err := changeset.Walk(tx.(ethdb.HasTx).Tx(), dbutils.StorageChangeSetBucket, ts, 0, func(blockN uint64, address, v []byte) (bool, error) { if blockN > *headNumber { @@ -1080,7 +1080,7 @@ func testGetProof(chaindata string, address common.Address, rewind int, regen bo } runtime.ReadMemStats(&m) log.Info("Constructed storage map", "size", len(storageMap), - "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC)) + "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys)) var unfurlList = make([]string, len(accountMap)+len(storageMap)) unfurl := trie.NewRetainList(0) i := 0 @@ -1125,7 +1125,7 @@ func testGetProof(chaindata string, address common.Address, rewind int, regen bo sort.Strings(unfurlList) runtime.ReadMemStats(&m) log.Info("Constructed account unfurl lists", - "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC)) + "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys)) loader := trie.NewFlatDBTrieLoader("checkRoots") if err = loader.Reset(unfurl, nil, nil, false); err != nil { @@ -1144,13 +1144,13 @@ func testGetProof(chaindata string, address common.Address, rewind int, regen bo } runtime.ReadMemStats(&m) log.Info("Loaded subtries", - "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC)) + "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys)) hash, err := rawdb.ReadCanonicalHash(tx, block) tool.Check(err) header := rawdb.ReadHeader(tx, hash, block) runtime.ReadMemStats(&m) log.Info("Constructed trie", - "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC)) + "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys)) fmt.Printf("Resulting root: %x, expected root: %x\n", root, header.Root) return nil } diff --git a/common/etl/collector.go b/common/etl/collector.go index b0ec605b09..f624379c64 100644 --- a/common/etl/collector.go +++ b/common/etl/collector.go @@ -185,7 +185,7 @@ func loadFilesIntoBucket(logPrefix string, db ethdb.RwTx, bucket string, provide } runtime.ReadMemStats(&m) - logArs = append(logArs, "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC)) + logArs = append(logArs, "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys)) log.Info(fmt.Sprintf("[%s] ETL [2/2] Loading", logPrefix), logArs...) } @@ -241,7 +241,7 @@ func loadFilesIntoBucket(logPrefix string, db ethdb.RwTx, bucket string, provide "bucket", bucket, "records", i, "current key", makeCurrentKeyStr(nil), - "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC)) + "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys)) return nil } diff --git a/common/etl/dataprovider.go b/common/etl/dataprovider.go index 408161932f..22664d9935 100644 --- a/common/etl/dataprovider.go +++ b/common/etl/dataprovider.go @@ -55,7 +55,7 @@ func FlushToDisk(encoder Encoder, currentKey []byte, b Buffer, tmpdir string) (d log.Info( "Flushed buffer file", "name", bufferFile.Name(), - "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC)) + "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys)) }() encoder.Reset(w) diff --git a/common/etl/etl.go b/common/etl/etl.go index 0feb4b497a..a82ea77c8b 100644 --- a/common/etl/etl.go +++ b/common/etl/etl.go @@ -140,7 +140,7 @@ func extractBucketIntoFiles( } runtime.ReadMemStats(&m) - logArs = append(logArs, "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC)) + logArs = append(logArs, "alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys)) log.Info(fmt.Sprintf("[%s] ETL [1/2] Extracting", logPrefix), logArs...) } if endkey != nil && bytes.Compare(k, endkey) > 0 { diff --git a/common/etl/heap.go b/common/etl/heap.go index 11ad30c809..2637cfa30f 100644 --- a/common/etl/heap.go +++ b/common/etl/heap.go @@ -2,6 +2,7 @@ package etl import ( "bytes" + "github.com/ledgerwatch/erigon/common/dbutils" ) @@ -16,10 +17,6 @@ type Heap struct { elems []HeapElem } -func (h Heap) SetComparator(cmp dbutils.CmpFunc) { - h.comparator = cmp -} - func (h Heap) Len() int { return len(h.elems) } diff --git a/eth/stagedsync/stage_bodies.go b/eth/stagedsync/stage_bodies.go index 7edc3f7506..31fa15e9f1 100644 --- a/eth/stagedsync/stage_bodies.go +++ b/eth/stagedsync/stage_bodies.go @@ -225,8 +225,7 @@ func logProgressBodies(logPrefix string, committed uint64, prevDeliveredCount, d "delivery /second", common.StorageSize(speed), "wasted /second", common.StorageSize(wastedSpeed), "alloc", common.StorageSize(m.Alloc), - "sys", common.StorageSize(m.Sys), - "numGC", int(m.NumGC)) + "sys", common.StorageSize(m.Sys)) } func UnwindBodiesStage(u *UnwindState, s *StageState, tx ethdb.RwTx, cfg BodiesCfg) error { diff --git a/eth/stagedsync/stage_call_traces.go b/eth/stagedsync/stage_call_traces.go index 0842fb1c55..1acf5953db 100644 --- a/eth/stagedsync/stage_call_traces.go +++ b/eth/stagedsync/stage_call_traces.go @@ -148,8 +148,7 @@ func promoteCallTraces(logPrefix string, tx ethdb.RwTx, startBlock, endBlock uin log.Info(fmt.Sprintf("[%s] Progress", logPrefix), "number", blockNum, "blk/second", speed, "alloc", common.StorageSize(m.Alloc), - "sys", common.StorageSize(m.Sys), - "numGC", int(m.NumGC)) + "sys", common.StorageSize(m.Sys)) case <-checkFlushEvery.C: if needFlush64(froms, bufLimit) { if err := flushBitmaps64(collectorFrom, froms); err != nil { @@ -192,8 +191,7 @@ func promoteCallTraces(logPrefix string, tx ethdb.RwTx, startBlock, endBlock uin runtime.ReadMemStats(&m) log.Info(fmt.Sprintf("[%s] Pruning call trace intermediate table", logPrefix), "number", blockNum, "alloc", common.StorageSize(m.Alloc), - "sys", common.StorageSize(m.Sys), - "numGC", int(m.NumGC)) + "sys", common.StorageSize(m.Sys)) } if err = traceCursor.DeleteCurrentDuplicates(); err != nil { return fmt.Errorf("%s: failed to remove trace call set for block %d: %v", logPrefix, blockNum, err) @@ -334,8 +332,7 @@ func unwindCallTraces(logPrefix string, db ethdb.RwTx, from, to uint64, quitCh < log.Info(fmt.Sprintf("[%s] Progress", logPrefix), "number", blockNum, "blk/second", speed, "alloc", common.StorageSize(m.Alloc), - "sys", common.StorageSize(m.Sys), - "numGC", int(m.NumGC)) + "sys", common.StorageSize(m.Sys)) } } if err != nil { diff --git a/eth/stagedsync/stage_execute.go b/eth/stagedsync/stage_execute.go index b5eb1caff6..f130bfe45c 100644 --- a/eth/stagedsync/stage_execute.go +++ b/eth/stagedsync/stage_execute.go @@ -385,8 +385,7 @@ func pruneDupSortedBucket(tx ethdb.RwTx, logPrefix string, name string, tableNam runtime.ReadMemStats(&m) log.Info(fmt.Sprintf("[%s] Pruning", logPrefix), "table", tableName, "number", blockNum, "alloc", common.StorageSize(m.Alloc), - "sys", common.StorageSize(m.Sys), - "numGC", int(m.NumGC)) + "sys", common.StorageSize(m.Sys)) } if err = changeSetCursor.DeleteCurrentDuplicates(); err != nil { return fmt.Errorf("%s: failed to remove %s for block %d: %v", logPrefix, name, blockNum, err) diff --git a/eth/stagedsync/stage_headers.go b/eth/stagedsync/stage_headers.go index 9772170b62..e7f8b40e69 100644 --- a/eth/stagedsync/stage_headers.go +++ b/eth/stagedsync/stage_headers.go @@ -338,8 +338,7 @@ func logProgressHeaders(logPrefix string, prev, now uint64) uint64 { "number", now, "blk/second", speed, "alloc", common.StorageSize(m.Alloc), - "sys", common.StorageSize(m.Sys), - "numGC", int(m.NumGC)) + "sys", common.StorageSize(m.Sys)) return now } diff --git a/metrics/debug.go b/metrics/debug.go deleted file mode 100644 index de4a2739fe..0000000000 --- a/metrics/debug.go +++ /dev/null @@ -1,76 +0,0 @@ -package metrics - -import ( - "runtime/debug" - "time" -) - -var ( - debugMetrics struct { - GCStats struct { - LastGC Gauge - NumGC Gauge - Pause Histogram - //PauseQuantiles Histogram - PauseTotal Gauge - } - ReadGCStats Timer - } - gcStats debug.GCStats -) - -// Capture new values for the Go garbage collector statistics exported in -// debug.GCStats. This is designed to be called as a goroutine. -func CaptureDebugGCStats(r Registry, d time.Duration) { - for range time.Tick(d) { - CaptureDebugGCStatsOnce(r) - } -} - -// Capture new values for the Go garbage collector statistics exported in -// debug.GCStats. This is designed to be called in a background goroutine. -// Giving a registry which has not been given to RegisterDebugGCStats will -// panic. -// -// Be careful (but much less so) with this because debug.ReadGCStats calls -// the C function runtime·lock(runtime·mheap) which, while not a stop-the-world -// operation, isn't something you want to be doing all the time. -func CaptureDebugGCStatsOnce(r Registry) { - lastGC := gcStats.LastGC - t := time.Now() - debug.ReadGCStats(&gcStats) - debugMetrics.ReadGCStats.UpdateSince(t) - - debugMetrics.GCStats.LastGC.Update(gcStats.LastGC.UnixNano()) - debugMetrics.GCStats.NumGC.Update(gcStats.NumGC) - if lastGC != gcStats.LastGC && 0 < len(gcStats.Pause) { - debugMetrics.GCStats.Pause.Update(int64(gcStats.Pause[0])) - } - //debugMetrics.GCStats.PauseQuantiles.Update(gcStats.PauseQuantiles) - debugMetrics.GCStats.PauseTotal.Update(int64(gcStats.PauseTotal)) -} - -// Register metrics for the Go garbage collector statistics exported in -// debug.GCStats. The metrics are named by their fully-qualified Go symbols, -// i.e. debug.GCStats.PauseTotal. -func RegisterDebugGCStats(r Registry) { - debugMetrics.GCStats.LastGC = NewGauge() - debugMetrics.GCStats.NumGC = NewGauge() - debugMetrics.GCStats.Pause = NewHistogram(NewExpDecaySample(1028, 0.015)) - //debugMetrics.GCStats.PauseQuantiles = NewHistogram(NewExpDecaySample(1028, 0.015)) - debugMetrics.GCStats.PauseTotal = NewGauge() - debugMetrics.ReadGCStats = NewTimer() - - r.Register("debug.GCStats.LastGC", debugMetrics.GCStats.LastGC) - r.Register("debug.GCStats.NumGC", debugMetrics.GCStats.NumGC) - r.Register("debug.GCStats.Pause", debugMetrics.GCStats.Pause) - //r.Register("debug.GCStats.PauseQuantiles", debugMetrics.GCStats.PauseQuantiles) - r.Register("debug.GCStats.PauseTotal", debugMetrics.GCStats.PauseTotal) - r.Register("debug.ReadGCStats", debugMetrics.ReadGCStats) -} - -// Allocate an initial slice for gcStats.Pause to avoid allocations during -// normal operation. -func init() { - gcStats.Pause = make([]time.Duration, 11) -} diff --git a/metrics/debug_test.go b/metrics/debug_test.go index 07eb867841..c4fe1a87bf 100644 --- a/metrics/debug_test.go +++ b/metrics/debug_test.go @@ -7,15 +7,6 @@ import ( "time" ) -func BenchmarkDebugGCStats(b *testing.B) { - r := NewRegistry() - RegisterDebugGCStats(r) - b.ResetTimer() - for i := 0; i < b.N; i++ { - CaptureDebugGCStatsOnce(r) - } -} - func TestDebugGCStatsBlocking(t *testing.T) { if g := runtime.GOMAXPROCS(0); g < 2 { t.Skipf("skipping TestDebugGCMemStatsBlocking with GOMAXPROCS=%d\n", g) diff --git a/metrics/metrics.go b/metrics/metrics.go index 7fd84fbe3a..ac9eddd370 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -8,6 +8,7 @@ package metrics import ( "os" "runtime" + "runtime/metrics" "strings" "sync/atomic" "time" @@ -30,6 +31,7 @@ var Enabled = false var callbacks atomic.Value func init() { + metrics.All() callbacks.Store([]func(){}) } func AddCallback(collect func()) { @@ -98,10 +100,8 @@ func CollectProcessMetrics(refresh time.Duration) { } // Define the various metrics to collect 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) + cpuSysLoad = GetOrRegisterGauge("system/cpu/sysload", DefaultRegistry) + cpuSysWait = GetOrRegisterGauge("system/cpu/syswait", DefaultRegistry) // disabled because of performance impact and because this info exists in logs memPauses = GetOrRegisterMeter("system/memory/pauses", DefaultRegistry) @@ -116,6 +116,7 @@ func CollectProcessMetrics(refresh time.Duration) { // copy from prometheus client goGoroutines = GetOrRegisterGauge("go/goroutines", DefaultRegistry) goThreads = GetOrRegisterGauge("go/threads", DefaultRegistry) + cgoCalls = GetOrRegisterGauge("go/cgo", DefaultRegistry) ruMinflt = GetOrRegisterGauge("ru/minflt", DefaultRegistry) ruMajflt = GetOrRegisterGauge("ru/majflt", DefaultRegistry) @@ -164,8 +165,7 @@ func CollectProcessMetrics(refresh time.Duration) { ruNvcsw.Update(nvcsw) ruNivcsw.Update(nivcsw) - cpuThreads.Update(int64(threadCreateProfile.Count())) - cpuGoroutines.Update(int64(runtime.NumGoroutine())) + cgoCalls.Update(numCgoCall()) if m, _ := mem.VirtualMemory(); m != nil { vmemTotal.Update(int64(m.Total)) diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index df36da0ade..1743f0c6d9 100644 --- a/metrics/metrics_test.go +++ b/metrics/metrics_test.go @@ -4,110 +4,15 @@ import ( "fmt" "io/ioutil" "log" - "sync" - "testing" "time" ) -const FANOUT = 128 - // Stop the compiler from complaining during debugging. var ( _ = ioutil.Discard _ = log.LstdFlags ) -func BenchmarkMetrics(b *testing.B) { - r := NewRegistry() - c := NewRegisteredCounter("counter", r) - g := NewRegisteredGauge("gauge", r) - gf := NewRegisteredGaugeFloat64("gaugefloat64", r) - h := NewRegisteredHistogram("histogram", r, NewUniformSample(100)) - m := NewRegisteredMeter("meter", r) - t := NewRegisteredTimer("timer", r) - RegisterDebugGCStats(r) - RegisterRuntimeMemStats(r) - b.ResetTimer() - ch := make(chan bool) - - wgD := &sync.WaitGroup{} - /* - wgD.Add(1) - go func() { - defer wgD.Done() - //log.Println("go CaptureDebugGCStats") - for { - select { - case <-ch: - //log.Println("done CaptureDebugGCStats") - return - default: - CaptureDebugGCStatsOnce(r) - } - } - }() - //*/ - - wgR := &sync.WaitGroup{} - //* - wgR.Add(1) - go func() { - defer wgR.Done() - //log.Println("go CaptureRuntimeMemStats") - for { - select { - case <-ch: - //log.Println("done CaptureRuntimeMemStats") - return - default: - CaptureRuntimeMemStatsOnce(r) - } - } - }() - //*/ - - wgW := &sync.WaitGroup{} - /* - wgW.Add(1) - go func() { - defer wgW.Done() - //log.Println("go Write") - for { - select { - case <-ch: - //log.Println("done Write") - return - default: - WriteOnce(r, ioutil.Discard) - } - } - }() - //*/ - - wg := &sync.WaitGroup{} - wg.Add(FANOUT) - for i := 0; i < FANOUT; i++ { - go func(i int) { - defer wg.Done() - //log.Println("go", i) - for i := 0; i < b.N; i++ { - c.Inc(1) - g.Update(int64(i)) - gf.Update(float64(i)) - h.Update(int64(i)) - m.Mark(1) - t.Update(1) - } - //log.Println("done", i) - }(i) - } - wg.Wait() - close(ch) - wgD.Wait() - wgR.Wait() - wgW.Wait() -} - func Example() { c := NewCounter() Register("money", c) diff --git a/metrics/runtime.go b/metrics/runtime.go deleted file mode 100644 index 9450c479ba..0000000000 --- a/metrics/runtime.go +++ /dev/null @@ -1,212 +0,0 @@ -package metrics - -import ( - "runtime" - "runtime/pprof" - "time" -) - -var ( - memStats runtime.MemStats - runtimeMetrics struct { - MemStats struct { - Alloc Gauge - BuckHashSys Gauge - DebugGC Gauge - EnableGC Gauge - Frees Gauge - HeapAlloc Gauge - HeapIdle Gauge - HeapInuse Gauge - HeapObjects Gauge - HeapReleased Gauge - HeapSys Gauge - LastGC Gauge - Lookups Gauge - Mallocs Gauge - MCacheInuse Gauge - MCacheSys Gauge - MSpanInuse Gauge - MSpanSys Gauge - NextGC Gauge - NumGC Gauge - GCCPUFraction GaugeFloat64 - PauseNs Histogram - PauseTotalNs Gauge - StackInuse Gauge - StackSys Gauge - Sys Gauge - TotalAlloc Gauge - } - NumCgoCall Gauge - NumGoroutine Gauge - NumThread Gauge - ReadMemStats Timer - } - frees uint64 - lookups uint64 - mallocs uint64 - numGC uint32 - numCgoCalls int64 - - threadCreateProfile = pprof.Lookup("threadcreate") -) - -// Capture new values for the Go runtime statistics exported in -// runtime.MemStats. This is designed to be called as a goroutine. -func CaptureRuntimeMemStats(r Registry, d time.Duration) { - for range time.Tick(d) { - CaptureRuntimeMemStatsOnce(r) - } -} - -// Capture new values for the Go runtime statistics exported in -// runtime.MemStats. This is designed to be called in a background -// goroutine. Giving a registry which has not been given to -// RegisterRuntimeMemStats will panic. -// -// Be very careful with this because runtime.ReadMemStats calls the C -// functions runtime·semacquire(&runtime·worldsema) and runtime·stoptheworld() -// and that last one does what it says on the tin. -func CaptureRuntimeMemStatsOnce(r Registry) { - t := time.Now() - runtime.ReadMemStats(&memStats) // This takes 50-200us. - runtimeMetrics.ReadMemStats.UpdateSince(t) - - runtimeMetrics.MemStats.Alloc.Update(int64(memStats.Alloc)) - runtimeMetrics.MemStats.BuckHashSys.Update(int64(memStats.BuckHashSys)) - if memStats.DebugGC { - runtimeMetrics.MemStats.DebugGC.Update(1) - } else { - runtimeMetrics.MemStats.DebugGC.Update(0) - } - if memStats.EnableGC { - runtimeMetrics.MemStats.EnableGC.Update(1) - } else { - runtimeMetrics.MemStats.EnableGC.Update(0) - } - - runtimeMetrics.MemStats.Frees.Update(int64(memStats.Frees - frees)) - runtimeMetrics.MemStats.HeapAlloc.Update(int64(memStats.HeapAlloc)) - runtimeMetrics.MemStats.HeapIdle.Update(int64(memStats.HeapIdle)) - runtimeMetrics.MemStats.HeapInuse.Update(int64(memStats.HeapInuse)) - runtimeMetrics.MemStats.HeapObjects.Update(int64(memStats.HeapObjects)) - runtimeMetrics.MemStats.HeapReleased.Update(int64(memStats.HeapReleased)) - runtimeMetrics.MemStats.HeapSys.Update(int64(memStats.HeapSys)) - runtimeMetrics.MemStats.LastGC.Update(int64(memStats.LastGC)) - runtimeMetrics.MemStats.Lookups.Update(int64(memStats.Lookups - lookups)) - runtimeMetrics.MemStats.Mallocs.Update(int64(memStats.Mallocs - mallocs)) - runtimeMetrics.MemStats.MCacheInuse.Update(int64(memStats.MCacheInuse)) - runtimeMetrics.MemStats.MCacheSys.Update(int64(memStats.MCacheSys)) - runtimeMetrics.MemStats.MSpanInuse.Update(int64(memStats.MSpanInuse)) - runtimeMetrics.MemStats.MSpanSys.Update(int64(memStats.MSpanSys)) - runtimeMetrics.MemStats.NextGC.Update(int64(memStats.NextGC)) - runtimeMetrics.MemStats.NumGC.Update(int64(memStats.NumGC - numGC)) - runtimeMetrics.MemStats.GCCPUFraction.Update(gcCPUFraction(&memStats)) - - // <https://code.google.com/p/go/source/browse/src/pkg/runtime/mgc0.c> - i := numGC % uint32(len(memStats.PauseNs)) - ii := memStats.NumGC % uint32(len(memStats.PauseNs)) - if memStats.NumGC-numGC >= uint32(len(memStats.PauseNs)) { - for i = 0; i < uint32(len(memStats.PauseNs)); i++ { - runtimeMetrics.MemStats.PauseNs.Update(int64(memStats.PauseNs[i])) - } - } else { - if i > ii { - for ; i < uint32(len(memStats.PauseNs)); i++ { - runtimeMetrics.MemStats.PauseNs.Update(int64(memStats.PauseNs[i])) - } - i = 0 - } - for ; i < ii; i++ { - runtimeMetrics.MemStats.PauseNs.Update(int64(memStats.PauseNs[i])) - } - } - frees = memStats.Frees - lookups = memStats.Lookups - mallocs = memStats.Mallocs - numGC = memStats.NumGC - - runtimeMetrics.MemStats.PauseTotalNs.Update(int64(memStats.PauseTotalNs)) - runtimeMetrics.MemStats.StackInuse.Update(int64(memStats.StackInuse)) - runtimeMetrics.MemStats.StackSys.Update(int64(memStats.StackSys)) - runtimeMetrics.MemStats.Sys.Update(int64(memStats.Sys)) - runtimeMetrics.MemStats.TotalAlloc.Update(int64(memStats.TotalAlloc)) - - currentNumCgoCalls := numCgoCall() - runtimeMetrics.NumCgoCall.Update(currentNumCgoCalls - numCgoCalls) - numCgoCalls = currentNumCgoCalls - - runtimeMetrics.NumGoroutine.Update(int64(runtime.NumGoroutine())) - - runtimeMetrics.NumThread.Update(int64(threadCreateProfile.Count())) -} - -// Register runtimeMetrics for the Go runtime statistics exported in runtime and -// specifically runtime.MemStats. The runtimeMetrics are named by their -// fully-qualified Go symbols, i.e. runtime.MemStats.Alloc. -func RegisterRuntimeMemStats(r Registry) { - runtimeMetrics.MemStats.Alloc = NewGauge() - runtimeMetrics.MemStats.BuckHashSys = NewGauge() - runtimeMetrics.MemStats.DebugGC = NewGauge() - runtimeMetrics.MemStats.EnableGC = NewGauge() - runtimeMetrics.MemStats.Frees = NewGauge() - runtimeMetrics.MemStats.HeapAlloc = NewGauge() - runtimeMetrics.MemStats.HeapIdle = NewGauge() - runtimeMetrics.MemStats.HeapInuse = NewGauge() - runtimeMetrics.MemStats.HeapObjects = NewGauge() - runtimeMetrics.MemStats.HeapReleased = NewGauge() - runtimeMetrics.MemStats.HeapSys = NewGauge() - runtimeMetrics.MemStats.LastGC = NewGauge() - runtimeMetrics.MemStats.Lookups = NewGauge() - runtimeMetrics.MemStats.Mallocs = NewGauge() - runtimeMetrics.MemStats.MCacheInuse = NewGauge() - runtimeMetrics.MemStats.MCacheSys = NewGauge() - runtimeMetrics.MemStats.MSpanInuse = NewGauge() - runtimeMetrics.MemStats.MSpanSys = NewGauge() - runtimeMetrics.MemStats.NextGC = NewGauge() - runtimeMetrics.MemStats.NumGC = NewGauge() - runtimeMetrics.MemStats.GCCPUFraction = NewGaugeFloat64() - runtimeMetrics.MemStats.PauseNs = NewHistogram(NewExpDecaySample(1028, 0.015)) - runtimeMetrics.MemStats.PauseTotalNs = NewGauge() - runtimeMetrics.MemStats.StackInuse = NewGauge() - runtimeMetrics.MemStats.StackSys = NewGauge() - runtimeMetrics.MemStats.Sys = NewGauge() - runtimeMetrics.MemStats.TotalAlloc = NewGauge() - runtimeMetrics.NumCgoCall = NewGauge() - runtimeMetrics.NumGoroutine = NewGauge() - runtimeMetrics.NumThread = NewGauge() - runtimeMetrics.ReadMemStats = NewTimer() - - r.Register("runtime.MemStats.Alloc", runtimeMetrics.MemStats.Alloc) - r.Register("runtime.MemStats.BuckHashSys", runtimeMetrics.MemStats.BuckHashSys) - r.Register("runtime.MemStats.DebugGC", runtimeMetrics.MemStats.DebugGC) - r.Register("runtime.MemStats.EnableGC", runtimeMetrics.MemStats.EnableGC) - r.Register("runtime.MemStats.Frees", runtimeMetrics.MemStats.Frees) - r.Register("runtime.MemStats.HeapAlloc", runtimeMetrics.MemStats.HeapAlloc) - r.Register("runtime.MemStats.HeapIdle", runtimeMetrics.MemStats.HeapIdle) - r.Register("runtime.MemStats.HeapInuse", runtimeMetrics.MemStats.HeapInuse) - r.Register("runtime.MemStats.HeapObjects", runtimeMetrics.MemStats.HeapObjects) - r.Register("runtime.MemStats.HeapReleased", runtimeMetrics.MemStats.HeapReleased) - r.Register("runtime.MemStats.HeapSys", runtimeMetrics.MemStats.HeapSys) - r.Register("runtime.MemStats.LastGC", runtimeMetrics.MemStats.LastGC) - r.Register("runtime.MemStats.Lookups", runtimeMetrics.MemStats.Lookups) - r.Register("runtime.MemStats.Mallocs", runtimeMetrics.MemStats.Mallocs) - r.Register("runtime.MemStats.MCacheInuse", runtimeMetrics.MemStats.MCacheInuse) - r.Register("runtime.MemStats.MCacheSys", runtimeMetrics.MemStats.MCacheSys) - r.Register("runtime.MemStats.MSpanInuse", runtimeMetrics.MemStats.MSpanInuse) - r.Register("runtime.MemStats.MSpanSys", runtimeMetrics.MemStats.MSpanSys) - r.Register("runtime.MemStats.NextGC", runtimeMetrics.MemStats.NextGC) - r.Register("runtime.MemStats.NumGC", runtimeMetrics.MemStats.NumGC) - r.Register("runtime.MemStats.GCCPUFraction", runtimeMetrics.MemStats.GCCPUFraction) - r.Register("runtime.MemStats.PauseNs", runtimeMetrics.MemStats.PauseNs) - r.Register("runtime.MemStats.PauseTotalNs", runtimeMetrics.MemStats.PauseTotalNs) - r.Register("runtime.MemStats.StackInuse", runtimeMetrics.MemStats.StackInuse) - r.Register("runtime.MemStats.StackSys", runtimeMetrics.MemStats.StackSys) - r.Register("runtime.MemStats.Sys", runtimeMetrics.MemStats.Sys) - r.Register("runtime.MemStats.TotalAlloc", runtimeMetrics.MemStats.TotalAlloc) - r.Register("runtime.NumCgoCall", runtimeMetrics.NumCgoCall) - r.Register("runtime.NumGoroutine", runtimeMetrics.NumGoroutine) - r.Register("runtime.NumThread", runtimeMetrics.NumThread) - r.Register("runtime.ReadMemStats", runtimeMetrics.ReadMemStats) -} diff --git a/metrics/runtime_gccpufraction.go b/metrics/runtime_gccpufraction.go deleted file mode 100644 index ca12c05bac..0000000000 --- a/metrics/runtime_gccpufraction.go +++ /dev/null @@ -1,9 +0,0 @@ -// +build go1.5 - -package metrics - -import "runtime" - -func gcCPUFraction(memStats *runtime.MemStats) float64 { - return memStats.GCCPUFraction -} diff --git a/metrics/runtime_no_gccpufraction.go b/metrics/runtime_no_gccpufraction.go deleted file mode 100644 index be96aa6f1b..0000000000 --- a/metrics/runtime_no_gccpufraction.go +++ /dev/null @@ -1,9 +0,0 @@ -// +build !go1.5 - -package metrics - -import "runtime" - -func gcCPUFraction(memStats *runtime.MemStats) float64 { - return 0 -} diff --git a/metrics/runtime_test.go b/metrics/runtime_test.go index f85f7868f7..c8aae60dcb 100644 --- a/metrics/runtime_test.go +++ b/metrics/runtime_test.go @@ -6,57 +6,6 @@ import ( "time" ) -func BenchmarkRuntimeMemStats(b *testing.B) { - r := NewRegistry() - RegisterRuntimeMemStats(r) - b.ResetTimer() - for i := 0; i < b.N; i++ { - CaptureRuntimeMemStatsOnce(r) - } -} - -func TestRuntimeMemStats(t *testing.T) { - r := NewRegistry() - RegisterRuntimeMemStats(r) - CaptureRuntimeMemStatsOnce(r) - zero := runtimeMetrics.MemStats.PauseNs.Count() // Get a "zero" since GC may have run before these tests. - runtime.GC() - CaptureRuntimeMemStatsOnce(r) - if count := runtimeMetrics.MemStats.PauseNs.Count(); count-zero != 1 { - t.Fatal(count - zero) - } - runtime.GC() - runtime.GC() - CaptureRuntimeMemStatsOnce(r) - if count := runtimeMetrics.MemStats.PauseNs.Count(); count-zero != 3 { - t.Fatal(count - zero) - } - for i := 0; i < 256; i++ { - runtime.GC() - } - CaptureRuntimeMemStatsOnce(r) - if count := runtimeMetrics.MemStats.PauseNs.Count(); count-zero != 259 { - t.Fatal(count - zero) - } - for i := 0; i < 257; i++ { - runtime.GC() - } - CaptureRuntimeMemStatsOnce(r) - if count := runtimeMetrics.MemStats.PauseNs.Count(); count-zero != 515 { // We lost one because there were too many GCs between captures. - t.Fatal(count - zero) - } -} - -func TestRuntimeMemStatsNumThread(t *testing.T) { - r := NewRegistry() - RegisterRuntimeMemStats(r) - CaptureRuntimeMemStatsOnce(r) - - if value := runtimeMetrics.NumThread.Value(); value < 1 { - t.Fatalf("got NumThread: %d, wanted at least 1", value) - } -} - func TestRuntimeMemStatsBlocking(t *testing.T) { if g := runtime.GOMAXPROCS(0); g < 2 { t.Skipf("skipping TestRuntimeMemStatsBlocking with GOMAXPROCS=%d\n", g) -- GitLab