From a10e1691dbe5b944812dc8761a450636bc247e3d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 12:07:53 -0300 Subject: agent: bench-agent and its numbers, hooks with names, an archive cc can link zig build bench-agent runs each capability against its native loop and prints what it costs and writes; README has the table. ztrace run looks at the ring a little longer before sleeping (a target that finds the tracer asleep pays a futex wake an event). A hooked function's name is defined as the string of its address, so the recorder's call stack reads fib. A static link without libc's malloc says which -u to add. The archive carries no stack probes, so cc links it in debug builds too. Tests for --wrap (musl, zig cc) and for a recording that fills. Co-Authored-By: Claude Fable 5.2 --- targets/agent_bench.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 targets/agent_bench.c (limited to 'targets') diff --git a/targets/agent_bench.c b/targets/agent_bench.c new file mode 100644 index 0000000..e1add0e --- /dev/null +++ b/targets/agent_bench.c @@ -0,0 +1,45 @@ +// Loops to measure the agent with: `agent_bench WHAT N` runs one N times +// and prints the nanoseconds an iteration took. +// malloc a malloc and a free +// syscall a getppid +// call a call of `work`, which writes one word of `kb64` +#include +#include +#include +#include +#include +#include + +// Memory to watch, of three sizes. +long kb1[1 << 7], kb64[1 << 13], mb1[1 << 17]; + +__attribute__((noinline)) long work(long i) { + kb64[i & 8191] = i; + return i * 3; +} + +static double now(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * 1e9 + ts.tv_nsec; +} + +int main(int argc, char **argv) { + if (argc < 3) return 2; + long n = atol(argv[2]); + long sum = 0; + void *slots[64] = {0}; + double start = now(); + if (!strcmp(argv[1], "malloc")) { + for (long i = 0; i < n; i++) { + free(slots[i & 63]); + slots[i & 63] = malloc(16 + (i & 255)); + } + } else if (!strcmp(argv[1], "syscall")) { + for (long i = 0; i < n; i++) sum += syscall(SYS_getppid); + } else if (!strcmp(argv[1], "call")) { + for (long i = 0; i < n; i++) sum += work(i); + } else return 2; + printf("%s: %.1f ns each (%ld)\n", argv[1], (now() - start) / n, sum & 1); + return 0; +} -- cgit v1.3