Course contentsShow
← Linux
Lesson 1665 of 1,81914. Observability and Performance TroubleshootingFree lesson

High-Resolution Time Profiling

Measure sub-millisecond execution phases using high-resolution hardware timers and clocks.

In this lesson, you will learn how to bypass standard system clocks to measure sub-millisecond execution phases using high-resolution hardware timers.

When troubleshooting performance, standard wall-clock measurements (like those from gettimeofday) often fall victim to low resolution or context-switch overheads. For sub-millisecond analysis, we must rely on the CPU’s hardware timestamp counters—specifically the Time Stamp Counter (TSC).

The Physics of Hardware Timers

Modern CPUs include a TSC register that increments at the constant frequency of the processor clock. By reading this register at the entry and exit points of a code block, you can measure execution time with nanosecond precision, effectively eliminating the noise inherent in kernel-level scheduling.

In an eBPF context, we use the bpf_ktime_get_ns() helper, which is backed by the kernel’s high-resolution clock source. However, for extreme performance sensitivity, we can capture raw TSC values. Consider this pattern for measuring a specific function phase:

// Capturing the start cycle
u64 start = rdtsc();

// ... execution phase being measured ...

// Capturing the end cycle
u64 end = rdtsc();
u64 delta = end - start;

// Normalize based on CPU frequency
u64 nanoseconds = delta / cycles_per_ns;

Why Precision Matters

Standard profiling often hides "micro-stutters"—fleeting delays caused by cache misses or pipeline stalls that last only a few hundred nanoseconds. If you rely on coarse timers, these events are averaged out into statistical noise. High-resolution profiling allows you to see the "long tail" of distribution for individual instruction sequences.

When you use this data to generate histograms, you can distinguish between a systematic delay (e.g., a hardware interrupt) and random jitter. This is critical when you are hunting for performance regressions that occur in the hot path of an application, where even a 500ns delay can aggregate into significant throughput loss under high load.

Key Takeaway: By utilizing CPU-cycle hardware counters, you can isolate micro-latency events that remain invisible to traditional system-level observability tools.