Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure mispredictions for a single branch on Linux?

I know that I can get the total percentage of branch mispredictions during the execution of a program with perf stat. But how can I get the statistics for a specific branch (if or switch statement in C code)?

like image 706
nwellnhof Avatar asked Nov 04 '15 13:11

nwellnhof


People also ask

How many cycles is a branch?

Branches take one cycle for instruction and then pipeline reload for target instruction. Non-taken branches are 1 cycle total.

How many cycles is a branch Misprediction?

— Mispredicting a branch means that two clock cycles are wasted.

How does branch predictor work?

In computer architecture, a branch predictor is a digital circuit that tries to guess which way a branch (e.g., an if–then–else structure) will go before this is known definitively. The purpose of the branch predictor is to improve the flow in the instruction pipeline.

Does ARM have branch prediction?

The processor uses branch prediction to reduce the core CPI loss that arises from the longer pipeline. To improve the branch prediction accuracy, the PFU uses dynamic techniques. In ARM processors that have no PFU, the target of a branch is not known until the end of the Execute stage.


1 Answers

You can sample on the branch-misses event:

sudo perf record -e branch-misses <yourapp>

and then report it (and even selecting the function you're interested in):

sudo perf report -n --symbols=<yourfunction>

There you can access the annotated code and get some statistics for a given branch. Or directly annotate it with the perf command with --symbol option.

like image 178
amigadev Avatar answered Oct 12 '22 02:10

amigadev