While optimizing software, you often want detailed performance information, such as cache hit rates, branch misprediction rates, and instructions per cycle.
On Linux, perf stat allows you to count cycles, instructions, and L1 data cache misses:
|
|
However, since perf stat reports total counts for the entire program, it is not suited for profiling specific code regions.
For example, even if you only want to measure compute_kernel() in the following program, the perf stat results also include counts from initialize().
|
|
To address this issue, I developed perf-counter, a library for measuring performance events within specific code regions.
This article introduces the basics of perf-counter. More practical usage and the internal implementation will be covered in other articles.
How to Use perf-counter
Measuring performance with perf-counter consists of four steps:
- Open a counter for the event you want to measure
- Enable the counter
- Read the counter value before and after the target code region
- Disable and release the counter
The following example measures the number of cycles compute_kernel() takes:
|
|
The arguments of perf_counter_open_by_id(event_type, event_config, group_fd) are as follows:
event_typeandevent_config: Specify the event to measure using a pair oftypeandconfigdescribed in perf_event_open(2).group_fd: Specify-1to measure a single event. To measure multiple events simultaneously, open the first counter with-1and the others with itsfd.
Here are some examples of event_type and event_config combinations:
event_type |
event_config |
Description |
|---|---|---|
PERF_TYPE_HARDWARE |
PERF_COUNT_HW_CPU_CYCLES |
Number of cycles |
PERF_TYPE_HARDWARE |
PERF_COUNT_HW_INSTRUCTIONS |
Number of instructions executed |
PERF_TYPE_HARDWARE |
PERF_COUNT_HW_BRANCH_INSTRUCTIONS |
Number of branch instructions |
PERF_TYPE_HARDWARE |
PERF_COUNT_HW_BRANCH_MISSES |
Number of branch mispredictions |
PERF_TYPE_SOFTWARE |
PERF_COUNT_SW_PAGE_FAULTS |
Number of page faults |
Opening a Counter by Event Name
Depending on the event, you may need to combine multiple constants to create event_config.
For example, measuring L1 data cache misses requires three constants, which is somewhat cumbersome.
|
|
In such cases, it is easier to open a counter by event name with perf_counter_open_by_name().
It is available when perf-counter is built with libpfm4.
For L1 data cache misses, specify perf::L1-DCACHE-LOAD-MISSES:
|
|
You can list the available event names with list_all_events.c, an example program bundled with perf-counter.
To build the program, see “Building and Running” below.
|
|
Each line of the output consists of an event name and its description.
|
|
perf_counter_open_by_name() also makes it easy to measure processor-specific events.
For example, on an AMD Ryzen 7 4700U (Zen 2), you can count retired SSE/AVX floating-point multiply operations.
|
|
Example: Measuring FMA Instruction Performance
As a practical example, let’s calculate the latency and throughput of 256-bit FMA (Fused Multiply-Add) instructions.
We use perf-counter to count the cycles needed to execute the instructions and then calculate the ratio between cycles and instructions.
For the complete code, refer to perf_avx2_fma.cpp.
Measurement Method
Latency is measured by running a long dependency chain of instructions. They execute one at a time because each instruction must wait for the previous one to complete. Dividing the elapsed cycles by the number of instructions gives the latency: how many cycles a single instruction takes (CPI).
|
|
Latency measurement
Throughput is measured by running many independent instructions, which can execute in parallel. Dividing the number of instructions by the elapsed cycles gives the throughput: how many instructions can be executed per cycle (IPC).
|
|
Throughput measurement
We run each test 10 times after three warm-up runs and use the minimum cycle count to eliminate noise from OS scheduling and interrupts.
Building and Running
To build the example programs, set PERF_COUNTER_BUILD_EXAMPLES to ON:
|
|
Results
Environment: AMD Ryzen 7 4700U (Zen 2), GCC 15.2.1
|
|
The results show a latency of 5.000 cycles and a throughput of 2.000 instructions per cycle. These values match those reported on uops.info.