Expand description
The processor: architecture and bitness, name and vendor, the counts (cores, hardware threads, sockets, and what this process may use), the frequency, usage, load, pressure, and instruction-set features.
use sys_lib as sys;
println!("{} ({:?}, {})", sys::cpu::name(), sys::cpu::vendor(), sys::cpu::arch().name());
println!("{} cores, {} threads, {} usable here", sys::cpu::physical(), sys::cpu::logical(), sys::cpu::available());
// Size a worker pool by what the container allows, not by the host.
let workers = sys::cpu::available();Structs§
- Counts
- Every count at once, with whether
physicalis exact. - CpuQuota
- A CPU quota: how much CPU time this process may use, in CPUs.
- Features
- The features detected on this machine, as a set.
- Frequency
- The base and maximum clock frequency.
- Load
- The load average: how many programs were running or waiting for a CPU, on average, over the last 1, 5, and 15 minutes.
- Sampler
- Measures CPU usage between samples. Owned by the caller: two samplers never
disturb each other (there is no hidden global baseline).
samplenever blocks and allocates nothing;sample_per_core_intoallocates nothing unless CPUs were hot-plugged since the previous sample. - Times
- The OS’s cumulative CPU time counters, since boot (Tier 3). States an OS does not keep are zero.
- Usage
- CPU usage over an interval.
Enums§
- Arch
- A processor architecture.
- Feature
- An instruction-set feature.
nameis rustc’s target-feature name. - Vendor
- A CPU designer or manufacturer.
- X86Level
- An x86-64 microarchitecture level (the psABI’s
x86-64-v1tov4).
Constants§
- DEFAULT_
WINDOW - The interval
usagemeasures over: 100 ms.
Functions§
- arch
- The architecture this program was compiled for.
- available
- The CPUs this process may use right now: the affinity mask, intersected
with the cgroup cpuset, then limited by the cgroup CPU quota (Linux), or the
group affinity and the job object’s CPU rate (Windows). Size worker pools by
this. A live reading: affinity, hot-plug, and quotas change. Always between
1 and
logical(). - count
- Every count at once;
availableandonlineare read now. - current_
frequency - The current frequency, averaged over the online CPUs.
Nonewhere the OS does not report it (macOS). - features
- The features this machine supports, detected once (about 1 ns afterwards).
- free
- The idle share over the next 100 ms:
usage()?.free(). Blocks forDEFAULT_WINDOW. - frequency
- The base and maximum frequency: a static fact. Apple silicon reports neither. There is no “total frequency”: a sum of per-core frequencies means nothing.
- has
- Whether this machine supports
feature: the same answer as std’sis_x86_feature_detected!andis_aarch64_feature_detected!, which the compiler’s#[target_feature]dispatch trusts. - is_
32bit - Whether the processor is 32-bit only.
- is_
64bit - Whether the processor is 64-bit. A 32-bit program on a 64-bit machine
still sees
true(D-024); the program’s own bitness iscpu::arch().is_64bit(). - load
- The load average, or
Noneon Windows, which keeps none (nothing is simulated). Inside a container it is the host’s (with LXCFS, the container’s). - logical
- Configured hardware threads: the right size for per-CPU arrays. A static
fact (E-15: present CPUs, not the
possiblecount VMs inflate). Windows counts every processor group (FR-12). At least 1. - name
- The cleaned product name: “AMD Ryzen 9 7950X”, “Apple M2 Pro”, “Cortex-A76 + Cortex-A55”. Never empty: falls back to the vendor and architecture (“ARM aarch64”), then “unknown”.
- online
- The CPUs online now, which hot-plug changes; between 1 and
logical(). Linux:/sys/devices/system/cpu/online; macOS:hw.activecpu; Windows: the active processors across every processor group. - packages
- Processor packages (sockets): a static fact, 1 when the OS does not say.
Always between 1 and
physical(). - physical
- Physical cores;
logicalwhen the topology is hidden (E-18). Always between 1 andlogical(). - pressure
- How much runnable work waited for a CPU over the last 10, 60, and 300
seconds (
Pressure): the direct form of what the load average approximates. Linux: the cgroup’scpu.pressure, else/proc/pressure/cpu;Noneon kernels without pressure-stall information. macOS and Windows have no stall measure:None. - quota
- The CPU quota on this process, or
Nonewhen there is none. Linux: the smallest cgroup v2cpu.maxover the cgroup and its ancestors, or cgroup v1’scpu.cfs_quota_usovercpu.cfs_period_us. Windows: the job object’s CPU rate cap. macOS:None.availablealready applies it, rounded up. - raw_
name - The name exactly as the OS reports it, for support output;
Nonewhere the OS reports no brand string (most ARM Linux systems). - times
- The OS’s cumulative CPU time counters (Tier 3). One reading only gives the
average since boot;
Samplergives usage. - usage
- CPU usage over the next 100 ms. Blocks for
DEFAULT_WINDOW; a monitor keeps aSamplerinstead. - usage_
over - CPU usage over
window, clamped to 10 ms ..= 10 s. Blocks for the window. - used
- The busy share over the next 100 ms:
usage()?.used(). Blocks forDEFAULT_WINDOW. - vendor
- The vendor.
- x86_
level - The x86-64 microarchitecture level this machine reaches;
Noneoff x86-64. Under Rosetta 2 it is what Rosetta emulates (no AVX:V2).