[release] cpufreq v1.0.0

A simple tool to show the current average CPU frequency across all
CPU cores. Can be given the argument `-a` to show frequency for each
individual core.
This commit is contained in:
2023-03-03 14:36:08 +01:00
commit 04f017aeed
6 changed files with 91 additions and 0 deletions

49
src/main.rs Normal file
View File

@ -0,0 +1,49 @@
use std::{env::args, fs};
static LINUX_SYSFS_CPUFREQ_BASE_DIR: &str = "/sys/devices/system/cpu/cpufreq";
fn show_all() {
let mut cpu_num = 0;
loop {
let path = format!("{LINUX_SYSFS_CPUFREQ_BASE_DIR}/policy{cpu_num}/scaling_cur_freq");
if let Ok(scaling_cur_freq) = fs::read_to_string(path) {
println!("{}", scaling_cur_freq.trim().parse::<u64>().unwrap() / 1000);
} else {
break;
}
cpu_num += 1;
}
}
fn show_average() {
let mut sum = 0;
let mut count = 0;
for dir in fs::read_dir(LINUX_SYSFS_CPUFREQ_BASE_DIR).unwrap() {
let mut path = dir.unwrap().path();
path.push("scaling_cur_freq");
if let Ok(scaling_cur_freq) = fs::read_to_string(path) {
let freq: u64 = scaling_cur_freq.trim().parse().unwrap();
sum += freq;
count += 1;
}
}
println!("{}", sum / count / 1000);
}
fn main() {
let all = args()
.nth(1)
.filter(|opt| opt == "-a" || opt == "--all")
.is_some();
match all {
true => show_all(),
false => show_average(),
}
}