Skip to main content

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