summaryrefslogtreecommitdiff
path: root/build
diff options
context:
space:
mode:
Diffstat (limited to 'build')
-rw-r--r--build/agent.zig106
1 files changed, 94 insertions, 12 deletions
diff --git a/build/agent.zig b/build/agent.zig
index bdb9317..3fea2e2 100644
--- a/build/agent.zig
+++ b/build/agent.zig
@@ -23,6 +23,10 @@ pub fn add(ctx: Ctx) void {
.optimize = ctx.optimize,
.imports = imports,
.pic = true,
+ // No stack probes: they live in Zig's runtime, which a program
+ // linked by cc does not have (and should not get: with it would
+ // come Zig's memcpy in place of libc's).
+ .stack_check = false,
}) });
b.installArtifact(lib.*);
}
@@ -41,17 +45,18 @@ pub fn add(ctx: Ctx) void {
const fib = ctx.cTarget("agent_fib", &.{"agent_fib.c"});
const sort = ctx.cTarget("agent_sort", &.{"agent_sort.c"});
const sys = ctx.cTarget("agent_sys", &.{"agent_sys.c"});
- for ([_]*Compile{ list, fib, sort, sys }) |exe| b.installArtifact(exe);
- t.expect("list", "100", t.record(list, true, &.{"--alloc"}, &.{"100"}));
- t.expect("fib", "10", t.record(fib, true, &.{ "--hook", "fib" }, &.{"10"}));
- t.expect("sort", "numbers", t.record(sort, true, &.{ "--hook", "swap", "--watch", "numbers" }, &.{}));
- t.expect("syscalls", "", t.record(sys, true, &.{"--syscalls"}, &.{}));
+ const bench = ctx.cTarget("agent_bench", &.{"agent_bench.c"});
+ for ([_]*Compile{ list, fib, sort, sys, bench }) |exe| b.installArtifact(exe);
+ t.expect("list", "100", t.record(list.getEmittedBin(), true, &.{"--alloc"}, &.{"100"}));
+ t.expect("fib", "10", t.record(fib.getEmittedBin(), true, &.{ "--hook", "fib" }, &.{"10"}));
+ t.expect("sort", "numbers", t.record(sort.getEmittedBin(), true, &.{ "--hook", "swap", "--watch", "numbers" }, &.{}));
+ t.expect("syscalls", "", t.record(sys.getEmittedBin(), true, &.{"--syscalls"}, &.{}));
// Everything at once.
- t.expect("list", "100", t.record(list, true, &.{ "--alloc", "--syscalls", "--hook", "main", "--watch", "stdout" }, &.{"100"}));
+ t.expect("list", "100", t.record(list.getEmittedBin(), true, &.{ "--alloc", "--syscalls", "--hook", "main", "--watch", "stdout" }, &.{"100"}));
// Stops: the target held at each return of swap while the tracer reads the array from outside.
- t.expect("sort", "numbers", t.record(sort, true, &.{ "--hook", "swap", "--stop-at", "ret", "--read", "numbers" }, &.{}));
- const asked = t.record(fib, true, &.{ "--hook", "fib", "--stop-at", "call", "-i" }, &.{"3"}).run;
+ t.expect("sort", "numbers", t.record(sort.getEmittedBin(), true, &.{ "--hook", "swap", "--stop-at", "ret", "--read", "numbers" }, &.{}));
+ const asked = t.record(fib.getEmittedBin(), true, &.{ "--hook", "fib", "--stop-at", "call", "-i" }, &.{"3"}).run;
asked.setStdIn(.{ .bytes = "s\n\nx 8 4\nc\n" });
asked.expectStdOutMatch("call tid=");
asked.expectStdOutMatch("not readable");
@@ -74,12 +79,89 @@ pub fn add(ctx: Ctx) void {
for ([_]*Compile{ api, api_static }) |exe| {
exe.root_module.addIncludePath(b.path("include"));
exe.root_module.linkLibrary(libs[1]);
- t.expect("api", "", t.record(exe, false, &.{}, &.{}));
+ t.expect("api", "", t.record(exe.getEmittedBin(), false, &.{}, &.{}));
}
const api_zig = ctx.zigTarget("agent_zig", "targets/agent_zig.zig");
api_zig.root_module.addImport("ztrace-agent", module);
api_zig.use_llvm = true;
- t.expect("api", "", t.record(api_zig, false, &.{}, &.{}));
+ t.expect("api", "", t.record(api_zig.getEmittedBin(), false, &.{}, &.{}));
+
+ // The allocator of a statically linked program, through --wrap: the build
+ // system has no option for it, zig cc does.
+ const wrap = b.addSystemCommand(&.{ b.graph.zig_exe, "cc", "-target", "x86_64-linux-musl", "-static", "-O1", "-g", "-o" });
+ const wrapped = wrap.addOutputFileArg("wrapped/agent_list");
+ wrap.addFileArg(b.path("targets/agent_list.c"));
+ wrap.addArtifactArg(libs[1]);
+ for ([_][]const u8{ "malloc", "calloc", "realloc", "free", "posix_memalign", "aligned_alloc", "memalign", "pthread_create", "fork" }) |name| wrap.addArg(b.fmt("-Wl,-wrap,{s}", .{name}));
+ t.expect("list", "100", t.record(wrapped, false, &.{ "--alloc", "--syscalls" }, &.{"100"}));
+
+ // A recording that reaches its size ends the run, closed and readable.
+ const full = b.addRunArtifact(ctx.exe);
+ full.addArgs(&.{ "run", "--max-size", "64K", "--alloc", "--preload" });
+ full.addArtifactArg(libs[0]);
+ full.addArg("-o");
+ const full_file = full.addOutputFileArg("full.ztrace");
+ full.addArg("--");
+ full.addArtifactArg(list);
+ full.addArg("200000");
+ full.expectStdErrMatch("full at 65536 bytes");
+ full.expectExitCode(1);
+ const full_show = b.addRunArtifact(ctx.exe);
+ full_show.addArg("show");
+ full_show.addFileArg(full_file);
+ full_show.expectStdOutMatch(" alloc");
+ t.step.dependOn(&full_show.step);
+
+ // zig build bench-agent -Doptimize=ReleaseFast: what each capability
+ // costs the target (the loop prints its nanoseconds an iteration) and
+ // what it writes (ztrace prints the recording's size), one after another.
+ const bench_step = b.step("bench-agent", "What the agent costs a malloc, a syscall, a call, a watched region");
+ const P = struct { record: bool = false, preload: bool = true, options: []const []const u8 = &.{}, loop: []const u8, n: []const u8 };
+ var before: ?*std.Build.Step = null;
+ for ([_]?P{
+ null, // the loops alone
+ .{ .options = &.{"--alloc"}, .loop = "malloc", .n = "5000000" },
+ .{ .options = &.{ "--alloc", "--cut", "alloc,free" }, .loop = "malloc", .n = "1000000" },
+ .{ .options = &.{"--syscalls"}, .loop = "syscall", .n = "1000000" },
+ .{ .record = true, .preload = false, .options = &.{"--syscalls"}, .loop = "syscall", .n = "200000" },
+ .{ .options = &.{ "--hook", "work" }, .loop = "call", .n = "1000000" },
+ .{ .options = &.{ "--hook", "work", "--watch", "kb1" }, .loop = "call", .n = "200000" },
+ .{ .options = &.{ "--hook", "work", "--watch", "kb64" }, .loop = "call", .n = "200000" },
+ .{ .options = &.{ "--hook", "work", "--watch", "mb1" }, .loop = "call", .n = "100000" },
+ .{ .options = &.{ "--hook", "work", "--stop-at", "ret" }, .loop = "call", .n = "100000" },
+ .{ .options = &.{ "--hook", "work", "--stop-at", "ret", "--read", "kb1" }, .loop = "call", .n = "100000" },
+ .{ .options = &.{ "--hook", "work", "--stop-at", "ret", "--read", "kb64" }, .loop = "call", .n = "100000" },
+ .{ .options = &.{ "--hook", "work", "--stop-at", "ret", "--read", "mb1" }, .loop = "call", .n = "50000" },
+ }) |case| {
+ const p = case orelse {
+ for ([_][]const u8{ "malloc", "syscall", "call" }) |loop| {
+ const native = b.addRunArtifact(bench);
+ native.addArgs(&.{ loop, "5000000" });
+ native.has_side_effects = true;
+ if (before) |step| native.step.dependOn(step);
+ before = &native.step;
+ }
+ continue;
+ };
+ const echo = b.addSystemCommand(&.{ "echo", if (p.record) "ztrace record" else "ztrace run" });
+ echo.addArgs(p.options);
+ echo.step.dependOn(before.?);
+ const run = b.addRunArtifact(ctx.exe);
+ run.addArgs(&.{ if (p.record) "record" else "run", "-o" });
+ _ = run.addOutputFileArg("bench.ztrace");
+ if (p.preload) {
+ run.addArg("--preload");
+ run.addArtifactArg(libs[0]);
+ }
+ run.addArgs(p.options);
+ run.addArg("--");
+ run.addArtifactArg(bench);
+ run.addArgs(&.{ p.loop, p.n });
+ run.has_side_effects = true;
+ run.step.dependOn(&echo.step);
+ before = &run.step;
+ }
+ bench_step.dependOn(before.?);
for ([_]*Compile{ api, api_static, api_zig }) |exe| b.installArtifact(exe);
}
@@ -92,7 +174,7 @@ const Tests = struct {
check: *Compile,
/// `ztrace run OPTIONS -- TARGET ARGS`, which has to end with the target's exit 0.
- fn record(t: Tests, target: *Compile, preload: bool, options: []const []const u8, args: []const []const u8) Recording {
+ fn record(t: Tests, target: std.Build.LazyPath, preload: bool, options: []const []const u8, args: []const []const u8) Recording {
const run = t.ctx.b.addRunArtifact(t.ctx.exe);
run.addArgs(&.{ "run", "-o" });
const file = run.addOutputFileArg("agent.ztrace");
@@ -102,7 +184,7 @@ const Tests = struct {
}
run.addArgs(options);
run.addArg("--");
- run.addArtifactArg(target);
+ run.addFileArg(target);
run.addArgs(args);
run.expectStdErrMatch("(0 dropped)");
run.expectStdErrMatch("exit 0");