Debian Codegraph @

third_party/rust/memtest/examples/usage.rs

raw
1 use {
2 anyhow::Context,
3 memtest::{Runner, RunnerArgs, TestKind},
4 rand::{seq::SliceRandom, thread_rng},
5 std::{
6 mem::size_of,
7 time::{Duration, Instant},
8 },
9 tracing::info,
10 tracing_subscriber::fmt::format::FmtSpan,
11 };
12
13 // TODO: Command line option for json output
14 fn main() -> anyhow::Result<()> {
15 tracing_subscriber::fmt()
16 .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
17 .with_max_level(tracing::Level::TRACE)
18 .with_writer(std::io::stderr)
19 .with_thread_ids(true)
20 .init();
21 let start_time = Instant::now();
22 let (mem_usize_count, runner_args, test_kinds) = match parse_args() {
23 Ok(parsed_args) => parsed_args,
24 Err(s) => {
25 eprintln!(concat!(
26 "Usage: memtest-runner ",
27 "<memsize in MiB> ",
28 "<timeout in ms> ",
29 "<mem_lock_mode> ",
30 "<allow_working_set_resize as bool> ",
31 "<allow_multithread as bool> ",
32 "<allow_early_temrmination as bool> ",
33 "<test_kinds as space separated string>"
34 ));
35 anyhow::bail!("Invalid/missing argument '{s}'");
36 }
37 };
38
39 info!("Running memtest-runner with: {runner_args:#?}");
40 let mut memory = vec![0; mem_usize_count];
41 let report_list = Runner::from_test_kinds(&runner_args, test_kinds)
42 .run(&mut memory)
43 .context("Failed to run memtest-runner")?;
44 println!("Tester ran for {:?}", start_time.elapsed());
45 println!("Test results: \n{report_list}");
46
47 anyhow::ensure!(
48 report_list.all_pass(),
49 "Found failures or errors among test reports"
50 );
51 Ok(())
52 }
53
54 /// Parse command line arguments to return a usize for the requested memory vector length and
55 /// other Runner arguments
56 fn parse_args() -> Result<(usize, RunnerArgs, Vec<TestKind>), &'static str> {
57 const KIB: usize = 1024;
58 const MIB: usize = 1024 * KIB;
59
60 let mut iter = std::env::args().skip(1);
61
62 macro_rules! parse_next(($n: literal) => {
63 iter.next().and_then(|s| s.parse().ok()).ok_or($n)?
64 });
65
66 let memsize: usize = parse_next!("memsize");
67 let mem_usize_count = memsize * MIB / size_of::<usize>();
68
69 let runner_args = RunnerArgs {
70 timeout: Duration::from_millis(parse_next!("timeout_ms")),
71 mem_lock_mode: parse_next!("mem_lock_mode"),
72 allow_working_set_resize: parse_next!("allow_working_set_resize"),
73 allow_multithread: parse_next!("allow_multithread"),
74 allow_early_termination: parse_next!("allow_early_termination"),
75 };
76
77 let test_kinds_string: String = parse_next!("test_kinds");
78 let test_kinds = test_kinds_from_str(&test_kinds_string)?;
79
80 Ok((mem_usize_count, runner_args, test_kinds))
81 }
82
83 /// Returns a vector of TestKind that contains all kinds, but prioritizes the given test kinds.
84 fn test_kinds_from_str(str: &str) -> Result<Vec<TestKind>, &'static str> {
85 let specified = str
86 .split_whitespace()
87 .map(|s| s.parse().map_err(|_| "test_kinds"))
88 .collect::<Result<Vec<TestKind>, &'static str>>()?;
89
90 let mut remaining: Vec<_> = TestKind::ALL
91 .iter()
92 .filter(|k| !specified.contains(k))
93 .cloned()
94 .collect();
95 remaining.shuffle(&mut thread_rng());
96
97 Ok([specified, remaining].concat())
98 }