cargo-critpath 0.1.0

Analyse the critical path of a Cargo build and estimate what changing it would save
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! The command line: arguments, the pipeline they drive, and the exit codes.
//!
//! Cargo invokes an external subcommand as `cargo-critpath critpath <args>`, so a leading
//! `critpath` word is accepted and skipped. Everything is written to a caller-supplied stream
//! rather than to the process's own, which is what lets the tests read the output back.

use core::fmt;
use std::ffi::OsString;
use std::io::{self, Write};
use std::num::NonZeroU32;
use std::path::{Path, PathBuf};

use critpath_core::{analyze, Millis, Policy};
use lexopt::ValueExt;

use crate::findings;
use crate::timings::TimingReport;
use crate::{json, parse_with_metadata, report, ParseError};

/// What the tool did, and the exit code that says so.
///
/// The codes are a contract with CI scripts: a threshold that was exceeded has to be
/// distinguishable from a tool that could not run, or the script has to parse prose.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Outcome {
    /// The analysis ran and every threshold was met.
    Completed,
    /// The arguments were wrong, or a path could not be read.
    Usage,
    /// The input was not something this tool can analyse.
    Rejected,
    /// The analysis ran and a `--fail-if` threshold was exceeded.
    ThresholdExceeded,
}

impl Outcome {
    /// The process exit code for this outcome.
    #[must_use]
    pub const fn code(self) -> i32 {
        match self {
            Self::Completed => 0,
            Self::Usage => 1,
            Self::Rejected => 2,
            Self::ThresholdExceeded => 3,
        }
    }
}

/// The help text, written by hand because the tool has no argument-parsing framework to generate
/// one. See `docs/decisions.md`, D3, for why.
const HELP: &str = "\
cargo-critpath — what to change in a Cargo build, and how much time it would save

USAGE
    cargo critpath --log <path> --timings <path> [options]

    Capture the two inputs from one clean build:

        cargo clean
        cargo build --timings -v --offline 2>build.log
        cargo critpath --log build.log --timings target/cargo-timings/cargo-timing.html

REQUIRED
    --log <path>        the verbose build log: Cargo's stderr from `cargo build -v`
    --timings <path>    a `cargo-timing.html` report from the same build. Repeatable: give one
                        per clean build of the same commit and the run-to-run spread of the
                        critical path is measured, so that a saving smaller than the noise can
                        be marked as such

OPTIONS
    --metadata <path>   `cargo metadata --format-version 1` output, which attributes a
                        workspace-local source path to its package exactly rather than by
                        directory name
    --jobs <n>          how many executors to analyse for. Defaults to the -j the build ran with
    --format <f>        human (default) or json. The JSON contract is documented at
                        https://github.com/eventvoid/cargo-critpath/blob/main/docs/json-schema.md
    --policy <p>        cargo-like (default), which reproduces Cargo's cost-blind ordering, or
                        longest-path, which schedules better than Cargo does. The gap between
                        them is what Cargo's ordering alone costs
    --include-units     add the per-unit array to the JSON output
    --top <n>           how many ranked units and scenarios to show (default 10)
    --fail-if-critical-path-over <ms>
                        exit 3 if the critical path is longer than this
    --help              print this and exit
    --version           print the version and exit

EXIT CODES
    0  the analysis completed
    1  the arguments were wrong, or a path could not be read
    2  the input was rejected: fresh units, an unreadable report, or a build that was not
       `cargo build`
    3  the analysis completed and a --fail-if threshold was exceeded

WHAT THIS DOES NOT DO
    It analyses `cargo build` only, and refuses a report from `cargo check`, `cargo test` or
    `cargo doc` by name: those are different graphs, not variations on one.
    Every weight comes from one real build at one executor count and is not invariant to it, so
    an absolute time for another executor count is marked unreliable rather than presented.
    A split cannot be modelled exactly, so it is reported as an interval with its assumptions
    printed, never as a single number.
";

/// The options a run was asked for.
struct Options {
    log: PathBuf,
    timings: Vec<PathBuf>,
    metadata: Option<PathBuf>,
    jobs: Option<NonZeroU32>,
    format: Format,
    policy: Policy,
    include_units: bool,
    top: usize,
    fail_if_over: Option<Millis>,
}

/// Which renderer to use.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Format {
    Human,
    Json,
}

/// What the arguments asked the tool to do.
enum Command {
    Help,
    Version,
    Analyse(Box<Options>),
}

