Skip to content

Commit

Permalink
gctuner: fix goleak in the memoryLimitTuner.tuning (#41523)
Browse files Browse the repository at this point in the history
close #41524
  • Loading branch information
hawkingrei authored Feb 16, 2023
1 parent cd51608 commit 218ca1c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions domain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ go_library(
"//util/etcd",
"//util/execdetails",
"//util/expensivequery",
"//util/gctuner",
"//util/intest",
"//util/logutil",
"//util/memory",
Expand Down
2 changes: 2 additions & 0 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import (
"github.com/pingcap/tidb/util/engine"
"github.com/pingcap/tidb/util/etcd"
"github.com/pingcap/tidb/util/expensivequery"
"github.com/pingcap/tidb/util/gctuner"
"github.com/pingcap/tidb/util/intest"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
Expand Down Expand Up @@ -938,6 +939,7 @@ func (do *Domain) Close() {
if do.onClose != nil {
do.onClose()
}
gctuner.WaitMemoryLimitTunerExitInTest()
logutil.BgLogger().Info("domain closed", zap.Duration("take time", time.Since(startTime)))
}

Expand Down
1 change: 1 addition & 0 deletions util/gctuner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//util",
"//util/intest",
"//util/memory",
"@com_github_pingcap_failpoint//:failpoint",
"@org_uber_go_atomic//:atomic",
Expand Down
19 changes: 19 additions & 0 deletions util/gctuner/memory_limit_tuner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/intest"
"github.com/pingcap/tidb/util/memory"
atomicutil "go.uber.org/atomic"
)
Expand All @@ -41,6 +42,17 @@ type memoryLimitTuner struct {
// fallbackPercentage indicates the fallback memory limit percentage when turning.
const fallbackPercentage float64 = 1.1

var memoryGoroutineCntInTest = *atomicutil.NewInt64(0)

// WaitMemoryLimitTunerExitInTest is used to wait memory limit tuner exit in test.
func WaitMemoryLimitTunerExitInTest() {
if intest.InTest {
for memoryGoroutineCntInTest.Load() > 0 {
time.Sleep(100 * time.Millisecond)
}
}
}

// tuning check the memory nextGC and judge whether this GC is trigger by memory limit.
// Go runtime ensure that it will be called serially.
func (t *memoryLimitTuner) tuning() {
Expand All @@ -62,10 +74,17 @@ func (t *memoryLimitTuner) tuning() {
if float64(r.HeapInuse)*ratio > float64(debug.SetMemoryLimit(-1)) {
if t.nextGCTriggeredByMemoryLimit.Load() && t.waitingReset.CompareAndSwap(false, true) {
go func() {
if intest.InTest {
memoryGoroutineCntInTest.Inc()
defer memoryGoroutineCntInTest.Dec()
}
memory.MemoryLimitGCLast.Store(time.Now())
memory.MemoryLimitGCTotal.Add(1)
debug.SetMemoryLimit(t.calcMemoryLimit(fallbackPercentage))
resetInterval := 1 * time.Minute // Wait 1 minute and set back, to avoid frequent GC
if intest.InTest {
resetInterval = 3 * time.Second
}
failpoint.Inject("testMemoryLimitTuner", func(val failpoint.Value) {
if val, ok := val.(bool); val && ok {
resetInterval = 1 * time.Second
Expand Down

0 comments on commit 218ca1c

Please sign in to comment.