pub struct Cycles { /* private fields */ }
Expand description

Cycles provides a way to represent the number of cycles

Implementations§

source§

impl Cycles

source

pub fn as_usage(&self, d: Duration, f: Cycles) -> Result<f64, &str>

Returns the average number of cpu usage within a specified time

May be >100%, because the current frequency read is only a suggested frequency, not a real frequency, but the cycles are real

d: record the time of cycles

f: frequencey of this cpu core as Cycles

Suggestion: Read /sys/devices/system/cpu/cpuX/cpufreq/scaling_cur_freq to get the current frequency

use std::{fs, time::{Duration, Instant}};
use cpu_cycles_reader::Cycles;

let now = Instant::now();
let cycles_former = Cycles::from_ghz(1); // Suppose it is 1ghz at this time

// The cpu has performed some operations, assuming we are recording cpu7

let dur = Instant::now() - now;
let cycles_later = Cycles::from_ghz(2); // Suppose it is 2ghz at this time

let path = format!("/sys/devices/system/cpu/cpu{}/cpufreq/scaling_cur_freq", 7);
let cur_freq = fs::read_to_string(&path).unwrap();
let cur_freq = cur_freq.parse().unwrap();
let freq_cycles = Cycles::from_khz(cur_freq);

let cycles = cycles_later - cycles_former;
println!("{:.2}", cycles.as_usage(dur, freq_cycles).unwrap()); // Suppose you read cycles on cpu7
source

pub fn as_diff(&self, d: Duration, f: Cycles) -> Result<Cycles, &str>

Similar to as_usage, but returns the difference from the current frequency Cycles

For the same reason, diff may be negative

d: record the time of cycles

f: frequencey of this cpu core as Cycles

Suggestion: Read /sys/devices/system/cpu/cpuX/cpufreq/scaling_cur_freq to get the current frequency

use std::{fs, time::{Duration, Instant}};
use cpu_cycles_reader::Cycles;

let now = Instant::now();
let cycles_former = Cycles::from_ghz(1); // Suppose it is 1ghz at this time

// The cpu has performed some operations, assuming we are recording the cpu7

let dur = Instant::now() - now;
let cycles_later = Cycles::from_ghz(2); // Suppose it is 2ghz at this time

let cycles = cycles_later - cycles_former;

let path = format!("/sys/devices/system/cpu/cpu{}/cpufreq/scaling_cur_freq", 7);
let cur_freq = fs::read_to_string(&path).unwrap();
let cur_freq = cur_freq.parse().unwrap();
let freq_cycles = Cycles::from_khz(cur_freq);

println!("{}", cycles.as_diff(dur, freq_cycles).unwrap());
source§

impl Cycles

source

pub fn from_hz(h: c_ll) -> Self

use cpu_cycles_reader::Cycles;

assert_eq!(Cycles::new(1000), Cycles::from_hz(1000));
source

pub fn from_khz(k: c_ll) -> Self

use cpu_cycles_reader::Cycles;

assert_eq!(Cycles::new(1000), Cycles::from_khz(1));
source

pub fn from_mhz(m: c_ll) -> Self

use cpu_cycles_reader::Cycles;

assert_eq!(Cycles::new(2_000_000), Cycles::from_mhz(2));
source

pub fn from_ghz(g: c_ll) -> Self

use cpu_cycles_reader::Cycles;

assert_eq!(Cycles::new(3_000_000_000), Cycles::from_ghz(3));
source

pub fn as_hz(&self) -> c_ll

use cpu_cycles_reader::Cycles;

assert_eq!(Cycles::new(1000).as_hz(), 1000);
source

pub fn as_khz(&self) -> c_ll

use cpu_cycles_reader::Cycles;

assert_eq!(Cycles::new(1_000_000).as_khz(), 1000);
source

pub fn as_mhz(&self) -> c_ll

use cpu_cycles_reader::Cycles;

assert_eq!(Cycles::new(1_000_000_000).as_mhz(), 1000);
source

pub fn as_ghz(&self) -> c_ll

use cpu_cycles_reader::Cycles;

assert_eq!(Cycles::new(1_000_000_000_000).as_ghz(), 1000);
source§

impl Cycles

source

pub fn new(raw: c_ll) -> Cycles

Trait Implementations§

source§

impl Add<Cycles> for Cycles

§

type Output = Cycles

The resulting type after applying the + operator.
source§

fn add(self, rhs: Cycles) -> Cycles

Performs the + operation. Read more
source§

impl Clone for Cycles

source§

fn clone(&self) -> Cycles

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Cycles

source§

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

Formats the value using the given formatter. Read more
source§

impl Display for Cycles

source§

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

Formats the value using the given formatter. Read more
source§

impl<__RhsT> Div<__RhsT> for Cycleswhere c_ll: Div<__RhsT, Output = c_ll>,

§

type Output = Cycles

The resulting type after applying the / operator.
source§

fn div(self, rhs: __RhsT) -> Cycles

Performs the / operation. Read more
source§

impl From<Cycles> for c_ll

source§

fn from(original: Cycles) -> Self

Converts to this type from the input type.
source§

impl From<i64> for Cycles

source§

fn from(original: c_ll) -> Cycles

Converts to this type from the input type.
source§

impl<__RhsT> Mul<__RhsT> for Cycleswhere c_ll: Mul<__RhsT, Output = c_ll>,

§

type Output = Cycles

The resulting type after applying the * operator.
source§

fn mul(self, rhs: __RhsT) -> Cycles

Performs the * operation. Read more
source§

impl Ord for Cycles

source§

fn cmp(&self, other: &Cycles) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Cycles> for Cycles

source§

fn eq(&self, other: &Cycles) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Cycles> for Cycles

source§

fn partial_cmp(&self, other: &Cycles) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Sub<Cycles> for Cycles

§

type Output = Cycles

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Cycles) -> Cycles

Performs the - operation. Read more
source§

impl Sum<Cycles> for Cycles

source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl Copy for Cycles

source§

impl Eq for Cycles

source§

impl StructuralEq for Cycles

source§

impl StructuralPartialEq for Cycles

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.