/// Every option the tool accepts, for the message an unknown one produces.
const OPTIONS: &str = "--log, --timings, --metadata, --jobs, --format, --policy, --include-units, \
                       --top, --fail-if-critical-path-over, --help, --version";

/// An argument that could not be used, and what to do about it.
enum UsageError {
    Unknown(String),
    Unexpected(String),
    MissingValue(String),
    BadValue {
        option: &'static str,
        value: String,
        expected: &'static str,
    },
    Missing(&'static str),
    Unreadable {
        path: String,
        detail: String,
    },
    /// The file was read but holds something other than the UTF-8 text Cargo writes.
    MisEncoded {
        detail: String,
    },
    ExecutorCountUnknown,
}

impl fmt::Display for UsageError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unknown(option) => write!(
                f,
                "unknown option `{option}`. The options are: {OPTIONS}. \
                 Run `cargo critpath --help` for what each one does"
            ),
            Self::Unexpected(value) => write!(
                f,
                "unexpected argument `{value}`: this tool takes options only, apart from the \
                 `critpath` word Cargo itself inserts. The options are: {OPTIONS}"
            ),
            Self::MissingValue(option) => write!(
                f,
                "the option `{option}` needs a value and was given none. \
                 Run `cargo critpath --help` for what it expects"
            ),
            Self::BadValue {
                option,
                value,
                expected,
            } => write!(
                f,
                "`{value}` is not a usable value for `{option}`: {expected}"
            ),
            Self::Missing(option) => write!(
                f,
                "`{option}` is required and was not given. \
                 Capture both inputs from one clean build with \
                 `cargo build --timings -v --offline 2>build.log`, then pass the log to `--log` \
                 and `target/cargo-timings/cargo-timing.html` to `--timings`"
            ),
            Self::Unreadable { path, detail } => write!(
                f,
                "cannot read `{path}`: {detail}. \
                 Check the path, and that the build that was supposed to write it completed"
            ),
            // No advice about the path or the build: the file was found and read, and neither is
            // the problem.
            Self::MisEncoded { detail } => write!(f, "{detail}"),
            Self::ExecutorCountUnknown => write!(
                f,
                "the timing report does not record what `-j` the build ran with, so there is no \
                 executor count to analyse for. Pass `--jobs <n>` to choose one"
            ),
        }
    }
}

impl From<lexopt::Error> for UsageError {
    fn from(error: lexopt::Error) -> Self {
        match error {
            lexopt::Error::UnexpectedOption(option) => Self::Unknown(option),
            lexopt::Error::UnexpectedArgument(value) => {
                Self::Unexpected(value.to_string_lossy().into_owned())
            }
            lexopt::Error::MissingValue { option } => {
                Self::MissingValue(option.unwrap_or_else(|| "an option".to_owned()))
            }
            other => Self::Unknown(other.to_string()),
        }
    }
}

/// Runs the tool, writing its report to `out` and anything that went wrong to `err`.
///
/// `args` is the whole command line including the program name, as [`std::env::args_os`] gives it.
/// Nothing is written to the process's own streams, so a caller can capture both.
///
/// ```no_run
/// let mut out = Vec::new();
/// let mut err = Vec::new();
/// let outcome = cargo_critpath::cli::run(std::env::args_os(), &mut out, &mut err);
/// std::process::exit(outcome.code());
/// ```
pub fn run<I>(args: I, out: &mut dyn Write, err: &mut dyn Write) -> Outcome
where
    I: IntoIterator,
    I::Item: Into<OsString>,
{
    let command = match parse(args) {
        Ok(command) => command,
        Err(problem) => {
            let _ = writeln!(err, "error: {problem}");
            return Outcome::Usage;
        }
    };
    match command {
        Command::Help => {
            let _ = write!(out, "{HELP}");
            Outcome::Completed
        }
        Command::Version => {
            let _ = writeln!(out, "cargo-critpath {}", env!("CARGO_PKG_VERSION"));
            Outcome::Completed
        }
        Command::Analyse(options) => analyse(&options, out, err),
    }
}

