use std::path::{Path, PathBuf};
use crate::error::{Error, Result};
use crate::util::parse;
#[derive(Debug)]
pub struct CpuFreqInfo {
pub cpu: u32,
pub current_khz: u32,
pub min_khz: u32,
pub max_khz: u32,
pub governor: Box<str>,
}
pub fn cpu_count() -> Result<u32> {
let cpus = online_cpus()?;
Ok(cpus.len() as u32)
}
pub fn online_cpus() -> Result<Vec<u32>> {
let path = Path::new("/sys/devices/system/cpu/online");
let bytes = parse::read_file(path)?;
let s = std::str::from_utf8(&bytes).unwrap_or("").trim();
if s.is_empty() {
return Ok(vec![0]);
}
let mut cpus = Vec::new();
for segment in s.split(',') {
let segment = segment.trim();
if segment.is_empty() {
continue;
}
if let Some(dash) = segment.find('-') {
let start = segment[..dash].parse::<u32>().map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: 0,
msg: "invalid range start",
})?;
let end = segment[dash + 1..]
.parse::<u32>()
.map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: 0,
msg: "invalid range end",
})?;
for cpu in start..=end {
cpus.push(cpu);
}
} else {
let cpu = segment.parse::<u32>().map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: 0,
msg: "invalid cpu number",
})?;
cpus.push(cpu);
}
}
cpus.sort();
cpus.dedup();
Ok(cpus)
}
pub fn cpu_freq(cpu: u32) -> Result<CpuFreqInfo> {
let base = PathBuf::from(format!("/sys/devices/system/cpu/cpu{}/cpufreq", cpu));
let current_khz = {
let path = base.join("scaling_cur_freq");
match parse::read_file(&path) {
Ok(bytes) => parse::parse_dec_u32(&bytes).unwrap_or(0),
Err(_) => 0,
}
};
let min_khz = {
let path = base.join("scaling_min_freq");
match parse::read_file(&path) {
Ok(bytes) => parse::parse_dec_u32(&bytes).unwrap_or(0),
Err(_) => 0,
}
};
let max_khz = {
let path = base.join("scaling_max_freq");
match parse::read_file(&path) {
Ok(bytes) => parse::parse_dec_u32(&bytes).unwrap_or(0),
Err(_) => 0,
}
};
let governor = {
let path = base.join("scaling_governor");
match parse::read_file(&path) {
Ok(bytes) => std::str::from_utf8(&bytes)
.unwrap_or("")
.trim()
.to_string()
.into_boxed_str(),
Err(_) => Box::from(""),
}
};
Ok(CpuFreqInfo {
cpu,
current_khz,
min_khz,
max_khz,
governor,
})
}