-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/runtime,cmd/trace-agent: set gomemlimit based on cgroups
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
//go:build !linux | ||
// +build !linux | ||
|
||
package runtime | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// SetGoMemLimit configures Go memory limit based on cgroups. Only supported on Linux. | ||
func SetGoMemLimit(isContainerized bool) error { | ||
return errors.New("unsupported on non-linux") | ||
} |
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,52 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
//go:build linux | ||
// +build linux | ||
|
||
package runtime | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"runtime/debug" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/util/cgroups" | ||
"github.com/DataDog/datadog-agent/pkg/util/log" | ||
) | ||
|
||
// SetGoMemLimit configures Go memory soft limit based on cgroups. | ||
// The soft limit is set to 90% of the cgroup memory hard limit. | ||
// The function is noop if | ||
// - GOMEMLIMIT is set already | ||
// - There is no cgroup limit | ||
// | ||
// Read more about Go memory limit in https://tip.golang.org/doc/gc-guide#Memory_limit | ||
func SetGoMemLimit(isContainerized bool) error { | ||
if _, ok := os.LookupEnv("GOMEMLIMIT"); ok { | ||
log.Debug("GOMEMLIMIT is set already, doing nothing") | ||
return nil | ||
} | ||
selfReader, err := cgroups.NewSelfReader("/proc", isContainerized) | ||
if err != nil { | ||
return err | ||
} | ||
cgroup := selfReader.GetCgroup(cgroups.SelfCgroupIdentifier) | ||
if cgroup == nil { | ||
return errors.New("cannot get cgroup") | ||
} | ||
var stats cgroups.MemoryStats | ||
if err := cgroup.GetMemoryStats(&stats); err != nil { | ||
return err | ||
} | ||
if stats.Limit == nil { | ||
log.Debug("Cgroup memory limit not found, doing nothing") | ||
return nil | ||
} | ||
softLimit := int64(0.9 * float64(*stats.Limit)) | ||
log.Infof("Cgroup memory limit is %d, setting gomemlimit to %d", *stats.Limit, softLimit) | ||
debug.SetMemoryLimit(softLimit) | ||
return nil | ||
} |