-
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.
- Loading branch information
Showing
7 changed files
with
120 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,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 | ||
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 Error loading related location Loading |
||
return fmt.Errorf("failed to madvise: %w", err) | ||
} | ||
} | ||
|
||
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,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 | ||
} |
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