Skip to main content

Sampler

Struct Sampler 

Source
pub struct Sampler { /* private fields */ }
Expand description

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.

use std::time::Duration;
use sys_lib as sys;

let mut cpu = sys::cpu::Sampler::new()?;
let mut per_core = vec![sys::Percent::ZERO; sys::cpu::logical()];
loop {
    std::thread::sleep(Duration::from_secs(5));
    let u = cpu.sample_per_core_into(&mut per_core)?;
    println!("used {} (user {}, system {}, steal {:?})", u.used(), u.user, u.system, u.steal);
}

Implementations§

Source§

impl Sampler

Source

pub fn new() -> Result<Sampler>

Takes the first reading, of the total and of every core.

§Errors

As times.

Source

pub fn sample(&mut self) -> Result<Usage>

Usage since the previous sample.

§Errors

TooSoon when no tick passed on the OS’s counters since the previous sample (about 10 ms on Linux: E-21), or when they went backwards, which re-bases the sampler (E-20). Never a meaningless 0% or 100%.

Source

pub fn sample_per_core(&mut self) -> Result<(Usage, Vec<Percent>)>

Usage since the previous sample, total and per core (allocates the Vec).

§Errors

As sample_per_core_into.

Source

pub fn sample_per_core_into(&mut self, out: &mut [Percent]) -> Result<Usage>

Usage since the previous sample, writing each core’s busy share into out (Tier 3: no allocation). Entries past the number of cores are left as they were. A core whose counters went backwards, or did not move, reads 0% for this interval and is re-based (E-20); the total is unaffected.

§Errors

TooLarge without writing anything when out is shorter than the number of cores (on Linux, the highest CPU number plus one: an offline CPU keeps its slot and reads 0%); TooSoon as sample; Unsupported per core on Windows with more than 64 logical processors or more than one processor group.

Trait Implementations§

Source§

impl Debug for Sampler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.