Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 9 additions & 2 deletions crates/data/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Copy link
Copy Markdown
Contributor

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 and System::cgroup_limits() for the namespace root. A limit on an intermediate slice (for example user.slice or a parent slice with MemoryMax) is enforced by the kernel but is not seen here, unless sysinfo 0.40's Process::cgroup_limits walks the ancestors itself. I could not verify that from this checkout. If it does not, either walk /proc/self/cgroup upwards here, or correct the comment and the PR description. Confidence: medium. A test with a parent-slice limit would settle it.

Copy link
Copy Markdown
Member Author

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_limits reaches memory_limits (src/unix/linux/cgroup.rs), which loops over base.ancestors() from the process's own group up to the mount root and keeps the lowest memory.max (or memory.limit_in_bytes on v1). A parent slice's MemoryMax is read there. The systemd-run --user --scope run 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.

});
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"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Naming: "container" now covers more than containers. A systemd-run scope or a service's MemoryMax now also yields this value, which is public output (gp.env limits.memorySource, the CLI and Python). The module comment (lines 3-5), the physical() and source() docs, and the comment above read() still say only "a container's limit". Either rename the value (for example to "cgroup") before the next release, while the output is new, or update every doc to say "a cgroup or container limit". Confidence: high that the wording is misleading; the rename is a maintainer call. Related quality point: the new min-selection logic has no unit test. Pulling it into a pure helper that takes total and the limits would let the cases (none, own lower, root lower, zero, above total) run in cargo test without a cgroup.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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"),
}
Expand Down
4 changes: 2 additions & 2 deletions docs/user-guide/analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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, systemd-run, or a service manager, not by RustGIS. Second, the line is about 180 characters, against the roughly 100-character wrap used in the rest of the guide. Something like: "computer's memory, or a memory limit on the process's control group (a container's, or a service's such as systemd's MemoryMax) when it is lower: each plans for a quarter of it, never". Confidence: high. This is a quality point; it can wait for the release quality pass.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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
Expand Down
Loading