Skip to main content

Module cpu

Module cpu 

Source
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 physical is 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). sample never blocks and allocates nothing; sample_per_core_into allocates 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. name is rustc’s target-feature name.
Vendor
A CPU designer or manufacturer.
X86Level
An x86-64 microarchitecture level (the psABI’s x86-64-v1 to v4).

Constants§

DEFAULT_WINDOW
The interval usage measures 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; available and online are read now.
current_frequency
The current frequency, averaged over the online CPUs. None where 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 for DEFAULT_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’s is_x86_feature_detected! and is_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 is cpu::arch().is_64bit().
load
The load average, or None on 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 possible count 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; logical when the topology is hidden (E-18). Always between 1 and logical().
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’s cpu.pressure, else /proc/pressure/cpu; None on kernels without pressure-stall information. macOS and Windows have no stall measure: None.
quota
The CPU quota on this process, or None when there is none. Linux: the smallest cgroup v2 cpu.max over the cgroup and its ancestors, or cgroup v1’s cpu.cfs_quota_us over cpu.cfs_period_us. Windows: the job object’s CPU rate cap. macOS: None. available already applies it, rounded up.
raw_name
The name exactly as the OS reports it, for support output; None where 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; Sampler gives usage.
usage
CPU usage over the next 100 ms. Blocks for DEFAULT_WINDOW; a monitor keeps a Sampler instead.
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 for DEFAULT_WINDOW.
vendor
The vendor.
x86_level
The x86-64 microarchitecture level this machine reaches; None off x86-64. Under Rosetta 2 it is what Rosetta emulates (no AVX: V2).