42

According to perf tutorials, perf stat is supposed to report cache misses using hardware counters. However, on my system (up-to-date Arch Linux), it doesn't:

[joel@panda goog]$ perf stat ./hash

 Performance counter stats for './hash':

    869.447863 task-clock                #    0.997 CPUs utilized          
            92 context-switches          #    0.106 K/sec                  
             4 cpu-migrations            #    0.005 K/sec                  
         1,041 page-faults               #    0.001 M/sec                  
 2,628,646,296 cycles                    #    3.023 GHz                    
   819,269,992 stalled-cycles-frontend   #   31.17% frontend cycles idle   
   132,355,435 stalled-cycles-backend    #    5.04% backend  cycles idle   
 4,515,152,198 instructions              #    1.72  insns per cycle        
                                         #    0.18  stalled cycles per insn
 1,060,739,808 branches                  # 1220.015 M/sec                  
     2,653,157 branch-misses             #    0.25% of all branches        

   0.871766141 seconds time elapsed

What am I missing? I already searched the man page and the web, but didn't find anything obvious.

Edit: my CPU is an Intel i5 2300K, if that matters.

3
  • It depends on your hardware counters. I've never used perf, but I've used PAPI (icl.cs.utk.edu/PAPI) and it's possible to check the available hardware counters to find out what can you get from your CPU.
    – SamGamgee
    Feb 3, 2013 at 16:19
  • 6
    Try perf stat -d - it will report some cache events. Check also new perf mem tool for record/report memory events - documented in linuxtag.org/2013/fileadmin/www.linuxtag.org/slides/… slide 10 and man7.org/linux/man-pages/man1/perf-mem.1.html
    – osgx
    Mar 15, 2015 at 22:42
  • 2
    osgx, perf stat -d will turn on event multiplexing, which sometimes may report incorrect rates. It is better to manually select no more than 5-7 hw events per run; and use perf stat -d only to get names of such events. Other way for Intel - try toplev.py from github.com/andikleen/pmu-tools
    – osgx
    Jan 9, 2016 at 5:05

3 Answers 3

59

On my system, an Intel Xeon X5570 @ 2.93 GHz I was able to get perf stat to report cache references and misses by requesting those events explicitly like this

perf stat -B -e cache-references,cache-misses,cycles,instructions,branches,faults,migrations sleep 5
Performance counter stats for 'sleep 5':

         10573 cache-references                                            
          1949 cache-misses              #   18.434 % of all cache refs    
       1077328 cycles                    #    0.000 GHz                    
        715248 instructions              #    0.66  insns per cycle        
        151188 branches                                                    
           154 faults                                                      
             0 migrations                                                  

   5.002776842 seconds time elapsed

The default set of events did not include cache events, matching your results, I don't know why

perf stat -B sleep 5

Performance counter stats for 'sleep 5':

      0.344308 task-clock                #    0.000 CPUs utilized          
             1 context-switches          #    0.003 M/sec                  
             0 CPU-migrations            #    0.000 M/sec                  
           154 page-faults               #    0.447 M/sec                  
        977183 cycles                    #    2.838 GHz                    
        586878 stalled-cycles-frontend   #   60.06% frontend cycles idle   
        430497 stalled-cycles-backend    #   44.05% backend  cycles idle   
        720815 instructions              #    0.74  insns per cycle        
                                         #    0.81  stalled cycles per insn
        152217 branches                  #  442.095 M/sec                  
          7646 branch-misses             #    5.02% of all branches        

   5.002763199 seconds time elapsed
6
  • Thanks, that was helpful. I guess they must have changed the default set of events that are captured. Feb 3, 2013 at 17:08
  • Nice, I was thinking it was weird to have to always record the info.. This approach is faster :)
    – SamGamgee
    Feb 3, 2013 at 17:10
  • A question inside the question, What is the fauts count in the perf output Jun 29, 2017 at 16:02
  • 1
    @ElvisTeixeira faults is an alias for page-faults, for a list of all events run perf list
    – amdn
    Jun 29, 2017 at 17:15
  • tnks @amdn. Now, what are page-faults? Jun 30, 2017 at 21:08
15

In the latest source code, the default event does not include cache-misses and cache-references again:

struct perf_event_attr default_attrs[] = {

  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK      },
  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES    },
  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS      },
  { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS     },

  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES      },
  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND  },
  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS        },
  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES       },

};

So the man and most web are out of date as so far.

4

I've spent some minutes trying to understand perf. I found out the cache-misses by first recording and then reporting the data (both perf tools).

To see a list of events:

perf list

For example, in order to check the last-level-cache load misses, you will need to use the event LLC-loads-misses like this

perf record -e LLC-loads-misses ./your_program

then report the results

perf report -v
2
  • What's the difference between the perf events cache-misses and LLC-loads-misses? Dec 6, 2020 at 3:53
  • 1
    It's been a long while since I looked at these things, but I think the cache-misses include misses for all levels of the cache (typically 3 levels) and LLC might only be for the last level L3, because that's the most critical one, if it misses there it goes to the memory.
    – SamGamgee
    Dec 7, 2020 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.