axbuild 0.4.6

An OS build lib toolkit used by arceos
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
use std::{
    env, fs,
    fs::File,
    io::{BufRead, BufReader, Write},
    path::{Path, PathBuf},
    process::{Command, ExitStatus, Stdio},
};

use anyhow::{Context, bail};
use serde::{Deserialize, Serialize};

use super::{ArgsBuild, ArgsPerf, PerfFormat, Starry, build, rootfs};
use crate::{
    context::{SnapshotPersistence, StarryCliArgs, starry_target_for_arch_checked},
    support::process::ProcessExt,
};

const QPERF_QUEUE_SIZE: usize = 4096;

#[derive(Deserialize, Serialize)]
struct PerfQemuConfig {
    args: Vec<String>,
    uefi: bool,
    to_bin: bool,
    success_regex: Vec<String>,
    fail_regex: Vec<String>,
    shell_prefix: Option<String>,
    shell_init_cmd: Option<String>,
    timeout: Option<u64>,
}

struct QperfTools {
    plugin: PathBuf,
    analyzer: PathBuf,
}

struct PerfOutputs {
    dir: PathBuf,
    raw: PathBuf,
    folded: PathBuf,
    flamegraph: PathBuf,
    summary: PathBuf,
    qemu_config: PathBuf,
}

pub(super) async fn run(starry: &mut Starry, args: ArgsPerf) -> anyhow::Result<()> {
    validate_args(&args)?;
    let arch = args
        .arch
        .clone()
        .unwrap_or_else(|| crate::context::DEFAULT_STARRY_ARCH.to_string());
    let target = starry_target_for_arch_checked(&arch)?.to_string();
    let outputs = prepare_outputs(starry.app.workspace_root(), &arch, args.out.as_deref())?;

    let tools = build_qperf_tools(starry.app.workspace_root())?;

    let build_args = ArgsBuild {
        config: None,
        arch: Some(arch.clone()),
        target: None,
        smp: None,
        debug: true,
    };
    let request = starry.prepare_request(
        StarryCliArgs::from(&build_args),
        None,
        None,
        SnapshotPersistence::Store,
    )?;

    let cargo = build::load_cargo_config(&request)?;
    starry.app.set_debug_mode(true)?;
    starry
        .app
        .build(cargo, request.build_info_path.clone())
        .await?;
    rootfs::ensure_qemu_rootfs_ready(&request, starry.app.workspace_root(), None).await?;
    let cargo = build::load_cargo_config(&request)?;
    let qemu = rootfs::load_patched_qemu_config(starry, &request, &cargo, None, true).await?;
    write_qemu_config(&outputs, &tools, &args, qemu.args)?;

    let kernel_bin = kernel_bin_path(starry.app.workspace_root(), &target);
    let qemu_status = run_qemu_direct(&outputs, &args, &arch, &kernel_bin)?;
    if !qemu_status.success() {
        if !file_nonempty(&outputs.raw) {
            bail!("qperf QEMU run failed before producing samples: {qemu_status}");
        }
        eprintln!("qperf: QEMU ended with {qemu_status} after producing samples");
    }

    let elf = kernel_elf_path(starry.app.workspace_root(), &target);
    run_analyzer(&tools.analyzer, &elf, &outputs.raw, &outputs.folded)?;

    let flamegraph_generated = if matches!(args.format, PerfFormat::Svg | PerfFormat::All) {
        try_generate_flamegraph(&outputs.folded, &outputs.flamegraph)?
    } else {
        false
    };

    write_summary(
        &outputs,
        &tools,
        &elf,
        &arch,
        &target,
        &args,
        flamegraph_generated,
    )?;
    print_report(&outputs, flamegraph_generated);
    Ok(())
}

fn validate_args(args: &ArgsPerf) -> anyhow::Result<()> {
    if args.freq == 0 {
        bail!("--freq must be greater than 0");
    }
    if args.max_depth == 0 {
        bail!("--max-depth must be greater than 0");
    }
    if matches!(args.format, PerfFormat::Pprof) {
        bail!("--format pprof is not supported yet; use --format folded, svg, or all");
    }
    Ok(())
}

fn prepare_outputs(root: &Path, arch: &str, out: Option<&Path>) -> anyhow::Result<PerfOutputs> {
    let dir = out.map(PathBuf::from).unwrap_or_else(|| {
        root.join("target")
            .join("qperf")
            .join(arch)
            .join(chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string())
    });
    fs::create_dir_all(&dir)
        .with_context(|| format!("failed to create qperf output directory {}", dir.display()))?;
    Ok(PerfOutputs {
        raw: dir.join("qperf.bin"),
        folded: dir.join("stack.folded"),
        flamegraph: dir.join("flamegraph.svg"),
        summary: dir.join("summary.txt"),
        qemu_config: dir.join("qemu.toml"),
        dir,
    })
}

