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 6450604 commit b202800
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
3 changes: 1 addition & 2 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ 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 @@ -625,7 +624,7 @@ var defaultSysVars = []*SysVar{
on := TiDBOptOn(val)
gctuner.EnableGOGCTuner.Store(on)
if !on {
util.SetGOGC(int(gctuner.DefaultGCPercent))
gctuner.SetDefaultGOGC()
}
return nil
}},
Expand Down
14 changes: 9 additions & 5 deletions util/gctuner/tuner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@ 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)
}
}

func SetDefaultGOGC() {
util.SetGOGC(int(defaultGCPercent))
}

// Tuning sets the threshold of heap which will be respect by gc tuner.
// When Tuning, the env GOGC will not be take effect.
// threshold: disable tuning if threshold == 0
Expand All @@ -62,7 +66,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 @@ -93,7 +97,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 @@ -142,7 +146,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 b202800

Please sign in to comment.