[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:
ddidderr 2023-03-03 14:36:08 +01:00
commit 581a039e91
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14
6 changed files with 91 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cpufreq"
version = "1.0.0"

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "cpufreq"
version = "1.0.0"
edition = "2021"
[profile.release]
lto = true
debug = false
strip = true
panic = "unwind"
codegen-units = 1

20
README.md Normal file
View File

@ -0,0 +1,20 @@
# cpufreq
## About
### Features
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 (cores sorted according to their number in the kernel).
### Usage
`cpufreq [-a]`
### Design goal
The goal was to create
* a minimal Linux program with no dependencies
* that runs on any `x86_64` system without the need for a specific C library version to be present
* and is very fast (takes less than 1ms as of `v1.0`)
## Platform support
This program is linux only.
There are _no_ plans to change that.

3
rustfmt.toml Normal file
View File

@ -0,0 +1,3 @@
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
imports_layout = "HorizontalVertical"

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(),
}
}