Ep6: Advance ZIG features we need
Before continuing with the BPF logic, let's separate the Zig language features from what the profiler is doing. Otherwise you'll end up understanding the profiler concept but getting stuck on syntax like extern, linksection, @ptrFromInt, @sizeOf, @truncate, pointers, callconv, etc.
Quick reference for the Zig features used in the Nusku eBPF loader/program.
1. extern struct
const MapDef = extern struct {
map_type: u32,
key_size: u32,
value_size: u32,
max_entries: u32,
};Meaning: Use an ABI-oriented, predictable memory layout for the struct.
Useful when another system will interpret the bytes directly.
For MapDef:
offset 0 → map_type 4 bytes
offset 4 → key_size 4 bytes
offset 8 → value_size 4 bytes
offset 12 → max_entries 4 bytes
total → 16 bytesThink:
extern struct= "the exact binary layout matters."
2. export
export var stacks: MapDef = ...;Meaning: Make the declaration externally visible in the generated object.
In Nusku, this helps the compiled ELF object contain named symbols such as:
stacks
counts
target_pidThink:
export= "make this declaration visible outside this Zig code."
3. linksection
export var stacks: MapDef linksection(".maps") = .{ ... };Meaning: Put the variable into a specific ELF section.
Here:
.maps
├── stacks
├── counts
└── target_pidThink:
linksection(".maps")= "put this object in the.mapssection."
This is important because Nusku's loader later reads that section.
4. @sizeOf
@sizeOf(StackKey)Meaning: Get the size of a type in bytes.
Examples:
@sizeOf(u32) // 4
@sizeOf(u64) // 8
@sizeOf(StackKey) // size of StackKeyIt is evaluated at compile time.
Think:
@sizeOf(T)= "How many bytes does typeToccupy?"
Pointers
5. Pointer: *T
var value: u32 = 42;
const ptr: *u32 = &value;ptr contains the address of value.
ptr
│
▼
┌──────┐
│ 42 │
└──────┘Think:
*T= pointer to aT.
6. *const T
const ptr: *const u32 = &value;The pointer can read the value but cannot modify it through that pointer.
const x = ptr.*; // OK
ptr.* = 50; // not allowedThink:
*const T= pointer to read-onlyT.
7. anyopaque
*const anyopaqueMeans:
"A pointer to some memory, but I don't know its concrete type."
Useful for generic APIs.
In Nusku, a map lookup can return generic memory because different maps contain different value types.
Example:
target_pid → u32
counts → u64The generic pointer does not know which one it is until we interpret it.
Type conversions
8. @as
const x: u32 = 10;
const y = @as(u64, x);Explicitly gives a value a desired type.
u32 10
↓
u64 10Think:
@as(T, value)= "use this value as typeT."
Zig uses explicit conversions heavily because it is strict about types.
9. @truncate
const x: u64 = 0x1234567890ABCDEF;
const y: u32 = @truncate(x);Keeps only the bits that fit into the destination type.
u64
12345678 90ABCDEF
↓
u32
90ABCDEFThink:
@truncate= "keep the lower bits that fit."
Nusku example:
const tgid: u32 =
@truncate(get_current_pid_tgid() >> 32);The helper returns u64; after shifting, the TGID is in the lower 32 bits, so @truncate extracts it as u32.
Pointer conversions
10. @ptrCast
const other: *u8 = @ptrCast(ptr);Changes the pointer type, not the memory.
The pointers still refer to the same address.
same memory
▲
│
┌──┴──┐
*u32 *u8Think:
@ptrCast= "view this address as another pointer type."
11. @alignCast
@alignCast(ptr)Tells Zig that the pointer has the alignment required by the destination type.
It does not move memory or allocate anything.
Think:
@alignCast= "this pointer is properly aligned."
Common combination:
@ptrCast(@alignCast(p))Read inside-out:
p
↓
@alignCast
↓
@ptrCast
↓
pointer with desired type12. @ptrFromInt
@ptrFromInt(1)Converts an integer into a pointer value.
Normally this is unusual and dangerous for normal application code.
In Nusku's BPF code it is intentional because BPF helpers are identified by numbers:
helper #1 → map lookup
helper #2 → map update
helper #14 → current PID/TGID
helper #27 → stack IDSo:
@ptrFromInt(1)is being used to represent BPF helper #1 as a function pointer.
Think:
@ptrFromInt(n)= "make a pointer from this integer address/value."
Functions and ABI
13. Function pointer
A type such as:
*const fn (u32) u64means:
Pointer to a function that accepts
u32and returnsu64.
Conceptually:
function address
│
▼
fn(u32) → u6414. callconv(.c)
fn (u32) callconv(.c) u64Specifies the calling convention.
The C calling convention defines how arguments, return values, registers, etc. are passed between caller and function.
Think:
callconv(.c)= "use the C ABI calling convention."
This is useful when matching an external ABI, such as BPF helper signatures.
Optional values
15. Optional: ?T
const result: ?u32 = something();Means:
result can be:
u32
or nullExample:
const value: ?u32 = null;16. orelse
const value = maybe_value orelse 100;If maybe_value contains a value, use it.
If it is null, use 100.
Nusku:
const target_ptr =
map_lookup_elem(&target_pid, &zero) orelse return 0;Meaning:
map lookup
│
├── found → continue
│
└── not found → return 0Think:
orelse= "if null, use this fallback."
Dereferencing
17. .*
const value = ptr.*;Dereferences the pointer and reads the value at that address.
ptr
│
▼
┌──────┐
│ 42 │
└──────┘
↑
ptr.*Think:
ptr.*= "read the value pointed to byptr."
18. &
const ptr = &value;Gets the address of a value.
Think:
&value= "give me a pointer tovalue."
Initialization
19. undefined
var key: StackKey = undefined;Allocates the variable without initializing its contents.
It is valid when you are going to initialize every field before reading it.
Example:
var key: StackKey = undefined;
key.tgid = tgid;
key.user_stack_id = user_stack_id;
key.kernel_stack_id = kernel_stack_id;Important:
undefineddoes NOT meannulland does NOT mean zero.
It means the initial contents are unspecified.
Concurrency
20. @atomicRmw
@atomicRmw(u64, count, .Add, 1, .monotonic);Performs an atomic read-modify-write operation.
Here it means:
type → u64
target → count
operation → Add
value → 1
ordering → monotonicConceptually:
count = 10
atomic +1
count = 11Why atomic?
Multiple CPUs can execute BPF programs at the same time.
Without an atomic increment:
CPU 1 reads 10
CPU 2 reads 10
CPU 1 writes 11
CPU 2 writes 11One sample would be lost.
With atomic increment:
CPU 1 → +1
CPU 2 → +1
10 → 11 → 12Think:
@atomicRmw= "change shared memory safely as one atomic operation."
Quick mental table
| Zig feature | Mental model |
|---|---|
extern struct | Exact ABI/binary layout matters |
export | Make declaration externally visible |
linksection | Put object in a named ELF section |
@sizeOf | Get type size in bytes |
*T | Pointer to T |
*const T | Read-only pointer to T |
anyopaque | Pointer to unknown-type memory |
@as | Explicitly use/convert to a type |
@truncate | Keep lower bits that fit |
@ptrCast | Change pointer type |
@alignCast | Assert required pointer alignment |
@ptrFromInt | Create pointer from integer |
callconv(.c) | Use C ABI calling convention |
?T | T or null |
orelse | Fallback when optional is null |
.* | Dereference pointer |
&x | Get address of x |
undefined | Uninitialized value |
@atomicRmw | Atomic read-modify-write |
Nusku-specific mental model
The advanced syntax in the BPF file mostly exists because we're doing three unusual things:
Zig
│
┌─────────┼─────────┐
▼ ▼ ▼
Binary ELF BPF ABI
layout sections helpers
│ │ │
└─────────┼─────────┘
▼
Linux kernelSo when you see something like:
export var counts: MapDef linksection(".maps")don't think:
"Why is this Zig code so weird?"
Think:
"We're deliberately constructing an ELF object that another program, our loader, will inspect."
That is the key to understanding the rest of Nusku.