use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
#[derive(Debug)]
pub struct Interrupts {
pub cpus: Vec<u32>,
pub rows: Vec<InterruptRow>,
pub counts: Vec<InterruptCount>,
}
#[derive(Debug)]
pub struct InterruptRow {
pub irq: u64,
pub per_cpu: Vec<u64>,
pub info: Box<str>,
}
#[derive(Debug)]
pub struct InterruptCount {
pub name: Box<str>,
pub values: Vec<u64>,
pub description: Box<str>,
}
pub fn interrupts() -> Result<Interrupts> {
let path = Path::new("/proc/interrupts");
let bytes = parse::read_file(path)?;
let mut cpus: Vec<u32> = Vec::new();
let mut rows: Vec<InterruptRow> = Vec::new();
let mut counts: Vec<InterruptCount> = Vec::new();
for (line_num, line) in bytes
.split(|&b| b == b'\n')
.filter(|l| !l.is_empty())
.enumerate()
{
let fields = parse::split_spaces(line);
if fields.is_empty() {
continue;
}
if fields[0].starts_with(b"CPU") {
cpus.clear();
for &field in &fields {
let id = field
.strip_prefix(&b"CPU"[..])
.ok_or_else(|| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid CPU column",
})?;
cpus.push(parse::parse_dec_u32(id).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid CPU id",
})?);
}
continue;
}
let (label, _) = parse::split_at_byte(fields[0], b':');
if label.len() == fields[0].len() {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "expected ':' after label",
});
}
if label.iter().all(|b| b.is_ascii_digit()) {
let irq = parse::parse_dec_u64(label).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid irq number",
})?;
let mut per_cpu = Vec::new();
let mut tail_start = 1usize;
for &field in &fields[1..] {
match parse::parse_dec_u64(field) {
Ok(v) => {
per_cpu.push(v);
tail_start += 1;
}
Err(_) => break,
}
}
if per_cpu.is_empty() {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "expected per-cpu counts",
});
}
let info = join_tail(&fields[tail_start..])?;
rows.push(InterruptRow {
irq,
per_cpu,
info: info.into(),
});
} else {
let name = std::str::from_utf8(label).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid counter name",
})?;
let mut values = Vec::new();
let mut tail_start = 1usize;
for &field in &fields[1..] {
match parse::parse_dec_u64(field) {
Ok(v) => {
values.push(v);
tail_start += 1;
}
Err(_) => break,
}
}
if values.is_empty() {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "expected counter values",
});
}
let description = join_tail(&fields[tail_start..])?;
counts.push(InterruptCount {
name: name.into(),
values,
description: description.into(),
});
}
}
Ok(Interrupts { cpus, rows, counts })
}
fn join_tail(fields: &[&[u8]]) -> Result<String> {
let mut tokens = Vec::with_capacity(fields.len());
for &f in fields {
let s = std::str::from_utf8(f).map_err(|_| Error::Parse {
path: Path::new("/proc/interrupts").to_path_buf(),
line: 0,
msg: "non-UTF8 info field",
})?;
tokens.push(s);
}
Ok(tokens.join(" "))
}