fn build_qperf_tools(root: &Path) -> anyhow::Result<QperfTools> {
    let manifest = root.join("tools/qperf/Cargo.toml");
    if !manifest.exists() {
        bail!(
            "qperf sources not found at {}; expected tools/qperf to be present",
            manifest.display()
        );
    }

    Command::new("cargo")
        .current_dir(root)
        .args(["build", "--manifest-path"])
        .arg(&manifest)
        .arg("--release")
        .exec()
        .context("failed to build qperf plugin")?;

    Command::new("cargo")
        .current_dir(root)
        .args(["build", "--manifest-path"])
        .arg(&manifest)
        .args(["--release", "-p", "qperf-analyzer"])
        .exec()
        .context("failed to build qperf-analyzer")?;

    let release_dir = root.join("tools/qperf/target/release");
    let tools = QperfTools {
        plugin: release_dir.join("libqperf.so"),
        analyzer: release_dir.join("qperf-analyzer"),
    };
    ensure_file(&tools.plugin, "qperf plugin")?;
    ensure_file(&tools.analyzer, "qperf analyzer")?;
    Ok(tools)
}

fn write_qemu_config(
    outputs: &PerfOutputs,
    tools: &QperfTools,
    args: &ArgsPerf,
    qemu_args: Vec<String>,
) -> anyhow::Result<()> {
    let mut perf_qemu_args = vec!["-plugin".to_string()];
    perf_qemu_args.push(format!(
        "{},freq={},max_depth={},queue_size={},out={}",
        tools.plugin.display(),
        args.freq,
        args.max_depth,
        QPERF_QUEUE_SIZE,
        outputs.raw.display()
    ));
    perf_qemu_args.extend(qemu_args);

    let config = PerfQemuConfig {
        args: perf_qemu_args,
        uefi: false,
        to_bin: true,
        success_regex: Vec::new(),
        fail_regex: vec![r"(?i)\bpanic(?:ked)?\b".to_string()],
        shell_prefix: None,
        shell_init_cmd: None,
        timeout: (args.timeout > 0).then_some(args.timeout),
    };
    fs::write(&outputs.qemu_config, toml::to_string_pretty(&config)?)
        .with_context(|| format!("failed to write {}", outputs.qemu_config.display()))?;
    Ok(())
}

fn run_qemu_direct(
    outputs: &PerfOutputs,
    args: &ArgsPerf,
    arch: &str,
    kernel_bin: &Path,
) -> anyhow::Result<ExitStatus> {
    ensure_file(kernel_bin, "StarryOS kernel image")?;
    let qemu = qemu_executable(arch)?;
    let qemu_args = qemu_args_from_config(&outputs.qemu_config)?;

    let mut command = if args.timeout > 0 {
        let mut command = Command::new("timeout");
        command.arg(format!("{}s", args.timeout));
        command.arg(qemu);
        command
    } else {
        Command::new(qemu)
    };

    command.args(qemu_args).arg("-kernel").arg(kernel_bin);
    eprintln!("running qperf QEMU: {command:?}");
    command.status().context("failed to spawn QEMU")
}

fn qemu_executable(arch: &str) -> anyhow::Result<&'static str> {
    match arch {
        "riscv64" => Ok("qemu-system-riscv64"),
        "loongarch64" => Ok("qemu-system-loongarch64"),
        _ => bail!("qperf currently supports StarryOS riscv64 and loongarch64 only"),
    }
}

fn qemu_args_from_config(path: &Path) -> anyhow::Result<Vec<String>> {
    let text = fs::read_to_string(path)
        .with_context(|| format!("failed to read qperf QEMU config {}", path.display()))?;
    let config: PerfQemuConfig = toml::from_str(&text)
        .with_context(|| format!("failed to parse qperf QEMU config {}", path.display()))?;
    Ok(config.args)
}

fn run_analyzer(analyzer: &Path, elf: &Path, raw: &Path, folded: &Path) -> anyhow::Result<()> {
    ensure_file(elf, "StarryOS kernel ELF")?;
    ensure_file(raw, "qperf raw samples")?;
    Command::new(analyzer)
        .arg("-e")
        .arg(elf)
        .arg(raw)
        .arg(folded)
        .exec()
        .context("failed to run qperf-analyzer")?;
    ensure_file(folded, "folded stack output")?;
    Ok(())
}

