Skip to content

Commit

Permalink
fix: reset GOGC when MaxRAMPercentage config rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
fangwentong committed Apr 26, 2024
1 parent 3b43cf4 commit d5e8230
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions gc_tunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 8 additions & 4 deletions gcsetter_go1.19.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit d5e8230

Please sign in to comment.