about summary refs log tree commit diff
path: root/library/std/src/sys/unix/thread.rs
AgeCommit message (Collapse)AuthorLines
2024-01-11std: begin moving platform support modules into `pal`joboet-992/+0
2023-12-30openbsd: available_parallelism: use the right APISebastien Marie-26/+6
use the standard sysconf(_SC_NPROCESSORS_ONLN) way to get the number of available processors (capable of running processes), and fallback to sysctl([CTL_HW, HW_NCPU]) (number of CPUs configured) only on error. it permits to differenciate CPUs online vs CPUs configured (and not necessary capable of running processes). while here, use the common code path for BSDs for doing that, and avoid code duplication. Problem initially reported to me by Jiri Navratil.
2023-12-03library: use c string literalsklensy-2/+1
2023-11-01Auto merge of #115356 - devnexen:haiku_set_name_use_return, r=thomccbors-1/+3
`std::thread::set_name` exploit the return on haiku
2023-10-09Support AIX in Rust standard libraryQiu Chaofan-0/+2
2023-09-22Rollup merge of #116038 - the8472:panic-on-sched_getaffinity-bug, r=cuviperMatthias Krüger-6/+19
Fall back to _SC_NPROCESSORS_ONLN if sched_getaffinity returns an empty mask Followup to #115946 A gentler fix for #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.
2023-09-21Auto merge of #115230 - Vtewari2311:mod-hurd-latest, r=b-naberbors-2/+8
added support for GNU/Hurd adding support for i686-unknown-hurd-gnu
2023-09-21Fall back to _SC_NPROCESSORS_ONLN if sched_getaffinity returns an empty maskThe 8472-6/+19
2023-09-21added support for GNU/HurdSamuel Thibault-2/+8
2023-09-18panic when encountering an illegal cpumaskThe 8472-2/+4
2023-08-29std::thread::set_name exploit the return on haikuDavid CARLIER-1/+3
2023-08-18QNX: pass a truncated thread name to the OSJorge Aparicio-0/+4
The maximum length the thread name can have is `_NTO_THREAD_NAME_MAX`
2023-07-05Revert "use c literals in library"León Orell Valerian Liehr-1/+2
This reverts commit f212ba6d6d60963c8101bb24fc3e53fca80c046f.
2023-06-21Finish up preliminary tvos support in libstdThom Chiovoloni-1/+7
2023-06-21wip: Support Apple tvOS in libstdThom Chiovoloni-1/+2
2023-06-06std: available_parallelism using native netbsd api firstDavid Carlier-0/+23
before falling back to existing code paths like FreeBSD does.
2023-05-31use c literals in libraryklensy-2/+1
2023-04-25Add FreeBSD cpuset support to std::thread::available_concurrencyThomas Hurst-0/+19
Use libc::cpuset_getaffinity to determine the CPUs available to the current process. The existing sysconf and sysctl paths are left as fallback.
2023-02-28Add QNX Neutrino support to libstdFlorian Bartels-2/+13
Co-authored-by: gh-tr <troach@qnx.com>
2023-01-17refactor[std]: do not use box syntaxjoboet-1/+1
2023-01-14Remove various double spaces in source comments.André Vennberg-8/+8
2022-12-28Rollup merge of #104493 - adamncasey:cgroupzeroperiod, r=m-ou-seMatthias Krüger-2/+2
available_parallelism: Gracefully handle zero value cfs_period_us There seem to be some scenarios where the cgroup cpu quota field `cpu.cfs_period_us` can contain `0`. This field is used to determine the "amount" of parallelism suggested by the function `std::thread::available_parallelism` A zero value of this field cause a panic when `available_parallelism()` is invoked. This issue was detected by the call from binaries built by `cargo test`. I really don't feel like `0` is a good value for `cpu.cfs_period_us`, but I also don't think applications should panic if this value is seen. This panic started happening with rust 1.64.0. This case is gracefully handled by other projects which read this information: [num_cpus](https://github.com/seanmonstar/num_cpus/blob/e437b9d9083d717692e35d917de8674a7987dd06/src/linux.rs#L207-L210), [ninja](https://github.com/ninja-build/ninja/pull/2174/files), [dotnet](https://github.com/dotnet/runtime/blob/c4341d45acca3ea662cd8d71e7d71094450dd045/src/coreclr/pal/src/misc/cgroup.cpp#L481-L483) Before this change, running `cargo test` in environments configured as described above would trigger this panic: ``` $ RUST_BACKTRACE=1 cargo test Finished test [unoptimized + debuginfo] target(s) in 3.55s Running unittests src/main.rs (target/debug/deps/x-9a42e145aca2934d) thread 'main' panicked at 'attempt to divide by zero', library/std/src/sys/unix/thread.rs:546:70 stack backtrace: 0: rust_begin_unwind 1: core::panicking::panic_fmt 2: core::panicking::panic 3: std::sys::unix::thread::cgroups::quota 4: std::sys::unix::thread::available_parallelism 5: std::thread::available_parallelism 6: test::helpers::concurrency::get_concurrency 7: test::console::run_tests_console 8: test::test_main 9: test::test_main_static 10: x::main at ./src/main.rs:1:1 11: core::ops::function::FnOnce::call_once at /tmp/rust-1.64-1.64.0-1/library/core/src/ops/function.rs:248:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. error: test failed, to rerun pass '--bin x' ``` I've tested this change in an environment which has the bad (questionable?) setup and rebuilding the test executable against a fixed std library fixes the panic.
2022-12-22std: only use LFS function on glibcmochaaP-2/+2
see #94173 and commit 27011b4185f5341e579d2a02cabd3dc7d7aa7149.
2022-12-14Rollup merge of #105399 - mikebenfield:lfs, r=thomccMatthias Krüger-2/+5
Use more LFS functions. On Linux, use mmap64, open64, openat64, and sendfile64 in place of their non-LFS counterparts. This is relevant to #94173. With these changes (together with rust-lang/backtrace-rs#501), the simple binaries I produce with rustc seem to have no non-LFS functions, so maybe #94173 is fixed. But I can't be sure if I've missed something and maybe some non-LFS functions could sneak in somehow.
2022-12-07Avoid heap allocation when truncating thread namesGavin Li-12/+7
Ensure that heap allocation does not occur in a thread until std::thread is ready. This fixes issues with custom allocators that call std::thread::current(), since doing so prematurely initializes THREAD_INFO and causes the following thread_info::set() to fail.
2022-12-07Use more LFS functions.Michael Benfield-2/+5
On Linux, use mmap64, open64, openat64, and sendfile64 in place of their non-LFS counterparts. This is relevant to #94173. With these changes (together with rust-lang/backtrace-rs#501), the simple binaries I produce with rustc seem to have no non-LFS functions, so maybe #94173 is fixed. But I can't be sure if I've missed something and maybe some non-LFS functions could sneak in somehow.
2022-11-16available_parallelism: Handle 0 cfs_period_usAdam Casey-2/+2
There seem to be some scenarios where `cpu.cfs_period_us` can contain `0` This causes a panic when calling `std::thread::available_parallelism()` as is done so from binaries built by `cargo test`, which was how the issue was discovered. I don't feel like `0` is a good value for `cpu.cfs_period_us`, but I also don't think applications should panic if this value is seen. This case is handled by other projects which read this information: - num_cpus: https://github.com/seanmonstar/num_cpus/blob/e437b9d9083d717692e35d917de8674a7987dd06/src/linux.rs#L207-L210 - ninja: https://github.com/ninja-build/ninja/pull/2174/files - dotnet: https://github.com/dotnet/runtime/blob/c4341d45acca3ea662cd8d71e7d71094450dd045/src/coreclr/pal/src/misc/cgroup.cpp#L481-L483 Before this change, this panic could be seen in environments setup as described above: ``` $ RUST_BACKTRACE=1 cargo test Finished test [unoptimized + debuginfo] target(s) in 3.55s Running unittests src/main.rs (target/debug/deps/x-9a42e145aca2934d) thread 'main' panicked at 'attempt to divide by zero', library/std/src/sys/unix/thread.rs:546:70 stack backtrace: 0: rust_begin_unwind 1: core::panicking::panic_fmt 2: core::panicking::panic 3: std::sys::unix::thread::cgroups::quota 4: std::sys::unix::thread::available_parallelism 5: std::thread::available_parallelism 6: test::helpers::concurrency::get_concurrency 7: test::console::run_tests_console 8: test::test_main 9: test::test_main_static 10: x::main at ./src/main.rs:1:1 11: core::ops::function::FnOnce::call_once at /tmp/rust-1.64-1.64.0-1/library/core/src/ops/function.rs:248:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. error: test failed, to rerun pass '--bin local-rabmq-amqpprox' ``` I've tested this change in an environment which has the bad setup and rebuilding the test executable against a fixed std library fixes the panic.
2022-10-26thread::set_name: debug-assert that things went wellRalf Jung-6/+10
2022-10-21Move truncation next to other thread tests for tidyJosh Stone-25/+0
2022-10-21Truncate thread names on Linux and Apple targetsJosh Stone-0/+43
These targets have system limits on the thread names, 16 and 64 bytes respectively, and `pthread_setname_np` returns an error if the name is longer. However, we're not in a context that can propagate errors when we call this, and we used to implicitly truncate on Linux with `prctl`, so now we manually truncate these names ahead of time.
2022-10-13Rollup merge of #102854 - semarie:openbsd-immutablestack, r=m-ou-seDylan DPC-0/+10
openbsd: don't reallocate a guard page on the stack. the kernel currently enforce that a stack is immutable. calling mmap(2) or mprotect(2) to change it will result in EPERM, which generate a panic!(). so just do like for Linux, and trust the kernel to do the right thing.
2022-10-09openbsd: don't reallocate a guard page on the stack.Sébastien Marie-0/+10
the kernel currently enforce that a stack is immutable. calling mmap(2) or mprotect(2) to change it will result in EPERM, which generate a panic!(). so just do like for Linux, and trust the kernel to do the right thing.
2022-10-03Reduce CString allocations in std as much as possibleAlex Saveau-2/+1
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-09-03more clippy::perf fixesMatthias Krüger-1/+1
2022-08-08linux: Use `pthread_setname_np` instead of `prctl`Josh Stone-3/+9
This function is available on Linux since glibc 2.12, musl 1.1.16, and uClibc 1.0.20. The main advantage over `prctl` is that it properly represents the pointer argument, rather than a multi-purpose `long`, so we're better representing strict provenance (#95496).
2022-07-23Auto merge of #97925 - the8472:cgroupv1, r=joshtriplettbors-46/+189
Add cgroupv1 support to available_parallelism Fixes #97549 My dev machine uses cgroup v2 so I was only able to test that code path. So the v1 code path is written only based on documentation. I could use some help testing that it works on a machine with cgroups v1: ``` $ x.py build --stage 1 # quota.rs fn main() { println!("{:?}", std::thread::available_parallelism()); } # assuming stage1 is linked in rustup $ rust +stage1 quota.rs # spawn a new cgroup scope for the current user $ sudo systemd-run -p CPUQuota="300%" --uid=$(id -u) -tdS # should print Ok(3) $ ./quota ``` If it doesn't work as expected an strace, the contents of `/proc/self/cgroups` and the structure of `/sys/fs/cgroups` would help.
2022-07-20Library changes for Apple WatchOSVladimir Michael Eatwell-1/+1
2022-06-24scan mountinfo when hardcoded cgroupv1 mountpoints don't workThe 8472-19/+83
2022-06-09add cgroupv1 support to available_parallelismThe 8472-47/+126
2022-04-06Change trailing prctl arguments to c_ulongBen Kimock-1/+7
2022-04-03Don't cast thread name to an integer for prctlBen Kimock-1/+1
libc::prctl and the prctl definitions in glibc, musl, and the kernel headers are C variadic functions. Therefore, all the arguments (except for the first) are untyped. It is only the Linux man page which says that prctl takes 4 unsigned long arguments. I have no idea why it says this. In any case, the upshot is that we don't need to cast the pointer to an integer and confuse Miri.
2022-03-30Fix library/std compilation on openbsd.Dan Gohman-1/+1
Fix a minor typo from #95241 which prevented compilation on x86_64-unknown-openbsd.
2022-03-29Make some linux/unix APIs better conform to strict provenance.Alexis Beingessner-23/+25
This largely makes the stdlib conform to strict provenance on Ubuntu. Some hairier things have been left alone for now.
2022-03-05do not attempt to open cgroup files under MiriRalf Jung-0/+5
2022-03-03hardcode /sys/fs/cgroup instead of doing a lookup via mountinfoThe 8472-53/+67
this avoids parsing mountinfo which can be huge on some systems and something might be emulating cgroup fs for sandboxing reasons which means it wouldn't show up as mountpoint additionally the new implementation operates on a single pathbuffer, reducing allocations
2022-03-03Use cgroup quotas for calculating `available_parallelism`The 8472-3/+68
Manually tested via ``` // spawn a new cgroup scope for the current user $ sudo systemd-run -p CPUQuota="300%" --uid=$(id -u) -tdS // quota.rs #![feature(available_parallelism)] fn main() { println!("{:?}", std::thread::available_parallelism()); // prints Ok(3) } ``` Caveats * cgroup v1 is ignored * funky mountpoints (containing spaces, newlines or control chars) for cgroupfs will not be handled correctly since that would require unescaping /proc/self/mountinfo The escaping behavior of procfs seems to be undocumented. systemd and docker default to `/sys/fs/cgroup` so it should be fine for most systems. * quota will be ignored when `sched_getaffinity` doesn't work * assumes procfs is mounted under `/proc` and cgroupfs mounted and readable somewhere in the directory tree
2022-02-04Hide Repr details from io::Error, and rework `io::Error::new_const`.Thom Chiovoloni-5/+5
2021-11-12Refactor weak symbols in std::sys::unixJosh Stone-7/+10
This makes a few changes to the weak symbol macros in `sys::unix`: - `dlsym!` is added to keep the functionality for runtime `dlsym` lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't want to show up in ELF symbol tables. - `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime behavior is just a simple null check. This is also used by `syscall!`. - On non-ELF targets (macos/ios) where that linkage is not known to behave, `weak!` is just an alias to `dlsym!` for the old behavior. - `raw_syscall!` is added to always call `libc::syscall` on linux and android, for cases like `clone3` that have no known libc wrapper. The new `weak!` linkage does mean that you'll get versioned symbols if you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`. This might seem problematic, but old non-weak symbols can tie the build to new versions too, like `dlsym@GLIBC_2.34` from their recent library unification. If you build with an old glibc like `dist-x86_64-linux` does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may be resolved based on the runtime glibc. I also found a few functions that don't need to be weak anymore: - Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as these were added in API 12, and our baseline is API 14. - Linux can directly use `splice`, added way back in glibc 2.5 and similarly old musl. Android only added it in API 21 though.
2021-10-31Make std::thread::available_concurrency support process-limited number of CPUsJosh Triplett-0/+8
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.
2021-10-10Auto merge of #88952 - skrap:add-armv7-uclibc, r=nagisabors-1/+2
Add new tier-3 target: armv7-unknown-linux-uclibceabihf This change adds a new tier-3 target: armv7-unknown-linux-uclibceabihf This target is primarily used in embedded linux devices where system resources are slim and glibc is deemed too heavyweight. Cross compilation C toolchains are available [here](https://toolchains.bootlin.com/) or via [buildroot](https://buildroot.org). The change is based largely on a previous PR #79380 with a few minor modifications. The author of that PR was unable to push the PR forward, and graciously allowed me to take it over. Per the [target tier 3 policy](https://github.com/rust-lang/rfcs/blob/master/text/2803-target-tier-policy.md), I volunteer to be the "target maintainer". This is my first PR to Rust itself, so I apologize if I've missed things!