Struct cpu_cycles_reader::Cycles
source · pub struct Cycles { /* private fields */ }Implementations§
source§impl Cycles
impl Cycles
sourcepub fn as_usage(&self, d: Duration, c: u64) -> Result<f64, Box<dyn Error>>
pub fn as_usage(&self, d: Duration, c: u64) -> 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
c: cpu core number
Read /sys/devices/system/cpu/cpuX/cpufreq/scaling_cur_freq to get the current frequency
ⓘ
use std::time::{Duration, Instant};
use cpu_cycles_reader::Cycles;
let now = Instant::now();
let cycles_former = Cycles::from_ghz(1);
// cpu进行了一些操作,假设我们记录的是cpu7
let dur = Instant::now() - now;
let cycles_later = Cycles::from_ghz(2);
let cycles = cycles_later - cycles_former;
println!("{:.2}", cycles.as_usage(dur, 7).unwrap());sourcepub fn as_diff(&self, d: Duration, c: c_int) -> Option<Cycles>
pub fn as_diff(&self, d: Duration, c: c_int) -> Option<Cycles>
Similar to as_usage, but returns the difference from the current frequency self::Cycles
For the same reason, diff may be negative
d: record the time of cycles
c: cpu core number
读取/sys/devices/system/cpu/cpuX/cpufreq/scaling_cur_freq来获取当前频率
ⓘ
use std::time::{Duration, Instant};
use cpu_cycles_reader::Cycles;
let now = Instant::now();
let cycles_former = Cycles::from_ghz(1);
// The cpu has performed some operations, assuming we are recording the cpu7
let dur = Instant::now() - now;
let cycles_later = Cycles::from_ghz(2);
let cycles = cycles_later - cycles_former;
println!("{}", cycles.as_diff(dur, 7).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