Skip to content

Commit

Permalink
gctuner
Browse files Browse the repository at this point in the history
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
  • Loading branch information
hawkingrei committed Oct 8, 2022
1 parent 643717c commit 6450604
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
4 changes: 4 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
_ "github.com/pingcap/tidb/types/parser_driver" // for parser driver
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/gctuner"
"github.com/pingcap/tidb/util/logutil"
Expand Down Expand Up @@ -623,6 +624,9 @@ var defaultSysVars = []*SysVar{
{Scope: ScopeGlobal, Name: TiDBEnableGOGCTuner, Value: BoolToOnOff(DefTiDBEnableGOGCTuner), Type: TypeBool, SetGlobal: func(s *SessionVars, val string) error {
on := TiDBOptOn(val)
gctuner.EnableGOGCTuner.Store(on)
if !on {
util.SetGOGC(int(gctuner.DefaultGCPercent))
}
return nil
}},
{Scope: ScopeGlobal, Name: TiDBEnableTelemetry, Value: BoolToOnOff(DefTiDBEnableTelemetry), Type: TypeBool},
Expand Down
11 changes: 5 additions & 6 deletions util/gctuner/tuner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ const (
MinGCPercent uint32 = 20
)

var defaultGCPercent uint32 = 100
var DefaultGCPercent uint32 = 100

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

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

Expand All @@ -49,7 +49,6 @@ func Tuning(threshold uint64) {
if threshold <= 0 && globalTuner != nil {
globalTuner.stop()
globalTuner = nil
util.SetGOGC(int(defaultGCPercent))
return
}

Expand All @@ -63,7 +62,7 @@ func Tuning(threshold uint64) {
// GetGOGC returns the current GCPercent.
func GetGOGC() uint32 {
if globalTuner == nil {
return defaultGCPercent
return DefaultGCPercent
}
return globalTuner.getGCPercent()
}
Expand Down Expand Up @@ -94,7 +93,7 @@ type tuner struct {

func newTuner(threshold uint64) *tuner {
t := &tuner{}
t.gcPercent.Store(defaultGCPercent)
t.gcPercent.Store(DefaultGCPercent)
t.threshold.Store(threshold)
t.finalizer = newFinalizer(t.tuning) // start tuning
return t
Expand Down Expand Up @@ -143,7 +142,7 @@ func (t *tuner) tuning() {
func calcGCPercent(inuse, threshold uint64) uint32 {
// invalid params
if inuse == 0 || threshold == 0 {
return defaultGCPercent
return DefaultGCPercent
}
// inuse heap larger than threshold, use min percent
if threshold <= inuse {
Expand Down
8 changes: 4 additions & 4 deletions util/gctuner/tuner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestTuner(t *testing.T) {
threshold := memLimit / 2
tn := newTuner(threshold)
require.Equal(t, threshold, tn.threshold.Load())
require.Equal(t, defaultGCPercent, tn.getGCPercent())
require.Equal(t, DefaultGCPercent, tn.getGCPercent())

// no heap
testHeap = make([]byte, 1)
Expand Down Expand Up @@ -77,9 +77,9 @@ func TestTuner(t *testing.T) {
func TestCalcGCPercent(t *testing.T) {
const gb = 1024 * 1024 * 1024
// use default value when invalid params
require.Equal(t, defaultGCPercent, calcGCPercent(0, 0))
require.Equal(t, defaultGCPercent, calcGCPercent(0, 1))
require.Equal(t, defaultGCPercent, calcGCPercent(1, 0))
require.Equal(t, DefaultGCPercent, calcGCPercent(0, 0))
require.Equal(t, DefaultGCPercent, calcGCPercent(0, 1))
require.Equal(t, DefaultGCPercent, calcGCPercent(1, 0))

require.Equal(t, MaxGCPercent, calcGCPercent(1, 3*gb))
require.Equal(t, MaxGCPercent, calcGCPercent(gb/10, 4*gb))
Expand Down

0 comments on commit 6450604

Please sign in to comment.