TheDeveloper
TheDeveloper

Reputation: 1447

Measure how often a branch is mispredicted

Assuming I have a if-else branch in C++ how can I (in-code) measure how often the branch is mispredicted? I would like to add some calls or macros around the branch (similar to how you do bottom-up profiling) that would report branch mispredictions.

It would be nice to have a generic method, but lets do Intel i5 2500k for starters.

Upvotes: 3

Views: 1042

Answers (3)

Omnifarious
Omnifarious

Reputation: 56068

I wonder if it would be possible to extract this information from g++ -fprofile-arcs? It has to measure exactly this in order to feed back into the optimizer in order to optimize branching.

Upvotes: 2

Necrolis
Necrolis

Reputation: 26181

If you are using an AMD CPU, AMD's CodeAnalyst is just what you need (works on windows and Linux)*.

if your not, then you may need to fork out for a VTune licence or build something using the on CPU performance registers and counters details in the instruction manuals.

You can also check out gperf & OProfile (linux only), see how well they perform (I've never used these, but I see them referred to quite a bit).

*CodeAnalyst should work on an Intel CPU, you just don't get all then nice CPU level analysis.

Upvotes: 2

deft_code
deft_code

Reputation: 59327

OProfile

OProfile is pretty complex, but it can profile anything your CPU tracks.

Look through the Event Type Reference and look for your particular CPU.

For instance here is the core2 events. After a quick search I don't see any event counters for missed branch prediction on the core2 architecture.

Upvotes: 1

Related Questions