Skip to content

Commit

Permalink
sessionctx: add tidb_gogc_tuner_threshold (#38432)
Browse files Browse the repository at this point in the history
close #38431
  • Loading branch information
hawkingrei authored Oct 20, 2022
1 parent c52b5a7 commit c1666d1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
38 changes: 36 additions & 2 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,36 @@ var defaultSysVars = []*SysVar{
return nil
},
},
{Scope: ScopeGlobal, Name: TiDBGOGCTunerThreshold, Value: strconv.FormatFloat(DefTiDBGOGCTunerThreshold, 'f', -1, 64), Type: TypeFloat, MinValue: 0, MaxValue: math.MaxUint64,
GetGlobal: func(_ context.Context, s *SessionVars) (string, error) {
return strconv.FormatFloat(GOGCTunerThreshold.Load(), 'f', -1, 64), nil
},
Validation: func(s *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
floatValue, err := strconv.ParseFloat(normalizedValue, 64)
if err != nil {
return "", err
}
globalMemoryLimitTuner := gctuner.GlobalMemoryLimitTuner.GetPercentage()
if floatValue < 0 && floatValue > 0.9 {
return "", ErrWrongValueForVar.GenWithStackByArgs(TiDBGOGCTunerThreshold, normalizedValue)
}
if globalMemoryLimitTuner < floatValue+0.05 {
return "", errors.New("tidb_gogc_tuner_threshold should be less than tidb_server_memory_limit_gc_trigger - 0.05")
}
return strconv.FormatFloat(floatValue, 'f', -1, 64), nil
},
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
factor, err := strconv.ParseFloat(val, 64)
if err != nil {
return err
}
GOGCTunerThreshold.Store(factor)
memTotal := memory.ServerMemoryLimit.Load()
threshold := float64(memTotal) * factor
gctuner.Tuning(uint64(threshold))
return nil
},
},
{Scope: ScopeGlobal, Name: TiDBServerMemoryLimit, Value: strconv.FormatUint(DefTiDBServerMemoryLimit, 10), Type: TypeUnsigned, MinValue: 0, MaxValue: math.MaxUint64,
GetGlobal: func(_ context.Context, s *SessionVars) (string, error) {
return memory.ServerMemoryLimit.String(), nil
Expand Down Expand Up @@ -792,10 +822,14 @@ var defaultSysVars = []*SysVar{
if err != nil {
return "", err
}
gogcTunerThreshold := GOGCTunerThreshold.Load()
if floatValue < 0.51 && floatValue > 1 { // 51% ~ 100%
s.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(TiDBServerMemoryLimitGCTrigger, originalValue))
floatValue = DefTiDBServerMemoryLimitGCTrigger
return "", ErrWrongValueForVar.GenWithStackByArgs(TiDBServerMemoryLimitGCTrigger, normalizedValue)
}
if floatValue < gogcTunerThreshold+0.05 {
return "", errors.New("tidb_server_memory_limit_gc_trigger should be greater than tidb_gogc_tuner_threshold + 0.05")
}

return strconv.FormatFloat(floatValue, 'f', -1, 64), nil
},
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,8 @@ const (
TiDBServerMemoryLimitGCTrigger = "tidb_server_memory_limit_gc_trigger"
// TiDBEnableGOGCTuner is to enable GOGC tuner. it can tuner GOGC
TiDBEnableGOGCTuner = "tidb_enable_gogc_tuner"
// TiDBGOGCTunerThreshold is to control the threshold of GOGC tuner.
TiDBGOGCTunerThreshold = "tidb_gogc_tuner_threshold"
)

// TiDB intentional limits
Expand Down Expand Up @@ -1063,6 +1065,8 @@ const (
DefTiDBMergePartitionStatsConcurrency = 1
DefTiDBServerMemoryLimitGCTrigger = 0.7
DefTiDBEnableGOGCTuner = true
// DefTiDBGOGCTunerThreshold is to limit TiDBGOGCTunerThreshold.
DefTiDBGOGCTunerThreshold float64 = 0.6
)

// Process global variables.
Expand Down Expand Up @@ -1120,6 +1124,7 @@ var (
// DefTiDBServerMemoryLimit indicates the default value of TiDBServerMemoryLimit(TotalMem * 80%).
// It should be a const and shouldn't be modified after tidb is started.
DefTiDBServerMemoryLimit = mathutil.Max(memory.GetMemTotalIgnoreErr()/10*8, 512<<20)
GOGCTunerThreshold = atomic.NewFloat64(DefTiDBGOGCTunerThreshold)
)

var (
Expand Down
11 changes: 0 additions & 11 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import (
"github.com/pingcap/tidb/util/deadlockhistory"
"github.com/pingcap/tidb/util/disk"
"github.com/pingcap/tidb/util/domainutil"
"github.com/pingcap/tidb/util/gctuner"
"github.com/pingcap/tidb/util/kvcache"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
Expand Down Expand Up @@ -208,7 +207,6 @@ func main() {
printInfo()
setupBinlogClient()
setupMetrics()
setupGCTuner()
storage, dom := createStoreAndDomain()
svr := createServer(storage, dom)

Expand Down Expand Up @@ -796,15 +794,6 @@ func setupTracing() {
opentracing.SetGlobalTracer(tracer)
}

func setupGCTuner() {
limit, err := memory.MemTotal()
if err != nil {
log.Fatal("setupGCTuner failed", zap.Error(err))
}
threshold := limit * 7 / 10
gctuner.Tuning(threshold)
}

func closeDomainAndStorage(storage kv.Storage, dom *domain.Domain) {
tikv.StoreShuttingDown(1)
dom.Close()
Expand Down
4 changes: 2 additions & 2 deletions util/gctuner/tuner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// MaxGCPercent is the default max cost of memory.
MaxGCPercent uint32 = 500
// MinGCPercent is the default min cost of memory.
MinGCPercent uint32 = 20
MinGCPercent uint32 = 100
)

var defaultGCPercent uint32 = 100
Expand All @@ -46,7 +46,7 @@ func SetDefaultGOGC() {
util.SetGOGC(int(defaultGCPercent))
}

// Tuning sets the threshold of heap which will be respect by gc tuner.
// Tuning sets the threshold of heap which will be respect by gogc tuner.
// When Tuning, the env GOGC will not be take effect.
// threshold: disable tuning if threshold == 0
func Tuning(threshold uint64) {
Expand Down
2 changes: 1 addition & 1 deletion util/gctuner/tuner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestCalcGCPercent(t *testing.T) {
require.Equal(t, uint32(300), calcGCPercent(1*gb, 4*gb))
require.Equal(t, uint32(166), calcGCPercent(1.5*gb, 4*gb))
require.Equal(t, uint32(100), calcGCPercent(2*gb, 4*gb))
require.Equal(t, uint32(33), calcGCPercent(3*gb, 4*gb))
require.Equal(t, uint32(100), calcGCPercent(3*gb, 4*gb))
require.Equal(t, MinGCPercent, calcGCPercent(4*gb, 4*gb))
require.Equal(t, MinGCPercent, calcGCPercent(5*gb, 4*gb))
}

0 comments on commit c1666d1

Please sign in to comment.