use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
#[derive(Debug)]
pub struct PageTypeInfo {
pub page_block_order: u64,
pub pages_per_block: u64,
pub free: Vec<PageTypeFree>,
pub blocks: Vec<PageTypeBlock>,
}
#[derive(Debug)]
pub struct PageTypeFree {
pub node: u32,
pub zone: Box<str>,
pub migrate_type: Box<str>,
pub orders: Vec<u64>,
}
#[derive(Debug)]
pub struct PageTypeBlock {
pub node: u32,
pub zone: Box<str>,
pub types: Vec<u64>,
}
pub fn pagetypeinfo() -> Result<PageTypeInfo> {
let path = Path::new("/proc/pagetypeinfo");
let bytes = parse::read_file(path)?;
let mut free: Vec<PageTypeFree> = Vec::new();
let mut blocks: Vec<PageTypeBlock> = Vec::new();
let mut page_block_order = 0u64;
let mut pages_per_block = 0u64;
enum Section {
Free,
Blocks,
}
let mut section = None;
for (line_num, line) in bytes
.split(|&b| b == b'\n')
.filter(|l| !l.is_empty())
.enumerate()
{
if let Some((key, value)) = parse::parse_key_value_line(line) {
match key {
b"Page block order" => {
page_block_order =
parse::parse_dec_u64(parse::trim_start(value)).map_err(|_| {
Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid page block order",
}
})?;
section = None;
continue;
}
b"Pages per block" => {
pages_per_block =
parse::parse_dec_u64(parse::trim_start(value)).map_err(|_| {
Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid pages per block",
}
})?;
}
_ => {}
}
}
if line.starts_with(b"Free pages count per migrate type at order") {
section = Some(Section::Free);
continue;
}
if line.starts_with(b"Number of blocks type") {
section = Some(Section::Blocks);
continue;
}
if line.starts_with(b"Number of mixed blocks") {
section = None;
continue;
}
if !line.starts_with(b"Node ") {
continue;
}
let fields = parse::SplitFields::<32>::new(line);
match section {
Some(Section::Free) => {
if fields.len() < 7 {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "expected 'Node <id>, zone <name>, type <name> <counts>'",
});
}
let (node, _) = parse::split_at_byte(fields[1], b',');
let node = parse::parse_dec_u32(node).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid node",
})?;
let (zone, _) = parse::split_at_byte(fields[3], b',');
let zone = std::str::from_utf8(zone).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid zone",
})?;
let migrate_type = std::str::from_utf8(fields[5]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid migrate type",
})?;
let mut orders = Vec::with_capacity(fields.len() - 6);
for field in &fields[6..] {
let field = if field.first() == Some(&b'>') {
&field[1..]
} else {
field
};
orders.push(parse::parse_dec_u64(field).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid order count",
})?);
}
free.push(PageTypeFree {
node,
zone: zone.into(),
migrate_type: migrate_type.into(),
orders,
});
}
Some(Section::Blocks) => {
if fields.len() < 5 {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "expected 'Node <id>, zone <name> <counts>'",
});
}
let (node, _) = parse::split_at_byte(fields[1], b',');
let node = parse::parse_dec_u32(node).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid node",
})?;
let (zone, _) = parse::split_at_byte(fields[3], b',');
let zone = std::str::from_utf8(zone).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid zone",
})?;
let mut types = Vec::with_capacity(fields.len() - 4);
for field in &fields[4..] {
types.push(parse::parse_dec_u64(field).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid block count",
})?);
}
blocks.push(PageTypeBlock {
node,
zone: zone.into(),
types,
});
}
None => {}
}
}
Ok(PageTypeInfo {
page_block_order,
pages_per_block,
free,
blocks,
})
}