-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
br: mem dump when about to OOM (#59234)
close #56971
- Loading branch information
1 parent
fed0251
commit 15f3c28
Showing
9 changed files
with
302 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2024 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package utils | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/pingcap/log" | ||
"github.com/pingcap/tidb/pkg/util/memory" | ||
"github.com/pingcap/tidb/pkg/util/memoryusagealarm" | ||
"go.uber.org/atomic" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
DefaultProfilesDir = "/tmp/profiles" | ||
// default memory usage alarm ratio (80%) | ||
defaultMemoryUsageAlarmRatio = 0.8 | ||
// default number of alarm records to keep | ||
defaultMemoryUsageAlarmKeepRecordNum = 3 | ||
) | ||
|
||
// BRConfigProvider implements memoryusagealarm.ConfigProvider for BR | ||
type BRConfigProvider struct { | ||
ratio *atomic.Float64 | ||
keepNum *atomic.Int64 | ||
logDir string | ||
} | ||
|
||
func (p *BRConfigProvider) GetMemoryUsageAlarmRatio() float64 { | ||
return p.ratio.Load() | ||
} | ||
|
||
func (p *BRConfigProvider) GetMemoryUsageAlarmKeepRecordNum() int64 { | ||
return p.keepNum.Load() | ||
} | ||
|
||
func (p *BRConfigProvider) GetLogDir() string { | ||
if p.logDir == "" { | ||
return DefaultProfilesDir | ||
} | ||
return p.logDir | ||
} | ||
|
||
func (p *BRConfigProvider) GetComponentName() string { | ||
return "br" | ||
} | ||
|
||
// RunMemoryMonitor starts monitoring memory usage and dumps profiles when thresholds are exceeded | ||
func RunMemoryMonitor(ctx context.Context, dumpDir string, memoryLimit uint64) error { | ||
// just in case | ||
if dumpDir == "" { | ||
dumpDir = DefaultProfilesDir | ||
} | ||
|
||
// Set memory limit if specified | ||
if memoryLimit > 0 { | ||
memory.ServerMemoryLimit.Store(memoryLimit) | ||
} | ||
|
||
log.Info("Memory monitor starting", | ||
zap.String("dump_dir", dumpDir), | ||
zap.Bool("using_temp_dir", dumpDir == os.TempDir()), | ||
zap.Float64("memory_usage_alarm_ratio", defaultMemoryUsageAlarmRatio), | ||
zap.Uint64("memory_limit_mb", memoryLimit/1024/1024)) | ||
|
||
// Initialize BR config provider with default values | ||
provider := &BRConfigProvider{ | ||
ratio: atomic.NewFloat64(defaultMemoryUsageAlarmRatio), | ||
keepNum: atomic.NewInt64(defaultMemoryUsageAlarmKeepRecordNum), | ||
logDir: dumpDir, | ||
} | ||
|
||
exitCh := make(chan struct{}) | ||
handle := memoryusagealarm.NewMemoryUsageAlarmHandle(exitCh, provider) | ||
// BR doesn't need session manager so setting to nil | ||
handle.SetSessionManager(nil) | ||
|
||
go func() { | ||
go handle.Run() | ||
<-ctx.Done() | ||
close(exitCh) | ||
}() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2024 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
func TestBRConfigProvider(t *testing.T) { | ||
provider := &BRConfigProvider{ | ||
ratio: atomic.NewFloat64(0.8), | ||
keepNum: atomic.NewInt64(3), | ||
logDir: "/custom/dir", | ||
} | ||
|
||
// Test GetMemoryUsageAlarmRatio | ||
require.Equal(t, 0.8, provider.GetMemoryUsageAlarmRatio()) | ||
|
||
// Test GetMemoryUsageAlarmKeepRecordNum | ||
require.Equal(t, int64(3), provider.GetMemoryUsageAlarmKeepRecordNum()) | ||
|
||
// Test GetLogDir | ||
require.Equal(t, "/custom/dir", provider.GetLogDir()) | ||
|
||
// Test GetLogDir with default | ||
provider.logDir = "" | ||
require.Equal(t, DefaultProfilesDir, provider.GetLogDir()) | ||
|
||
// Test GetComponentName | ||
require.Equal(t, "br", provider.GetComponentName()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.