Debian Codegraph @

debian/rust-vendor/codspeed/instrument-hooks/AGENTS.md

raw
1 # instrument-hooks
2
3 Zig library that compiles to a single-file C library (`dist/core.c`) for controlling CodSpeed instrumentations via IPC. Consumed as FFI by integrations in multiple languages (Python, Rust, C/C++, etc.).
4
5 ## Build & Test
6
7 ```bash
8 zig build # Build (outputs zig-out/lib/core.linux.c, zig-out/lib/core.macos.c)
9 zig build test --summary all # Run tests
10 just release # Build + generate dist/core.c
11 just cmake-run-example # Build and run the C example via CMake
12 just bazel-run-example # Build and run the C example via Bazel
13 just test-valgrind # Compile example with gcc/clang and run under valgrind
14 just fmt # Format Zig source
15 ```
16
17 Requires **Zig 0.14+**. Optional: [Just](https://github.com/casey/just) for convenience commands.
18
19 ## Architecture
20
21 ### Source layout
22
23 - `src/c.zig` — C FFI export layer. All public API functions are defined here with `pub export fn`.
24 - `src/instrument_hooks.zig` — Main `InstrumentHooks` struct combining all subsystems.
25 - `src/instruments/root.zig` — Instrumentation backend union (valgrind, walltime, analysis, none). Fallback chain: valgrind (Linux only) > analysis > walltime > none.
26 - `src/instruments/{valgrind,walltime,analysis}.zig` — Individual backends. `walltime` subscribes to the `IntegrationMode.Walltime` runner mode (covers any walltime profiler: perf, Instruments, etc.).
27 - `src/runner_fifo.zig` — IPC protocol with the CodSpeed runner via named pipes.
28 - `src/fifo/` — Low-level pipe reader/writer with bincode serialization. `root.zig` dispatches by `builtin.os.tag` to `posix.zig` (Linux + macOS, mkfifo) or `windows.zig` (stub returning `error.Unsupported`; real Win32 named-pipe port pending).
29 - `src/shared.zig` — Protocol types (`Command` union, `MarkerType`, version).
30 - `src/bincode.zig` — Bincode binary serialization (port of qbradley/bincode-zig).
31 - `src/environment/root.zig` — Key-value integration/toolchain metadata and linked library collection, serialized to JSON.
32 - `src/environment/linked_libraries/root.zig` — Collects metadata about dynamically linked libraries (path, soname, build ID, verdef).
33 - `src/environment/linked_libraries/elf_view.zig` — Read-only ELF navigation helper for parsing in-memory program headers (PT_DYNAMIC, PT_NOTE, DT_VERDEF). Portable across all Linux architectures.
34 - `src/environment/linked_libraries/testdata/test_lib.c` — Minimal C source compiled into a test fixture `.so` by `build.zig`. Linked into the test binary so it appears in `dl_iterate_phdr` for ElfView tests.
35 - `src/features.zig` — Runtime feature flags (bit set).
36 - `src/logger.zig` — Simple leveled logging via C `printf`.
37 - `src/utils.zig` — Low-level utilities (`sleep` with Windows/POSIX branches, `print` via C `printf`). The monotonic-clock implementation lives directly in `src/c.zig` with per-OS branches (Linux `clock_gettime`, macOS Mach time, Windows returns 0).
38 - `src/root.zig` — Test runner entry point (imports all modules for testing).
39
40 ### Headers & distribution
41
42 - `includes/core.h` — Manually maintained C API header. Must be updated when adding/removing exported functions.
43 - `includes/compat.h` — Cross-platform compatibility macros.
44 - `includes/valgrind.h`, `includes/callgrind.h` — Valgrind client request headers.
45 - `dist/core.c` — Generated single-file C library with three branches: Linux (`#else`) and macOS (`#elif defined(__APPLE__)`) get the full Zig-transpiled implementation; Windows (`#if defined(_WIN32)`) gets hand-written stubs from `scripts/stub.c`.
46 - `scripts/release.py` — Reads `zig-out/lib/core.linux.c` and `zig-out/lib/core.macos.c`, applies post-processing (struct-name unmangling), and stitches them together with `scripts/stub.c` and the valgrind wrapper into `dist/core.c`.
47 - `scripts/stub.c` — Hand-written Windows-only stubs. Must be updated when adding exported functions until the Zig source compiles for windows targets, after which this file can be dropped.
48
49 ### IPC protocol
50
51 Communication with the CodSpeed runner happens over two named pipes (POSIX FIFOs on Linux/macOS):
52 - `/tmp/runner.ctl.fifo` — Commands (hooks -> runner)
53 - `/tmp/runner.ack.fifo` — Acknowledgments (runner -> hooks)
54
55 Messages use bincode serialization with `[u32 length][payload]` framing. Protocol version: 2.
56
57 `IntegrationMode` (negotiated via `Command.GetIntegrationMode`/`IntegrationModeResponse`):
58 - `Walltime` — any walltime profiler (perf on Linux, Instruments on macOS, etc.).
59 - `Simulation` — runner-side simulation.
60 - `Analysis` — analysis pass.
61
62 Wire format uses ordinal positions, so renaming variants in Zig is safe as long as ordering stays in sync with `runner_shared` on the Rust side.
63
64 ## Conventions
65
66 ### Adding a new exported function
67
68 1. Implement the logic in the appropriate module.
69 2. Add the `pub export fn` wrapper in `src/c.zig`. Return `u8` status codes (0 = success, non-zero = error).
70 3. Add the declaration in `includes/core.h`.
71 4. Add a stub in `scripts/stub.c`.
72 5. Run `just release` to regenerate `dist/core.c`.
73
74 ### Code style
75
76 - Zig code is formatted with `zig fmt`.
77 - C/C++ code follows Google style (`.clang-format`), pointer-left alignment.
78 - Pre-commit hooks enforce formatting and build checks.
79 - Error handling at FFI boundary: return `u8` (0 = ok, 1 = error). Never panic in release builds (`std.debug.no_panic`).
80 - Memory: `std.heap.c_allocator` in production, `std.testing.allocator` in tests.
81 - Hot paths (FIFO read/write) use pre-allocated buffers to avoid allocations.
82
83 ### Testing
84
85 - Tests live alongside the code in each `.zig` file.
86 - `src/root.zig` imports all modules to run all tests.
87 - `build.zig` compiles a test fixture shared library (`libtest_fixture.so`) from `src/environment/linked_libraries/testdata/test_lib.c` and links it into the test binary. ElfView tests use `dl_iterate_phdr` to find it at runtime — no file parsing, same code path as production.
88 - CI tests across multiple platforms (Linux x86/ARM, macOS, Windows), compiler versions (GCC 9-15, Clang 13-19), and cross-compilation targets.
89
90 ### Commit messages
91
92 Follow conventional commits. Reference Linear issues when applicable (inferred from branch name).