2

I want to understand branch prediction behavior of a program I work on. For this, I use the perf tool. I recorded with:

perf record -e branches,branch-misses

and visualizing it with

perf report --hierarchy -M intel

I get results, but I don't understand how to interpret them. I have percentages in the left column, but instead of branches, mispredictions happen on normal instructions, for example, add rax, rcr, or move rsi QWORD PTR [r14+0x8], etc.

What does it mean? How is it possible for mispredictions happen in these places? How to interpret them.

Konstantin Solomatov
  • 10,252
  • 8
  • 58
  • 88
  • 3
    Are those the first instructions *after* a branch, either the target or fall-through? What CPU are you using? Skew is possible in terms of where RIP points when an interrupt triggers from the counter overflow, if you're not using PEBS for precise events. https://www.brendangregg.com/perf.html mentions skew. Similar to [How does perf record (or other profilers) pick which instruction to count as costing time?](https://stackoverflow.com/q/69351189) for events like `cycles`, but more weird for an event like `br_misp_retired.all_branches` vs. `br_misp_retired.all_branches_pebs` on Skylake, e.g – Peter Cordes Jul 01 '22 at 04:27
  • @PeterCordes it looks to me that these percentages don’t show misses and show just how often they appear in profiles, i.e just cpu perf – Konstantin Solomatov Jul 01 '22 at 15:06
  • 3
    perf report is going to show you one event or the other, not mispredict *rate* as a fraction of times it was executed. You'll see a distribution of where the branch misses were throughout your program, if you look at the `branch-misses` event instead of `branches`. – Peter Cordes Jul 01 '22 at 15:11
  • @PeterCordes Thanks, you were right. Unfortunately my AMD processor doesn't support it for branch misses. Need to buy Intel one instead :-) Thanks a lot! – Konstantin Solomatov Jul 01 '22 at 23:06
  • I'd be surprised if any modern AMD CPUs didn't have some hardware event related to branch misses. `perf` might not know a good mapping for its generic `branch-misses` event, but have a look at `perf list` (and maybe some AMD optimization manuals) – Peter Cordes Jul 02 '22 at 00:50
  • @PeterCordes AMD has their own profiler, which is proprietary, called uProf. There're no relevant events in perf list. – Konstantin Solomatov Jul 02 '22 at 02:03
  • 1
    Huh, I didn't realize that. https://github.com/AMDESE/amd-perf-tools seems to provide some access to the AMD PMUs, but not for perf record to use them for sampling events. https://lwn.net/Articles/875869/ discusses a patch series that adds support for branch-likelihood profiling for AMD Fam19h using hardware PMUs. Oh, but that's actual path, not recording mispredicts by the actual BPU. – Peter Cordes Jul 02 '22 at 02:11

0 Answers0