diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 11:39:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 12:19:06 -0300 |
| commit | db923fa823354270217362db06356f2b1a41315e (patch) | |
| tree | 676ad2dbfd59f0677200d85616ef1ed14d058981 /targets | |
| parent | a7504c6bb2ec9e906891e42bc3df28b276e67143 (diff) | |
| download | computeranim-db923fa823354270217362db06356f2b1a41315e.tar.gz computeranim-db923fa823354270217362db06356f2b1a41315e.zip | |
ztrace perf: the kernel's view of a program, unprivileged
perf_event_open on a held child: counters, a sampling profile, every
page fault with its registers, maps, threads, context switches, every
syscall (the per-syscall tracepoints, the only ones open to a user),
and the debug registers as watchpoints and breakpoints that do not stop
the target. One time-ordered stream of Events into a Sink; --threads
follows with a ring per CPU. usdt.zig: the notes of <sys/sdt.h> and
their arguments from a Regs and a memory reader, independent of BPF.
test-kernel runs all of it on targets/kernel_*.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'targets')
| -rw-r--r-- | targets/kernel_spin.c | 10 | ||||
| -rw-r--r-- | targets/kernel_touch.c | 45 | ||||
| -rw-r--r-- | targets/kernel_usdt.c | 15 | ||||
| -rw-r--r-- | targets/kernel_usdt_tick.c | 15 |
4 files changed, 85 insertions, 0 deletions
diff --git a/targets/kernel_spin.c b/targets/kernel_spin.c new file mode 100644 index 0000000..206b0bd --- /dev/null +++ b/targets/kernel_spin.c @@ -0,0 +1,10 @@ +// A known amount of work: a loop of exactly four instructions, N times +// (argv[1], default ten million). What the counters are checked against, +// and the run that tracing overhead is measured on. +#include <stdlib.h> + +int main(int argc, char **argv) { + unsigned long n = argc > 1 ? strtoul(argv[1], 0, 0) : 10000000; + __asm__ volatile("1: dec %0\n nop\n nop\n jnz 1b" : "+r"(n) : : "cc"); + return 0; +} diff --git a/targets/kernel_touch.c b/targets/kernel_touch.c new file mode 100644 index 0000000..b2db1ce --- /dev/null +++ b/targets/kernel_touch.c @@ -0,0 +1,45 @@ +// One of everything the kernel can report about a program, in this order: +// fresh pages touched one by one (argv[1] of them, default 64), a global +// written and a function called (argv[2] times, default 100), a file opened +// and written, a sleep, a thread that calls the function three more times. +#include <fcntl.h> +#include <pthread.h> +#include <stdlib.h> +#include <sys/mman.h> +#include <time.h> +#include <unistd.h> + +volatile unsigned long counter; + +__attribute__((noinline)) unsigned long step(unsigned long i, unsigned long acc) { + counter = acc + i; + return acc + i; +} + +static void *worker(void *arg) { + unsigned long acc = 0; + for (unsigned long i = 1000; i < 1003; i++) acc = step(i, acc); + return arg; +} + +int main(int argc, char **argv) { + unsigned long pages = argc > 1 ? strtoul(argv[1], 0, 0) : 64; + unsigned long steps = argc > 2 ? strtoul(argv[2], 0, 0) : 100; + + volatile char *fresh = mmap(0, pages * 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + for (unsigned long i = 0; i < pages; i++) fresh[i * 4096] = 1; + + unsigned long acc = 0; + for (unsigned long i = 0; i < steps; i++) acc = step(i, acc); + + int fd = open("/dev/null", O_WRONLY); + if (write(fd, "hello", 5) != 5) return 1; + close(fd); + + nanosleep(&(struct timespec){.tv_nsec = 2000000}, 0); + + pthread_t thread; + pthread_create(&thread, 0, worker, 0); + pthread_join(thread, 0); + return acc == steps * (steps - 1) / 2 ? 0 : 1; +} diff --git a/targets/kernel_usdt.c b/targets/kernel_usdt.c new file mode 100644 index 0000000..d5b48b0 --- /dev/null +++ b/targets/kernel_usdt.c @@ -0,0 +1,15 @@ +// Static probes the DTrace way: <sys/sdt.h> leaves a nop and an ELF note +// where each one is. `start` and `done` are always there; `tick` (in +// kernel_usdt_tick.c) hides behind a semaphore until a tracer raises it. +#include <stdlib.h> +#include <sys/sdt.h> + +long work(long n); + +int main(int argc, char **argv) { + long n = argc > 1 ? atol(argv[1]) : 5; + DTRACE_PROBE1(ztrace, start, n); + long sum = work(n); + DTRACE_PROBE2(ztrace, done, n, sum); + return 0; +} diff --git a/targets/kernel_usdt_tick.c b/targets/kernel_usdt_tick.c new file mode 100644 index 0000000..29a7ed8 --- /dev/null +++ b/targets/kernel_usdt_tick.c @@ -0,0 +1,15 @@ +// The probe of kernel_usdt.c that has a semaphore: a file is all with or +// all without. +#define _SDT_HAS_SEMAPHORES 1 +#include <sys/sdt.h> + +unsigned short ztrace_tick_semaphore __attribute__((section(".probes"))); + +long work(long n) { + long sum = 0; + for (long i = 0; i < n; i++) { + sum += i * i; + if (ztrace_tick_semaphore) DTRACE_PROBE2(ztrace, tick, i, sum); + } + return sum; +} |
