Make std::thread::available_concurrency support process-limited number of CPUs - #89310
Conversation
|
r? @m-ou-se (rust-highfive has picked a reviewer for you, use r? to override) |
std::thread::available_concurrency support process-limited number of CPUs
This comment has been minimized.
This comment has been minimized.
d6affdc to
33e4614
Compare
yoshuawuyts
left a comment
There was a problem hiding this comment.
Compared this against num_cpus/linux.rs, and this looks good!
33e4614 to
433439e
Compare
|
I believe this PR should be ready to be merged? |
|
@bors r+ |
|
📌 Commit 433439e has been approved by |
…affinity, r=m-ou-se Make `std::thread::available_concurrency` support process-limited number of CPUs Use `libc::sched_getaffinity` and count the number of CPUs in the returned mask. This handles cases where the process doesn't have access to all CPUs, such as when limited via `taskset` or similar. This also covers cgroup cpusets.
…affinity, r=m-ou-se Make `std::thread::available_concurrency` support process-limited number of CPUs Use `libc::sched_getaffinity` and count the number of CPUs in the returned mask. This handles cases where the process doesn't have access to all CPUs, such as when limited via `taskset` or similar. This also covers cgroup cpusets.
|
Fixed the missing macros on Android in rust-lang/libc#2470 . Once that goes in, I'll do a libc release and update. |
433439e to
645e46e
Compare
|
@bors r=m-ou-se |
…r of CPUs Use libc::sched_getaffinity and count the number of CPUs in the returned mask. This handles cases where the process doesn't have access to all CPUs, such as when limited via taskset or similar.
645e46e to
7c9611d
Compare
|
Updated to libc 0.2.106 which includes the fix for the issue that broke CI on macOS. @bors r=m-ou-se |
|
📌 Commit 7c9611d has been approved by |
|
⌛ Testing commit 7c9611d with merge bd73825db1627e06ead2c3e17513f3aaa9b2c46d... |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
💔 Test failed - checks-actions |
|
💡 This pull request was already approved, no need to approve it again.
|
|
📌 Commit 7c9611d has been approved by |
|
@bors retry |
|
☀️ Test successful - checks-actions |
|
Finished benchmarking commit (fecfc0e): comparison url. Summary: This benchmark run did not return any relevant changes. If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. @rustbot label: -perf-regression |
Every internal thread pool (prune.rs/assemble.rs's own num_workers, Rayon's default global pool for conflate.match) sizes itself from std::thread::available_parallelism() -- which historically ignored Docker/podman's --cpus (a CFS bandwidth quota, not a cpuset) and just reported the host's full core count. Rust's std did fix this for cgroup v2 (rust-lang/rust#89310), and this toolchain (1.98) is well past that fix, but it's exactly the kind of library behavior worth confirming empirically rather than trusting -- this is that confirmation mechanism, for whenever a --cpus sweep (see docs/PRODUCTION.md) actually happens: log the value once, structured, and check it matches N under a real --cpus=N. While touching this call site: also moves the pipeline version out of the message text and into a structured field, matching this module's own documented convention (logging.rs's own doc comment: "Call sites that have actual structured data to log ... should attach it via the log crate's key-value API instead of interpolating it into the message text") -- the version was doing exactly that before. Uses the bare CARGO_PKG_VERSION value (no "v" prefix), consistent with how version already appears elsewhere (e.g. pipeline::provenance's tool_component()), not the "vX.Y.Z" tag format. Extends logging::init()'s existing one-time startup log record rather than adding a second one -- already the natural, only-runs-once home for this kind of environment fact. Verified: cargo test (253 passed, extended the existing startup-record test to check both new structured fields instead of parsing the old message string)/clippy/fmt clean. Manually ran against the zugerland.osm.pbf fixture and confirmed the real JSON shape: {"fields":{"available_parallelism":10,"version":"0.8.2"},..., "message":"pipeline starting up",...} Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Every internal thread pool (prune.rs/assemble.rs's own num_workers, Rayon's default global pool for conflate.match) sizes itself from std::thread::available_parallelism() -- which historically ignored Docker/podman's --cpus (a CFS bandwidth quota, not a cpuset) and just reported the host's full core count. Rust's std did fix this for cgroup v2 (rust-lang/rust#89310), and this toolchain (1.98) is well past that fix, but it's exactly the kind of library behavior worth confirming empirically rather than trusting -- this is that confirmation mechanism, for whenever a --cpus sweep (see docs/PRODUCTION.md) actually happens: log the value once, structured, and check it matches N under a real --cpus=N. While touching this call site: also moves the pipeline version out of the message text and into a structured field, matching this module's own documented convention (logging.rs's own doc comment: "Call sites that have actual structured data to log ... should attach it via the log crate's key-value API instead of interpolating it into the message text") -- the version was doing exactly that before. Keeps the "vX.Y.Z" form (concat!("v", env!("CARGO_PKG_VERSION")), a zero-cost compile-time concatenation of two string literals, not a runtime format!) to match both the previously-logged string and the release tag format (see RELEASING.md) -- not the bare semver pipeline::provenance's tool_component() uses, which is a different field for a different purpose. Extends logging::init()'s existing one-time startup log record rather than adding a second one -- already the natural, only-runs-once home for this kind of environment fact. Verified: cargo test (253 passed, extended the existing startup-record test to check both new structured fields instead of parsing the old message string)/clippy/fmt clean. Manually ran against the zugerland.osm.pbf fixture and confirmed the real JSON shape: {"fields":{"available_parallelism":10,"version":"v0.8.2"},..., "message":"pipeline starting up",...} Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Use
libc::sched_getaffinityand count the number of CPUs in the returned mask. This handles cases where the process doesn't have access to all CPUs, such as when limited viatasksetor similar.This also covers cgroup cpusets.