Skip to content

Commit

Permalink
feat: support change min/max gc percent limit (bytedance#131)
Browse files Browse the repository at this point in the history
* feat: support change min/max gc percent limit

* fix: make max/min gc percent private

* feat: add get min/max gc percent interface
  • Loading branch information
joway authored Apr 24, 2022
1 parent 65bf48f commit 87ba34a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
36 changes: 28 additions & 8 deletions util/gctuner/tuner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"sync/atomic"
)

const (
MaxGCPercent uint32 = 500
MinGCPercent uint32 = 50
var (
maxGCPercent uint32 = 500
minGCPercent uint32 = 50
)

var defaultGCPercent uint32 = 100
Expand Down Expand Up @@ -64,6 +64,26 @@ func GetGCPercent() uint32 {
return globalTuner.getGCPercent()
}

// GetMaxGCPercent returns the max gc percent value.
func GetMaxGCPercent() uint32 {
return atomic.LoadUint32(&maxGCPercent)
}

// SetMaxGCPercent sets the new max gc percent value.
func SetMaxGCPercent(n uint32) uint32 {
return atomic.SwapUint32(&maxGCPercent, n)
}

// GetMinGCPercent returns the min gc percent value.
func GetMinGCPercent() uint32 {
return atomic.LoadUint32(&minGCPercent)
}

// SetMinGCPercent sets the new min gc percent value.
func SetMinGCPercent(n uint32) uint32 {
return atomic.SwapUint32(&minGCPercent, n)
}

// only allow one gc tuner in one process
var globalTuner *tuner = nil

Expand Down Expand Up @@ -111,13 +131,13 @@ func calcGCPercent(inuse, threshold uint64) uint32 {
}
// inuse heap larger than threshold, use min percent
if threshold <= inuse {
return MinGCPercent
return minGCPercent
}
gcPercent := uint32(math.Floor(float64(threshold-inuse) / float64(inuse) * 100))
if gcPercent < MinGCPercent {
return MinGCPercent
} else if gcPercent > MaxGCPercent {
return MaxGCPercent
if gcPercent < minGCPercent {
return minGCPercent
} else if gcPercent > maxGCPercent {
return maxGCPercent
}
return gcPercent
}
Expand Down
32 changes: 23 additions & 9 deletions util/gctuner/tuner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestTuner(t *testing.T) {
// no heap
for i := 0; i < 100; i++ {
runtime.GC()
is.Equal(MaxGCPercent, tn.getGCPercent())
is.Equal(maxGCPercent, tn.getGCPercent())
}

// 1/4 threshold
Expand All @@ -61,15 +61,15 @@ func TestTuner(t *testing.T) {
runtime.GC()
for i := 0; i < 100; i++ {
runtime.GC()
is.Equal(MinGCPercent, tn.getGCPercent())
is.Equal(minGCPercent, tn.getGCPercent())
}

// out of threshold
testHeap = make([]byte, threshold+1024)
runtime.GC()
for i := 0; i < 100; i++ {
runtime.GC()
is.Equal(MinGCPercent, tn.getGCPercent())
is.Equal(minGCPercent, tn.getGCPercent())
}
}

Expand All @@ -81,13 +81,27 @@ func TestCalcGCPercent(t *testing.T) {
is.Equal(defaultGCPercent, calcGCPercent(0, 1))
is.Equal(defaultGCPercent, calcGCPercent(1, 0))

is.Equal(MaxGCPercent, calcGCPercent(1, 3*gb))
is.Equal(MaxGCPercent, calcGCPercent(gb/10, 4*gb))
is.Equal(MaxGCPercent, calcGCPercent(gb/2, 4*gb))
is.Equal(maxGCPercent, calcGCPercent(1, 3*gb))
is.Equal(maxGCPercent, calcGCPercent(gb/10, 4*gb))
is.Equal(maxGCPercent, calcGCPercent(gb/2, 4*gb))
is.Equal(uint32(300), calcGCPercent(1*gb, 4*gb))
is.Equal(uint32(166), calcGCPercent(1.5*gb, 4*gb))
is.Equal(uint32(100), calcGCPercent(2*gb, 4*gb))
is.Equal(MinGCPercent, calcGCPercent(3*gb, 4*gb))
is.Equal(MinGCPercent, calcGCPercent(4*gb, 4*gb))
is.Equal(MinGCPercent, calcGCPercent(5*gb, 4*gb))
is.Equal(minGCPercent, calcGCPercent(3*gb, 4*gb))
is.Equal(minGCPercent, calcGCPercent(4*gb, 4*gb))
is.Equal(minGCPercent, calcGCPercent(5*gb, 4*gb))
}

func TestChangeGCPercent(t *testing.T) {
is := assert.New(t)

old := minGCPercent
retOld := SetMinGCPercent(10)
is.Equal(old, retOld)
is.Equal(uint32(10), minGCPercent)

old = maxGCPercent
retOld = SetMaxGCPercent(1000)
is.Equal(old, retOld)
is.Equal(uint32(1000), maxGCPercent)
}

0 comments on commit 87ba34a

Please sign in to comment.