-
Notifications
You must be signed in to change notification settings - Fork 115
fix: read cgroup memory limit for cache sizing when GOMEMLIMIT is not set #7573
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
Open
ycombinator
wants to merge
7
commits into
elastic:main
Choose a base branch
from
ycombinator:fix/cgroup-aware-memory-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1b312e0
fix: read cgroup memory limit for cache sizing when GOMEMLIMIT is not…
ycombinator c0915f9
chore: remove ristretto-specific wording from comments and changelog
ycombinator c21d6ad
chore: drop #7568 changelog fragment from this branch
ycombinator 93ffcca
fix: use MiB label in RAM warning log (values are 1024^2 bytes)
ycombinator 1528e87
test: expose cgroupMemMB as a stubbable var for hermetic testing
ycombinator 75a2d10
test: stub cgroupMemMB in TestContainerMemoryMB for hermetic fallback…
ycombinator 7044331
changelog: change kind from enhancement to bug-fix (fixes OOMKill-ind…
ycombinator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
13 changes: 0 additions & 13 deletions
13
changelog/fragments/1786038287-fix-cache-container-memory.yaml
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
changelog/fragments/1786058255-fix-cgroup-aware-cache-memory.yaml
This file contains hidden or 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,10 @@ | ||
| kind: bug-fix | ||
|
|
||
| summary: Read cgroup memory limit for cache sizing when GOMEMLIMIT is not set | ||
|
|
||
| description: > | ||
| containerMemoryMB() now falls back to the cgroup memory limit (v2 then v1) | ||
| before host RAM. This ensures fleet-server is correctly sized for the | ||
| container even in deployments that do not explicitly set GOMEMLIMIT. | ||
|
|
||
| component: fleet-server |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -10,8 +10,10 @@ import ( | |
| "io" | ||
| "io/fs" | ||
| "math" | ||
| "os" | ||
| "runtime" | ||
| "runtime/debug" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
|
|
@@ -314,7 +316,7 @@ func loadLimits(log *zerolog.Logger, agentLimit int) *envLimits { | |
| log.Info().Msgf("Using system limits for %d to %d agents for a configured value of %d agents", l.Agents.Min, l.Agents.Max, agentLimit) | ||
| ramSize := memMB() | ||
| if ramSize < l.RecommendedRAM { | ||
| log.Warn().Msgf("Detected %d MB of available memory, which is lower than the recommended amount (%d MB) for the configured agent limit", ramSize, l.RecommendedRAM) | ||
| log.Warn().Msgf("Detected %d MiB of available memory, which is lower than the recommended amount (%d MiB) for the configured agent limit", ramSize, l.RecommendedRAM) | ||
| } | ||
| return l | ||
| } | ||
|
|
@@ -323,17 +325,55 @@ func loadLimits(log *zerolog.Logger, agentLimit int) *envLimits { | |
| return defaultEnvLimits() | ||
| } | ||
|
|
||
| // containerMemoryMB returns available memory in MiB, preferring the GOMEMLIMIT | ||
| // runtime setting over host RAM so the ristretto cache is sized for the container, | ||
| // not the node. Falls back to memory.TotalMemory() when GOMEMLIMIT is unset. | ||
| // cgroupMemMB returns the cgroup memory limit in MiB. | ||
| // It is a var so that unit tests can replace it. | ||
| var cgroupMemMB func() (uint64, bool) = cgroupMemoryLimitMB | ||
|
|
||
| // containerMemoryMB returns available memory in MiB using this priority order: | ||
| // 1. GOMEMLIMIT, if explicitly set | ||
| // 2. cgroup memory limit (v2, then v1), for containers without an explicit GOMEMLIMIT | ||
| // 3. host total RAM, for non-containerised deployments | ||
| func containerMemoryMB() uint64 { | ||
| limit := debug.SetMemoryLimit(-1) | ||
| if limit > 0 && limit != math.MaxInt64 { | ||
| return uint64(limit) / 1024 / 1024 | ||
| } | ||
| if mb, ok := cgroupMemMB(); ok { | ||
| return mb | ||
| } | ||
| return memory.TotalMemory() / 1024 / 1024 | ||
| } | ||
|
|
||
| // cgroupMemoryLimitMB reads the container memory limit from cgroup files. | ||
| // It tries cgroup v2 first, then cgroup v1. Returns (0, false) when no | ||
| // applicable limit is found (unlimited, missing file, or parse error). | ||
| func cgroupMemoryLimitMB() (uint64, bool) { | ||
| if mb, ok := readCgroupMemoryFile("/sys/fs/cgroup/memory.max"); ok { | ||
| return mb, true | ||
| } | ||
| return readCgroupMemoryFile("/sys/fs/cgroup/memory/memory.limit_in_bytes") | ||
| } | ||
|
|
||
| // readCgroupMemoryFile parses a cgroup memory limit file and returns the | ||
| // value in MiB. Returns (0, false) when the file doesn't exist, contains | ||
| // "max" (unlimited), or the value is at or above MaxInt64 (cgroup v1's | ||
| // sentinel for unlimited). | ||
| func readCgroupMemoryFile(path string) (uint64, bool) { | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return 0, false | ||
| } | ||
| s := strings.TrimSpace(string(data)) | ||
| if s == "max" { | ||
| return 0, false | ||
| } | ||
| n, err := strconv.ParseUint(s, 10, 64) | ||
| if err != nil || n >= math.MaxInt64 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return 0, false | ||
| } | ||
| return n / 1024 / 1024, true | ||
| } | ||
|
|
||
| // memMB returns available memory in MiB. | ||
| // It is a var so that unit tests can replace it. | ||
| var memMB func() uint64 = containerMemoryMB | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to add a comment here that this doesn't work with nested cgroups.