Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sessionctx: add tidb_gogc_tuner_threshold #38432

Merged
merged 21 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
33 changes: 30 additions & 3 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,33 @@ 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(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 || globalMemoryLimitTuner < floatValue+0.05 {
return "", ErrTruncatedWrongValue.GenWithStackByArgs(TiDBGOGCTunerThreshold, originalValue)
}
return strconv.FormatFloat(floatValue, 'f', -1, 64), nil
},
SetGlobal: func(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(s *SessionVars) (string, error) {
return memory.ServerMemoryLimit.String(), nil
Expand Down Expand Up @@ -791,9 +818,9 @@ var defaultSysVars = []*SysVar{
if err != nil {
return "", err
}
if floatValue < 0.51 && floatValue > 1 { // 51% ~ 100%
s.StmtCtx.AppendWarning(ErrTruncatedWrongValue.GenWithStackByArgs(TiDBServerMemoryLimitGCTrigger, originalValue))
floatValue = DefTiDBServerMemoryLimitGCTrigger
gogcTunerThreshold := GOGCTunerThreshold.Load()
if floatValue < 0.51 && floatValue > 1 || floatValue < gogcTunerThreshold+0.05 { // 51% ~ 100%
return "", ErrTruncatedWrongValue.GenWithStackByArgs(TiDBServerMemoryLimitGCTrigger, originalValue)
}
return strconv.FormatFloat(floatValue, 'f', -1, 64), nil
},
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 @@ -829,6 +829,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 @@ -1059,6 +1061,8 @@ const (
DefTiDBServerMemoryLimitSessMinSize = 128 << 20
DefTiDBServerMemoryLimitGCTrigger = 0.7
DefTiDBEnableGOGCTuner = true
// DefTiDBGOGCTunerThreshold is to limit TiDBGOGCTunerThreshold.
DefTiDBGOGCTunerThreshold float64 = 0.6
)

// Process global variables.
Expand Down Expand Up @@ -1116,6 +1120,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 @@ -61,7 +61,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 @@ -205,7 +204,6 @@ func main() {
printInfo()
setupBinlogClient()
setupMetrics()
setupGCTuner()
storage, dom := createStoreAndDomain()
svr := createServer(storage, dom)

Expand Down Expand Up @@ -783,15 +781,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))
}