Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/trace-agent: set gomemlimit based on cgroups #14552

Merged
merged 2 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/trace-agent/main_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"context"
"flag"

"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/runtime"
"github.com/DataDog/datadog-agent/pkg/trace/watchdog"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

// main is the main application entry point
Expand All @@ -22,6 +24,9 @@ func main() {

// prepare go runtime
runtime.SetMaxProcs()
if err := runtime.SetGoMemLimit(config.IsContainerized()); err != nil {
log.Debugf("Couldn't set Go memory limit: %s", err)
}

// Handle stops properly
go func() {
Expand Down
18 changes: 18 additions & 0 deletions pkg/runtime/gomemlimit.go
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")
}
52 changes: 52 additions & 0 deletions pkg/runtime/gomemlimit_linux.go
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to have a build constraint to build this for Go 1.19 and older?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea yes, thanks! Added.


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
}