megafine 0.2.0

A multithreaded command-line benchmarking tool
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
use anyhow::{Context, Result, anyhow, bail};
use tracing::debug;

use crate::cli::Cli;
use crate::format::TimeUnit;
use crate::measurement::Metric;
use crate::stats::Estimator;

/// Destination of a measured command's stdout (--output). A file is truncated
/// on every run, so it holds the last run's output.
pub enum OutputMode {
    Null,
    Inherit,
    File(std::path::PathBuf),
}

/// Source of a measured command's stdin (--input). A file is reopened on
/// every run, so each run reads it from the start.
pub enum InputMode {
    Null,
    File(std::path::PathBuf),
}

/// Row order of the ranking table (--sort).
#[derive(PartialEq)]
pub enum Sort {
    /// The commands' input order (the `Benchmark N` numbering).
    Command,
    /// Best central value first.
    Metric,
}

/// How runs are dispatched to the workers (--schedule). `sequential` has no
/// variant: it is `Saturate` forced to a single job.
#[derive(PartialEq)]
pub enum Schedule {
    /// Keep every worker busy: round-robin over the commands that have work.
    Saturate,
    /// At most one in-flight run per command at any time.
    Exclusive,
    /// Lockstep rounds: every command runs once per round, and a round fully
    /// drains before the next starts, so run counts stay level.
    Fair,
}

/// A command line pre-parsed into the argv it will be spawned with, so the
/// parse happens once at startup (fail-fast) instead of on every run.
pub struct Invocation {
    /// The original command line, for labels and error messages.
    pub line: String,
    pub argv: Vec<String>,
}

pub struct Options {
    pub jobs: usize,
    /// How runs are dispatched to the workers.
    pub schedule: Schedule,
    pub warmup: u64,
    pub runs: Option<u64>,
    /// Stop a command once the 95% CI half-width of its mean is below this
    /// percentage of the mean.
    pub target: Option<f64>,
    /// `None` runs commands directly; `Some(path)` runs them through that shell.
    pub shell: Option<String>,
    /// Keep timing runs that exit non-zero as failed measurements.
    pub ignore_failure: bool,
    /// Kill a timing run's process group once it exceeds this duration.
    pub timeout: Option<std::time::Duration>,
    /// stdout destination of the timing runs.
    pub output: OutputMode,
    /// stdin source of the timing runs.
    pub input: InputMode,
    pub setup: Option<Invocation>,
    pub prepare: Option<Invocation>,
    pub conclude: Option<Invocation>,
    pub cleanup: Option<Invocation>,
    pub time_unit: Option<TimeUnit>,
    /// Digits after the decimal point for displayed times.
    pub precision: usize,
    /// Central-value statistic reported for the metric and relative ratios.
    pub estimator: Estimator,
    /// The per-run quantity all statistics are computed on.
    pub metric: Metric,
    /// Time only the command's `megafine_start()`/`megafine_stop()` region.
    pub region: bool,
    /// Calibrate the measurement floor against `/bin/true` before timing.
    pub calibrate: bool,
    /// Collect hardware perf counters for every run.
    pub counters: bool,
    /// Pin each concurrent job to its own disjoint subset of the allowed CPUs.
    pub pin: bool,
    /// CPUs booked for megafine's own threads, excluded from the workers' partition.
    pub pin_reserved: usize,
    /// Row order of the ranking table.
    pub sort: Sort,
    /// Print only the relative-speed ratios on stdout.
    pub raw: bool,
    /// 0-based index of the command used as the relative-speed baseline.
    pub reference: usize,
}

pub fn all_cores() -> usize {
    std::thread::available_parallelism()
        .map(|n| n.into())
        .unwrap_or(1)
}

/// Parse `line` into the argv `execute` will spawn: `[shell, -c, line]`
/// when a shell is configured, otherwise split into words here, once.
fn invocation(shell: Option<&str>, line: &str) -> Result<Invocation> {
    let argv = match shell {
        None => {
            let parts = shell_words::split(line)
                .with_context(|| format!("could not parse command '{line}'"))?;
            if parts.is_empty() {
                bail!("empty command '{line}'");
            }
            parts
        }
        Some(shell) => vec![shell.to_string(), "-c".into(), line.into()],
    };
    Ok(Invocation {
        line: line.to_string(),
        argv,
    })
}

