Skip to content

Commit

Permalink
variable: control max/min gogc for gogctuner (#47148)
Browse files Browse the repository at this point in the history
close #47166
  • Loading branch information
hawkingrei authored Sep 22, 2023
1 parent 241e846 commit eef9f72
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
34 changes: 34 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,40 @@ var defaultSysVars = []*SysVar{
gctuner.GlobalMemoryLimitTuner.UpdateMemoryLimit()
return nil
}},
{Scope: ScopeGlobal, Name: TiDBGOGCTunerMaxValue, Value: strconv.Itoa(DefTiDBGOGCMaxValue),
Type: TypeInt, MinValue: 10, SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
maxValue := TidbOptInt64(val, DefTiDBGOGCMaxValue)
gctuner.SetMaxGCPercent(uint32(maxValue))
gctuner.GlobalMemoryLimitTuner.UpdateMemoryLimit()
return nil
},
GetGlobal: func(ctx context.Context, vars *SessionVars) (string, error) {
return strconv.FormatInt(int64(gctuner.MaxGCPercent()), 10), nil
},
Validation: func(s *SessionVars, normalizedValue string, origin string, scope ScopeFlag) (string, error) {
maxValue := TidbOptInt64(origin, DefTiDBGOGCMaxValue)
if maxValue <= int64(gctuner.MinGCPercent()) {
return "", errors.New("tidb_gogc_tuner_max_value should be more than tidb_gogc_tuner_min_value")
}
return origin, nil
}},
{Scope: ScopeGlobal, Name: TiDBGOGCTunerMinValue, Value: strconv.Itoa(DefTiDBGOGCMinValue),
Type: TypeInt, MinValue: 10, SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
minValue := TidbOptInt64(val, DefTiDBGOGCMinValue)
gctuner.SetMinGCPercent(uint32(minValue))
gctuner.GlobalMemoryLimitTuner.UpdateMemoryLimit()
return nil
},
GetGlobal: func(ctx context.Context, vars *SessionVars) (string, error) {
return strconv.FormatInt(int64(gctuner.MinGCPercent()), 10), nil
},
Validation: func(s *SessionVars, normalizedValue string, origin string, scope ScopeFlag) (string, error) {
minValue := TidbOptInt64(origin, DefTiDBGOGCMinValue)
if minValue >= int64(gctuner.MaxGCPercent()) {
return "", errors.New("tidb_gogc_tuner_min_value should be less than tidb_gogc_tuner_max_value")
}
return origin, nil
}},
{Scope: ScopeGlobal, Name: TiDBEnableTelemetry, Value: BoolToOnOff(DefTiDBEnableTelemetry), Type: TypeBool},
{Scope: ScopeGlobal, Name: TiDBEnableHistoricalStats, Value: On, Type: TypeBool, Depended: true},
/* tikv gc metrics */
Expand Down
11 changes: 11 additions & 0 deletions sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,17 @@ func TestTiDBServerMemoryLimitGCTrigger(t *testing.T) {
require.Error(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBServerMemoryLimitGCTrigger, "51%")
require.NoError(t, err)

err = mock.SetGlobalSysVar(context.Background(), TiDBGOGCTunerMaxValue, "50")
require.Error(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBGOGCTunerMinValue, "200")
require.NoError(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBGOGCTunerMinValue, "1000")
require.Error(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBGOGCTunerMinValue, "100")
require.NoError(t, err)
err = mock.SetGlobalSysVar(context.Background(), TiDBGOGCTunerMaxValue, "200")
require.NoError(t, err)
}

func TestSetAggPushDownGlobally(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,10 @@ const (
TiDBEnableGOGCTuner = "tidb_enable_gogc_tuner"
// TiDBGOGCTunerThreshold is to control the threshold of GOGC tuner.
TiDBGOGCTunerThreshold = "tidb_gogc_tuner_threshold"
// TiDBGOGCTunerMaxValue is the max value of GOGC that GOGC tuner can change to.
TiDBGOGCTunerMaxValue = "tidb_gogc_tuner_max_value"
// TiDBGOGCTunerMinValue is the min value of GOGC that GOGC tuner can change to.
TiDBGOGCTunerMinValue = "tidb_gogc_tuner_min_value"
// TiDBExternalTS is the ts to read through when the `TiDBEnableExternalTsRead` is on
TiDBExternalTS = "tidb_external_ts"
// TiDBTTLJobEnable is used to enable/disable scheduling ttl job
Expand Down Expand Up @@ -1349,6 +1353,8 @@ const (
DefTiDBEnableGOGCTuner = true
// DefTiDBGOGCTunerThreshold is to limit TiDBGOGCTunerThreshold.
DefTiDBGOGCTunerThreshold float64 = 0.6
DefTiDBGOGCMaxValue = 500
DefTiDBGOGCMinValue = 100
DefTiDBOptPrefixIndexSingleScan = true
DefTiDBExternalTS = 0
DefTiDBEnableExternalTSRead = false
Expand Down
13 changes: 11 additions & 2 deletions util/gctuner/tuner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ var (
minGCPercent atomic.Uint32

// EnableGOGCTuner is to control whether enable the GOGC tuner.
EnableGOGCTuner atomic.Bool

EnableGOGCTuner atomic.Bool
defaultGCPercent uint32 = 100
)

Expand All @@ -43,11 +42,21 @@ func SetMaxGCPercent(percent uint32) {
maxGCPercent.Store(percent)
}

// MaxGCPercent get the max cost of memory.
func MaxGCPercent() uint32 {
return maxGCPercent.Load()
}

// SetMinGCPercent sets the max cost of memory.
func SetMinGCPercent(percent uint32) {
minGCPercent.Store(percent)
}

// MinGCPercent get the min cost of memory.
func MinGCPercent() uint32 {
return minGCPercent.Load()
}

func init() {
if val, err := strconv.Atoi(os.Getenv("GOGC")); err == nil {
defaultGCPercent = uint32(val)
Expand Down

0 comments on commit eef9f72

Please sign in to comment.