Rollup merge of #89670 - yoshuawuyts:available-parallelism-docs, r=joshtriplett
Improve `std:🧵:available_parallelism` docs _Tracking issue: https://github.com/rust-lang/rust/issues/74479_ This PR reworks the documentation of `std:🧵:available_parallelism`, as requested [here](https://github.com/rust-lang/rust/pull/89324#issuecomment-934343254). ## Changes The following changes are made: - We've removed prior mentions of "hardware threads" and instead centers the docs around "parallelism" as a resource available to a program. - We now provide examples of when `available_parallelism` may return numbers that differ from the number of CPU cores in the host machine. - We now mention that the amount of available parallelism may change over time. - We make note of which platform components we don't take into account which more advanced users may want to take note of. - The example has been updated, which should be a bit easier to use. - We've added a docs alias to `num-cpus` which provides similar functionality to `available_parallelism`, and is one of the most popular crates on crates.io. --- Thanks! r? `@BurntSushi`
This commit is contained in:
commit
06110c0c46
1 changed files with 54 additions and 17 deletions
|
@ -1428,39 +1428,76 @@ fn _assert_sync_and_send() {
|
||||||
_assert_both::<Thread>();
|
_assert_both::<Thread>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of hardware threads available to the program.
|
/// Returns an estimate of the default amount of parallelism a program should use.
|
||||||
///
|
///
|
||||||
/// This value should be considered only a hint.
|
/// Parallelism is a resource. A given machine provides a certain capacity for
|
||||||
|
/// parallelism, i.e., a bound on the number of computations it can perform
|
||||||
|
/// simultaneously. This number often corresponds to the amount of CPUs or
|
||||||
|
/// computer has, but it may diverge in various cases.
|
||||||
///
|
///
|
||||||
/// # Platform-specific behavior
|
/// Host environments such as VMs or container orchestrators may want to
|
||||||
|
/// restrict the amount of parallelism made available to programs in them. This
|
||||||
|
/// is often done to limit the potential impact of (unintentionally)
|
||||||
|
/// resource-intensive programs on other programs running on the same machine.
|
||||||
///
|
///
|
||||||
/// If interpreted as the number of actual hardware threads, it may undercount on
|
/// # Limitations
|
||||||
/// Windows systems with more than 64 hardware threads. If interpreted as the
|
///
|
||||||
/// available concurrency for that process, it may overcount on Windows systems
|
/// The purpose of this API is to provide an easy and portable way to query
|
||||||
/// when limited by a process wide affinity mask or job object limitations, and
|
/// the default amount of parallelism the program should use. Among other things it
|
||||||
/// it may overcount on Linux systems when limited by a process wide affinity
|
/// does not expose information on NUMA regions, does not account for
|
||||||
/// mask or affected by cgroups limits.
|
/// differences in (co)processor capabilities, and will not modify the program's
|
||||||
|
/// global state in order to more accurately query the amount of available
|
||||||
|
/// parallelism.
|
||||||
|
///
|
||||||
|
/// The value returned by this function should be considered a simplified
|
||||||
|
/// approximation of the actual amount of parallelism available at any given
|
||||||
|
/// time. To get a more detailed or precise overview of the amount of
|
||||||
|
/// parallelism available to the program, you may wish to use
|
||||||
|
/// platform-specific APIs as well. The following platform limitations currently
|
||||||
|
/// apply to `available_parallelism`:
|
||||||
|
///
|
||||||
|
/// On Windows:
|
||||||
|
/// - It may undercount the amount of parallelism available on systems with more
|
||||||
|
/// than 64 logical CPUs. However, programs typically need specific support to
|
||||||
|
/// take advantage of more than 64 logical CPUs, and in the absence of such
|
||||||
|
/// support, the number returned by this function accurately reflects the
|
||||||
|
/// number of logical CPUs the program can use by default.
|
||||||
|
/// - It may overcount the amount of parallelism available on systems limited by
|
||||||
|
/// process-wide affinity masks, or job object limitations.
|
||||||
|
///
|
||||||
|
/// On Linux:
|
||||||
|
/// - It may overcount the amount of parallelism available when limited by a
|
||||||
|
/// process-wide affinity mask, or when affected by cgroup limits.
|
||||||
|
///
|
||||||
|
/// On all targets:
|
||||||
|
/// - It may overcount the amount of parallelism available when running in a VM
|
||||||
|
/// with CPU usage limits (e.g. an overcommitted host).
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// This function will return an error in the following situations, but is not
|
/// This function will, but is not limited to, return errors in the following
|
||||||
/// limited to just these cases:
|
/// cases:
|
||||||
///
|
///
|
||||||
/// - If the number of hardware threads is not known for the target platform.
|
/// - If the amount of parallelism is not known for the target platform.
|
||||||
/// - The process lacks permissions to view the number of hardware threads
|
/// - If the program lacks permission to query the amount of parallelism made
|
||||||
/// available.
|
/// available to it.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # #![allow(dead_code)]
|
/// # #![allow(dead_code)]
|
||||||
/// #![feature(available_parallelism)]
|
/// #![feature(available_parallelism)]
|
||||||
/// use std::thread;
|
/// use std::{io, thread};
|
||||||
///
|
///
|
||||||
/// let count = thread::available_parallelism().map(|n| n.get()).unwrap_or(1);
|
/// fn main() -> io::Result<()> {
|
||||||
|
/// let count = thread::available_parallelism()?.get();
|
||||||
|
/// assert!(count >= 1_usize);
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
#[doc(alias = "available_concurrency")] // Alias for a previous name we gave this API on unstable.
|
||||||
#[doc(alias = "hardware_concurrency")] // Alias for C++ `std::thread::hardware_concurrency`.
|
#[doc(alias = "hardware_concurrency")] // Alias for C++ `std::thread::hardware_concurrency`.
|
||||||
#[doc(alias = "available_concurrency")] // Alias for a name we gave this API on unstable.
|
#[doc(alias = "num_cpus")] // Alias for a popular ecosystem crate which provides similar functionality.
|
||||||
#[unstable(feature = "available_parallelism", issue = "74479")]
|
#[unstable(feature = "available_parallelism", issue = "74479")]
|
||||||
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
|
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
|
||||||
imp::available_parallelism()
|
imp::available_parallelism()
|
||||||
|
|
Loading…
Add table
Reference in a new issue