impl Options {
    /// Parse `line` into the argv `execute` will spawn (see `invocation`).
    pub fn invocation(&self, line: &str) -> Result<Invocation> {
        invocation(self.shell.as_deref(), line)
    }

    pub fn from_cli(cli: &Cli) -> Result<Self> {
        let schedule = match cli.schedule.as_deref() {
            None | Some("saturate") | Some("sequential") => Schedule::Saturate,
            Some("exclusive") => Schedule::Exclusive,
            Some("fair") => Schedule::Fair,
            Some(s) => {
                bail!("invalid schedule '{s}' (use saturate, sequential, exclusive or fair)")
            }
        };
        let jobs = if matches!(cli.schedule.as_deref(), Some("sequential")) {
            if cli.jobs.is_some() {
                bail!("--schedule sequential runs one task at a time (--jobs 1); drop --jobs");
            }
            1
        } else {
            match cli.jobs {
                None | Some(0) => {
                    let cores = all_cores();
                    if cores <= cli.pin_reserved {
                        bail!(
                            "--pin-reserved {} leaves no CPU for jobs ({cores} available); \
                             reduce it or set --jobs explicitly",
                            cli.pin_reserved
                        );
                    }
                    cores - cli.pin_reserved
                }
                Some(n) => n,
            }
        };

        let shell = if cli.shell {
            let ppid = std::os::unix::process::parent_id();
            let exe = format!("/proc/{ppid}/exe");
            let exe = std::fs::read_link(&exe)
                .with_context(|| format!("could not resolve the current shell via {exe}"))?;
            debug!("Current shell is {}", exe.display());
            let exe = exe
                .into_os_string()
                .into_string()
                .map_err(|p| anyhow!("the shell path {p:?} is not valid UTF-8"))?;
            Some(exe)
        } else {
            None
        };

        let time_unit = cli
            .time_unit
            .as_deref()
            .map(|s| {
                TimeUnit::parse(s)
                    .with_context(|| format!("invalid time unit '{s}' (use us, ms or s)"))
            })
            .transpose()?;

        let estimator = cli
            .estimator
            .as_deref()
            .map(|s| {
                Estimator::parse(s).with_context(|| {
                    format!(
                        "invalid estimator '{s}' (use mean, median, or a percentile like p90 or p999)"
                    )
                })
            })
            .transpose()?
            .unwrap_or(Estimator::Mean);

        let metric = cli
            .metric
            .as_deref()
            .map(|s| {
                Metric::parse(s).ok_or_else(|| {
                    anyhow!(
                        "invalid metric '{s}' (use time, user, sys, rss, major-faults, \
                         minor-faults, vol-ctx, invol-ctx, instructions, cycles, \
                         cache-misses or branch-misses)"
                    )
                })
            })
            .transpose()?
            .unwrap_or(Metric::Time);

        if let Some(t) = cli.target {
            if t <= 0.0 {
                bail!("--target must be positive");
            }
            if estimator != Estimator::Mean {
                bail!("--target needs the mean estimator (percentile CIs are not supported)");
            }
            if schedule == Schedule::Fair {
                bail!(
                    "--schedule fair keeps run counts equal, but --target stops each command \
                     at its own count; they conflict"
                );
            }
        }

        let timeout = cli
            .timeout
            .map(|s| {
                if s <= 0.0 || !s.is_finite() {
                    bail!("--timeout must be positive");
                }
                Ok(std::time::Duration::from_secs_f64(s))
            })
            .transpose()?;

        // Keywords win over paths; a file literally named `null` is `./null`.
        let output = match cli.output.as_deref() {
            None | Some("null") => OutputMode::Null,
            Some("inherit") => OutputMode::Inherit,
            Some(path) => OutputMode::File(path.into()),
        };
        let input = match cli.input.as_deref() {
            None | Some("null") => InputMode::Null,
            Some(path) => {
                // Fail fast: probe once here, reopened for every run.
                std::fs::File::open(path)
                    .with_context(|| format!("--input: cannot open '{path}'"))?;
                InputMode::File(path.into())
            }
        };

        let sort = match cli.sort.as_deref() {
            None | Some("command") => Sort::Command,
            Some("metric") => Sort::Metric,
            Some(s) => bail!("invalid sort order '{s}' (use command or metric)"),
        };

        if cli.raw && cli.commands.len() < 2 {
            bail!("--raw needs at least 2 commands (it prints relative-speed ratios)");
        }

        let names = cli.command_name.as_deref().unwrap_or_default();
        if names.len() > cli.commands.len() {
            bail!(
                "got {} command names but only {} command(s)",
                names.len(),
                cli.commands.len()
            );
        }

        let reference = match cli.reference {
            None => 0,
            Some(0) => bail!("--reference must be at least 1 (1-based command index)"),
            Some(n) if n > cli.commands.len() => {
                bail!("--reference {n} out of range (1..={})", cli.commands.len())
            }
            Some(n) => n - 1,
        };

        let hook = |cmd: &Option<String>| {
            cmd.as_deref()
                .map(|s| invocation(shell.as_deref(), s))
                .transpose()
        };
        let setup = hook(&cli.setup)?;
        let prepare = hook(&cli.prepare)?;
        let conclude = hook(&cli.conclude)?;
        let cleanup = hook(&cli.cleanup)?;

        Ok(Options {
            jobs,
            schedule,
            warmup: cli.warmup,
            runs: cli.runs.map(std::num::NonZeroU64::get),
            target: cli.target,
            shell,
            ignore_failure: cli.ignore_failure,
            timeout,
            output,
            input,
            setup,
            prepare,
            conclude,
            cleanup,
            time_unit,
            precision: cli.precision,
            estimator,
            metric,
            region: cli.region,
            calibrate: !cli.region && !cli.no_calibrate,
            // Counter metrics need the counters; perf::probe() at startup
            // still reports a clear error when perf events are unavailable.
            counters: cli.counters || metric.needs_counters(),
            pin: !cli.no_pin,
            pin_reserved: cli.pin_reserved,
            sort,
            raw: cli.raw,
            reference,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser;

    /// Parse args (clap) then build `Options`; binary name is implicit.
    fn opts(args: &[&str]) -> Result<Options> {
        let argv = std::iter::once("megafine").chain(args.iter().copied());
        let cli = Cli::try_parse_from(argv).expect("args should parse at the clap level");
        Options::from_cli(&cli)
    }

    #[test]
    fn defaults() {
        let o = opts(&["echo hi"]).unwrap();
        assert_eq!(o.jobs, all_cores());
        assert_eq!(o.reference, 0);
        assert!(o.pin);
        assert!(!o.raw);
    }

    #[test]
    fn ignore_failure_plumbs() {
        assert!(!opts(&["a"]).unwrap().ignore_failure);
        assert!(opts(&["-i", "a"]).unwrap().ignore_failure);
    }

    #[test]
    fn timeout_validation() {
        assert!(opts(&["--timeout", "0", "a"]).is_err());
        assert!(opts(&["--timeout=-1", "a"]).is_err());
        assert_eq!(
            opts(&["--timeout", "1.5", "a"]).unwrap().timeout,
            Some(std::time::Duration::from_millis(1500))
        );
    }

    #[test]
    fn output_input_parsing() {
        assert!(matches!(opts(&["a"]).unwrap().output, OutputMode::Null));
        assert!(matches!(
            opts(&["--output", "null", "a"]).unwrap().output,
            OutputMode::Null
        ));
        assert!(matches!(
            opts(&["--output", "inherit", "a"]).unwrap().output,
            OutputMode::Inherit
        ));
        assert!(matches!(
            opts(&["--output", "out.log", "a"]).unwrap().output,
            OutputMode::File(_)
        ));
        assert!(matches!(opts(&["a"]).unwrap().input, InputMode::Null));
        assert!(opts(&["--input", "/nonexistent/megafine-in", "a"]).is_err());
    }

    #[test]
    fn metric_defaults_and_parsing() {
        assert!(opts(&["a"]).unwrap().metric == Metric::Time);
        assert!(opts(&["--metric", "rss", "a"]).unwrap().metric == Metric::Rss);
        assert!(opts(&["--metric", "ipc", "a"]).is_err());
        assert!(opts(&["--metric", "bogus", "a"]).is_err());
    }

    #[test]
    fn counter_metric_auto_enables_counters() {
        assert!(!opts(&["a"]).unwrap().counters);
        assert!(opts(&["--metric", "instructions", "a"]).unwrap().counters);
        // A non-counter metric leaves counters off.
        assert!(!opts(&["--metric", "rss", "a"]).unwrap().counters);
    }

    #[test]
    fn sort_parsing() {
        assert!(opts(&["a"]).unwrap().sort == Sort::Command);
        assert!(opts(&["--sort", "metric", "a"]).unwrap().sort == Sort::Metric);
        assert!(opts(&["--sort", "time", "a"]).is_err());
    }

    #[test]
    fn explicit_jobs() {
        assert_eq!(opts(&["-j", "3", "a"]).unwrap().jobs, 3);
    }

    #[test]
    fn schedule_parsing() {
        assert!(opts(&["a"]).unwrap().schedule == Schedule::Saturate);
        assert!(opts(&["--schedule", "exclusive", "a"]).unwrap().schedule == Schedule::Exclusive);
        assert!(opts(&["--schedule", "fair", "a"]).unwrap().schedule == Schedule::Fair);
        assert!(opts(&["--schedule", "bogus", "a"]).is_err());
    }

    #[test]
    fn schedule_sequential_forces_one_job() {
        let o = opts(&["--schedule", "sequential", "a"]).unwrap();
        assert!(o.schedule == Schedule::Saturate);
        assert_eq!(o.jobs, 1);
        assert!(opts(&["--schedule", "sequential", "-j", "2", "a"]).is_err());
    }

    #[test]
    fn schedule_fair_conflicts_with_target() {
        assert!(opts(&["--schedule", "fair", "--target", "1", "a"]).is_err());
        assert!(opts(&["--schedule", "fair", "-r", "3", "a"]).is_ok());
    }

    #[test]
    fn default_jobs_minus_reserved() {
        // Only meaningful when more than one CPU is available.
        if all_cores() > 1 {
            let o = opts(&["--pin-reserved", "1", "a"]).unwrap();
            assert_eq!(o.jobs, all_cores() - 1);
        }
    }

    #[test]
    fn runs_zero_rejected() {
        // Enforced by clap (NonZeroU64), so parsing itself fails.
        assert!(Cli::try_parse_from(["megafine", "-r", "0", "a"]).is_err());
    }

    #[test]
    fn estimator_defaults_to_mean() {
        assert!(opts(&["a"]).unwrap().estimator == Estimator::Mean);
    }

    #[test]
    fn estimator_parsing() {
        let e = opts(&["--estimator", "p999", "a"]).unwrap().estimator;
        assert!(e == Estimator::Percentile(99.9));
        assert!(opts(&["--estimator", "avg", "a"]).is_err());
    }

    #[test]
    fn target_validation() {
        assert!(opts(&["--target", "0", "a"]).is_err());
        assert!(opts(&["--target", "1", "--estimator", "p90", "a"]).is_err());
        assert_eq!(opts(&["--target", "1", "a"]).unwrap().target, Some(1.0));
    }

    #[test]
    fn raw_needs_two_commands() {
        assert!(opts(&["--raw", "a"]).is_err());
        assert!(opts(&["--raw", "a", "b"]).is_ok());
    }

    #[test]
    fn reference_validation() {
        assert!(opts(&["--reference", "0", "a", "b"]).is_err());
        assert!(opts(&["--reference", "99", "a", "b"]).is_err());
        assert_eq!(opts(&["--reference", "2", "a", "b"]).unwrap().reference, 1);
    }

    #[test]
    fn too_many_command_names() {
        assert!(opts(&["a", "-n", "x", "y"]).is_err());
    }

    #[test]
    fn no_pin_conflicts_with_pin_reserved() {
        // Enforced by clap, so parsing itself fails.
        let argv = ["megafine", "--no-pin", "--pin-reserved", "1", "a"];
        assert!(Cli::try_parse_from(argv).is_err());
    }
}