From 04f017aeed7ea246005636a8fd1b55c8cb293829 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Fri, 3 Mar 2023 14:36:08 +0100 Subject: [PATCH] [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. --- .gitignore | 1 + Cargo.lock | 7 +++++++ Cargo.toml | 11 +++++++++++ README.md | 20 ++++++++++++++++++++ rustfmt.toml | 3 +++ src/main.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 rustfmt.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..2e70b78 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9a3a7c0 --- /dev/null +++ b/Cargo.toml @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..c336ed5 --- /dev/null +++ b/README.md @@ -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. diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..e270811 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,3 @@ +group_imports = "StdExternalCrate" +imports_granularity = "Crate" +imports_layout = "HorizontalVertical" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..70d5541 --- /dev/null +++ b/src/main.rs @@ -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::().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(), + } +}