axbuild 0.4.19

An OS build lib toolkit used by arceos
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
use std::{
    env,
    ffi::OsString,
    fs,
    path::{Path, PathBuf},
    process::Command,
};

use anyhow::{Context, bail};

use super::{
    super::ArgsPerf,
    args_support::{
        effective_callchain, effective_max_depth, host_time_enabled, perf_needs_debuginfo,
        perf_needs_frame_pointers,
    },
    outputs::{PerfOutputs, ensure_file, file_nonempty},
};
use crate::support::process::ProcessExt;

const HARNESS_KIT_REPO: &str = "https://github.com/cg24-THU/tgoskit-harness_kit.git";
const HARNESS_KIT_COMMIT: &str = "762c22725024a065e85b26e0b01121eccea651c0";

pub(super) struct QperfTools {
    pub(super) plugin: PathBuf,
    pub(super) analyzer: PathBuf,
}

pub(super) fn build_qperf_tools(
    root: &Path,
    analyzer_flamegraph: bool,
) -> anyhow::Result<QperfTools> {
    let qperf_root = qperf_source_root(root)?;
    let manifest = qperf_root.join("Cargo.toml");
    let analyzer_manifest = qperf_root.join("analyzer/Cargo.toml");
    let target_dir = qperf_root.join("target");
    if !analyzer_manifest.exists() {
        bail!(
            "qperf analyzer sources not found at {}",
            analyzer_manifest.display()
        );
    }

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

    let mut analyzer_build = Command::new("cargo");
    analyzer_build
        .current_dir(root)
        .args(["build", "--manifest-path"])
        .arg(&analyzer_manifest)
        .arg("--release")
        .arg("--target-dir")
        .arg(&target_dir);
    if analyzer_flamegraph {
        analyzer_build.args(["--features", "flamegraph"]);
    }
    analyzer_build
        .exec()
        .context("failed to build qperf-analyzer")?;

    let release_dir = target_dir.join("release");
    let plugin_name = if cfg!(target_os = "macos") {
        "libqperf.dylib"
    } else {
        "libqperf.so"
    };
    let tools = QperfTools {
        plugin: release_dir.join(plugin_name),
        analyzer: release_dir.join("qperf-analyzer"),
    };
    ensure_file(&tools.plugin, "qperf plugin")?;
    ensure_file(&tools.analyzer, "qperf analyzer")?;
    Ok(tools)
}

fn qperf_source_root(root: &Path) -> anyhow::Result<PathBuf> {
    if let Some(path) = [root.join("apps/qperf"), root.join("tools/qperf")]
        .into_iter()
        .find(|path| path.join("Cargo.toml").exists())
    {
        return Ok(path);
    }

    let checkout = ensure_harness_kit_checkout(root)?;
    let fixed_qperf = checkout.join("tools/qperf");
    if fixed_qperf.join("Cargo.toml").exists() {
        return Ok(fixed_qperf);
    }

    Err(anyhow::anyhow!(
        "qperf sources not found; expected apps/qperf, tools/qperf, or fixed harness kit \
         tools/qperf to be present"
    ))
}

