Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions pp/go/cppbridge/c_garbage_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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{}),
Expand Down Expand Up @@ -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))
Expand All @@ -93,19 +96,19 @@ 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
cgc.memoryThreshold.Set(cgc.threshold)
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 {
Expand All @@ -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)

Expand All @@ -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()

Expand All @@ -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)

Expand Down
Loading