fn parse<I>(args: I) -> Result<Command, UsageError>
where
    I: IntoIterator,
    I::Item: Into<OsString>,
{
    let mut log: Option<PathBuf> = None;
    let mut timings: Vec<PathBuf> = Vec::new();
    let mut metadata: Option<PathBuf> = None;
    let mut jobs: Option<NonZeroU32> = None;
    let mut format = Format::Human;
    let mut policy = Policy::CargoLike;
    let mut include_units = false;
    let mut top = 10usize;
    let mut fail_if_over: Option<Millis> = None;
    let mut seen_subcommand = false;

    let mut parser = lexopt::Parser::from_iter(args);
    while let Some(argument) = parser.next()? {
        match argument {
            lexopt::Arg::Long("log") => log = Some(PathBuf::from(parser.value()?)),
            lexopt::Arg::Long("timings") => timings.push(PathBuf::from(parser.value()?)),
            lexopt::Arg::Long("metadata") => metadata = Some(PathBuf::from(parser.value()?)),
            lexopt::Arg::Long("jobs") => {
                let raw = parser.value()?.string()?;
                jobs = Some(raw.parse().ok().and_then(NonZeroU32::new).ok_or(
                    UsageError::BadValue {
                        option: "--jobs",
                        value: raw.clone(),
                        expected: "it must be a whole number of executors, at least 1",
                    },
                )?);
            }
            lexopt::Arg::Long("format") => {
                let raw = parser.value()?.string()?;
                format = match raw.as_str() {
                    "human" => Format::Human,
                    "json" => Format::Json,
                    _ => {
                        return Err(UsageError::BadValue {
                            option: "--format",
                            value: raw,
                            expected: "the formats are `human` and `json`",
                        })
                    }
                };
            }
            lexopt::Arg::Long("policy") => {
                let raw = parser.value()?.string()?;
                policy = match raw.as_str() {
                    "cargo-like" => Policy::CargoLike,
                    "longest-path" => Policy::LongestPath,
                    _ => {
                        return Err(UsageError::BadValue {
                            option: "--policy",
                            value: raw,
                            expected: "the policies are `cargo-like` and `longest-path`",
                        })
                    }
                };
            }
            lexopt::Arg::Long("include-units") => include_units = true,
            lexopt::Arg::Long("top") => {
                let raw = parser.value()?.string()?;
                top = raw.parse().map_err(|_| UsageError::BadValue {
                    option: "--top",
                    value: raw.clone(),
                    expected: "it must be a whole number of entries, and may be 0",
                })?;
            }
            lexopt::Arg::Long("fail-if-critical-path-over") => {
                let raw = parser.value()?.string()?;
                let ms: u64 = raw.parse().map_err(|_| UsageError::BadValue {
                    option: "--fail-if-critical-path-over",
                    value: raw.clone(),
                    expected: "it must be a whole number of milliseconds",
                })?;
                fail_if_over = Some(Millis::new(ms));
            }
            lexopt::Arg::Long("help") => return Ok(Command::Help),
            lexopt::Arg::Long("version") => return Ok(Command::Version),
            // Cargo runs an external subcommand as `cargo-critpath critpath …`, so the first
            // positional argument is the subcommand's own name.
            lexopt::Arg::Value(value) if !seen_subcommand && value == *"critpath" => {
                seen_subcommand = true;
            }
            lexopt::Arg::Value(value) => {
                return Err(UsageError::Unexpected(value.to_string_lossy().into_owned()))
            }
            lexopt::Arg::Long(name) => return Err(UsageError::Unknown(format!("--{name}"))),
            lexopt::Arg::Short(letter) => return Err(UsageError::Unknown(format!("-{letter}"))),
        }
    }

    let log = log.ok_or(UsageError::Missing("--log"))?;
    if timings.is_empty() {
        return Err(UsageError::Missing("--timings"));
    }
    Ok(Command::Analyse(Box::new(Options {
        log,
        timings,
        metadata,
        jobs,
        format,
        policy,
        include_units,
        top,
        fail_if_over,
    })))
}