fn ensure_harness_kit_checkout(root: &Path) -> anyhow::Result<PathBuf> {
    if let Some(checkout) = env::var_os("TGOSKIT_HARNESS_KIT_DIR").map(PathBuf::from) {
        validate_harness_kit_override(&checkout)?;
        return Ok(checkout);
    }

    let checkout = root
        .join("target/tgoskit-harness-kit")
        .join(HARNESS_KIT_COMMIT);
    if checkout.join(".git").is_dir() {
        let actual = git_stdout(Some(&checkout), &["rev-parse", "HEAD"])?;
        if actual == HARNESS_KIT_COMMIT {
            return Ok(checkout);
        }
        git_status(
            Some(&checkout),
            &["fetch", "--depth", "1", "origin", HARNESS_KIT_COMMIT],
        )?;
        git_status(
            Some(&checkout),
            &["checkout", "--detach", HARNESS_KIT_COMMIT],
        )?;
        git_status(Some(&checkout), &["reset", "--hard", HARNESS_KIT_COMMIT])?;
        return Ok(checkout);
    }

    let parent = checkout.parent().ok_or_else(|| {
        anyhow::anyhow!("invalid harness kit checkout path {}", checkout.display())
    })?;
    fs::create_dir_all(parent).with_context(|| format!("failed to create {}", parent.display()))?;
    let tmp_name = format!(
        "{}.tmp-{}",
        checkout
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or("harness-kit"),
        std::process::id()
    );
    let tmp_checkout = checkout.with_file_name(tmp_name);
    if tmp_checkout.exists() {
        fs::remove_dir_all(&tmp_checkout)
            .with_context(|| format!("failed to remove {}", tmp_checkout.display()))?;
    }

    let clone_result = (|| -> anyhow::Result<()> {
        git_status(None, &["init", "-q", path_str(&tmp_checkout)?])?;
        git_status(
            Some(&tmp_checkout),
            &["remote", "add", "origin", HARNESS_KIT_REPO],
        )?;
        git_status(
            Some(&tmp_checkout),
            &["fetch", "--depth", "1", "origin", HARNESS_KIT_COMMIT],
        )?;
        git_status(Some(&tmp_checkout), &["checkout", "--detach", "FETCH_HEAD"])?;
        let actual = git_stdout(Some(&tmp_checkout), &["rev-parse", "HEAD"])?;
        if actual != HARNESS_KIT_COMMIT {
            bail!("fetched harness kit {actual}, expected {HARNESS_KIT_COMMIT}");
        }
        Ok(())
    })();

    if clone_result.is_err() {
        let _ = fs::remove_dir_all(&tmp_checkout);
    }
    clone_result?;
    if checkout.exists() {
        fs::remove_dir_all(&checkout)
            .with_context(|| format!("failed to remove {}", checkout.display()))?;
    }
    fs::rename(&tmp_checkout, &checkout).with_context(|| {
        format!(
            "failed to move {} to {}",
            tmp_checkout.display(),
            checkout.display()
        )
    })?;
    Ok(checkout)
}

fn validate_harness_kit_override(checkout: &Path) -> anyhow::Result<()> {
    ensure_file(
        &checkout.join("tools/qperf/Cargo.toml"),
        "TGOSKIT_HARNESS_KIT_DIR qperf manifest",
    )?;
    ensure_file(
        &checkout.join("tools/qperf/analyzer/Cargo.toml"),
        "TGOSKIT_HARNESS_KIT_DIR qperf analyzer manifest",
    )?;
    ensure_file(
        &checkout.join("tools/starry-syscall-harness/harness.py"),
        "TGOSKIT_HARNESS_KIT_DIR harness script",
    )?;

    if !checkout.join(".git").is_dir() {
        bail!(
            "TGOSKIT_HARNESS_KIT_DIR={} is not a git checkout; cannot verify pinned harness kit \
             commit {}. Use a read-only git checkout at the pinned commit, or unset the variable \
             to let xtask manage target/tgoskit-harness-kit",
            checkout.display(),
            HARNESS_KIT_COMMIT
        );
    }

    let actual = git_stdout(Some(checkout), &["rev-parse", "HEAD"])?;
    if actual != HARNESS_KIT_COMMIT {
        bail!(
            "TGOSKIT_HARNESS_KIT_DIR={} is at commit {}, expected {}; the override path is \
             read-only and will not be fetched, reset, or replaced",
            checkout.display(),
            actual,
            HARNESS_KIT_COMMIT
        );
    }
    Ok(())
}

fn git_status(cwd: Option<&Path>, args: &[&str]) -> anyhow::Result<()> {
    let mut command = Command::new("git");
    if let Some(cwd) = cwd {
        command.arg("-C").arg(cwd);
    }
    let status = command
        .args(args)
        .status()
        .with_context(|| format!("failed to run git {}", args.join(" ")))?;
    if !status.success() {
        bail!("git {} failed with {status}", args.join(" "));
    }
    Ok(())
}

