go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/tsmon/runtimestats/runtimestats.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package runtimestats exposes metrics related to the Go runtime. 16 // 17 // It exports the allocator statistics (go/mem/* metrics) and the current number 18 // of goroutines (go/goroutine/num). 19 // 20 // Should be invoked manually for now. Call Report(c) to populate the metrics 21 // prior tsmon Flush. 22 package runtimestats 23 24 import ( 25 "context" 26 "runtime" 27 28 "go.chromium.org/luci/common/tsmon/metric" 29 "go.chromium.org/luci/common/tsmon/types" 30 ) 31 32 var ( 33 // Some per-process memory allocator stats. 34 // See https://golang.org/pkg/runtime/#MemStats 35 36 MemAlloc = metric.NewInt("go/mem/alloc", "Bytes allocated and not yet freed.", &types.MetricMetadata{types.Bytes}) 37 MemTotalAlloc = metric.NewCounter("go/mem/total_alloc", "Bytes allocated (even if freed).", &types.MetricMetadata{types.Bytes}) 38 MemMallocs = metric.NewCounter("go/mem/mallocs", "Number of mallocs.", nil) 39 MemFrees = metric.NewCounter("go/mem/frees", "Number of frees.", nil) 40 MemNextGC = metric.NewInt("go/mem/next_gc", "Next GC will happen when go/mem/alloc > this amount.", nil) 41 MemNumGC = metric.NewCounter("go/mem/num_gc", "Number of garbage collections.", nil) 42 MemPauseTotal = metric.NewCounter("go/mem/pause_total", "Total GC pause, in microseconds.", nil) 43 MemHeapSys = metric.NewInt("go/mem/heap_sys", "Bytes obtained from system.", &types.MetricMetadata{types.Bytes}) 44 MemHeapIdle = metric.NewInt("go/mem/heap_idle", "Bytes in idle spans.", &types.MetricMetadata{types.Bytes}) 45 MemHeapInuse = metric.NewInt("go/mem/heap_in_use", "Bytes in non-idle span.", &types.MetricMetadata{types.Bytes}) 46 MemHeapObjects = metric.NewInt("go/mem/heap_objects", "Total number of allocated objects.", nil) 47 MemStackInuse = metric.NewInt("go/mem/stack_in_use", "Bytes used by stack allocator.", &types.MetricMetadata{types.Bytes}) 48 MemStackSys = metric.NewInt("go/mem/stack_in_sys", "Bytes allocated to stack allocator.", &types.MetricMetadata{types.Bytes}) 49 MemMSpanInuse = metric.NewInt("go/mem/mspan_in_use", "Bytes used by mspan structures.", &types.MetricMetadata{types.Bytes}) 50 MemMSpanSys = metric.NewInt("go/mem/mspan_in_sys", "Bytes allocated to mspan structures.", &types.MetricMetadata{types.Bytes}) 51 MemMCacheInuse = metric.NewInt("go/mem/mcache_in_use", "Bytes used by mcache structures.", &types.MetricMetadata{types.Bytes}) 52 MemMCacheSys = metric.NewInt("go/mem/mcache_in_sys", "Bytes allocated to mcache structures.", &types.MetricMetadata{types.Bytes}) 53 54 // Other runtime stats. 55 56 GoroutineNum = metric.NewInt("go/goroutine/num", "The number of goroutines that currently exist.", nil) 57 ) 58 59 // Report updates runtime stats metrics. 60 // 61 // Call it periodically (ideally right before flushing the metrics) to gather 62 // runtime stats metrics. 63 func Report(ctx context.Context) { 64 var stats runtime.MemStats 65 runtime.ReadMemStats(&stats) 66 67 MemAlloc.Set(ctx, int64(stats.Alloc)) 68 MemTotalAlloc.Set(ctx, int64(stats.TotalAlloc)) 69 MemMallocs.Set(ctx, int64(stats.Mallocs)) 70 MemFrees.Set(ctx, int64(stats.Frees)) 71 MemNextGC.Set(ctx, int64(stats.NextGC)) 72 MemNumGC.Set(ctx, int64(stats.NumGC)) 73 MemPauseTotal.Set(ctx, int64(stats.PauseTotalNs/1000)) 74 MemHeapSys.Set(ctx, int64(stats.HeapSys)) 75 MemHeapIdle.Set(ctx, int64(stats.HeapIdle)) 76 MemHeapInuse.Set(ctx, int64(stats.HeapInuse)) 77 MemHeapObjects.Set(ctx, int64(stats.HeapObjects)) 78 MemStackInuse.Set(ctx, int64(stats.StackInuse)) 79 MemStackSys.Set(ctx, int64(stats.StackSys)) 80 MemMSpanInuse.Set(ctx, int64(stats.MSpanInuse)) 81 MemMSpanSys.Set(ctx, int64(stats.MSpanSys)) 82 MemMCacheInuse.Set(ctx, int64(stats.MCacheInuse)) 83 MemMCacheSys.Set(ctx, int64(stats.MCacheSys)) 84 85 GoroutineNum.Set(ctx, int64(runtime.NumGoroutine())) 86 }