Skip to content

Commit

Permalink
Reduce agent RSS
Browse files Browse the repository at this point in the history
  • Loading branch information
L3n41c committed Dec 17, 2024
1 parent 593c137 commit 7eea4fb
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

"github.com/DataDog/datadog-agent/cmd/agent/command"
"github.com/DataDog/datadog-agent/cmd/agent/subcommands"

"github.com/DataDog/datadog-agent/cmd/internal/rssshrinker"
"github.com/DataDog/datadog-agent/cmd/internal/runcmd"
"github.com/spf13/cobra"
)
Expand All @@ -37,6 +39,10 @@ func init() {
}

func main() {
if err := rssshrinker.ReleaseMemory(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to release memory: %s\n", err)
}

process := strings.TrimSpace(os.Getenv("DD_BUNDLED_AGENT"))

if process == "" {
Expand Down
76 changes: 76 additions & 0 deletions cmd/internal/rssshrinker/rssshrinker_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 2024-present Datadog, Inc.

//go:build linux

// Package rssshrinker provides functions to reduce the process’ RSS
package rssshrinker

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"syscall"
"unsafe"
)

//revive:disable:var-naming

// MADV_PAGEOUT Reclaim these pages.
const MADV_PAGEOUT = 21

//revive:enable:var-naming

// ReleaseMemory releases memory by calling madvise with MADV_PAGEOUT on all
func ReleaseMemory() error {
selfMap, err := os.Open("/proc/self/maps")
if err != nil {
return fmt.Errorf("failed to open /proc/self/maps: %w", err)
}
defer selfMap.Close()

scanner := bufio.NewScanner(selfMap)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())

if len(fields) != 6 {
continue
}

address, perms, _ /* offset */, _ /* device */, _ /* inode */, pathname := fields[0], fields[1], fields[2], fields[3], fields[4], fields[5]

if strings.HasPrefix(pathname, "[") {
continue
}

if len(perms) != 4 || perms[0] != 'r' || perms[1] != '-' {
continue
}

beginStr, endStr, found := strings.Cut(address, "-")
if !found {
continue
}

begin, err := strconv.ParseUint(beginStr, 16, 64)
if err != nil {
return fmt.Errorf("failed to parse begin address %q: %w", beginStr, err)
}

end, err := strconv.ParseUint(endStr, 16, 64)
if err != nil {
return fmt.Errorf("failed to parse end address %q: %w", endStr, err)
}

// nolint:govet:unsafeptr
if err := syscall.Madvise(unsafe.Slice((*byte)(unsafe.Pointer(uintptr(begin))), end-begin), MADV_PAGEOUT); err != nil {

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of an unsigned 64-bit integer from
strconv.ParseUint
to a lower bit size type uintptr without an upper bound check.
return fmt.Errorf("failed to madvise: %w", err)
}
}

return nil
}
14 changes: 14 additions & 0 deletions cmd/internal/rssshrinker/rssshrinker_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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 2024-present Datadog, Inc.

//go:build !linux

// Package rssshrinker provides functions to reduce the process’ RSS
package rssshrinker

// ReleaseMemory isn’t implemented on this platform
func ReleaseMemory() error {
return nil
}
6 changes: 6 additions & 0 deletions cmd/process-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
package main

import (
"fmt"
_ "net/http/pprof"
"os"

"github.com/DataDog/datadog-agent/cmd/internal/rssshrinker"
"github.com/DataDog/datadog-agent/cmd/internal/runcmd"
"github.com/DataDog/datadog-agent/cmd/process-agent/command"
"github.com/DataDog/datadog-agent/cmd/process-agent/subcommands"
Expand All @@ -18,6 +20,10 @@ import (

// main is the main application entry point
func main() {
if err := rssshrinker.ReleaseMemory(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to release memory: %s\n", err)
}

flavor.SetFlavor(flavor.ProcessAgent)

os.Args = command.FixDeprecatedFlags(os.Args, os.Stdout)
Expand Down
6 changes: 6 additions & 0 deletions cmd/security-agent/main_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
package main

import (
"fmt"
"os"

_ "expvar" // Blank import used because this isn't directly used in this file
_ "net/http/pprof" // Blank import used because this isn't directly used in this file

"github.com/DataDog/datadog-agent/cmd/internal/rssshrinker"
"github.com/DataDog/datadog-agent/cmd/internal/runcmd"
"github.com/DataDog/datadog-agent/cmd/security-agent/command"
"github.com/DataDog/datadog-agent/cmd/security-agent/subcommands"
"github.com/DataDog/datadog-agent/pkg/util/flavor"
)

func main() {
if err := rssshrinker.ReleaseMemory(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to release memory: %s\n", err)
}

// set the Agent flavor
flavor.SetFlavor(flavor.SecurityAgent)

Expand Down
6 changes: 6 additions & 0 deletions cmd/system-probe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
package main

import (
"fmt"
"os"

"github.com/DataDog/datadog-agent/cmd/internal/rssshrinker"
"github.com/DataDog/datadog-agent/cmd/internal/runcmd"
"github.com/DataDog/datadog-agent/cmd/system-probe/command"
"github.com/DataDog/datadog-agent/cmd/system-probe/subcommands"
)

func main() {
if err := rssshrinker.ReleaseMemory(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to release memory: %s\n", err)
}

rootCmd := command.MakeCommand(subcommands.SysprobeSubcommands())
command.SetDefaultCommandIfNonePresent(rootCmd)
os.Exit(runcmd.Run(rootCmd))
Expand Down
6 changes: 6 additions & 0 deletions cmd/trace-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
package main

import (
"fmt"
"os"

"github.com/DataDog/datadog-agent/cmd/internal/rssshrinker"
"github.com/DataDog/datadog-agent/cmd/internal/runcmd"
"github.com/DataDog/datadog-agent/cmd/trace-agent/command"
"github.com/DataDog/datadog-agent/pkg/util/flavor"
)

func main() {
if err := rssshrinker.ReleaseMemory(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to release memory: %s\n", err)
}

flavor.SetFlavor(flavor.TraceAgent)

os.Args = command.FixDeprecatedFlags(os.Args, os.Stdout)
Expand Down

0 comments on commit 7eea4fb

Please sign in to comment.