fn git_stdout(cwd: Option<&Path>, args: &[&str]) -> anyhow::Result<String> {
    let mut command = Command::new("git");
    if let Some(cwd) = cwd {
        command.arg("-C").arg(cwd);
    }
    let output = command
        .args(args)
        .output()
        .with_context(|| format!("failed to run git {}", args.join(" ")))?;
    if !output.status.success() {
        bail!("git {} failed with {}", args.join(" "), output.status);
    }
    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

fn path_str(path: &Path) -> anyhow::Result<&str> {
    path.to_str()
        .ok_or_else(|| anyhow::anyhow!("path is not valid UTF-8: {}", path.display()))
}

pub(super) fn run_report_postprocess(
    root: &Path,
    outputs: &PerfOutputs,
    args: &ArgsPerf,
    arch: &str,
    returncode: i32,
) -> anyhow::Result<PathBuf> {
    let harness =
        match workspace_harness_path(&outputs.work_dir).or_else(|| workspace_harness_path(root)) {
            Some(harness) => harness,
            None => {
                let checkout = ensure_harness_kit_checkout(root)?;
                let harness = checkout.join("tools/starry-syscall-harness/harness.py");
                ensure_file(&harness, "fixed harness kit postprocess script")?;
                harness
            }
        };
    let python = env::var_os("STARRY_SYSCALL_HARNESS_PYTHON")
        .or_else(|| env::var_os("PYTHON"))
        .unwrap_or_else(|| OsString::from("python3"));
    let mut command = Command::new(python);
    command
        .arg(&harness)
        .arg("perf-postprocess")
        .arg("--repo-root")
        .arg(root)
        .arg("--arch")
        .arg(arch)
        .arg("--work-dir")
        .arg(&outputs.work_dir)
        .arg("--qperf-dir")
        .arg(&outputs.dir)
        .arg("--returncode")
        .arg(returncode.to_string())
        .arg("--timeout")
        .arg(args.timeout.to_string())
        .arg("--format")
        .arg(format!("{:?}", args.format).to_ascii_lowercase())
        .arg("--freq")
        .arg(args.freq.to_string())
        .arg("--max-depth")
        .arg(effective_max_depth(args).to_string())
        .arg("--mode")
        .arg(args.mode.to_string())
        .arg("--callchain")
        .arg(effective_callchain(args).to_string())
        .arg("--top")
        .arg(args.top.to_string())
        .arg("--min-percent")
        .arg(args.min_percent.to_string())
        .arg("--symbol-style")
        .arg(args.symbol_style.to_string())
        .arg("--profile-stdout")
        .arg(&outputs.profile_stdout)
        .arg("--profile-stderr")
        .arg(&outputs.profile_stderr);
    if args.debug {
        command.arg("--debug");
    }
    if args.kernel_filter {
        command.arg("--kernel-filter");
    }
    if host_time_enabled(args) {
        command.arg("--host-time");
    }
    if args.host_perf {
        command
            .arg("--host-perf")
            .arg("--host-perf-events")
            .arg(&args.host_perf_events);
    }
    if let Some(cmd) = &args.shell_init_cmd {
        command.arg("--shell-init-cmd").arg(cmd);
    }
    if let Some(prefix) = &args.shell_prefix {
        command.arg("--shell-prefix").arg(prefix);
    }
    if let Some(marker) = &args.start_marker {
        command.arg("--start-marker").arg(marker);
    }
    if let Some(marker) = &args.stop_marker {
        command.arg("--stop-marker").arg(marker);
    }
    if let Some(timeout) = args.workload_timeout {
        command.arg("--workload-timeout").arg(timeout.to_string());
    }
    if args.qperf_metrics {
        command.arg("--qperf-metrics");
    }
    if args.full_stack {
        command.arg("--full-stack");
    }
    if perf_needs_debuginfo(args) {
        command.arg("--perf-debuginfo");
    }
    if perf_needs_frame_pointers(args) {
        command.arg("--perf-force-frame-pointers");
    }
    if let Some(focus) = &args.focus {
        command.arg("--focus").arg(focus);
    }
    if args.no_truncate {
        command.arg("--no-truncate");
    }
    for qemu_arg in &args.qemu_args {
        command.arg("--qemu-arg").arg(qemu_arg);
    }
    let status = command
        .status()
        .context("failed to run qperf report postprocess")?;
    if !status.success() {
        bail!("qperf report postprocess failed with {status}");
    }
    ensure_report_outputs(outputs)?;
    Ok(harness)
}

fn ensure_report_outputs(outputs: &PerfOutputs) -> anyhow::Result<()> {
    for path in [
        &outputs.report_json,
        &outputs.report_md,
        &outputs.hotspots_csv,
        &outputs.hotspot_categories_csv,
    ] {
        if !file_nonempty(path) {
            bail!(
                "qperf report postprocess did not generate expected artifact: {}",
                path.display()
            );
        }
    }
    Ok(())
}

fn workspace_harness_path(work_dir: &Path) -> Option<PathBuf> {
    let mut current = Some(work_dir);
    while let Some(path) = current {
        for candidate in [
            path.join("apps/OScope-harness/harness.py"),
            path.join("tools/starry-syscall-harness/harness.py"),
        ] {
            if candidate.exists() {
                return Some(candidate);
            }
        }
        current = path.parent();
    }
    None
}