Struct cpu_cycles_reader::Cycles
source · pub struct Cycles { /* private fields */ }Expand description
Cycles provides a way to represent the number of cycles
Implementations§
source§impl Cycles
impl Cycles
sourcepub fn as_usage(&self, d: Duration, f: Self) -> Result<f64, Box<dyn Error>>
pub fn as_usage(&self, d: Duration, f: Self) -> Result<f64, Box<dyn Error>>
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
Errors
Divide by zero or fail to trans u128 to i64
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 cpu7sourcepub fn as_diff(&self, d: Duration, f: Self) -> Result<Self, Box<dyn Error>>
pub fn as_diff(&self, d: Duration, f: Self) -> Result<Self, Box<dyn Error>>
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
Errors
Divide by zero
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
impl Cycles
sourcepub const fn from_hz(h: c_ll) -> Self
pub const fn from_hz(h: c_ll) -> Self
use cpu_cycles_reader::Cycles;
assert_eq!(Cycles::new(1000), Cycles::from_hz(1000));sourcepub const fn from_khz(k: c_ll) -> Self
pub const fn from_khz(k: c_ll) -> Self
use cpu_cycles_reader::Cycles;
assert_eq!(Cycles::new(1000), Cycles::from_khz(1));sourcepub const fn from_mhz(m: c_ll) -> Self
pub const fn from_mhz(m: c_ll) -> Self
use cpu_cycles_reader::Cycles;
assert_eq!(Cycles::new(2_000_000), Cycles::from_mhz(2));sourcepub const fn from_ghz(g: c_ll) -> Self
pub const fn from_ghz(g: c_ll) -> Self
use cpu_cycles_reader::Cycles;
assert_eq!(Cycles::new(3_000_000_000), Cycles::from_ghz(3));sourcepub const fn as_hz(&self) -> c_ll
pub const fn as_hz(&self) -> c_ll
use cpu_cycles_reader::Cycles;
assert_eq!(Cycles::new(1000).as_hz(), 1000);sourcepub const fn as_khz(&self) -> c_ll
pub const fn as_khz(&self) -> c_ll
use cpu_cycles_reader::Cycles;
assert_eq!(Cycles::new(1_000_000).as_khz(), 1000);Trait Implementations§
source§impl AddAssign<Cycles> for Cycles
impl AddAssign<Cycles> for Cycles
source§fn add_assign(&mut self, rhs: Cycles)
fn add_assign(&mut self, rhs: Cycles)
+= operation. Read moresource§impl<__RhsT> DivAssign<__RhsT> for Cycleswhere
c_ll: DivAssign<__RhsT>,
impl<__RhsT> DivAssign<__RhsT> for Cycleswhere c_ll: DivAssign<__RhsT>,
source§fn div_assign(&mut self, rhs: __RhsT)
fn div_assign(&mut self, rhs: __RhsT)
/= operation. Read moresource§impl<__RhsT> MulAssign<__RhsT> for Cycleswhere
c_ll: MulAssign<__RhsT>,
impl<__RhsT> MulAssign<__RhsT> for Cycleswhere c_ll: MulAssign<__RhsT>,
source§fn mul_assign(&mut self, rhs: __RhsT)
fn mul_assign(&mut self, rhs: __RhsT)
*= operation. Read moresource§impl Ord for Cycles
impl Ord for Cycles
source§impl PartialEq<Cycles> for Cycles
impl PartialEq<Cycles> for Cycles
source§impl PartialOrd<Cycles> for Cycles
impl PartialOrd<Cycles> for Cycles
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl<__RhsT> RemAssign<__RhsT> for Cycleswhere
c_ll: RemAssign<__RhsT>,
impl<__RhsT> RemAssign<__RhsT> for Cycleswhere c_ll: RemAssign<__RhsT>,
source§fn rem_assign(&mut self, rhs: __RhsT)
fn rem_assign(&mut self, rhs: __RhsT)
%= operation. Read moresource§impl SubAssign<Cycles> for Cycles
impl SubAssign<Cycles> for Cycles
source§fn sub_assign(&mut self, rhs: Cycles)
fn sub_assign(&mut self, rhs: Cycles)
-= operation. Read more