Ep21: The Rules of the Project
This episode is a reference, not a story. It collects the rules Nusku's code follows, so that new code reads like the old, and so that adding a feature means following a recipe instead of redesigning. When in doubt, look for the closest existing example and do the same.
It is organised by question:
- How the project is shaped and where new code goes.
- The rule of the layers.
- How an API looks.
- How code looks: names, size, assertions, limits, errors, memory.
- How comments look.
- How logs look.
- How tests look.
- How to build, test and commit.
- Recipes: a metric, a layer, a protocol type, a query, a command, a transport.
- The checklist before calling something done.
1. The shape
shared/ what every module uses: the protocol, ELF, files
agent/ profiles a process with eBPF, watches every process; sends raw addresses
server/ keeps the reports, names them with the binaries' symbols, answers with profiles
client/ how a program reaches a server: shared by the CLI and the app
src/ the nusku program: commands and views
cli/ the command-line parser src/ is built on
logger/ the logger every module logs through
testing/ small C, Go and Zig apps with known profiles, and the Makefile that checks them
app/ the desktop app, nusku-app: a second client, with its own build that imports
the root package (client, shared) through its build.zig.zonEach module has one way in:
| Module | What it exposes |
|---|---|
agent | Run (init · deinit · next · stop · binary_want) and Watch (init · deinit · next · stop) |
server | Server (feed · watch_take · profile · processes), Feed (take · close), Controller (start · stop · wait · running · interrupt) |
shared | protocol, elf, File, directory |
client | profile · processes · processes_watch · stop |
src | the nusku program |
app | the nusku-app program |
What crosses between the agent and the server is a shared/protocol type, never an agent or a server type. The agent never imports the server; the server imports the agent only in its control layer, the local client.
A shared type is imported from shared, by every module, the CLI too. No module re-exports it: server.ProcessFrame would make the CLI depend on the server for a type the server does not own, and hide where the type lives. The CLI writes @import("shared").protocol.ProcessFrame; it takes from server only what the server defines (Profile, Frame, Controller, ...).
2. The rule of the layers
Every module is made of layers. A layer is a folder, and its facade is the file of the same name in it: target/target.zig. A module's root holds only its parent files (root.zig, run.zig, main.zig, ...) and log.zig; every layer, os too, is a folder.
agent/
root.zig run.zig watch.zig message.zig log.zig the parent
os/os.zig os/process.zig os/timer.zig ... a toolbox
target/target.zig target/regions.zig ... a layer
metrics/metrics.zig metrics/cpu/ metrics/bpf/ ... a layerA layer is named for what it gives, not for how it works. metrics gives the metrics; that it does so with BPF programs is its own business, and a name like ebpf would make a reader wonder why it imports unwind. Read the name next to its imports: if one looks out of place, the name is wrong, not the import.
- The file named like its folder is the facade. It is the only file of the layer another layer may import. It holds the layer's one struct, with
init,deinitand one to four functions that do the job, plus the few types that struct takes and gives. A layer of small unrelated tools (os,elf,term,view) is a toolbox: its facade lists the tools by name instead. - The folder is the layer's own business. Its files import each other freely.
- A layer imports only layers below it, and only their facades.
- Layers of the same rank never import each other. They do not know the others exist.
- Only the parent puts layers together: files in no layer (
run.zig,root.zig,main.zig) may import every facade. - Anyone may import
log.zig, thestd,sharedand other named modules, and test helpers intesting/.
The ranks today, bottom up:
| agent | server | src |
|---|---|---|
0 os | 0 os | 0 term |
1 elf | 1 windows, symbols, processes | 1 client, view |
2 target | 2 naming | 2 cmd |
3 unwind | 3 server | parent: main.zig |
4 metrics, symbols, processes | 4 control | |
parent: run, watch, message, root | parent: root.zig |
The rule is kept by review, not by a test. An import reads as a path, so it shows what it crosses: @import("../target/target.zig") is a facade; @import("../target/regions.zig") from another layer goes past one, and @import("../symbols/symbols.zig") from metrics/ reaches beside. Look at the imports of every file you touch.
When the rule seems to get in the way, it is usually saying something true:
- Two layers beside each other need the same thing: that thing is a layer below both. (The ELF reader was in
unwind/andsymbols/borrowed it; it becameelf.) - A layer needs something from a layer beside it: the parent should pass it. (
Symbolsgets the vDSO image from theTargetthe parent hands it, not frommetrics.) - A test needs another layer's insides: use the facade. (A test needing this process's regions calls
Target.init(gpa, current_pid).)
3. How an API looks
init and deinit
initchecks everything it can before allocating or loading anything, and refuses a bad option with an error named after it:InvalidPid,InvalidInterval,NoMetrics.initreturns the value (Run.init(gpa, options) !Run) when the struct may move. It takes a pointer (metrics.init(gpa, …) !void) when something points into it (a thread, a collector, a ring reader); its doc says it must stay at its address untildeinit.deinitnever fails, frees in the reverse order ofinit, and ends withx.* = undefined.- Every step of
initthat holds something is followed by itserrdefer.
Pull, not callbacks
Something that produces a stream is pulled: next() returns the next event, waiting for it, and null after the last. The caller owns the loop, on whatever thread it likes, and switches over a union: the compiler checks every event is handled. stop() ends the stream at its next wait, and is safe from any thread and from a signal handler (one eventfd write).
var run = try agent.Run.init(gpa, .{ .pid = pid });
defer run.deinit();
while (try run.next()) |event| switch (event) { ... };Something that consumes a stream takes one event and gives back what to answer: Feed.take(event) !Answer. No comptime hooks, no context pointers.
Lifetimes are said, every time
Every value that points into someone's buffers says how long it lives, in its doc: "valid until the next next", "points into arena", "good until the next put". The caller copies what it keeps (Message.clone). A function that copies says where: "copied into arena".
Options
Options are a struct with defaults for everything but what has no sensible default (a pid). Each field says its unit and what 0 means: duration_s: u32 = 0, // 0: until the target exits.
4. How code looks
Names
snake_casefor functions, variables and fields;PascalCasefor types; filessnake_case.- The noun first, then the verb:
regions_see,target_add,binary_want,kernel_read,window_resolve. Related functions sort together. - Units in the name:
_ns,_ms,_s,_bytes,_kib,_mib. A time isstart_unix_nsorwindow_start_monotonic_ns: which clock, too. - Limits end in
_max, counts in_count:binaries_max,pending_count. - No abbreviations a reader must decode:
previous, notprev;index, notidx. Short names for short scopes only. - A parameter is named after its type:
run: *Run,symbols: *Symbols, notself.
Size
- Lines of at most 100 columns. Break a long call into a local or one argument per line.
- Functions of at most 70 lines. Past that, split by what the parts do, and name them.
zig fmton everything.
Assertions
Assert what must be true, at both ends: arguments on the way in, the result on the way out, and the same fact on both sides of a boundary (the pair assertion: the writer asserts what it writes, the reader asserts what it reads). An assertion is not error handling: a bad input from outside is an error; a broken invariant inside is an assertion. The agent and the server have over 200.
assert(transfer.stage != .done);
assert(buffer.len > 0);
...
assert(filled == count);Limits
Everything is bounded, and every bound is a named constant with the reason for its number:
/// At 99 Hz on 64 CPUs a tick sees at most 634 samples: the ring cannot fill between two.
const tick_ms_max = 100;
/// Bytes of /proc/kallsyms read per loop round: about 12 ms of the kernel's time. Read in one
/// go, it would hold up the loop for more than half a second.
pub const kallsyms_bytes_per_round = 256 * 1024;- Every loop is bounded or waits. An event loop waits on poll every round, so it cannot spin.
- Work per round is bounded: a few chunks per tick, a piece of a file per step. Nothing holds a loop for longer than its tick.
- Full means refuse, not grow: a queue that is full refuses, and says so once.
Memory
- Allocate at
init; not per report, per sample or per frame. A ring of fixed slots, a buffer sized once. - Per-window memory lives in an arena reset with
.retain_capacity: a steady target stops allocating after the first window. - Large structs go on the heap (
gpa.create), not the stack. - Tests use
testing.allocator, so a leak fails the test.
Errors
- A part failing drops that part, not the whole. A binary that cannot be sent is dropped and the run goes on; a window too big is counted and the run goes on. Only what makes the whole meaningless stops it.
- Errors are named for what happened:
ProcessNotFound,WindowTooLarge,AlreadyKnown. - Everything from outside is untrusted and checked: a file's offsets, an upload's chunks against its header,
/proc's lines.
Small things
- No commented-out code, no
TODOleft in a commit. - No global state, except what a signal handler must reach (and then written outside the handler only).
5. How comments look
Comments are sentences, in plain words, and say what and why, not how; the code says how.
- Every file starts with
//!: what it is, in a line or two, and for a facade the folder's files with one line each. A facade's header shows how to use it, as code. - Every public declaration has
///saying what it is or does, and anything a caller must know: lifetimes, what null means, errors that matter, whether it is safe from another thread. - A number gets its reason, a choice gets its alternative: "99, not 100, so sampling does not run in lockstep with work on a 10 ms timer".
- No "this function…", no restating the name:
/// Drops a binary still arriving.not/// This function deinitializes the receiver. - Short. A comment that needs a paragraph is often a design that needs a devlog.
//! One binary on its way: `step` gives its next part, the header, a chunk, or the end.
/// Whether the header went out: a transfer dropped after it must be aborted.
pub fn begun(transfer: *const Transfer) bool {6. How logs look
Each module logs under its scope, through its log.zig:
const log = @import("log.zig").agent; // or .server
log.warn("binary refused: too big to receive", .{ .path = header.path, .kib = kib });- The message is lowercase, says what happened, then why after a colon: "binary not sent: it cannot be opened", "target is gone: keeping its last regions".
- Facts go in fields, never in the message:
.pid = pid,.cause = err,.kib = …. Sizes inkibormib(log_file.kib(bytes)rounds up), times in_msor_s. - Levels mean something:
err: the whole thing failed (a run, the server). Once.warn: something is lost or degraded, and the user may want to act (needs = "CAP_SYSLOG").info: a lifecycle step: started, stopped, with the numbers that sum it up.debug: what a developer follows: each binary, each table.
- A repeated problem is said once: the first window too big, the first time a table is full. Counts go in the stop line.
7. How tests look
Tests are at the bottom of the file they test, after the separator line:
zig// ------------------------------------------------------------------------------------- testsA test's name is a sentence about behaviour:
"a binary that cannot be opened is dropped, not retried","stop ends the run at its next wait, with its last report".Each facade's test block lists its folder's files, so a file is tested as soon as its layer is. The module root's test block lists the facades.
A test that needs root says so in its name,
(root), and skips without it: it returnserror.SkipZigTestonPermissionDenied, so the suite stays green for anyone.End to end tests are in
testing/e2e/, against real processes (children the test spawns, named withprctl) with a behaviour known in advance. They print one summary line of what they measured, so a failure says how far off it was.Examples are tests. The usage example of the agent (
testing/examples/server_feed.zig) is compiled and run with every build, so it cannot go stale.The main code stays light: test-only code lives in
testing/. A file's own tests sit at its bottom; a helper that tests of other files need (fixtures, fake binaries, waiting for a zombie) goes in the module'stesting/folder, never in a main file, not even as apub fn"for testing". Test helpers may import anything; nothing but tests imports them.Bounds in tests are loose enough for a loaded machine, and a test never sleeps hoping: it polls for the condition, with a limit.
8. Building, testing, committing
zig build build nusku, the tests' binaries and the BPF objects
zig build test every test; the root ones skip
sudo zig build test every test, the root ones too
zig-out/bin/agent-tests the agent's tests alone (installed by `zig build test`), e.g. with sudo
zig fmt --check agent server shared src
cd testing && make check profile the C, Go and Zig apps and check their known profiles (sudo)- The machine has 7.6 GiB of RAM: cap heavy builds,
systemd-run --user --scope -p MemoryMax=5G zig build test, and avoid big comptime concatenations. - Green after every step. A change made of steps (move, then rewire, then remove) builds and passes between each.
- A commit message is one line, saying what changed, in lowercase.
app/builds on its own, with its own tooling.
9. Recipes
A new metric (off-CPU, memory, …)
All in the metrics layer; nothing below it changes.
- A folder
agent/metrics/offcpu/: its program inagent/metrics/kernel/offcpu.bpf.zig(added inagent/build.zigwithadd_kernel_program), its collector withinit,deinit,start,tick,collect, likemetrics/cpu/collector.zig. A walking program binds the unwind maps (UnwindMaps.bindings()). - In
metrics/metrics.zig: a fieldoffcpu: ?OffcpuCollector, a line ininit,deinit,tickandcollect, and a fieldoffcpu: ?OffcpuWindowinWindows. Remove it from theMetricNotImplementedlist. - In
shared/protocol: what a message carries for it. - In
agent/message.zig: the window into the message. - In
server/naming: if its stacks are named the same way, nothing; otherwise its own step.
A new file in a layer
Put it in the layer's folder, import it from the facade (or a sibling), and add it to the facade's test block. If another layer needs it, export what it needs from the facade.
A new layer
- A folder
name/withname/name.zig: the struct and its header. Its other files beside it. - Pick its rank: the highest rank of what it imports, plus one. Check it imports nothing beside.
- A line in the root's layer list and test block.
A new type between the agent and the server
It goes in shared/protocol/, exported from shared/protocol/protocol.zig. The agent fills it, the server reads it, the CLI shows it; each imports it from shared, and none defines or re-exports its own copy. A new event is a new field of RunEvent or WatchEvent: the compiler then points at every switch that must handle it.
A new query on the server
A function on Server that asks its layers: copy what it needs under each layer's lock, compute without any lock. If it is new computation (not just a copy), it is a new layer above the ones it reads, like naming.
A new command
A file in src/cmd/ with its cli.Command, listed in cmd/cmd.zig's subcommands and test block. It reads its flags (cmd/flags.zig for the shared ones), asks client/client.zig, and prints through a view; a new view goes in src/view/, exported from view/view.zig.
A remote transport (gRPC)
Nothing in the agent or the server's layers changes:
- Agent side: a program that runs
Run.next(), sends eachprotocol.RunEvent, and applies eachprotocol.Answerit gets back withbinary_want. - Server side: a receiver that opens a
Feedper agent run and callsfeed.take(event), sending the answer back;watch_takefor a watch. It is a layer besidecontrol. - CLI side:
src/client/remote.zigwith the same three functions aslocal.zig, andclient/client.zigchoosing one.
10. The checklist
Before calling something done: