runnerd_core/api/jobs.rs
1//! Job history as clients read it: one record per GitHub job a runner of this daemon ran, its timeline, steps,
2//! metrics and log size; the queries that list them; sample series for graphs; per-repository aggregates; cache
3//! sizes. Times are seconds since the Unix epoch, sizes are bytes, CPU is in millicores (1000 = one core busy).
4
5use serde::{Deserialize, Serialize};
6
7use super::{cpu_time, in_cores};
8use crate::config::ByteSize;
9
10mod facts;
11
12pub use facts::{
13 JobTitle, Milestone, Outcome, OutcomeKind, Phase, PhaseKind, RunComparison, SampleUsage,
14 job_title,
15};
16
17/// Where a job is in its life, as this daemon saw it.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
19#[serde(rename_all = "kebab-case")]
20pub enum JobState {
21 /// Waiting for a runner.
22 Queued,
23 /// A runner of this daemon took it.
24 Running,
25 /// GitHub reports it completed; `conclusion` says how.
26 Completed,
27 /// Its runner's container is gone and GitHub did not report the job completed in time.
28 Lost,
29 /// A runner of another machine took it, or it ended before any runner did, as when a newer push cancelled it.
30 /// One no runner took has no `started_at`, except in records of versions that kept the start GitHub gives it.
31 Elsewhere,
32}
33
34impl JobState {
35 /// Whether the job can still change.
36 pub fn is_final(self) -> bool {
37 matches!(
38 self,
39 JobState::Completed | JobState::Lost | JobState::Elsewhere
40 )
41 }
42}
43
44/// A job without its steps: what lists and events carry.
45#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
46pub struct JobSummary {
47 /// GitHub's job ID, also its check run ID.
48 pub id: u64,
49 /// `owner/name` on GitHub.
50 pub repository: String,
51 /// The workflow run it belongs to.
52 pub run_id: u64,
53 /// 1 for the first attempt of the run, higher for re-runs.
54 pub run_attempt: u32,
55 /// The workflow's name.
56 pub workflow: Option<String>,
57 /// The job's name in the workflow.
58 pub name: String,
59 /// The branch the run is for.
60 pub branch: Option<String>,
61 /// The job's page on GitHub.
62 pub url: Option<String>,
63 /// The workflow's `runs-on` labels.
64 pub labels: Vec<String>,
65 /// Where the job is.
66 pub state: JobState,
67 /// GitHub's conclusion once completed: `success`, `failure`, `cancelled`, `skipped`, `timed_out`, ….
68 pub conclusion: Option<String>,
69 /// The runner that ran it.
70 pub runner: Option<JobRunner>,
71 /// When each phase started.
72 pub timeline: Timeline,
73 /// CPU, memory and network use while it ran.
74 pub metrics: JobMetrics,
75 /// How much of its log is stored.
76 pub log: LogSummary,
77}
78
79/// `GET /v1/jobs/{id}`: a job with its steps.
80#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
81pub struct Job {
82 /// Everything but the steps.
83 #[serde(flatten)]
84 pub summary: JobSummary,
85 /// The steps as GitHub numbers them, including `Set up job` and `Complete job`.
86 pub steps: Vec<Step>,
87 /// Its record, for a failed job of a profile that records failures.
88 #[serde(default)]
89 pub record: Option<super::RecordNote>,
90 /// How its runner was admitted; `None` for one started before runnerd recorded it.
91 #[serde(default)]
92 pub admission: Option<super::Admission>,
93}
94
95impl Job {
96 /// The lines across its memory and CPU charts, as every client labels them: what admission reserved of the
97 /// budget, when the record has it, and the cap its container ran with, or for a record without it, `profile`'s
98 /// limit as configured now. A reservation of the whole cap is one line.
99 pub fn chart_lines(&self, profile: Option<&super::ProfileStatus>) -> ChartLines {
100 let limits = self.limits(profile);
101 let admission = self.admission.as_ref();
102 let name = profile.map_or("profile", |profile| profile.name.as_str());
103 ChartLines {
104 memory: chart_lines(
105 limits.memory,
106 admission.map(|admission| admission.reserved),
107 |value| ByteSize(value).to_string(),
108 name,
109 ),
110 cpu: chart_lines(
111 limits.cpu,
112 admission
113 .and_then(|admission| admission.cpu)
114 .map(|cpu| cpu.reserved),
115 super::cpus,
116 name,
117 ),
118 }
119 }
120
121 /// What its container may use of each resource, as every client scales its use against: the cap it ran with,
122 /// or, for a record without it, `profile`'s limit as configured now. Each resource falls back on its own, so a
123 /// reload that changes the profile never replaces a cap the record has.
124 pub fn limits(&self, profile: Option<&super::ProfileStatus>) -> Limits {
125 let runner = self.summary.runner.as_ref();
126 let limit = |cap: Option<u64>, now: Option<u64>| match (cap, now) {
127 (Some(value), _) => Some(Limit {
128 value,
129 source: LimitSource::Cap,
130 }),
131 (None, Some(value)) => Some(Limit {
132 value,
133 source: LimitSource::ProfileNow,
134 }),
135 (None, None) => None,
136 };
137 Limits {
138 memory: limit(
139 runner.and_then(|runner| runner.memory_cap),
140 profile.map(|profile| profile.memory),
141 ),
142 cpu: limit(
143 runner.and_then(|runner| runner.cpu_cap),
144 profile.map(|profile| u64::from(profile.cpus) * 1000),
145 ),
146 }
147 }
148}
149
150/// What a job's container may use, from `Job::limits`.
151#[derive(Debug, Clone, Copy, PartialEq, Eq)]
152pub struct Limits {
153 /// In bytes.
154 pub memory: Option<Limit>,
155 /// In millicores.
156 pub cpu: Option<Limit>,
157}
158
159impl Limits {
160 /// The limits as one clause, such as `capped at 6G and 4 CPUs`, `capped at 6G; profile now 4 CPUs` or `profile
161 /// now 6G and 4 CPUs`; `None` without either.
162 pub fn line(&self) -> Option<String> {
163 let memory = self
164 .memory
165 .map(|limit| (limit.source, ByteSize(limit.value).to_string()));
166 let cpu = self
167 .cpu
168 .map(|limit| (limit.source, super::cpus(limit.value)));
169 let clause = |source: LimitSource, figures: &str| match source {
170 LimitSource::Cap => format!("capped at {figures}"),
171 LimitSource::ProfileNow => format!("profile now {figures}"),
172 };
173 match (memory, cpu) {
174 (Some((memory_source, memory)), Some((cpu_source, cpu)))
175 if memory_source == cpu_source =>
176 {
177 Some(clause(memory_source, &format!("{memory} and {cpu}")))
178 }
179 (Some((memory_source, memory)), Some((cpu_source, cpu))) => Some(format!(
180 "{}; {}",
181 clause(memory_source, &memory),
182 clause(cpu_source, &cpu)
183 )),
184 (Some((source, figure)), None) | (None, Some((source, figure))) => {
185 Some(clause(source, &figure))
186 }
187 (None, None) => None,
188 }
189 }
190}
191
192/// One resource's limit for a job's container.
193#[derive(Debug, Clone, Copy, PartialEq, Eq)]
194pub struct Limit {
195 /// Bytes or millicores.
196 pub value: u64,
197 /// Where it comes from.
198 pub source: LimitSource,
199}
200
201/// Where a job's limit comes from.
202#[derive(Debug, Clone, Copy, PartialEq, Eq)]
203pub enum LimitSource {
204 /// The cap its container ran with.
205 Cap,
206 /// Its profile's limit in the config now, for a record without its container's cap; a reload since may have
207 /// changed it.
208 ProfileNow,
209}
210
211/// One resource's lines: `value` writes a figure, such as `8.5G` or `3.6 CPUs`.
212fn chart_lines(
213 limit: Option<Limit>,
214 reserved: Option<u64>,
215 value: fn(u64) -> String,
216 profile: &str,
217) -> Vec<ChartLine> {
218 let line = |figure, kind, label: &str| ChartLine {
219 value: figure,
220 kind,
221 label: format!("{label} {}", value(figure)),
222 };
223 let mut lines = vec![];
224 match limit {
225 Some(Limit {
226 value: cap,
227 source: LimitSource::Cap,
228 }) if reserved == Some(cap) => {
229 return vec![line(cap, ChartLineKind::CapAndReserved, "cap and reserved")];
230 }
231 Some(Limit {
232 value: cap,
233 source: LimitSource::Cap,
234 }) => lines.push(line(cap, ChartLineKind::Cap, "cap")),
235 Some(Limit {
236 value: now,
237 source: LimitSource::ProfileNow,
238 }) => lines.push(line(
239 now,
240 ChartLineKind::ProfileNow,
241 &format!("{profile} now"),
242 )),
243 None => {}
244 }
245 if let Some(reserved) = reserved {
246 lines.push(line(reserved, ChartLineKind::Reserved, "reserved"));
247 }
248 lines
249}
250
251/// The lines across a job's memory and CPU charts, from `Job::chart_lines`.
252#[derive(Debug, Clone, PartialEq, Eq)]
253pub struct ChartLines {
254 /// In bytes.
255 pub memory: Vec<ChartLine>,
256 /// In millicores.
257 pub cpu: Vec<ChartLine>,
258}
259
260/// A line across a job's memory or CPU chart.
261#[derive(Debug, Clone, PartialEq, Eq)]
262pub struct ChartLine {
263 /// Bytes or millicores.
264 pub value: u64,
265 /// What it stands for.
266 pub kind: ChartLineKind,
267 /// Its label, such as `cap 8.5G` or `reserved 3.6 CPUs`.
268 pub label: String,
269}
270
271/// What a line across a job's chart stands for.
272#[derive(Debug, Clone, Copy, PartialEq, Eq)]
273pub enum ChartLineKind {
274 /// The most its container may use: its cap.
275 Cap,
276 /// Its profile's limit in the config now, for a record without its container's cap; a reload since may have
277 /// changed it.
278 ProfileNow,
279 /// What admission counted against the budget for its runner.
280 Reserved,
281 /// A reservation of the whole cap, as admission by caps makes.
282 CapAndReserved,
283}
284
285/// The runner a job ran on.
286#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
287pub struct JobRunner {
288 /// The runner's name, also its container's.
289 pub name: String,
290 /// The profile it ran in.
291 pub profile: String,
292 /// The slot it ran in, 1-based.
293 pub slot: usize,
294 /// Its container's memory cap in bytes, which a reload since may have changed in the profile. `None` in records
295 /// from before runnerd kept it.
296 pub memory_cap: Option<u64>,
297 /// Its container's CPU limit in millicores, the unit of the CPU use in `Sample` and `JobMetrics`. `None` in
298 /// records from before runnerd kept it, or when Docker reports no limit.
299 pub cpu_cap: Option<u64>,
300}
301
302/// A job's phases, in the order they happen. A runner's container starts before GitHub gives it a job.
303#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
304pub struct Timeline {
305 /// GitHub queued the job.
306 pub queued_at: u64,
307 /// The container of the runner that took it started.
308 pub container_started_at: Option<u64>,
309 /// The runner started the job.
310 pub started_at: Option<u64>,
311 /// The job finished, by GitHub's clock; or when its container was found gone.
312 pub finished_at: Option<u64>,
313 /// The container and the runner's registration were removed.
314 pub cleaned_at: Option<u64>,
315 /// Seconds of its wait that passed while no runnerd ran, as the daemon that started after measured them. Waits
316 /// for a slot, as sizing, insights and the history's figures count them, leave them out; its own wait does not.
317 #[serde(default)]
318 pub unseen: u64,
319 /// When runnerd first listed it queued (ADR 0092). `None` for a job no listing showed queued, such as one a
320 /// warm runner took first, and for one an older runnerd recorded.
321 #[serde(default)]
322 pub seen_at: Option<u64>,
323}
324
325impl Timeline {
326 /// When the runner that took it began to start for it: its container's start, or the job's queueing when that
327 /// container waited for jobs before, as a warm runner does. `None` until a runner took it.
328 pub fn runner_from(&self) -> Option<u64> {
329 self.container_started_at
330 .map(|started| started.max(self.queued_at))
331 }
332}
333
334/// What a job used, over the samples taken while it ran (every few seconds). `None` until a sample exists.
335#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
336pub struct JobMetrics {
337 /// CPU time, in milliseconds.
338 pub cpu_time: Option<u64>,
339 /// The highest CPU use of a sample interval, in millicores.
340 pub cpu_peak: Option<u64>,
341 /// CPU time over wall time, in millicores.
342 pub cpu_average: Option<u64>,
343 /// The highest memory use without page cache.
344 pub memory_peak: Option<u64>,
345 /// The average memory use without page cache.
346 pub memory_average: Option<u64>,
347 /// Bytes the container received.
348 pub network_received: Option<u64>,
349 /// Bytes the container sent.
350 pub network_sent: Option<u64>,
351 /// Of the bytes received, those the Docker Hub mirror served.
352 pub mirror_bytes: u64,
353 /// What the shared build cache served it and took from it.
354 pub build_cache: BuildCacheCounts,
355 /// Times the kernel held its container's sockets' receive windows small because the container's memory was
356 /// under pressure (`memory.events` `sock_throttled`): reclaim went badly, mostly because the host was short of
357 /// memory, and its downloads slowed. `None` when the kernel does not count it, or in records from before.
358 #[serde(default, skip_serializing_if = "Option::is_none")]
359 pub sock_throttled: Option<u64>,
360 /// The periods of its CPU quota, 100 ms each, in which it had work: its busy time (`cpu.stat` `nr_periods`).
361 /// `None` where the kernel keeps no CPU quota's figures, or in records from before.
362 #[serde(default, skip_serializing_if = "Option::is_none")]
363 pub cpu_periods: Option<u64>,
364 /// Of `cpu_periods`, those in which it used up its quota and was held until the next (`nr_throttled`): its CPU
365 /// cap held it back.
366 #[serde(default, skip_serializing_if = "Option::is_none")]
367 pub cpu_throttled: Option<u64>,
368 /// Milliseconds in which a task of it could run and waited for a CPU (`cpu.pressure` `some`): held by its cap,
369 /// or by the other runners and programs on the host's CPUs. `None` without pressure stall information.
370 #[serde(default, skip_serializing_if = "Option::is_none")]
371 pub cpu_stalled: Option<u64>,
372}
373
374/// One figure of what a job used, worded for every client.
375#[derive(Debug, Clone, PartialEq, Eq)]
376pub struct Total {
377 /// What it measures: `Memory peak`, `Received`.
378 pub label: &'static str,
379 /// The figure with its unit.
380 pub value: String,
381}
382
383impl JobMetrics {
384 /// What the job used, over its whole life: memory, CPU and network, each where the daemon measured it. The
385 /// peaks are of the samples taken every few seconds, so the figures' note says that
386 /// ([`sampled_note`](super::sampled_note)).
387 pub fn totals(&self) -> Vec<Total> {
388 let mut totals = Vec::new();
389 let mut add = |label: &'static str, value: Option<String>| {
390 totals.extend(value.map(|value| Total { label, value }));
391 };
392 let bytes = |bytes: u64| ByteSize(bytes).to_string();
393 add("Memory peak", self.memory_peak.map(bytes));
394 add("Average memory", self.memory_average.map(bytes));
395 add("CPU peak", self.cpu_peak.map(in_cores));
396 add("Average CPU", self.cpu_average.map(in_cores));
397 add("CPU time", self.cpu_time.map(cpu_time));
398 add("Received", self.network_received.map(bytes));
399 add("Sent", self.network_sent.map(bytes));
400 // Counted from the mirror's own log, so a job without network samples still has it.
401 add(
402 "From the mirror",
403 (self.network_received.is_some() || self.mirror_bytes > 0)
404 .then(|| bytes(self.mirror_bytes)),
405 );
406 add(
407 "Throttled for memory pressure",
408 self.sock_throttled
409 .filter(|times| *times > 0)
410 .map(|times| format!("{times} times")),
411 );
412 totals
413 }
414}
415
416/// What the shared build cache served one runner and took from it (ADR 0052), counted by the cache itself.
417#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
418pub struct BuildCacheCounts {
419 /// Lookups that found an entry.
420 pub hits: u64,
421 /// Lookups that found none.
422 pub misses: u64,
423 /// Bytes of the entries it served.
424 pub read_bytes: u64,
425 /// Bytes of the entries it stored.
426 pub written_bytes: u64,
427 /// Entries it was sent and did not store: the job may not write (a pull request, another branch), or the
428 /// cache was full.
429 pub refused: u64,
430 /// Whether the job could write: a push, scheduled or manually started run of a branch that may write.
431 pub writer: bool,
432}
433
434impl BuildCacheCounts {
435 /// Hits and misses.
436 pub fn lookups(&self) -> u64 {
437 self.hits + self.misses
438 }
439
440 /// Hits in percent of lookups, `None` without any.
441 pub fn hit_percent(&self) -> Option<u64> {
442 let lookups = self.lookups();
443 (lookups > 0).then(|| self.hits * 100 / lookups)
444 }
445
446 /// Adds `other`'s counts; `writer` stays.
447 pub fn add(&mut self, other: &BuildCacheCounts) {
448 self.hits += other.hits;
449 self.misses += other.misses;
450 self.read_bytes += other.read_bytes;
451 self.written_bytes += other.written_bytes;
452 self.refused += other.refused;
453 }
454}
455
456/// A job's stored log.
457#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
458pub struct LogSummary {
459 /// Lines stored.
460 pub lines: u64,
461 /// Bytes stored.
462 pub bytes: u64,
463 /// Set once the log reached `history.log_max` and later lines were dropped.
464 pub truncated: bool,
465}
466
467/// One step of a job, as GitHub reports it.
468#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
469pub struct Step {
470 /// GitHub's step number; numbers may skip.
471 pub number: u32,
472 /// The name GitHub shows, such as `Set up job` or `Run docker run --rm hello-world`.
473 pub name: String,
474 /// `pending`, `queued`, `in_progress` or `completed`.
475 pub status: String,
476 /// Once completed: `success`, `failure`, `skipped`, `cancelled`, ….
477 pub conclusion: Option<String>,
478 /// When it started.
479 pub started_at: Option<u64>,
480 /// When it completed.
481 pub completed_at: Option<u64>,
482}
483
484/// `GET /v1/jobs` query parameters. Jobs come newest first (by ID, which grows with time).
485#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
486pub struct JobQuery {
487 /// Only this `owner/name`.
488 pub repository: Option<String>,
489 /// Only jobs in this state.
490 pub state: Option<JobState>,
491 /// Only jobs with this conclusion, such as `failure`.
492 pub conclusion: Option<String>,
493 /// Only jobs of the workflow with this name, such as `CI`.
494 pub workflow: Option<String>,
495 /// Only jobs with this name in their workflow, such as `build`.
496 pub name: Option<String>,
497 /// Only jobs queued at or after this time.
498 pub since: Option<u64>,
499 /// Only jobs queued before this time.
500 pub until: Option<u64>,
501 /// Only jobs with a smaller ID: the `next` of the previous page.
502 pub before: Option<u64>,
503 /// At most this many jobs; default 50, at most 500.
504 pub limit: Option<u32>,
505}
506
507/// `GET /v1/jobs`: one page of jobs.
508#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
509pub struct JobPage {
510 /// Newest first.
511 pub jobs: Vec<JobSummary>,
512 /// Pass as `before` for the next page; `None` on the last page.
513 pub next: Option<u64>,
514}
515
516/// `GET /v1/jobs/{id}/logs` query parameters. The response is `text/plain`, one log line per line.
517#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
518pub struct LogQuery {
519 /// Start at this line, 0-based.
520 pub from: Option<u64>,
521 /// Keep the response open and send lines as they are stored, until the job finishes.
522 #[serde(default)]
523 pub follow: bool,
524}
525
526/// One sample of a container's use.
527#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
528pub struct Sample {
529 /// When it was taken.
530 pub at: u64,
531 /// CPU use since the previous sample, in millicores.
532 pub cpu: u64,
533 /// Memory use without page cache.
534 pub memory: u64,
535 /// Bytes received since the container started.
536 pub network_received: u64,
537 /// Bytes sent since the container started.
538 pub network_sent: u64,
539}
540
541/// `GET /v1/jobs/{id}/samples`: what the job's container used over time, from its start to its removal. Samples
542/// older than `history.samples` are deleted; the job's metrics stay.
543#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
544pub struct SampleSeries {
545 /// The job.
546 pub job: u64,
547 /// Seconds between samples.
548 pub interval: u64,
549 /// Oldest first.
550 pub samples: Vec<Sample>,
551}
552
553/// One sample of the whole slice runners run in.
554#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
555pub struct HostSample {
556 /// When it was taken.
557 pub at: u64,
558 /// CPU use of the slice since the previous sample, in millicores.
559 pub cpu: u64,
560 /// Memory use of the slice less its inactive page cache, as `docker stats` counts it: a conservative number,
561 /// since active page cache still counts, which the high-water mark of measured admission compares to
562 /// `MemoryMax`.
563 pub memory: u64,
564}
565
566/// The host's memory at one moment, beside the slice's: what admission fits new runners in besides `MemoryMax`
567/// (ADR 0055). Read every few seconds; not kept in the history.
568#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
569pub struct HostMemory {
570 /// When it was read.
571 pub at: u64,
572 /// `MemTotal` in `/proc/meminfo`, in bytes.
573 pub total: u64,
574 /// `MemAvailable`: what the host could give without swapping, page cache it can drop included, in bytes.
575 pub available: u64,
576 /// The slice's memory without page cache and reclaimable slab: what the runners hold that `MemAvailable` leaves
577 /// out.
578 pub slice: u64,
579 /// `/proc/pressure/memory`'s `some` line: the share of time at least one task waited for memory. `None` on a
580 /// kernel without pressure stall information.
581 pub some: Option<Pressure>,
582 /// Its `full` line: the share of time every task that could run waited for memory.
583 pub full: Option<Pressure>,
584 /// The programs outside the slice that hold the most memory, largest first: at most a few, none under a floor.
585 /// Read less often than the rest. Empty when none is that large.
586 #[serde(default, skip_serializing_if = "Vec::is_empty")]
587 pub holders: Vec<Holder>,
588 /// `[host] memory_ceiling`: the most memory the host can grow to, in bytes, for a VM whose hypervisor adds
589 /// memory as the guest asks for it (ADR 0084). From the config, not from `/proc`; absent unless it is set.
590 #[serde(default, skip_serializing_if = "Option::is_none")]
591 pub ceiling: Option<u64>,
592}
593
594impl HostMemory {
595 /// The host's memory as admission counts it, in bytes: `MemTotal`, or the ceiling when that is more (ADR 0084).
596 pub fn grown(&self) -> u64 {
597 self.ceiling
598 .map_or(self.total, |ceiling| ceiling.max(self.total))
599 }
600
601 /// What the host can still grow by before it reaches its ceiling, in bytes: 0 without one, or once `MemTotal`
602 /// reached it.
603 pub fn growable(&self) -> u64 {
604 self.grown().saturating_sub(self.total)
605 }
606
607 /// What the host holds outside the slice's runners, in bytes: its other programs, tmpfs and the kernel, without
608 /// page cache it can drop.
609 pub fn outside(&self) -> u64 {
610 self.total
611 .saturating_sub(self.available)
612 .saturating_sub(self.slice)
613 }
614
615 /// What holds the host's memory outside the slice, as the clients say it: `programs outside CI hold 30G` and,
616 /// with `holders`, `, most java 9.3G and qemu-system-x86 5.8G`.
617 pub fn outside_line(&self) -> String {
618 let size = |bytes: u64| ByteSize(bytes).to_string();
619 let line = format!("programs outside CI hold {}", size(self.outside()));
620 let named: Vec<String> = self
621 .holders
622 .iter()
623 .map(|holder| format!("{} {}", holder.name, size(holder.memory)))
624 .collect();
625 match named.as_slice() {
626 [] => line,
627 [one] => format!("{line}, most {one}"),
628 [rest @ .., last] => format!("{line}, most {} and {last}", rest.join(", ")),
629 }
630 }
631}
632
633/// A program outside the slice and the memory its processes hold.
634#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
635pub struct Holder {
636 /// Its processes' name, as `/proc/<pid>/comm` says it: never a command line, argument or path.
637 pub name: String,
638 /// What its processes hold that the host can't drop as page cache (`RssAnon` and `RssShmem`), in bytes.
639 pub memory: u64,
640}
641
642/// One line of a pressure stall file, in hundredths of a percent: 250 is 2.5% of the time.
643#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
644pub struct Pressure {
645 /// Over the last 10 seconds.
646 pub avg10: u32,
647 /// Over the last 60 seconds.
648 pub avg60: u32,
649}
650
651/// `GET /v1/host/samples` query parameters.
652#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
653pub struct HostSampleQuery {
654 /// Only samples taken at or after this time; default the last hour, at most the last 24 hours.
655 pub since: Option<u64>,
656}
657
658/// `GET /v1/host/samples`: the slice's use over time, oldest first.
659#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
660pub struct HostSamples {
661 /// Seconds between samples.
662 pub interval: u64,
663 /// Oldest first.
664 pub samples: Vec<HostSample>,
665}
666
667/// How `GET /v1/stats` groups jobs in time.
668#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
669#[serde(rename_all = "kebab-case")]
670pub enum Bucket {
671 /// By UTC hour.
672 Hour,
673 /// By UTC day.
674 #[default]
675 Day,
676}
677
678/// `GET /v1/activity` query parameters.
679#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
680pub struct ActivityQuery {
681 /// The start of the window; default 24 hours ago. It ends now.
682 pub since: Option<u64>,
683}
684
685/// `GET /v1/activity`: every job runnerd's runners started or finished in a window, by when that happened rather
686/// than when the job was queued, so a job that waited or ran long counts in the window it started or ended in.
687#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
688pub struct Activity {
689 /// The start of the window.
690 pub since: u64,
691 /// When it was read: the end of the window.
692 pub until: u64,
693 /// Each job that started in the window, oldest start first.
694 pub started: Vec<Started>,
695 /// The jobs that finished in the window: completed, or lost with their runner.
696 pub finished: u64,
697 /// Of those, the ones that failed, timed out or were lost, as `GET /v1/stats` counts failures.
698 pub failed: u64,
699 /// Of the failed, the ones their runner failed rather than their own steps: lost with their runner, or failed
700 /// after the kernel killed a process of theirs for memory at their cap.
701 pub runner_failed: u64,
702}
703
704impl Activity {
705 /// The 95th percentile of the waits of the jobs that started, by nearest rank as sizing's are; `None` when none
706 /// started.
707 pub fn wait_p95(&self) -> Option<u64> {
708 let mut waits: Vec<u64> = self.started.iter().map(|start| start.waited).collect();
709 waits.sort_unstable();
710 let rank = (waits.len() * 95).div_ceil(100);
711 waits.get(rank.checked_sub(1)?).copied()
712 }
713
714 /// The longest wait of the jobs that started in each of the 24 hours before `until`, oldest hour first; 0 for
715 /// an hour without a start. Counted back from `until`, so the hours are the day the other figures count.
716 pub fn wait_hours(&self) -> [u64; 24] {
717 const HOUR: u64 = 3600;
718 let since = self.until.saturating_sub(24 * HOUR);
719 let mut hours = [0u64; 24];
720 for start in self
721 .started
722 .iter()
723 .filter(|start| (since..=self.until).contains(&start.at))
724 {
725 // A start exactly 24 hours ago is in the oldest hour.
726 let back =
727 usize::try_from((self.until - start.at) / HOUR).map_or(23, |back| back.min(23));
728 hours[23 - back] = hours[23 - back].max(start.waited);
729 }
730 hours
731 }
732}
733
734/// A job that started: when, and how long it had waited since it was queued.
735#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
736pub struct Started {
737 /// When it started.
738 pub at: u64,
739 /// Seconds it waited, leaving out the time before the history's start and the time no runnerd ran (ADR 0081).
740 pub waited: u64,
741}
742
743/// `GET /v1/stats` query parameters.
744#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
745pub struct StatsQuery {
746 /// Only this `owner/name`.
747 pub repository: Option<String>,
748 /// Only jobs queued at or after this time; default 30 days ago. At most 744 buckets before `until` (or now)
749 /// are counted: 31 days of hours, two years of days.
750 pub since: Option<u64>,
751 /// Only jobs queued before this time.
752 pub until: Option<u64>,
753 /// The bucket size; default a day.
754 #[serde(default)]
755 pub bucket: Bucket,
756}
757
758/// `GET /v1/stats`: finished jobs per repository and time bucket, for trends. Sums, so clients can average.
759#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
760pub struct Stats {
761 /// The bucket size.
762 pub bucket: Bucket,
763 /// By bucket start, then repository.
764 pub rows: Vec<StatsRow>,
765}
766
767/// The finished jobs of one repository in one bucket.
768#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
769pub struct StatsRow {
770 /// The bucket's start.
771 pub start: u64,
772 /// `owner/name`.
773 pub repository: String,
774 /// Jobs completed or lost.
775 pub jobs: u64,
776 /// Of those, concluded `success`.
777 pub succeeded: u64,
778 /// Of those, concluded `failure` or `timed_out`, or lost.
779 pub failed: u64,
780 /// Of those, concluded `cancelled`.
781 pub cancelled: u64,
782 /// Seconds from queued to started, summed, leaving out the time before the history's start and the time no runnerd
783 /// ran (ADR 0081).
784 pub queue_seconds: u64,
785 /// Seconds from started to finished, summed.
786 pub run_seconds: u64,
787 /// CPU time, in milliseconds, summed.
788 pub cpu_time: u64,
789 /// The highest memory peak of a job.
790 pub memory_peak: u64,
791 /// Bytes received, summed.
792 pub network_received: u64,
793 /// Bytes from the mirror, summed.
794 pub mirror_bytes: u64,
795}
796
797/// What a cache volume holds.
798#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
799#[serde(rename_all = "kebab-case", tag = "kind")]
800pub enum CacheKind {
801 /// A profile's read-only snapshot.
802 Snapshot {
803 /// The profile.
804 profile: String,
805 },
806 /// A slot's layer changes and Docker Engine data.
807 Slot {
808 /// The profile.
809 profile: String,
810 /// 1-based.
811 slot: usize,
812 },
813 /// The pnpm store all slots share.
814 Pnpm,
815 /// The Maven repository all slots share.
816 Maven,
817 /// The Docker Hub mirror's storage.
818 Mirror,
819 /// The build cache every runner shares: a directory of the daemon's, not a Docker volume.
820 Build,
821}
822
823/// One cache volume and its size.
824#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
825pub struct CacheVolume {
826 /// The Docker volume.
827 pub name: String,
828 /// What it holds.
829 #[serde(flatten)]
830 pub kind: CacheKind,
831 /// Its size at `measured_at`.
832 pub bytes: u64,
833 /// The size above which runnerd empties it, or for a snapshot starts it over at its next reseed, if it has one.
834 pub limit: Option<u64>,
835}
836
837/// `GET /v1/caches`: every cache volume of the configured profiles, as last measured. Docker measures volumes
838/// slowly, so runnerd measures them at start and whenever no runner runs after jobs ended.
839#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
840pub struct Caches {
841 /// When the sizes were measured; `None` before the first measurement.
842 pub measured_at: Option<u64>,
843 /// By name.
844 pub volumes: Vec<CacheVolume>,
845 /// The shared build cache now, when it is on.
846 pub build: Option<BuildCacheStats>,
847}
848
849/// The shared build cache (ADR 0052): its size now, and what it served the jobs of the last 7 days.
850#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
851pub struct BuildCacheStats {
852 /// The entries' bytes.
853 pub bytes: u64,
854 /// How many entries it holds.
855 pub entries: u64,
856 /// `[caches.build] max_size`.
857 pub limit: u64,
858 /// The runners of the last 7 days, summed.
859 pub week: BuildCacheCounts,
860 /// `[caches.build] actions_cache`: whether it also serves GitHub Actions' cache (ADR 0082). `false` from a
861 /// daemon older than it, which never served it.
862 #[serde(default)]
863 pub actions_cache: bool,
864}
865
866/// `GET /v1/caches/history` query parameters.
867#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
868pub struct CacheHistoryQuery {
869 /// Only measurements at or after this time; default 30 days ago.
870 pub since: Option<u64>,
871}
872
873/// One volume's size at one measurement.
874#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
875pub struct CacheSize {
876 /// When it was measured.
877 pub at: u64,
878 /// The Docker volume.
879 pub name: String,
880 /// Its size.
881 pub bytes: u64,
882}
883
884/// `GET /v1/caches/history`: every measurement, oldest first.
885#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
886pub struct CacheHistory {
887 /// Oldest first.
888 pub sizes: Vec<CacheSize>,
889}