Repository navigation
fix(data): memory limits count the process's own control group #2495
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,8 +66,15 @@ fn read() -> (Option<u64>, &'static str) { | |
| let Some(mut s) = sysinfo::System::new().ok() else { return (None, "unknown") }; | ||
| s.refresh_memory_specifics(sysinfo::MemoryRefreshKind::nothing().with_ram()); | ||
| let Some(total) = Some(s.total_memory()).filter(|m| *m > 0) else { return (None, "unknown") }; | ||
| // A container's limit, on Linux; none is reported elsewhere. | ||
| match s.cgroup_limits().map(|c| c.total_memory).filter(|m| *m > 0 && *m < total) { | ||
| // A container's limit, on Linux; none is reported elsewhere. The limits of this process's own | ||
| // control group and those above it count, not only the root's: a `systemd-run` scope or a | ||
| // service's `MemoryMax` sits below the root, which is all a container's own namespace shows. | ||
| let own = sysinfo::get_current_pid().ok().and_then(|pid| { | ||
| s.refresh_processes_specifics(sysinfo::ProcessesToUpdate::Some(&[pid]), false, sysinfo::ProcessRefreshKind::nothing()); | ||
| s.process(pid)?.cgroup_limits() | ||
| }); | ||
| let limits = [own, s.cgroup_limits()].into_iter().flatten().map(|c| c.total_memory); | ||
| match limits.filter(|m| *m > 0 && *m < total).min() { | ||
| Some(limit) => (Some(limit), "container"), | ||
|
Contributor
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. Naming:
Member
Author
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. Left for the release quality pass, with the test: the value name is a maintainer call, and the docs and the helper can follow whichever name it keeps. |
||
| None => (Some(total), "computer"), | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1549,9 +1549,9 @@ cores; `gp.env` returns the threads and cores as `limits.threads` and `limits.co | |
| runs on one thread. | ||
|
|
||
| `gp.env` also returns the memory limits on this computer (`limits`). On the desktop they follow the | ||
| computer's memory, or a container's limit when it is lower: each plans for a quarter of it, never | ||
| computer's memory, or a memory limit set on RustGIS (a container's, or a service's such as systemd's `MemoryMax`) when it is lower: each plans for a quarter of it, never | ||
|
Contributor
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. Two small points on this line. First, "a memory limit set on RustGIS" is not accurate: the limit is set on the process's control group by the container runtime,
Member
Author
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. Left for the release quality pass, as you suggest. |
||
| below the web app's fixed limits and never above a ceiling. `limits.memorySource` says what they | ||
| follow: `computer`, `container` (a container's limit, lower than the computer's memory), | ||
| follow: `computer`, `container` (such a limit, lower than the computer's memory), | ||
| `override` or `unknown`. For tests, or to reproduce a smaller computer, set the environment | ||
| variable `RUSTGIS_MEMORY_BYTES` to the memory in bytes (such as `8589934592` for 8 GiB) before | ||
| starting RustGIS: it replaces the memory read from the computer and any container limit, and the | ||
|
|
||
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.
Possible gap: only the leaf cgroup and the root are read, not the groups between. The comment and the PR description say "each group above" the process's own group counts, but the code only asks
Process::cgroup_limits()for the process's own group andSystem::cgroup_limits()for the namespace root. A limit on an intermediate slice (for exampleuser.sliceor a parent slice withMemoryMax) is enforced by the kernel but is not seen here, unless sysinfo 0.40'sProcess::cgroup_limitswalks the ancestors itself. I could not verify that from this checkout. If it does not, either walk/proc/self/cgroupupwards here, or correct the comment and the PR description. Confidence: medium. A test with a parent-slice limit would settle it.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.
sysinfo 0.40.1 walks them:
Process::cgroup_limitsreachesmemory_limits(src/unix/linux/cgroup.rs), which loops overbase.ancestors()from the process's own group up to the mount root and keeps the lowestmemory.max(ormemory.limit_in_byteson v1). A parent slice'sMemoryMaxis read there. Thesystemd-run --user --scoperun in the description sets the limit on the scope itself; the 27.2 GiB run is a limit on a slice above this process's own group, and it was found.