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
//! wprof tracer configuration shipped into auto-repro VMs.
//!
//! [wprof](https://github.com/anakryiko/wprof) is a BSD-3-Clause
//! BPF-based system-wide tracer/profiler that emits Perfetto-format
//! `.pb` traces. ktstr ships the binary at `/bin/wprof` in the guest
//! initramfs when a test enables wprof capture, and the guest init
//! invokes it during auto-repro to produce a trace alongside the
//! standard failure-dump output.
//!
//! The wprof bytes live in the `cargo-ktstr` binary (built by
//! `build.rs`, embedded via `include_bytes!` in
//! `src/bin/cargo_ktstr/blobs.rs`). At cargo-ktstr startup the
//! `install_env` hook extracts the bytes to a tempfile and exports
//! `KTSTR_WPROF_PATH=<path>`; [`WprofConfig::from_env`] reads that
//! env var and loads the file for the builder.
use Result;
/// Minimum guest memory (MiB) for test entries that enable wprof.
///
/// Derivation: `WprofConfig::default_args` requests
/// `--ringbuf-size=256000 --ringbuf-cnt=8`. wprof rounds the
/// 256000 KB request up to the next power of two (262144 KB =
/// 256 MiB) per ringbuf, times 8 ringbufs = 2048 MiB BPF arena.
/// On guests below this, wprof OOM-kills mid-run before it can
/// emit the Perfetto `.pb` trace, producing a truncated artifact
/// and a confused test author.
///
/// Applied as a floor by
/// `crate::test_support::runtime::derive_test_memory_mib` —
/// the single derivation site shared by the entry-derived
/// topology path AND every dispatch site that constructs a
/// `crate::test_support::topo::TopoOverride` from CLI / preset
/// topology (so e.g. `cargo ktstr test --ktstr-topo NnNlNcNt` and
/// gauntlet preset runs honor the floor identically). Both
/// primary and auto-repro VMs route through the same derivation
/// so the floor propagates to both; identical topology between
/// the two VMs is also required for stall reproducibility.
///
/// The floor applies when the derived memory
/// `max(cpus*64, 256, entry.memory_mib)` falls below 2048 MiB
/// AND `entry.wprof` is true. An operator-supplied
/// `crate::test_support::topo::TopoOverride` with explicit
/// `memory_mib` is honored verbatim per the override-is-verbatim
/// contract — a warn-level log fires when the override conflicts
/// with the floor, but the operator's choice wins. Shell-mode VMs
/// (`cargo ktstr shell --kernel ...`) bypass this floor entirely;
/// the operator sets memory size via shell-mode CLI args.
///
/// Tracks `WprofConfig::default_args`: a future change to
/// `--ringbuf-size` or `--ringbuf-cnt` invalidates the 2048
/// derivation and this const should move with the args.
pub const WPROF_MIN_MEMORY_MIB: u32 = 2048;
/// Apply the wprof memory floor to a raw memory size.
///
/// Returns `WPROF_MIN_MEMORY_MIB` when `wprof` is true and `raw_mib`
/// falls below the floor; otherwise returns `raw_mib` unchanged.
/// Pure function — no logging, no side effects — so it can be
/// called from multiple resolution paths without duplicating the
/// floor formula.
///
/// Single source of truth shared by
/// `crate::test_support::runtime::derive_test_memory_mib` (the
/// test-launch path used by `cargo ktstr test`) AND by
/// `cargo ktstr shell --test <NAME>`'s router (which constructs a
/// shell VM matching the named test's topology). A future change
/// to the floor formula updates here once; both call sites pick
/// it up. Inline copies of the `raw < WPROF_MIN_MEMORY_MIB`
/// conditional are a regression per the
/// derive_test_memory_mib/attach_wprof_if_requested precedent.
/// wprof invocation args + binary path. Passed to
/// [`crate::vmm::KtstrVmBuilder::wprof`] when a test (or
/// `cargo ktstr shell`) wants the wprof tracer available inside
/// the guest VM.
///
/// The binary is sourced from a host path (typically the tempfile
/// path that `cargo-ktstr` exports via `KTSTR_WPROF_PATH`). The
/// library packs it at `bin/wprof` in the initramfs using the
/// existing include_files mechanism, which performs `DT_NEEDED`
/// resolution and pulls every shared library wprof links against
/// (libelf, libz, etc.) so the binary actually runs inside the
/// guest.
///
/// The args render to the kernel cmdline as
/// `KTSTR_WPROF_ARGS=<space-joined>`; guest init parses them and
/// invokes `/bin/wprof` with those args during auto-repro.