title: Can I measure branch-prediction failures on a modern Intel Core CPU?
body: <p><a href="https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array">This question</a> and its answer, which was recently tagged as an Epic Answer, has prompted me to wonder; Can I measure the performance of a running application in Windows in terms of its CPU branch prediction failures?  I know that some static analysis tools exist, that might help with optimizing code for good performance in branch-prediction situations, and that manual techniques could help by simply making changes and re-testing, but I'm looking for some automatic mechanism that can report a total number of branch prediction failures, over a period of time, as a Windows application runs, and I'm hoping that some Profiler tool for Visual C++ could help me.</p>
  
  <p>For the sake of this question, the application in question is either built with a native-compiler such as Visual C++ for Windows, or using some other native compiler, such as GCC, FreePascal, Delphi, or TurboAssembler. The executable may not have any debug information at all. I want to know if I can detect, and count branch prediction failures, perhaps by reading internal CPU information through some Windows service like WMI, or perhaps by running entirely inside a virtualized environment running Windows, such as using VirtualBox, and then running a completely virtualized windows environment with my test application, inside VirtualBox, and doing runtime analysis of the virtual CPU. Or some other technique that I don't know of, thus this question.</p>
  
  <p>Yes, I googled.  The only thing that looks promising is <a href="http://developer.amd.com/Assets/intro_to_ca_v3_final.pdf" rel="nofollow noreferrer">this PDF</a> from AMD. Page 18 mentions something very close to what I'd like to do, but seems written for those working without any operating system, on raw evaluation hardware platforms:</p>
  
  <blockquote>
    <p>5.1. Branches.  Applicability. Conditional branch mispredictions may be a significant issue in code with a lot of  decision-making logic.</p>
    
    <p>Conditional branches may be mispredicted when the likelihood of
    choosing  the true or false path is random or near a 50-50 split.  The
    branch prediction hardware cannot  "learn" a pattern and branches are
    not predicted correctly.  Collection. Collect the events in this table
    to measure branch prediction performance:  </p>
    
    <p>Branches  Compute the rate at which branches are
    taken and the ratio of the number of instructions per  branch using
    these formulas:    Branch taken rate = Taken_branches /
    Ret_instructions    Branch taken ratio = Taken_branches / Branches<br>
    Instructions per branch = Ret_instructions / Branches</p>
  </blockquote>
  
  <p>Update: I guess I could say I'm looking for a way to read the Intel Core i7 PMU module, or equivalent functions of other CPUs. It looks like Intel VTUNE (from the comments by Adrian) is very close to what I asked for.</p>