use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
#[derive(Debug)]
pub struct CpuFlag(pub Box<str>);
#[derive(Debug)]
pub struct CpuCore {
pub processor: u32,
pub vendor_id: Box<str>,
pub cpu_family: u32,
pub model: u32,
pub model_name: Box<str>,
pub stepping: u32,
pub microcode: Box<str>,
pub cpu_mhz: f64,
pub cache_size_kb: u32,
pub physical_id: u32,
pub siblings: u32,
pub core_id: u32,
pub cpu_cores: u32,
pub flags: Vec<CpuFlag>,
}
enum CpuFieldValue {
U32(u32),
F64(f64),
Str(Box<str>),
Flags(Vec<CpuFlag>),
None,
}
fn bytes_to_box_str(b: &[u8]) -> Box<str> {
std::str::from_utf8(b)
.unwrap_or("")
.to_string()
.into_boxed_str()
}
fn parse_cpu_flags(value: &[u8]) -> Vec<CpuFlag> {
parse::split_spaces(value)
.into_iter()
.filter(|f| !f.is_empty())
.map(|f| CpuFlag(bytes_to_box_str(f)))
.collect()
}
fn parse_cpu_field(key: &[u8], value: &[u8]) -> CpuFieldValue {
let value = parse::trim_start(value);
match key {
b"processor" => CpuFieldValue::U32(parse::parse_dec_u32(value).unwrap_or(0)),
b"vendor_id" => CpuFieldValue::Str(bytes_to_box_str(value)),
b"cpu family" => CpuFieldValue::U32(parse::parse_dec_u32(value).unwrap_or(0)),
b"model" => CpuFieldValue::U32(parse::parse_dec_u32(value).unwrap_or(0)),
b"model name" => CpuFieldValue::Str(bytes_to_box_str(value)),
b"stepping" => CpuFieldValue::U32(parse::parse_dec_u32(value).unwrap_or(0)),
b"microcode" => CpuFieldValue::Str(bytes_to_box_str(value)),
b"cpu MHz" => CpuFieldValue::F64(parse::parse_dec_f64(value).unwrap_or(0.0)),
b"cache size" => {
let v = parse::trim_end(value);
let num = v.split(|&b| b == b' ').next().unwrap_or(v);
CpuFieldValue::U32(parse::parse_dec_u32(num).unwrap_or(0))
}
b"physical id" => CpuFieldValue::U32(parse::parse_dec_u32(value).unwrap_or(0)),
b"siblings" => CpuFieldValue::U32(parse::parse_dec_u32(value).unwrap_or(0)),
b"core id" => CpuFieldValue::U32(parse::parse_dec_u32(value).unwrap_or(0)),
b"cpu cores" => CpuFieldValue::U32(parse::parse_dec_u32(value).unwrap_or(0)),
b"flags" => CpuFieldValue::Flags(parse_cpu_flags(value)),
_ => CpuFieldValue::None,
}
}
fn build_cpu_core(fields: &[(Box<str>, Box<str>)]) -> CpuCore {
let mut processor = 0u32;
let mut vendor_id = Box::from("");
let mut cpu_family = 0u32;
let mut model = 0u32;
let mut model_name = Box::from("");
let mut stepping = 0u32;
let mut microcode = Box::from("");
let mut cpu_mhz = 0.0f64;
let mut cache_size_kb = 0u32;
let mut physical_id = 0u32;
let mut siblings = 0u32;
let mut core_id = 0u32;
let mut cpu_cores = 0u32;
let mut flags = Vec::new();
for (key, value) in fields {
let key_bytes = key.as_bytes();
let value_bytes = value.as_bytes();
match parse_cpu_field(key_bytes, value_bytes) {
CpuFieldValue::U32(v) => match key.as_ref() {
"processor" => processor = v,
"cpu family" => cpu_family = v,
"model" => model = v,
"stepping" => stepping = v,
"cache size" => cache_size_kb = v,
"physical id" => physical_id = v,
"siblings" => siblings = v,
"core id" => core_id = v,
"cpu cores" => cpu_cores = v,
_ => {}
},
CpuFieldValue::F64(v) => {
if key.as_ref() == "cpu MHz" {
cpu_mhz = v;
}
}
CpuFieldValue::Str(v) => match key.as_ref() {
"vendor_id" => vendor_id = v,
"model name" => model_name = v,
"microcode" => microcode = v,
_ => {}
},
CpuFieldValue::Flags(v) => flags = v,
CpuFieldValue::None => {}
}
}
CpuCore {
processor,
vendor_id,
cpu_family,
model,
model_name,
stepping,
microcode,
cpu_mhz,
cache_size_kb,
physical_id,
siblings,
core_id,
cpu_cores,
flags,
}
}
pub fn cpuinfo() -> Result<Vec<CpuCore>> {
let path = Path::new("/proc/cpuinfo");
let bytes = parse::read_file(path)?;
let mut cores = Vec::new();
let mut current_fields: Vec<(Box<str>, Box<str>)> = Vec::new();
for (line_num, line) in bytes.split(|&b| b == b'\n').enumerate() {
if line.is_empty() {
if !current_fields.is_empty() {
cores.push(build_cpu_core(¤t_fields));
current_fields.clear();
}
continue;
}
let (key, value) = parse::parse_key_value_line(line).ok_or_else(|| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid key-value line",
})?;
let key_str = std::str::from_utf8(key)
.map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid utf8 in key",
})?
.to_string()
.into_boxed_str();
let value_str = std::str::from_utf8(value)
.map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid utf8 in value",
})?
.to_string()
.into_boxed_str();
current_fields.push((key_str, value_str));
}
if !current_fields.is_empty() {
cores.push(build_cpu_core(¤t_fields));
}
if cores.is_empty() {
return Err(Error::Parse {
path: path.to_path_buf(),
line: 0,
msg: "no CPU cores found",
});
}
Ok(cores)
}