-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
std::thread::available_parallelism() return 0 #115868
Comments
Looking at the relevant code, it appears that if rust/library/std/src/sys/unix/thread.rs Line 325 in 56e1aaa
Could you try to run this small program, and return us the output of it? fn main() {
let mut set: libc::cpu_set_t = unsafe { std::mem::zeroed() };
unsafe {
if dbg!(libc::sched_getaffinity(0, std::mem::size_of::<libc::cpu_set_t>(), &mut set)) == 0 {
dbg!(libc::CPU_COUNT(&set));
}
}
} If it prints out @rustbot label +T-libs +I-prioritize +O-linux -needs-triage |
Can you post the output of running your test program with |
Right, that's what I thought. Let's try to pin point where the bug is by trying the same thing in C. Could you try to run this small program, and return us the output of it? #define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
int main() {
cpu_set_t set;
printf("sizeof(set) = %d\n", sizeof(set));
int ret = sched_getaffinity(0, sizeof(set), &set);
printf("sched_getaffinity = %d\n", ret);
int count = CPU_COUNT(&set);
printf("CPU_COUNT(&set) = %d\n", count);
return 0;
} |
I think for any reason, when the value of 'CPU_COUNT' is 0, 'available_parallelism' should return Err |
Sorry, I don't know how to compile C program onto the router |
Well yes, but we shouldn't get 0 for starter.
Hum, that's a bummer. Then I don't know how to debug this further. |
Can you provide the compiled binary for me to run? The system environment has been given |
I'm sorry, I don't know your environment, and I haven't used any MIPS. However if you able to compile with Could you try to run this small program, and return us the output of it? fn main() {
dbg!(std::mem::size_of::<libc::cpu_set_t>());
dbg!(std::mem::size_of::<rustix::process::CpuSet>());
if let Ok(cpu_set) = dbg!(rustix::process::sched_getaffinity(None)) {
dbg!(cpu_set.count());
}
} Cargo.toml if needed
|
Related: seanmonstar/num_cpus#69 Unlike num_cpus, which returns usize, std returns NoneZeroUsize, so this is a soundness issue. rust/library/std/src/sys/unix/thread.rs Line 328 in 56e1aaa
@rustbot label +I-unsound |
|
Maybe it's worth saying this explicitly: As far as I can tell by reading the man pages, this is a bug in glibc or Linux. The man pages for Has anyone tried reporting this upstream? This issue is not the first time this problem has been encountered, but it looks to me like everyone else has papered over it. |
If we're down for cursed things, this program will print the raw bytes in the cpuset which would at least let us know if the kernel is putting anything in there: use libc::{cpu_set_t, sched_getaffinity, CPU_COUNT};
use std::mem::size_of;
fn main() {
unsafe {
let mut set: cpu_set_t = std::mem::zeroed();
let ret = sched_getaffinity(0, size_of::<cpu_set_t>(), &mut set);
dbg!(ret);
// Assume the kernel doesn't deinit any bytes in the set
let set_as_bytes = std::mem::transmute::<_, &[u8; std::mem::size_of::<cpu_set_t>()]>(&set);
println!("{:?}", set_as_bytes);
let count = CPU_COUNT(&set);
dbg!(count);
}
} |
libc::CPU_COUNT is implemented in pure rust with straightforward code: https://docs.rs/libc/latest/src/libc/unix/linux_like/linux/mod.rs.html#3988-3999 I'm not sure I think there's a way for that code to fail to miss any bits. |
@lbl8603 you haven't mentioned which target-triple you're compiling for. Some are tier 2 and some are tier 3. Some use glibc, some musl. This could be relevant if it's a bug in libc. |
This kernel version has reached EOL and is below the minimum kernel version for the tier 2 mips-gnu targets. |
The docs for But I'll admit the docs do also seem to contradict themselves a few paragraphs later:
|
Regardless of whether this kernel is EOL (we support plenty of EOL kernels) and whether the wobbliness of A quick Google search for "mips sched_getaffinity" turns up this patch: Just going off vibes, this looks like it fixes the bug we are talking about. As was pointed out correctly above, the kernel you're using is not supported by this target, and upgrading to a supported kernel would bring in the patch above. Can you confirm that upgrading to a supported kernel (at least 4.4)? If an upgrade to at least Linux 4.4 fixes this issue, I do not think we should try to fall back to returning 1 if |
Yeah, panic makes sense. We should notify users that the system is bugged and unsupported. |
Rollup merge of rust-lang#115946 - the8472:panic-on-sched_getaffinity-bug, r=Mark-Simulacrum panic when encountering an illegal cpumask in thread::available_parallelism Fixes rust-lang#115868 by panicking instead of returning an invalid `NonZeroUsize`
…-bug, r=cuviper Fall back to _SC_NPROCESSORS_ONLN if sched_getaffinity returns an empty mask Followup to rust-lang#115946 A gentler fix for rust-lang#115868, one that doesn't panic, [suggested on zulip](https://rust-lang.zulipchat.com/#narrow/stream/259402-t-libs.2Fmeetings/topic/Meeting.202023-09-19/near/391942927) In that situation - on the buggy kernel versions - a zero-mask means no affinities have been set so `_SC_NPROCESSORS_ONLN` provides the right value.
Rollup merge of rust-lang#116038 - the8472:panic-on-sched_getaffinity-bug, r=cuviper Fall back to _SC_NPROCESSORS_ONLN if sched_getaffinity returns an empty mask Followup to rust-lang#115946 A gentler fix for rust-lang#115868, one that doesn't panic, [suggested on zulip](https://rust-lang.zulipchat.com/#narrow/stream/259402-t-libs.2Fmeetings/topic/Meeting.202023-09-19/near/391942927) In that situation - on the buggy kernel versions - a zero-mask means no affinities have been set so `_SC_NPROCESSORS_ONLN` provides the right value.
available_parallelism() return 0
I tried this code:
rustc --version --verbose
:cat /proc/cpuinfo
The text was updated successfully, but these errors were encountered: