//! The kernel area: perf events, USDT, BPF. Its targets, and its tests on them. const std = @import("std"); const Ctx = @import("../build.zig").Ctx; pub fn add(ctx: Ctx) void { const b = ctx.b; const step = ctx.areaTest("test-kernel", "Perf events on real targets, USDT notes against readelf, BPF programs offline"); // The tests run these: they get the paths as `@import("targets")`. const paths = b.addOptions(); var targets: [3]*std.Build.Step.Compile = undefined; inline for (.{ .{ "spin", &.{"kernel_spin.c"} }, .{ "touch", &.{"kernel_touch.c"} }, .{ "usdt", &.{ "kernel_usdt.c", "kernel_usdt_tick.c" } }, }, &targets) |t, *target| { const exe = ctx.cTarget("kernel_" ++ t[0], t[1]); target.* = exe; paths.addOptionPath(t[0], exe.getEmittedBin()); // ... and `zig build` leaves them in zig-out/targets to try things on. b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = "targets" } } }).step); } const tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/trace/kernel_test.zig"), .target = ctx.target, .optimize = ctx.optimize, .imports = &.{ .{ .name = "ztrace", .module = ctx.ztrace }, .{ .name = "targets", .module = paths.createModule() } }, }) }); step.dependOn(&b.addRunArtifact(tests).step); // ... and the library's own tests of this area: what needs no target. step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = ctx.ztrace, .filters = &.{ "perf.test", "usdt.test", "bpf.test" } })).step); // The static probes without root: the debugger stops at each for as // long as it takes to record it. start, four ticks, done. const probed = b.addRunArtifact(ctx.exe); probed.addArgs(&.{ "record", "--by", "break", "-b", "usdt:ztrace:*", "-o" }); const probes = probed.addOutputFileArg("usdt.ztrace"); probed.addArg("--"); probed.addArtifactArg(targets[2]); probed.addArg("4"); probed.expectStdErrMatch("exit 0"); const shown = b.addRunArtifact(ctx.exe); shown.addArg("show"); shown.addFileArg(probes); shown.expectStdOutMatch("6 probe"); step.dependOn(&shown.step); // The BPF side for real: this machine's kernel booted in QEMU, where we // are root. Not part of `test`: it wants qemu, /dev/kvm, cpio and a // kernel image a user can read. const vm = b.addSystemCommand(&.{"sh"}); vm.addFileArg(b.path("build/kernel_vm.sh")); vm.addArtifactArg(ctx.zigTarget("kernel_vminit", "targets/kernel_vminit.zig")); vm.addFileArg(b.addWriteFiles().add("commands", \\/ztrace probe -o /touch.ztrace --syscalls --sched --kprobe do_sys_openat2 --uprobe step --uprobe /usr/lib/libc.so.6:malloc -- /kernel_touch 4 3 \\/ztrace dump /touch.ztrace \\/ztrace probe -o /usdt.ztrace --usdt ztrace:start --usdt ztrace:tick --usdt ztrace:done -- /kernel_usdt 4 \\/ztrace dump /usdt.ztrace \\ )); vm.addArtifactArg(ctx.exe); for (targets[1..]) |target| vm.addArtifactArg(target); // Nothing lost; a syscall (clock_nanosleep) and a switch; malloc(1003) // in libc; step(1002, 2001) in the thread; tick(3, 14) with its semaphore. for ([_][]const u8{ "(0 lost)", " syscall_enter ", " aux=230 ", " sched ", " a=f166cdb822920ddd 3eb ", " a=a02712eb14a9b985 3ea 7d1 ", " a=ff4909df02928d94 3 e ", }) |expected| vm.expectStdOutMatch(expected); b.step("test-kernel-vm", "Run the BPF programs as root in a QEMU VM of this kernel").dependOn(&vm.step); }