talira

num_cpus

Count the CPUs on the current machine from Rust

open sourceMIT OR Apache-2.0free

num_cpus is a small Rust crate that counts the CPUs on the current machine. num_cpus::get() returns the logical cores the process could use, taking Linux scheduler affinity and cgroups into account, so it reports the smaller number a container is limited to, and get_physical() returns physical cores. Sean McArthur wrote it.

Problems it solves

  • find out how many CPU cores my Rust program can use
  • size a Rust thread pool to the CPUs a container is limited to
  • get the number of physical rather than logical cores in Rust

Features

  • logical CPU count
  • physical core count
  • respects Linux sched affinity
  • respects cgroup CPU limits in containers

Limitations

  • The CPU count is only a rough guide to how many tasks can run in parallel.1