use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::time::Duration;
use super::metrics::Paths;
pub const WINDOW: Duration = Duration::from_mins(10);
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Device {
major: u32,
minor: u32,
name: String,
}
const LOOP_DEPTH: u8 = 4;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Disks {
charged: Vec<Device>,
drive: Vec<Device>,
btrfs: Option<String>,
}
impl Disks {
pub fn resolve(paths: &Paths, root: &Path) -> Option<Disks> {
let mountinfo = std::fs::read_to_string(paths.proc.join("self/mountinfo")).ok()?;
Disks::under(paths, &mountinfo, root, LOOP_DEPTH)
}
fn under(paths: &Paths, mountinfo: &str, path: &Path, depth: u8) -> Option<Disks> {
let held = Held::find(paths, mountinfo, path)?;
let mut btrfs = held.btrfs;
let mut charged = BTreeSet::new();
let mut drive = BTreeSet::new();
for disk in held.disks {
let device = Device::read(&disk)?;
if let Some(backing) = backing_file(&disk) {
let under = Disks::under(paths, mountinfo, &backing, depth.checked_sub(1)?)?;
drive.extend(under.drive);
btrfs = under.btrfs;
} else {
drive.insert(device.clone());
}
charged.insert(device);
}
(!charged.is_empty()).then(|| Disks {
charged: charged.into_iter().collect(),
drive: drive.into_iter().collect(),
btrfs,
})
}
pub fn store(&self) -> Option<String> {
(self.charged != self.drive).then(|| join(&self.charged))
}
pub fn name(&self) -> String {
join(&self.drive)
}
pub fn written(&self, cgroup: &Path) -> Option<Written> {
let stat = std::fs::read_to_string(cgroup.join("io.stat")).ok()?;
let mut written = Written::default();
for line in stat.lines() {
let mut fields = line.split_whitespace();
let Some((major, minor)) = fields.next().and_then(|device| device.split_once(':'))
else {
continue;
};
if !self.charged.iter().any(|device| {
major.parse() == Ok(device.major) && minor.parse() == Ok(device.minor)
}) {
continue;
}
for field in fields {
let Some((key, value)) = field.split_once('=') else {
continue;
};
let Ok(value) = value.parse::<u64>() else {
return None;
};
match key {
"wbytes" => written.bytes = written.bytes.saturating_add(value),
"wios" => written.ios = written.ios.saturating_add(value),
_ => {}
}
}
}
Some(written)
}
pub fn read(&self, paths: &Paths, slice: &Path, at: u64) -> Reading {
let mut drive = Some(0u64);
for device in &self.drive {
let sectors = std::fs::read_to_string(
paths
.sys
.join("class/block")
.join(&device.name)
.join("stat"),
)
.ok()
.and_then(|stat| stat.split_whitespace().nth(6)?.parse::<u64>().ok());
drive = drive
.zip(sectors)
.map(|(sum, sectors)| sum.saturating_add(sectors.saturating_mul(512)));
}
let commits = self.btrfs.as_ref().and_then(|uuid| {
let stats =
std::fs::read_to_string(paths.sys.join("fs/btrfs").join(uuid).join("commit_stats"))
.ok()?;
stats
.lines()
.find_map(|line| line.strip_prefix("commits ")?.trim().parse().ok())
});
let daemon = std::fs::read_to_string(paths.proc.join("self/io"))
.ok()
.and_then(|io| {
io.lines()
.find_map(|line| line.strip_prefix("write_bytes:")?.trim().parse().ok())
});
Reading {
at,
drive,
slice: self.written(slice).map(|written| written.bytes),
daemon,
commits,
device: self.name(),
store: self.store(),
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Written {
pub bytes: u64,
pub ios: u64,
}
struct Mount {
device: String,
point: PathBuf,
kind: String,
source: String,
}
impl Mount {
fn holding(mountinfo: &str, path: &Path) -> Option<Mount> {
mountinfo
.lines()
.filter_map(Mount::parse)
.filter(|mount| path.starts_with(&mount.point))
.max_by_key(|mount| mount.point.components().count())
}
fn parse(line: &str) -> Option<Mount> {
let (front, back) = line.split_once(" - ")?;
let mut front = front.split(' ');
let device = front.nth(2)?.to_owned();
let point = PathBuf::from(unescape(front.nth(1)?));
let mut back = back.split(' ');
Some(Mount {
device,
point,
kind: back.next()?.to_owned(),
source: unescape(back.next()?),
})
}
}
fn unescape(field: &str) -> String {
let bytes = field.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut at = 0;
while at < bytes.len() {
let code = bytes
.get(at + 1..at + 4)
.filter(|_| bytes[at] == b'\\')
.and_then(|digits| u8::from_str_radix(std::str::from_utf8(digits).ok()?, 8).ok());
if let Some(code) = code {
out.push(code);
at += 4;
} else {
out.push(bytes[at]);
at += 1;
}
}
String::from_utf8_lossy(&out).into_owned()
}
struct Held {
btrfs: Option<String>,
disks: Vec<PathBuf>,
}
impl Held {
fn find(paths: &Paths, mountinfo: &str, path: &Path) -> Option<Held> {
let mount = Mount::holding(mountinfo, path)?;
let class = paths.sys.join("class/block");
let btrfs = (mount.kind == "btrfs")
.then(|| btrfs_uuid(paths, &mount.source))
.flatten();
let names: Vec<String> = if let Some(uuid) = &btrfs {
std::fs::read_dir(paths.sys.join("fs/btrfs").join(uuid).join("devices"))
.ok()?
.flatten()
.filter_map(|entry| entry.file_name().into_string().ok())
.collect()
} else {
let link = paths.sys.join("dev/block").join(&mount.device);
vec![file_name(&std::fs::canonicalize(link).ok()?)?]
};
let mut disks = Vec::new();
for name in names {
let dir = class.join(&name);
disks.push(if dir.join("partition").is_file() {
std::fs::canonicalize(&dir).ok()?.parent()?.to_path_buf()
} else {
std::fs::canonicalize(&dir).ok()?
});
}
Some(Held { btrfs, disks })
}
}
fn backing_file(disk: &Path) -> Option<PathBuf> {
let backing = std::fs::read_to_string(disk.join("loop/backing_file")).ok()?;
let backing = backing.trim_end();
Some(PathBuf::from(
backing.strip_suffix(" (deleted)").unwrap_or(backing),
))
}
pub fn free_space(
paths: &Paths,
root: &Path,
free: impl Fn(&Path) -> std::io::Result<u64>,
) -> std::io::Result<u64> {
let own = free(root)?;
let under = std::fs::read_to_string(paths.proc.join("self/mountinfo"))
.ok()
.and_then(|mountinfo| free_under(paths, &mountinfo, root, &free, LOOP_DEPTH));
Ok(under.map_or(own, |under| own.min(under)))
}
fn free_under(
paths: &Paths,
mountinfo: &str,
path: &Path,
free: &impl Fn(&Path) -> std::io::Result<u64>,
depth: u8,
) -> Option<u64> {
let depth = depth.checked_sub(1)?;
let held = Held::find(paths, mountinfo, path)?;
held.disks
.iter()
.filter_map(|disk| backing_file(disk))
.filter_map(|backing| {
let point = Mount::holding(mountinfo, &backing)?.point;
let own = free(&point).ok();
let under = free_under(paths, mountinfo, &backing, free, depth);
own.into_iter().chain(under).min()
})
.min()
}
impl Device {
fn read(dir: &Path) -> Option<Device> {
let number = std::fs::read_to_string(dir.join("dev")).ok()?;
let (major, minor) = number.trim().split_once(':')?;
Some(Device {
major: major.parse().ok()?,
minor: minor.parse().ok()?,
name: file_name(dir)?,
})
}
}
fn join(devices: &[Device]) -> String {
devices
.iter()
.map(|device| device.name.as_str())
.collect::<Vec<_>>()
.join("+")
}
fn file_name(path: &Path) -> Option<String> {
path.file_name()?.to_str().map(str::to_owned)
}
fn btrfs_uuid(paths: &Paths, source: &str) -> Option<String> {
let source = Path::new(source);
let name = std::fs::canonicalize(source)
.ok()
.and_then(|path| file_name(&path))
.or_else(|| file_name(source))?;
std::fs::read_dir(paths.sys.join("fs/btrfs"))
.ok()?
.flatten()
.find(|entry| entry.path().join("devices").join(&name).exists())
.and_then(|entry| entry.file_name().into_string().ok())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Reading {
at: u64,
drive: Option<u64>,
slice: Option<u64>,
daemon: Option<u64>,
commits: Option<u64>,
device: String,
store: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Window {
pub start: u64,
pub end: u64,
pub device: String,
pub drive: Option<u64>,
pub slice: Option<u64>,
pub daemon: Option<u64>,
pub commits: Option<u64>,
pub store: Option<String>,
}
#[derive(Debug)]
struct Open {
first: u64,
last: Reading,
drive: Option<u64>,
slice: Option<u64>,
daemon: Option<u64>,
commits: Option<u64>,
}
impl Open {
fn new(reading: Reading) -> Open {
Open {
first: reading.at,
drive: Some(0),
slice: Some(0),
daemon: Some(0),
commits: reading.commits.map(|_| 0),
last: reading,
}
}
fn add(&mut self, reading: Reading) {
let rose = |sum: Option<u64>, before: Option<u64>, now: Option<u64>| {
let (sum, before, now) = (sum?, before?, now?);
Some(sum.saturating_add(now.saturating_sub(before)))
};
self.drive = rose(self.drive, self.last.drive, reading.drive);
self.slice = rose(self.slice, self.last.slice, reading.slice);
self.daemon = rose(self.daemon, self.last.daemon, reading.daemon);
self.commits = rose(self.commits, self.last.commits, reading.commits);
self.last = reading;
}
fn close(&self) -> Window {
Window {
start: self.first,
end: self.last.at,
device: self.last.device.clone(),
store: self.last.store.clone(),
drive: self.drive,
slice: self.slice,
daemon: self.daemon,
commits: self.commits,
}
}
}
#[derive(Debug, Default)]
pub struct Windows {
open: Option<Open>,
}
impl Windows {
pub fn add(&mut self, reading: Reading) -> Option<Window> {
let Some(open) = self.open.as_mut() else {
self.open = Some(Open::new(reading));
return None;
};
if reading.device != open.last.device || reading.store != open.last.store {
let closed = open.close();
self.open = Some(Open::new(reading));
return Some(closed).filter(|window| window.end > window.start);
}
open.add(reading);
let span = u64::try_from(WINDOW.as_millis()).unwrap_or(u64::MAX);
if open.last.at.saturating_sub(open.first) < span {
return None;
}
let closed = open.close();
let last = open.last.clone();
self.open = Some(Open::new(last));
Some(closed)
}
pub fn close(&mut self) -> Option<Window> {
self.open
.take()
.map(|open| open.close())
.filter(|window| window.end > window.start)
}
}