use std::fmt::Write as _;
use serde::{Deserialize, Serialize};
use super::{Tone, duration};
use crate::config::ByteSize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiskWritten {
pub bytes: u64,
pub ios: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub dirty: Option<u64>,
pub measure: Measure,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Measure {
Complete,
Partial,
#[serde(other)]
Unknown,
}
impl DiskWritten {
pub fn short(&self) -> String {
match self.measure {
Measure::Complete => ByteSize(self.bytes).to_string(),
Measure::Partial | Measure::Unknown => format!("≥{}", ByteSize(self.bytes)),
}
}
pub fn line(&self) -> String {
let writes = if self.ios == 1 { "write" } else { "writes" };
let mut line = match self.measure {
Measure::Complete => format!("{} in {} {writes}", ByteSize(self.bytes), self.ios),
Measure::Partial | Measure::Unknown => format!(
"at least {} in {} {writes}: part of its run was not read",
ByteSize(self.bytes),
self.ios
),
};
if let Some(dirty) = self.dirty.filter(|dirty| *dirty > 0) {
let _ = write!(
line,
"; {} more waited to be written back as its container ended",
ByteSize(dirty)
);
}
line
}
pub fn tone(&self) -> Tone {
match self.measure {
Measure::Complete => Tone::Neutral,
Measure::Partial | Measure::Unknown => Tone::Quiet,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiskWrites {
pub jobs: JobWrites,
pub workflows: Vec<WriteTop>,
pub top_jobs: Vec<WriteTop>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub drive: Option<DriveWrites>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct JobWrites {
pub measured: u32,
pub partial: u32,
pub total: u64,
pub median: u64,
pub p90: u64,
pub dirty: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WriteTop {
pub repository: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub workflow: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub job: Option<String>,
pub jobs: u32,
pub total: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DriveWrites {
pub device: String,
pub watched: u64,
pub written: u64,
pub shares: DriveShares,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub commits: Option<Commits>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub store: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct DriveShares {
pub watched: u64,
pub written: u64,
pub jobs: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub slice: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub daemon: Option<u64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Commits {
pub count: u64,
pub watched: u64,
}
fn percent(part: u64, whole: u64) -> u64 {
if whole == 0 {
return 0;
}
let share = (u128::from(part) * 100 + u128::from(whole) / 2) / u128::from(whole);
u64::try_from(share.min(100)).unwrap_or(100)
}
impl JobWrites {
pub fn line(&self) -> String {
let jobs = if self.measured == 1 { "job" } else { "jobs" };
let mut line = format!(
"{} {jobs} wrote {}: median {}, p90 {} a job",
self.measured,
ByteSize(self.total),
ByteSize(self.median),
ByteSize(self.p90)
);
if self.partial > 0 {
let _ = write!(line, "; {} partial, counted as far as read", self.partial);
}
if self.dirty > 0 {
let _ = write!(
line,
"; {} more waited to be written back as their containers ended",
ByteSize(self.dirty)
);
}
line
}
}
impl WriteTop {
pub fn line(&self) -> String {
let mut name = self.repository.clone();
if let Some(workflow) = &self.workflow {
name.push(' ');
name.push_str(workflow);
}
if let Some(job) = &self.job {
name.push_str(if self.workflow.is_some() { " / " } else { " " });
name.push_str(job);
}
let jobs = if self.jobs == 1 { "job" } else { "jobs" };
format!(
"{name}: {} over {} {jobs}, {} a job",
ByteSize(self.total),
self.jobs,
ByteSize(self.total / u64::from(self.jobs.max(1)))
)
}
}
impl DriveWrites {
pub fn per_day(&self) -> u64 {
let day = u128::from(self.written) * 86_400 / u128::from(self.watched.max(1));
u64::try_from(day).unwrap_or(u64::MAX)
}
pub fn lines(&self) -> Vec<String> {
let mut lines = vec![format!(
"{} wrote {} over the {} runnerd read it: {} a day at this rate",
self.device,
ByteSize(self.written),
duration(self.watched),
ByteSize(self.per_day())
)];
if let Some(store) = &self.store {
lines.push(format!(
"Docker's root is a file system in a file on it ({store}): the jobs' and the runner slice's figures \
are bytes written to that file system; {} also wrote its journal and the overhead of the file \
system under it",
self.device
));
}
lines.push(self.shares.line(self.watched));
if let Some(commits) = self.commits {
let tenths = u128::from(commits.count) * 10 / u128::from(commits.watched.max(1));
lines.push(format!(
"btrfs committed {}.{} times a second",
tenths / 10,
tenths % 10
));
}
lines
}
}
impl DriveShares {
pub fn line(&self, watched: u64) -> String {
let whole = self.written;
let counted = self.jobs.min(whole);
let mut line = if self.watched < watched {
format!(
"Over the {} runnerd read the runner slice: runner jobs",
duration(self.watched)
)
} else {
"Runner jobs".to_owned()
};
let _ = write!(line, " {}%", percent(counted, whole));
let owned = match self.slice {
Some(slice) => {
let slice = slice.clamp(counted, whole);
let _ = write!(
line,
"; the rest of the runner slice {}% (seeds, the mirror, runners before and after their jobs, \
write-back after a container ended)",
percent(slice - counted, whole)
);
slice
}
None => counted,
};
let daemon = self.daemon.map(|daemon| daemon.min(whole - owned));
if let Some(daemon) = daemon {
let _ = write!(line, "; runnerd itself {}%", percent(daemon, whole));
}
let _ = write!(
line,
"; no owner runnerd knows {}%",
percent(whole - owned - daemon.unwrap_or(0), whole)
);
if self.slice.is_none() {
line.push_str(", the runner slice's share unread");
}
line
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WriteSection {
pub title: &'static str,
pub lines: Vec<String>,
}
impl DiskWrites {
pub const TITLE: &'static str = "Disk writes";
pub fn sections(&self) -> Vec<WriteSection> {
let mut sections = Vec::new();
if self.jobs.measured > 0 {
sections.push(WriteSection {
title: "Jobs",
lines: vec![self.jobs.line()],
});
}
if let Some(drive) = &self.drive {
sections.push(WriteSection {
title: "Drive",
lines: drive.lines(),
});
}
for (title, top) in [
("Workflows that wrote most", &self.workflows),
("Jobs that wrote most", &self.top_jobs),
] {
if !top.is_empty() {
sections.push(WriteSection {
title,
lines: top.iter().map(WriteTop::line).collect(),
});
}
}
sections
}
}
#[cfg(test)]
mod tests {
use super::*;
const M: u64 = 1 << 20;
const G: u64 = 1 << 30;
#[test]
fn a_partial_figure_reads_as_a_floor_and_says_why() {
let mut written = DiskWritten {
bytes: 640 * M,
ios: 1,
dirty: Some(0),
measure: Measure::Complete,
};
assert_eq!(written.short(), "640M");
assert_eq!(written.line(), "640M in 1 write");
written.measure = Measure::Partial;
written.dirty = Some(96 * M);
assert_eq!(written.short(), "≥640M");
assert_eq!(written.tone(), Tone::Quiet, "a floor is in the background");
assert_eq!(
written.line(),
"at least 640M in 1 write: part of its run was not read; 96M more waited to be written back as its \
container ended"
);
}
#[test]
fn the_drive_s_lines_share_its_bytes_out_without_passing_the_whole() {
let shares = DriveShares {
watched: 6 * 3600,
written: 600 * G,
jobs: 150 * G,
slice: Some(200 * G),
daemon: Some(6 * G),
};
let drive = DriveWrites {
device: "nvme0n1".to_owned(),
watched: 6 * 3600,
written: 600 * G,
shares,
commits: Some(Commits {
count: 43_200,
watched: 6 * 3600,
}),
store: None,
};
assert_eq!(
drive.lines(),
[
"nvme0n1 wrote 600G over the 6h 00m runnerd read it: 2.34T a day at this rate",
"Runner jobs 25%; the rest of the runner slice 8% (seeds, the mirror, runners before and after \
their jobs, write-back after a container ended); runnerd itself 1%; no owner runnerd knows 66%",
"btrfs committed 2.0 times a second",
]
);
let over = DriveWrites {
shares: DriveShares {
slice: Some(100 * G),
jobs: 700 * G,
..shares
},
commits: None,
..drive.clone()
};
assert_eq!(
over.lines()[1],
"Runner jobs 100%; the rest of the runner slice 0% (seeds, the mirror, runners before and after their \
jobs, write-back after a container ended); runnerd itself 0%; no owner runnerd knows 0%"
);
assert_eq!(over.lines().len(), 2, "no commits line off btrfs");
let in_a_file = DriveWrites {
store: Some("loop0".to_owned()),
..over.clone()
};
assert_eq!(
in_a_file.lines()[1],
"Docker's root is a file system in a file on it (loop0): the jobs' and the runner slice's figures are \
bytes written to that file system; nvme0n1 also wrote its journal and the overhead of the file system \
under it"
);
let part = DriveWrites {
shares: DriveShares {
watched: 3600,
written: 100 * G,
jobs: 50 * G,
slice: Some(60 * G),
daemon: None,
},
..drive
};
assert_eq!(
part.lines()[1],
"Over the 1h 00m runnerd read the runner slice: runner jobs 50%; the rest of the runner slice 10% (seeds, \
the mirror, runners before and after their jobs, write-back after a container ended); no owner runnerd \
knows 40%"
);
}
}