fn try_generate_flamegraph(folded: &Path, svg: &Path) -> anyhow::Result<bool> {
    let Some(generator) = find_flamegraph_generator() else {
        eprintln!(
            "qperf: flamegraph generator not found; install inferno-flamegraph or flamegraph.pl \
             to generate {}",
            svg.display()
        );
        return Ok(false);
    };

    let input =
        File::open(folded).with_context(|| format!("failed to open {}", folded.display()))?;
    let output =
        File::create(svg).with_context(|| format!("failed to create {}", svg.display()))?;
    let status = Command::new(&generator)
        .stdin(Stdio::from(input))
        .stdout(Stdio::from(output))
        .status()
        .with_context(|| format!("failed to run {}", generator.display()))?;
    if !status.success() {
        eprintln!(
            "qperf: flamegraph generator {} failed with {status}; folded stacks are still at {}",
            generator.display(),
            folded.display()
        );
        return Ok(false);
    }
    Ok(true)
}

fn find_flamegraph_generator() -> Option<PathBuf> {
    ["inferno-flamegraph", "flamegraph", "flamegraph.pl"]
        .into_iter()
        .find_map(find_executable)
}

fn find_executable(name: &str) -> Option<PathBuf> {
    let path = Path::new(name);
    if path.components().count() > 1 && path.is_file() {
        return Some(path.to_path_buf());
    }
    env::var_os("PATH").and_then(|paths| {
        env::split_paths(&paths)
            .map(|dir| dir.join(name))
            .find(|candidate| candidate.is_file())
    })
}

fn write_summary(
    outputs: &PerfOutputs,
    tools: &QperfTools,
    elf: &Path,
    arch: &str,
    target: &str,
    args: &ArgsPerf,
    flamegraph_generated: bool,
) -> anyhow::Result<()> {
    let folded_lines = count_lines(&outputs.folded).unwrap_or(0);
    let plugin_summary = outputs.raw.with_extension("summary.txt");
    let plugin_summary_text = fs::read_to_string(plugin_summary).ok();

    let mut file = File::create(&outputs.summary)
        .with_context(|| format!("failed to create {}", outputs.summary.display()))?;
    writeln!(file, "qperf_format_version = 1")?;
    writeln!(file, "arch = {arch}")?;
    writeln!(file, "target = {target}")?;
    writeln!(file, "frequency_hz = {}", args.freq)?;
    writeln!(file, "max_stack_depth = {}", args.max_depth)?;
    writeln!(file, "queue_size = {QPERF_QUEUE_SIZE}")?;
    writeln!(file, "timeout_seconds = {}", args.timeout)?;
    writeln!(file, "kernel_elf = {}", elf.display())?;
    writeln!(file, "plugin = {}", tools.plugin.display())?;
    writeln!(file, "analyzer = {}", tools.analyzer.display())?;
    writeln!(file, "raw_samples = {}", outputs.raw.display())?;
    writeln!(file, "folded_stack = {}", outputs.folded.display())?;
    writeln!(file, "folded_stack_lines = {folded_lines}")?;
    writeln!(file, "flamegraph_generated = {flamegraph_generated}")?;
    if flamegraph_generated {
        writeln!(file, "flamegraph = {}", outputs.flamegraph.display())?;
    }
    if let Some(plugin_summary_text) = plugin_summary_text {
        writeln!(file)?;
        writeln!(file, "[plugin_summary]")?;
        write!(file, "{plugin_summary_text}")?;
    } else {
        writeln!(file, "dropped_samples = unknown")?;
        writeln!(
            file,
            "plugin_summary = unavailable; QEMU may have been stopped by timeout before plugin \
             shutdown"
        )?;
    }
    Ok(())
}

fn print_report(outputs: &PerfOutputs, flamegraph_generated: bool) {
    println!("qperf report generated:");
    println!("  output dir: {}", outputs.dir.display());
    println!("  raw samples: {}", outputs.raw.display());
    println!("  folded stack: {}", outputs.folded.display());
    if flamegraph_generated {
        println!("  flamegraph: {}", outputs.flamegraph.display());
    }
    println!("  summary: {}", outputs.summary.display());
}

fn kernel_elf_path(root: &Path, target: &str) -> PathBuf {
    root.join("target")
        .join(target)
        .join("debug")
        .join("starryos")
}

fn kernel_bin_path(root: &Path, target: &str) -> PathBuf {
    root.join("target")
        .join(target)
        .join("debug")
        .join("starryos.bin")
}

fn ensure_file(path: &Path, label: &str) -> anyhow::Result<()> {
    if file_nonempty(path) {
        Ok(())
    } else {
        bail!("{label} not found or empty at {}", path.display())
    }
}

fn file_nonempty(path: &Path) -> bool {
    path.metadata().map(|meta| meta.len() > 0).unwrap_or(false)
}

fn count_lines(path: &Path) -> anyhow::Result<u64> {
    let file = File::open(path).with_context(|| format!("failed to open {}", path.display()))?;
    let mut count = 0;
    for line in BufReader::new(file).lines() {
        line?;
        count += 1;
    }
    Ok(count)
}