#[must_use]
pub fn is_cgroup_available() -> bool {
#[cfg(target_os = "linux")]
{
std::path::Path::new("/sys/fs/cgroup/cpu").exists()
|| std::path::Path::new("/sys/fs/cgroup/unified").exists()
}
#[cfg(not(target_os = "linux"))]
{
false
}
}
#[must_use]
pub fn get_cpu_info() -> String {
#[cfg(target_os = "linux")]
{
if let Ok(content) = std::fs::read_to_string("/proc/cpuinfo") {
for line in content.lines() {
if line.starts_with("model name")
&& let Some(name) = line.split(':').nth(1)
{
return name.trim().to_string();
}
}
}
}
"Unknown CPU".to_string()
}
#[must_use]
pub fn cpu_count() -> usize {
std::thread::available_parallelism()
.map(std::num::NonZero::get)
.unwrap_or(1)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_cgroup_available_returns_bool() {
let _ = is_cgroup_available();
}
#[test]
fn test_get_cpu_info_non_empty() {
let info = get_cpu_info();
assert!(!info.is_empty());
}
#[test]
fn test_cpu_count_positive() {
assert!(cpu_count() >= 1);
}
#[cfg(target_os = "linux")]
#[test]
fn test_cgroup_on_linux() {
let _ = is_cgroup_available();
}
}