feat: add memlimit flags for setMaxResources (#19400)

This commit is contained in:
jiuker 2024-04-04 20:06:57 +08:00 committed by GitHub
parent 95bf4a57b6
commit 272367ccd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 3 deletions

View File

@ -172,6 +172,12 @@ var ServerFlags = []cli.Flag{
Hidden: true,
EnvVar: "MINIO_CROSSDOMAIN_XML",
},
cli.StringFlag{
Name: "memlimit",
Usage: "set global memory limit per server via GOMEMLIMIT",
Hidden: true,
EnvVar: "MINIO_MEMLIMIT",
},
}
var gatewayCmd = cli.Command{
@ -731,7 +737,7 @@ func serverMain(ctx *cli.Context) {
// Set system resources to maximum.
bootstrapTrace("setMaxResources", func() {
_ = setMaxResources()
_ = setMaxResources(ctx)
})
// Verify kernel release and version.

View File

@ -21,6 +21,8 @@ import (
"runtime"
"runtime/debug"
"github.com/dustin/go-humanize"
"github.com/minio/cli"
"github.com/minio/madmin-go/v3/kernel"
"github.com/minio/minio/internal/logger"
"github.com/minio/pkg/v2/sys"
@ -43,7 +45,7 @@ func oldLinux() bool {
return currentKernel < kernel.Version(4, 0, 0)
}
func setMaxResources() (err error) {
func setMaxResources(ctx *cli.Context) (err error) {
// Set the Go runtime max threads threshold to 90% of kernel setting.
sysMaxThreads, err := sys.GetMaxThreads()
if err == nil {
@ -75,6 +77,27 @@ func setMaxResources() (err error) {
return err
}
// set debug memory limit instead of GOMEMLIMIT env
_ = setDebugMemoryLimit(ctx)
err = sys.SetMaxMemoryLimit(maxLimit, maxLimit)
return err
}
func setDebugMemoryLimit(ctx *cli.Context) error {
if ctx == nil {
return nil
}
if ctx.IsSet("memlimit") {
memlimit := ctx.String("memlimit")
if memlimit == "" {
memlimit = ctx.GlobalString("memlimit")
}
mlimit, err := humanize.ParseBytes(memlimit)
if err != nil {
return err
}
debug.SetMemoryLimit(int64(mlimit))
}
return nil
}

View File

@ -106,7 +106,7 @@ func TestMain(m *testing.M) {
// logger.AddTarget(console.New())
// Set system resources to maximum.
setMaxResources()
setMaxResources(nil)
// Initialize globalConsoleSys system
globalConsoleSys = NewConsoleLogger(context.Background())