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)?
Branches take one cycle for instruction and then pipeline reload for target instruction. Non-taken branches are 1 cycle total.
— Mispredicting a branch means that two clock cycles are wasted.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With