Expand description

Only for reading CpuCycles specialization, not a complete package of perf_event_read

⚠ Permission requirements: Make sure the program has root permissions

Example:

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

let reader = CyclesReader::new(&[0, 1, 2, 3, 4, 5, 6, 7]).unwrap();
reader.enable();

let now = Instant::now();
let cycles_former = reader.read().unwrap();
let cycles_former = cycles_former.get(&7).unwrap(); // get cycles

// The cpu has performed some operations, here we record cpu7

let dur = Instant::now() - now;
let cycles_later = reader.read().unwrap();
let cycles_later = cycles_later.get(&7).unwrap(); // get cycles

let cycles = *cycles_later - *cycles_former; // Calculate difference
// NOTE: There is no need to calculate the difference as a value within 1 second, there is such logic inside Cycles::as_usage() or Cycles::as_diff()

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 usage = cycles.as_usage(dur, freq_cycles).unwrap();
println!("{:.2}", usage);

Modules

  • Base bindings to c code

Structs