1use std::fmt::Write as _;
6
7use serde::{Deserialize, Serialize};
8
9use super::{Tone, duration};
10use crate::config::ByteSize;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
16pub struct DiskWritten {
17 pub bytes: u64,
19 pub ios: u64,
21 #[serde(default, skip_serializing_if = "Option::is_none")]
24 pub dirty: Option<u64>,
25 pub measure: Measure,
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
31#[serde(rename_all = "kebab-case")]
32pub enum Measure {
33 Complete,
35 Partial,
39 #[serde(other)]
41 Unknown,
42}
43
44impl DiskWritten {
45 pub fn short(&self) -> String {
47 match self.measure {
48 Measure::Complete => ByteSize(self.bytes).to_string(),
49 Measure::Partial | Measure::Unknown => format!("≥{}", ByteSize(self.bytes)),
50 }
51 }
52
53 pub fn line(&self) -> String {
56 let writes = if self.ios == 1 { "write" } else { "writes" };
57 let mut line = match self.measure {
58 Measure::Complete => format!("{} in {} {writes}", ByteSize(self.bytes), self.ios),
59 Measure::Partial | Measure::Unknown => format!(
60 "at least {} in {} {writes}: part of its run was not read",
61 ByteSize(self.bytes),
62 self.ios
63 ),
64 };
65 if let Some(dirty) = self.dirty.filter(|dirty| *dirty > 0) {
66 let _ = write!(
67 line,
68 "; {} more waited to be written back as its container ended",
69 ByteSize(dirty)
70 );
71 }
72 line
73 }
74
75 pub fn tone(&self) -> Tone {
77 match self.measure {
78 Measure::Complete => Tone::Neutral,
79 Measure::Partial | Measure::Unknown => Tone::Quiet,
80 }
81 }
82}
83
84#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
87pub struct DiskWrites {
88 pub jobs: JobWrites,
90 pub workflows: Vec<WriteTop>,
92 pub top_jobs: Vec<WriteTop>,
94 #[serde(default, skip_serializing_if = "Option::is_none")]
96 pub drive: Option<DriveWrites>,
97}
98
99#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
101pub struct JobWrites {
102 pub measured: u32,
104 pub partial: u32,
106 pub total: u64,
108 pub median: u64,
110 pub p90: u64,
112 pub dirty: u64,
114}
115
116#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
118pub struct WriteTop {
119 pub repository: String,
121 #[serde(default, skip_serializing_if = "Option::is_none")]
123 pub workflow: Option<String>,
124 #[serde(default, skip_serializing_if = "Option::is_none")]
126 pub job: Option<String>,
127 pub jobs: u32,
129 pub total: u64,
131}
132
133#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
137pub struct DriveWrites {
138 pub device: String,
140 pub watched: u64,
142 pub written: u64,
144 pub shares: DriveShares,
146 #[serde(default, skip_serializing_if = "Option::is_none")]
148 pub commits: Option<Commits>,
149 #[serde(default, skip_serializing_if = "Option::is_none")]
153 pub store: Option<String>,
154}
155
156#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
159pub struct DriveShares {
160 pub watched: u64,
162 pub written: u64,
164 pub jobs: u64,
166 #[serde(default, skip_serializing_if = "Option::is_none")]
168 pub slice: Option<u64>,
169 #[serde(default, skip_serializing_if = "Option::is_none")]
171 pub daemon: Option<u64>,
172}
173
174#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
176pub struct Commits {
177 pub count: u64,
179 pub watched: u64,
181}
182
183fn percent(part: u64, whole: u64) -> u64 {
185 if whole == 0 {
186 return 0;
187 }
188 let share = (u128::from(part) * 100 + u128::from(whole) / 2) / u128::from(whole);
189 u64::try_from(share.min(100)).unwrap_or(100)
190}
191
192impl JobWrites {
193 pub fn line(&self) -> String {
196 let jobs = if self.measured == 1 { "job" } else { "jobs" };
197 let mut line = format!(
198 "{} {jobs} wrote {}: median {}, p90 {} a job",
199 self.measured,
200 ByteSize(self.total),
201 ByteSize(self.median),
202 ByteSize(self.p90)
203 );
204 if self.partial > 0 {
205 let _ = write!(line, "; {} partial, counted as far as read", self.partial);
206 }
207 if self.dirty > 0 {
208 let _ = write!(
209 line,
210 "; {} more waited to be written back as their containers ended",
211 ByteSize(self.dirty)
212 );
213 }
214 line
215 }
216}
217
218impl WriteTop {
219 pub fn line(&self) -> String {
221 let mut name = self.repository.clone();
222 if let Some(workflow) = &self.workflow {
223 name.push(' ');
224 name.push_str(workflow);
225 }
226 if let Some(job) = &self.job {
227 name.push_str(if self.workflow.is_some() { " / " } else { " " });
228 name.push_str(job);
229 }
230 let jobs = if self.jobs == 1 { "job" } else { "jobs" };
231 format!(
232 "{name}: {} over {} {jobs}, {} a job",
233 ByteSize(self.total),
234 self.jobs,
235 ByteSize(self.total / u64::from(self.jobs.max(1)))
236 )
237 }
238}
239
240impl DriveWrites {
241 pub fn per_day(&self) -> u64 {
243 let day = u128::from(self.written) * 86_400 / u128::from(self.watched.max(1));
244 u64::try_from(day).unwrap_or(u64::MAX)
245 }
246
247 pub fn lines(&self) -> Vec<String> {
251 let mut lines = vec![format!(
252 "{} wrote {} over the {} runnerd read it: {} a day at this rate",
253 self.device,
254 ByteSize(self.written),
255 duration(self.watched),
256 ByteSize(self.per_day())
257 )];
258 if let Some(store) = &self.store {
259 lines.push(format!(
260 "Docker's root is a file system in a file on it ({store}): the jobs' and the runner slice's figures \
261 are bytes written to that file system; {} also wrote its journal and the overhead of the file \
262 system under it",
263 self.device
264 ));
265 }
266 lines.push(self.shares.line(self.watched));
267 if let Some(commits) = self.commits {
268 let tenths = u128::from(commits.count) * 10 / u128::from(commits.watched.max(1));
269 lines.push(format!(
270 "btrfs committed {}.{} times a second",
271 tenths / 10,
272 tenths % 10
273 ));
274 }
275 lines
276 }
277}
278
279impl DriveShares {
280 pub fn line(&self, watched: u64) -> String {
284 let whole = self.written;
285 let counted = self.jobs.min(whole);
287 let mut line = if self.watched < watched {
288 format!(
289 "Over the {} runnerd read the runner slice: runner jobs",
290 duration(self.watched)
291 )
292 } else {
293 "Runner jobs".to_owned()
294 };
295 let _ = write!(line, " {}%", percent(counted, whole));
296 let owned = match self.slice {
297 Some(slice) => {
298 let slice = slice.clamp(counted, whole);
299 let _ = write!(
300 line,
301 "; the rest of the runner slice {}% (seeds, the mirror, runners before and after their jobs, \
302 write-back after a container ended)",
303 percent(slice - counted, whole)
304 );
305 slice
306 }
307 None => counted,
308 };
309 let daemon = self.daemon.map(|daemon| daemon.min(whole - owned));
310 if let Some(daemon) = daemon {
311 let _ = write!(line, "; runnerd itself {}%", percent(daemon, whole));
312 }
313 let _ = write!(
314 line,
315 "; no owner runnerd knows {}%",
316 percent(whole - owned - daemon.unwrap_or(0), whole)
317 );
318 if self.slice.is_none() {
319 line.push_str(", the runner slice's share unread");
320 }
321 line
322 }
323}
324
325#[derive(Debug, Clone, PartialEq, Eq)]
327pub struct WriteSection {
328 pub title: &'static str,
330 pub lines: Vec<String>,
332}
333
334impl DiskWrites {
335 pub const TITLE: &'static str = "Disk writes";
337
338 pub fn sections(&self) -> Vec<WriteSection> {
341 let mut sections = Vec::new();
342 if self.jobs.measured > 0 {
343 sections.push(WriteSection {
344 title: "Jobs",
345 lines: vec![self.jobs.line()],
346 });
347 }
348 if let Some(drive) = &self.drive {
349 sections.push(WriteSection {
350 title: "Drive",
351 lines: drive.lines(),
352 });
353 }
354 for (title, top) in [
355 ("Workflows that wrote most", &self.workflows),
356 ("Jobs that wrote most", &self.top_jobs),
357 ] {
358 if !top.is_empty() {
359 sections.push(WriteSection {
360 title,
361 lines: top.iter().map(WriteTop::line).collect(),
362 });
363 }
364 }
365 sections
366 }
367}
368
369#[cfg(test)]
370mod tests {
371 use super::*;
372
373 const M: u64 = 1 << 20;
374 const G: u64 = 1 << 30;
375
376 #[test]
377 fn a_partial_figure_reads_as_a_floor_and_says_why() {
378 let mut written = DiskWritten {
379 bytes: 640 * M,
380 ios: 1,
381 dirty: Some(0),
382 measure: Measure::Complete,
383 };
384 assert_eq!(written.short(), "640M");
385 assert_eq!(written.line(), "640M in 1 write");
386 written.measure = Measure::Partial;
387 written.dirty = Some(96 * M);
388 assert_eq!(written.short(), "≥640M");
389 assert_eq!(written.tone(), Tone::Quiet, "a floor is in the background");
390 assert_eq!(
391 written.line(),
392 "at least 640M in 1 write: part of its run was not read; 96M more waited to be written back as its \
393 container ended"
394 );
395 }
396
397 #[test]
398 fn the_drive_s_lines_share_its_bytes_out_without_passing_the_whole() {
399 let shares = DriveShares {
400 watched: 6 * 3600,
401 written: 600 * G,
402 jobs: 150 * G,
403 slice: Some(200 * G),
404 daemon: Some(6 * G),
405 };
406 let drive = DriveWrites {
407 device: "nvme0n1".to_owned(),
408 watched: 6 * 3600,
409 written: 600 * G,
410 shares,
411 commits: Some(Commits {
412 count: 43_200,
413 watched: 6 * 3600,
414 }),
415 store: None,
416 };
417 assert_eq!(
418 drive.lines(),
419 [
420 "nvme0n1 wrote 600G over the 6h 00m runnerd read it: 2.34T a day at this rate",
421 "Runner jobs 25%; the rest of the runner slice 8% (seeds, the mirror, runners before and after \
422 their jobs, write-back after a container ended); runnerd itself 1%; no owner runnerd knows 66%",
423 "btrfs committed 2.0 times a second",
424 ]
425 );
426 let over = DriveWrites {
428 shares: DriveShares {
429 slice: Some(100 * G),
430 jobs: 700 * G,
431 ..shares
432 },
433 commits: None,
434 ..drive.clone()
435 };
436 assert_eq!(
437 over.lines()[1],
438 "Runner jobs 100%; the rest of the runner slice 0% (seeds, the mirror, runners before and after their \
439 jobs, write-back after a container ended); runnerd itself 0%; no owner runnerd knows 0%"
440 );
441 assert_eq!(over.lines().len(), 2, "no commits line off btrfs");
442 let in_a_file = DriveWrites {
443 store: Some("loop0".to_owned()),
444 ..over.clone()
445 };
446 assert_eq!(
447 in_a_file.lines()[1],
448 "Docker's root is a file system in a file on it (loop0): the jobs' and the runner slice's figures are \
449 bytes written to that file system; nvme0n1 also wrote its journal and the overhead of the file system \
450 under it"
451 );
452 let part = DriveWrites {
454 shares: DriveShares {
455 watched: 3600,
456 written: 100 * G,
457 jobs: 50 * G,
458 slice: Some(60 * G),
459 daemon: None,
460 },
461 ..drive
462 };
463 assert_eq!(
464 part.lines()[1],
465 "Over the 1h 00m runnerd read the runner slice: runner jobs 50%; the rest of the runner slice 10% (seeds, \
466 the mirror, runners before and after their jobs, write-back after a container ended); no owner runnerd \
467 knows 40%"
468 );
469 }
470}