fn analyse(options: &Options, out: &mut dyn Write, err: &mut dyn Write) -> Outcome {
    let log = match read(&options.log) {
        Ok(text) => text,
        Err(problem) => return usage(err, &problem),
    };
    let metadata = match options.metadata.as_deref() {
        Some(path) => match read(path) {
            Ok(text) => Some(text),
            Err(problem) => return usage(err, &problem),
        },
        None => None,
    };
    let mut reports = Vec::with_capacity(options.timings.len());
    for path in &options.timings {
        match read(path) {
            Ok(text) => reports.push(text),
            Err(problem) => return usage(err, &problem),
        }
    }

    // The subcommand is decided before anything is matched, because a parser written against the
    // `cargo build` alphabet does not merely degrade on another subcommand's report: it silently
    // produces a smaller graph with a meaningless critical path.
    for text in &reports {
        let report = match crate::parse_timing_report(text) {
            Ok(report) => report,
            Err(problem) => return rejected(err, &problem),
        };
        if let Some(subcommand) = subcommand_of(&report) {
            let _ = writeln!(
                err,
                "error: this timing report is from `{subcommand}`, which this tool does not \
                 analyse. Only `cargo build` is supported, because the other subcommands build \
                 genuinely different graphs: on one measured project, `cargo build` and \
                 `cargo check` shared exactly one unit on their critical paths. Capture a build \
                 with `cargo clean && cargo build --timings -v --offline 2>build.log`"
            );
            return Outcome::Rejected;
        }
    }

    let mut builds = Vec::with_capacity(reports.len());
    for text in &reports {
        match parse_with_metadata(&log, text, metadata.as_deref()) {
            Ok(build) => builds.push(build),
            Err(problem) => return rejected(err, &problem),
        }
    }
    let Some(primary) = builds.first() else {
        return usage(err, &UsageError::Missing("--timings"));
    };

    let executors = match options
        .jobs
        .or_else(|| primary.build.jobs.and_then(NonZeroU32::new))
    {
        Some(executors) => executors,
        None => return usage(err, &UsageError::ExecutorCountUnknown),
    };
    let samples: Vec<Millis> = builds
        .iter()
        .map(|build| analyze(&build.graph).critical_path_len())
        .collect();

    let findings = findings::assemble(primary, &samples, executors, options.policy, options.top);
    let written = match options.format {
        Format::Human => report::write(out, &findings),
        Format::Json => json::write(out, &findings, options.include_units),
    };
    if let Err(problem) = written {
        let _ = writeln!(err, "error: cannot write the report: {problem}");
        return Outcome::Usage;
    }

    match options.fail_if_over {
        Some(limit) if findings.baseline.critical_path > limit => {
            let _ = writeln!(
                err,
                "error: the critical path is {} against the {limit} limit given by \
                 --fail-if-critical-path-over",
                findings.baseline.critical_path
            );
            Outcome::ThresholdExceeded
        }
        _ => Outcome::Completed,
    }
}

/// The Cargo subcommand a timing report came from, when it was not `cargo build`.
///
/// The `target` field is a display string whose alphabet depends on the subcommand, recorded in
/// `docs/cargo-data-sources.md`: `cargo check` suffixes `(check)`, `cargo test` `(test)` and
/// `(doc test)`, and so on. `cargo build` never suffixes anything.
fn subcommand_of(report: &TimingReport) -> Option<&'static str> {
    const MARKERS: [(&str, &str); 5] = [
        ("(doc test)", "cargo test"),
        ("(test)", "cargo test"),
        ("(bench)", "cargo bench"),
        ("(check)", "cargo check"),
        ("(doc)", "cargo doc"),
    ];
    report.units.iter().find_map(|unit| {
        let label = unit.target.label();
        MARKERS
            .iter()
            .find(|(marker, _)| label.ends_with(marker))
            .map(|(_, command)| *command)
    })
}

fn read(path: &Path) -> Result<String, UsageError> {
    crate::collect::read_text(path).map_err(|problem| {
        if problem.kind() == io::ErrorKind::InvalidData {
            UsageError::MisEncoded {
                detail: problem.to_string(),
            }
        } else {
            UsageError::Unreadable {
                path: path.display().to_string(),
                detail: problem.to_string(),
            }
        }
    })
}

fn usage(err: &mut dyn Write, problem: &UsageError) -> Outcome {
    let _ = writeln!(err, "error: {problem}");
    match problem {
        // The file was found and read; what it holds is unusable. That is the same outcome as a
        // report this tool cannot analyse, and it must not depend on whether the file happened to
        // carry a byte-order mark: a mis-encoded log without one is caught by the parser and
        // reported as rejected, and one with a BOM has to agree.
        UsageError::MisEncoded { .. } => Outcome::Rejected,
        _ => Outcome::Usage,
    }
}

fn rejected(err: &mut dyn Write, problem: &ParseError) -> Outcome {
    let _ = writeln!(err, "error: {problem}");
    Outcome::Rejected
}