rust/library/std/src/sys/unix
Matthias Krüger 2dd2fb728e
Rollup merge of #104493 - adamncasey:cgroupzeroperiod, r=m-ou-se
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:🧵: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](e437b9d908/src/linux.rs (L207-L210)), [ninja](https://github.com/ninja-build/ninja/pull/2174/files), [dotnet](c4341d45ac/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:🧵:cgroups::quota
   4: std::sys::unix:🧵:available_parallelism
   5: std:🧵: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-28 22:22:18 +01:00
..
fd I/O safety. 2021-08-19 12:02:39 -07:00
kernel_copy add test for failing io::copy specialization 2021-02-22 21:41:32 +01:00
locks std: cleanup timeouts in pthread condvar 2022-12-02 14:38:20 +01:00
os Refactor weak symbols in std::sys::unix 2021-11-12 15:25:16 -08:00
os_str Expose Utf8Lossy as Utf8Chunks 2022-08-20 12:49:20 -04:00
process More verbose Debug implementation of std::process:Command 2022-12-27 09:50:01 +01:00
thread_parker Auto merge of #100539 - joboet:horizon_timeout_clock, r=thomcc 2022-12-28 03:56:46 +00:00
alloc.rs Horizon OS STD support 2022-06-13 20:44:39 -07:00
android.rs Refactor weak symbols in std::sys::unix 2021-11-12 15:25:16 -08:00
args.rs Library changes for Apple WatchOS 2022-07-20 08:57:36 +01:00
cmath.rs Reuse unix::cmath 2021-04-28 14:25:04 +02:00
env.rs Library changes for Apple WatchOS 2022-07-20 08:57:36 +01:00
fd.rs Address reviewer comments 2022-08-18 10:34:40 +01:00
fs.rs std: only use LFS function on glibc 2022-12-22 16:01:27 +08:00
futex.rs std: panic instead of deadlocking in mutex implementation on Fuchsia 2022-07-18 10:56:10 +02:00
io.rs Add IsTerminal trait to determine if a descriptor or handle is a terminal 2022-10-15 00:35:38 +01:00
kernel_copy.rs std: only use LFS function on glibc 2022-12-22 16:01:27 +08:00
l4re.rs Use Rust 2021 prelude in std itself. 2022-05-09 11:12:32 +02:00
memchr.rs mark FIXMES for all the places found that are probably offset_from 2022-03-29 20:18:28 -04:00
mod.rs std: only use LFS function on glibc 2022-12-22 16:01:27 +08:00
net.rs Rollup merge of #96334 - devnexen:socket_mark, r=dtolnay 2022-08-29 06:34:42 +02:00
os_str.rs Expose Utf8Lossy as Utf8Chunks 2022-08-20 12:49:20 -04:00
os.rs Reduce CString allocations in std as much as possible 2022-10-03 11:13:17 -07:00
path.rs Unix path::absolute: Fix leading "." component 2022-03-05 17:57:12 +00:00
pipe.rs Add read_to_end for AnonPipe 2022-12-08 18:12:15 +05:30
rand.rs Revert "Use getentropy when possible on all Apple platforms" 2022-10-05 00:48:22 -05:00
stack_overflow.rs std: only use LFS function on glibc 2022-12-22 16:01:27 +08:00
stdio.rs Consolidate AsFd instances for stdio types into library/std/src/os/fd/owned.rs 2022-10-10 14:47:22 +01:00
thread_local_dtor.rs sync thread_local key conditions exactly with what the macro uses 2022-10-13 14:09:08 +02:00
thread_local_key.rs std: optimize TLS on Windows 2022-10-08 20:19:21 +02:00
thread.rs Rollup merge of #104493 - adamncasey:cgroupzeroperiod, r=m-ou-se 2022-12-28 22:22:18 +01:00
time.rs std: cleanup timeouts in pthread condvar 2022-12-02 14:38:20 +01:00
weak.rs Reimplement weak! using Option. 2022-12-05 15:05:43 -08:00