Find Hot Instructions With perf annotate
Use perf annotate to see which assembly instructions and source lines inside a hot function receive the most samples, and how to read skid correctly.

A function that tops the report is still a large place to look. perf annotate goes one level lower: it takes the samples that landed in a function and attaches them to individual instructions, with source lines interleaved when the binary has debug information. This post uses perf 6.8 on Linux 6.8 with GCC 13 on x86-64 to locate the hot loop inside a function, and continues from finding hot functions with perf record and perf report.
A function with an uneven loop
The function counts how many values in an array exceed a threshold, but it also accumulates a checksum through a dependent multiply, which makes one line far more expensive than the rest.
#include <stdio.h>
#include <stdlib.h>
#define N 4000000
static long scan(const int *a, long n, int limit, unsigned long *check) {
long over = 0;
unsigned long h = 1;
for (long i = 0; i < n; i++) {
if (a[i] > limit)
over++;
h = h * 2654435761UL + (unsigned long)a[i];
}
*check = h;
return over;
}
int main(void) {
int *a = malloc(sizeof(int) * N);
for (long i = 0; i < N; i++)
a[i] = (int)((i * 7919) % 1000);
long total = 0;
unsigned long c = 0;
for (int r = 0; r < 400; r++)
total += scan(a, N, 500, &c);
printf("%ld %lu\n", total, c);
free(a);
return 0;
}Build with -O2 -g and record. The -g is what lets perf annotate show source lines:
gcc -O2 -g scan.c -o scan
perf record -F 999 ./scan798400000 8934175412893301021
[ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.911 MB perf.data (9132 samples) ]The first number is exact: values 501 to 999 are above the limit, which is 499 of every 1,000 values, so each pass counts 1,996,000 and 400 passes give 798,400,000. The second number is a wraparound hash that the program prints only so the compiler cannot discard the work, so treat the value shown as an example.
Open the annotation
Run perf annotate on the symbol, with --stdio for plain text. Add -s scan or just name the symbol after the options:
perf annotate --stdio -s scan Percent | Source code & Disassembly of scan for cycles (9120 samples)
-----------------------------------------------------------------------
: scan():
: for (long i = 0; i < n; i++) {
0.42 : 4011d0: mov (%rdi,%rax,4),%r8d
: if (a[i] > limit)
0.88 : 4011d4: cmp %edx,%r8d
1.02 : 4011d7: setg %r9b
: h = h * 2654435761UL + (unsigned long)a[i];
58.31 : 4011db: imul %r10,%rcx
31.20 : 4011df: movslq %r8d,%r8
2.45 : 4011e2: add %r8,%rcx
4.14 : 4011e5: add $0x1,%rax
1.58 : 4011e9: cmp %rax,%rsi
0.00 : 4011ec: jne 4011d0Your addresses and percentages will differ, and so can the instruction selection if GCC chooses to vectorize or unroll. The structure is what to learn. Each row is an instruction address with the share of the function's samples, and the lines starting with : are source lines the compiler attributed the following instructions to. The imul row carries 58%, the multiply in the hash. That matches the source: h depends on its own previous value, so each iteration waits for the previous multiply, and the chain is the bottleneck, not the comparison.
Read skid before trusting a row
The row with the most samples is often not the instruction that is slow. When a sample interrupt fires, the processor reports the address of an instruction a short distance after the one that caused the event, because the pipeline is deep. The effect is called skid. Here the 31% on movslq right after imul is a hint: some of the time truly belonging to the multiply is being credited to the next instruction.
The practical rule is to read the cluster, not the single row. A hot instruction and the one or two after it belong together, and the instruction that waits on a long-latency result (a multiply, a load) tends to collect the samples while the instruction after it is where the sample is reported. Precise sampling modes such as Intel PEBS reduce skid; how sampling profilers work explains the mechanism.
Choose the syntax and the view
By default perf annotate prints AT&T syntax on x86. For Intel syntax pass the disassembler flag:
perf annotate --stdio -M intel -s scan 58.31 : 4011db: imul rcx,r10
31.20 : 4011df: movsxd r8,r8dThe -M intel option selects Intel disassembly syntax for the objdump that perf calls. To see percentages per source line rather than per instruction, add --stdio -l:
perf annotate --stdio -l -s scan Percent | Source code & Disassembly of scan for cycles
------------------------------------------------------------
89.51 scan.c:14
3.30 scan.c:12
1.75 scan.c:11Line 14 is the hash update. A summary by line is the quickest check that the hot instructions map to the code you suspect, and it needs -g.
Interactive use
Without --stdio, perf annotate opens a TUI. Move to the hottest line with the cursor keys, press Tab to jump between hot instructions, and s to toggle source. In perf report, press a on a symbol to open its annotation without leaving the report. If annotation shows only hexadecimal with no disassembly, perf could not run objdump on the binary; install binutils, and note that perf needs the same binary that was profiled on disk.
Fix and confirm
The dependency chain is the problem, so break it with independent accumulators:
static long scan(const int *a, long n, int limit, unsigned long *check) {
long over = 0;
unsigned long h0 = 1, h1 = 2;
long i = 0;
for (; i + 1 < n; i += 2) {
over += a[i] > limit;
over += a[i + 1] > limit;
h0 = h0 * 2654435761UL + (unsigned long)a[i];
h1 = h1 * 2654435761UL + (unsigned long)a[i + 1];
}
for (; i < n; i++) {
over += a[i] > limit;
h0 = h0 * 2654435761UL + (unsigned long)a[i];
}
*check = h0 ^ h1;
return over;
}This changes the hash value, so it is only a fix if the checksum's exact value is not part of the contract. Two multiplies per iteration now have no dependency on each other and the processor can overlap them. Re-recording and annotating again should show the imul share drop and the samples spread across more rows. Confirm the gain with perf stat on cycles rather than with percentages.
When annotate misleads
Heavily inlined code can attribute instructions to the wrong source line, because one instruction can serve two lines. Optimization levels above -O2, vectorization and -ffast-math reorder instructions, so the source interleaving becomes noisy; trust the assembly first. And annotation shows where samples land, not why: reading the cause still needs the processor's documentation for the latency of the instruction in question.
A habit for reading annotations
Work from the top of the function's sample distribution rather than from the source. Sort your attention by the percentage column, find the first cluster that holds a large share, then look at what the source line and the neighbouring instructions are waiting on. Three patterns recur. A hot instruction right after a load usually means the load missed the cache and the next instruction is stalled on its result. A hot instruction right after a division or a multiply usually means a long dependency chain, as in the example. A hot conditional jump with no obvious cause often means a branch the predictor gets wrong, which perf stat -e branch-misses confirms.
Percentages in the annotation are relative to the function, not to the whole program. A 58% instruction inside a function that holds 3% of total samples is still a small prize. Read the report first to decide which function is worth the effort, then annotate only that one. If the function is small and inlined into its caller, annotate the caller, because the instructions now live there and the hot function no longer exists as a separate symbol.
Finally, keep the binary that you profiled. If you rebuild before annotating, the addresses in perf.data no longer line up with the code on disk and the annotation either fails or shows plausible-looking nonsense. A build ID check catches this: perf refuses to annotate a file whose build ID does not match the recording, which is another reason to link with --build-id as shown in shipping separate debug symbols.


