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: Cycles) -> Result<f64, &str>
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 cpu7sourcepub fn as_diff(&self, d: Duration, f: Cycles) -> Result<Cycles, &str>
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
impl Cycles
sourcepub fn from_hz(h: c_ll) -> Self
pub fn from_hz(h: c_ll) -> Self
use cpu_cycles_reader::Cycles;
assert_eq!(Cycles::new(1000), Cycles::from_hz(1000));sourcepub fn from_khz(k: c_ll) -> Self
pub fn from_khz(k: c_ll) -> Self
use cpu_cycles_reader::Cycles;
assert_eq!(Cycles::new(1000), Cycles::from_khz(1));sourcepub fn from_mhz(m: c_ll) -> Self
pub fn from_mhz(m: c_ll) -> Self
use cpu_cycles_reader::Cycles;
assert_eq!(Cycles::new(2_000_000), Cycles::from_mhz(2));sourcepub fn from_ghz(g: c_ll) -> Self
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));sourcepub fn as_hz(&self) -> c_ll
pub fn as_hz(&self) -> c_ll
use cpu_cycles_reader::Cycles;
assert_eq!(Cycles::new(1000).as_hz(), 1000);sourcepub fn as_khz(&self) -> c_ll
pub 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 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
This method tests less than or equal to (for
self and other) and is used by the <=
operator. Read moreimpl Copy for Cycles
impl Eq for Cycles
impl StructuralEq for Cycles
impl StructuralPartialEq for Cycles
Auto Trait Implementations§
impl RefUnwindSafe for Cycles
impl Send for Cycles
impl Sync for Cycles
impl Unpin for Cycles
impl UnwindSafe for Cycles
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more