From d5e82303286ab21884ec22e52c80cdb38ac0bbcb Mon Sep 17 00:00:00 2001 From: fangwentong Date: Fri, 26 Apr 2024 14:56:07 +0800 Subject: [PATCH] fix: reset GOGC when MaxRAMPercentage config rollback --- gc_tunner.go | 4 ++-- gcsetter_go1.19.go | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/gc_tunner.go b/gc_tunner.go index 17cd20b..5fdda1a 100644 --- a/gc_tunner.go +++ b/gc_tunner.go @@ -162,10 +162,10 @@ func (a *adaptiveGCHandler) watchConfigUpdate() { newVal, _ := a.configurator.GetConfig() oldVal, _ := a.prevConfig.Load().(Config) if reflect.DeepEqual(newVal, oldVal) { - // The config has not changed + // The config has no change continue } - // The config has changed + // The config has been changed select { case a.ch <- struct{}{}: default: diff --git a/gcsetter_go1.19.go b/gcsetter_go1.19.go index 3c7910f..25ebacf 100644 --- a/gcsetter_go1.19.go +++ b/gcsetter_go1.19.go @@ -13,12 +13,12 @@ import ( // setGCParameter sets the GC parameters func setGCParameter(oldConfig, newConfig Config, logger Logger) { if reflect.DeepEqual(oldConfig, newConfig) { - // The config has not changed + // The config has no change return } if newConfig.MaxRAMPercentage > 0 { if oldConfig.MaxRAMPercentage == newConfig.MaxRAMPercentage { - // The memory limit has not changed + // The memory limit has not been changed return } memLimit, err := getMemoryLimit() @@ -34,13 +34,15 @@ func setGCParameter(oldConfig, newConfig Config, logger Logger) { } if oldConfig.MaxRAMPercentage != 0 { - // The config has changed, reset the memory limit + // The config has been changed, reset the memory limit and GOGC defaultMemLimit := readGOMEMLIMIT(logger) if defaultMemLimit != 0 { logger.Logf("gctuner: reset memory limit %v", printMemorySize(uint64(defaultMemLimit))) debug.SetMemoryLimit(defaultMemLimit) - return } + // reset GOGC + setGOGCOrDefault(newConfig.GOGC, logger) + return } // Set the GOGC value @@ -58,8 +60,10 @@ func readGOMEMLIMIT(logger Logger) int64 { } n, ok := parseByteCount(p) if !ok { + // shouldn't be here, the `runtime.readGOMEMLIMIT` would exit the process in advance logger.Errorf("GOMEMLIMIT=", p, "\n") logger.Errorf("malformed GOMEMLIMIT; see `go doc runtime/debug.SetMemoryLimit`") + return math.MaxInt64 } return n }