Skip to content

Ep7: How Nusku Collects CPU Samples ​

1. Introduction ​

Nusku uses CPU sampling to understand where a process spends its execution time.

Instead of recording every function call, Nusku periodically captures the call stack of a running process. Each sample shows which functions were active at that moment.

By collecting many samples and counting how often each stack appears, Nusku can estimate which parts of a program consume the most CPU time.

2. The General Algorithm ​

The algorithm is simple:

  1. Periodically take a sample of the target process.
  2. Capture its current call stack.
  3. Check whether this stack has been seen before.
  4. If it is new, store it. Otherwise, reuse the existing stack.
  5. Increment the sample count for that stack.

Example ​

Imagine profiling process 1999 and collecting 100 samples.

The first sample produces this stack:

main → handle_request → parse

Nusku stores the stack and sets its count to 1.

If the same stack appears again, Nusku increments its count instead of storing another copy.

Later, another stack appears:

main → handle_request → send

Nusku stores this new stack separately.

After 100 samples, the results might look like this:

Unique stacks

Stack IDCall stack
1main → handle_request → parse
2main → handle_request → send

Sample counts

Stack IDSamples
165
235

The counts add up to 100 samples. The first stack appeared in 65 samples, while the second appeared in 35.

This is the core idea: store each unique stack once and count how often it appears.

Algorithm diagram ​

The resulting sample counts estimate where the process spends its CPU time. They are statistical estimates, not exact measurements of execution time.

3. How Linux Collects Samples ​

Nusku uses Linux perf_event and eBPF to implement this algorithm efficiently.

The collection flow looks like this:

perf_event: when to sample ​

Linux perf_event provides the sampling trigger. Nusku configures a sampling frequency, such as approximately 99 samples per second per CPU.

When a sampling event occurs, the attached eBPF program runs. The program checks the current process and ignores samples that do not belong to the target.

eBPF: what to collect ​

The eBPF program executes in the kernel. For a sample belonging to the target process, it captures the current user-space and kernel-space call stacks.

It then updates the maps that hold stack traces and sample counts. This aggregation happens as samples arrive, rather than requiring Nusku to store every sample as a separate record.

4. The Three BPF Maps ​

The eBPF program uses three maps, each with a distinct responsibility.

MapWhat it storesPurpose
target_pidTarget process IDTells the program which process to profile
stacksStack ID → return addressesStores captured stack traces
countsStack key → sample countTracks how often each unique stack appears

The counts map uses a key containing the process ID and the user and kernel stack IDs. This allows samples to be aggregated by their stack combination.

The stacks map stores the actual stack addresses. The counts map refers to those stacks by their IDs, avoiding repeated storage of identical stack traces.

5. What Happens in Userspace? ​

The Nusku userspace agent prepares the profiling session, creates and configures the BPF maps, and attaches the eBPF program to the sampling events.

While profiling runs, the kernel updates the maps. The userspace agent can read their contents to retrieve the collected stacks and counts.

Nusku can then use that data to build a profile, resolve addresses to function names when symbol information is available, and make the results available for analysis and visualization.

The BPF maps hold the live collection data in kernel memory. If Nusku needs to preserve the results beyond the session, it must copy them into its own storage.

6. Summary ​

Nusku's on-CPU profiler combines three ideas:

  • Sampling: periodically observe what a process is executing.
  • Stack capture: record the call path active at each sample.
  • Aggregation: store unique stacks and count their occurrences.

Linux perf_event triggers the samples, eBPF collects and aggregates them, and Nusku's userspace agent reads the results to build a usable CPU profile.

This approach avoids recording every function call while still providing a useful statistical picture of where a program spends its CPU time.

Built in the open with Zig and eBPF.