feat(thread_aware): add ProcessorCount::AtMost for capped processor count#541
Conversation
…ount Add an AtMost(NonZero<usize>) variant to ProcessorCount that requests an upper bound on processors, clamping to every available processor when the hardware offers fewer than requested. Unlike Manual, this never fails when the machine is smaller than the ceiling, making it suitable for callers that want to cap resource usage while still running on smaller machines. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds a new ProcessorCount::AtMost(NonZero<usize>) option to thread_aware’s ThreadRegistry processor-selection policy so callers can cap processor usage without failing on smaller machines.
Changes:
- Introduces
ProcessorCount::AtMostand documents intended behavior vsManual/All. - Updates
ThreadRegistry::with_hardwareto clamp requested processors to the hardware’s available processor count. - Adds deterministic fake-hardware tests covering below/equal/exceeding available processor counts.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #541 +/- ##
=======================================
Coverage 100.0% 100.0%
=======================================
Files 355 355
Lines 26963 26968 +5
=======================================
+ Hits 26963 26968 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Address PR review: rely on the non-empty processor-set invariant in the AtMost arm by taking all available processors when the ceiling meets or exceeds the machine's count, removing the needless zero-length handling and NonZero re-wrap. Reword the Manual doc to say construction panics rather than fails. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Intentional. Adding |
Disambiguate from the recently added AtMost variant, which reviewers noted sounded confusingly similar to Manual. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The enum should not have been exhaustive; allowing new variants (e.g. AtMost) to be added without a breaking change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
|
Reuse a single hardware.processors() snapshot for both to_builder() and the AtMost len() comparison, avoiding a second query that could observe hotplug/quota changes and improving readability. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
What
Adds a new
ProcessorCount::AtMost(NonZero<usize>)variant that requests an upper bound on the number of processors, clamping down to every available processor when the hardware offers fewer than requested.Also renames the existing
ProcessorCount::Manualvariant toProcessorCount::Exactly. Reviewers noted thatManualwas easy to confuse with the newly addedAtMostvariant;Exactlymakes the "require exactly this many processors" semantics unambiguous versusAtMost's upper-bound behavior.Why
Exactly(N)requires exactlyNprocessors and panics viaThreadRegistry::new/with_hardwarewhen the machine has fewer. There was no way to express "use up toN, but accept fewer if that is all the machine has".AtMostfills that gap for callers that want to cap resource usage while still running unchanged on smaller machines. For example,AtMost(32)uses 32 processors on a machine with at least 32, and all of them on a machine with fewer.How
ThreadRegistry::with_hardwareclamps the ceiling tohardware.processors().len()(the quota-enforced available count) and calls the existingProcessorSetBuilder::take(). No change tomany_cpusis required. UnlikeExactly,AtMostnever fails when the machine is smaller than the ceiling.Testing
Added three deterministic tests using the
many_cpus::fakehardware harness: request below available, request equal to available, and request exceeding available (clamps to all, no panic).cargo test -p thread_aware,just clippy,just format-check, and the docs.rs doc build all pass.