From 2bc8eeadc13b0f84d4400895c32504111f6236ef Mon Sep 17 00:00:00 2001 From: Alexandr Yudin <57181751+u-veles-a@users.noreply.github.com> Date: Mon, 27 Jul 2026 08:51:10 +0000 Subject: [PATCH] update default GC parameters Signed-off-by: Alexandr Yudin <57181751+u-veles-a@users.noreply.github.com> --- pp/go/cppbridge/c_garbage_collector.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/pp/go/cppbridge/c_garbage_collector.go b/pp/go/cppbridge/c_garbage_collector.go index d0008832bf..22e3392328 100644 --- a/pp/go/cppbridge/c_garbage_collector.go +++ b/pp/go/cppbridge/c_garbage_collector.go @@ -10,20 +10,22 @@ import ( "github.com/prometheus/prometheus/pp/go/util" ) -// garbage collector for objects initiated in GO but filled in C/C++, +// CGOGC is a garbage collector for objects initiated in GO but filled in C/C++, // because native GC knows nothing about the used memory and starts cleaning up memory too late. const ( - defaultGCDecay float64 = 1.0 / 3.0 + defaultGCDecay float64 = 0.2 + defaultGCHeadroom float64 = 0.30 // GC when InUse >= value * 1.30 defaultGCWarmupValue float64 = 0 gcDelayThreshold time.Duration = 2 * time.Second ) -// CGOGC - implement wise garbage collector for c/c++ objects. +// CGOGC implements a wise garbage collector for c/c++ objects. type CGOGC struct { threshold float64 decay float64 multiplier float64 + headroom float64 value float64 warmupValue float64 stop chan struct{} @@ -36,11 +38,12 @@ type CGOGC struct { cGoGCCount prometheus.Counter } -// NewCGOGC - init new CGOGC. +// NewCGOGC initializes a new [CGOGC]. func NewCGOGC(registerer prometheus.Registerer) *CGOGC { factory := util.NewUnconflictRegisterer(registerer) cgc := &CGOGC{ decay: defaultGCDecay, + headroom: defaultGCHeadroom, threshold: defaultGCWarmupValue, warmupValue: defaultGCWarmupValue, stop: make(chan struct{}), @@ -81,7 +84,7 @@ func NewCGOGC(registerer prometheus.Registerer) *CGOGC { return cgc } -// set - set a value to the series and updates the moving average. +// set a value to the series and updates the moving average. func (cgc *CGOGC) set(memInfo MemInfo) { cgc.memoryInUse.Set(float64(memInfo.InUse)) cgc.memoryResident.Set(float64(memInfo.Resident)) @@ -93,7 +96,7 @@ func (cgc *CGOGC) set(memInfo MemInfo) { cgc.value = (float64(memInfo.InUse) * cgc.decay) + (cgc.value * cgc.multiplier) } -// calcThreshold - calculate max expotential threshold value. +// calcThreshold calculate max expotential threshold value. func (cgc *CGOGC) calcThreshold() { if cgc.value <= cgc.warmupValue { cgc.threshold = cgc.warmupValue @@ -101,11 +104,11 @@ func (cgc *CGOGC) calcThreshold() { return } - cgc.threshold = cgc.value + (cgc.value * cgc.multiplier) + cgc.threshold = cgc.value * (1 + cgc.headroom) cgc.memoryThreshold.Set(cgc.threshold) } -// isOverThreshold - check and adjustment threshold. +// isOverThreshold check and adjustment threshold. func (cgc *CGOGC) isOverThreshold(memInfo MemInfo) bool { cgc.set(memInfo) if float64(memInfo.InUse) >= cgc.threshold { @@ -119,7 +122,7 @@ func (cgc *CGOGC) isOverThreshold(memInfo MemInfo) bool { return false } -// run - run gc if the number of objects initiated more threshold. +// run GC if the number of objects initiated more threshold. func (cgc *CGOGC) run() { timer := time.NewTimer(gcDelayThreshold) @@ -138,7 +141,7 @@ func (cgc *CGOGC) run() { } } -// gc - run gc if over threshold. +// gc runs GC if over threshold. func (cgc *CGOGC) gc() { memInfo := GetMemInfo() @@ -149,7 +152,7 @@ func (cgc *CGOGC) gc() { cgc.cGoGCCount.Inc() } -// Shutdown - stop loop with gc. +// Shutdown stops loop with GC. func (cgc *CGOGC) Shutdown(ctx context.Context) error { close(cgc.stop)