14

This is my requirement, I know that certain algorithms makes good use of Cache, some do not, some do more I/O than others on particular data set, etc. I would like to see and analyze that happening myself.

So I was wondering if there was a way I could know how a certain memory/variable is read, i.e. is it from cache, or was there a cache miss. Further if there was a page fault while retrieving this value etc.

Thanks a lot!

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
SnoopyMe
  • 1,015
  • 1
  • 12
  • 21
  • 1
    You might be interested in this SO question: http://stackoverflow.com/questions/3052776/how-to-detect-cache-misses-from-users-codes – Travis Gockel Jul 31 '11 at 01:13

4 Answers4

7

If you really want to know when your caches are hitting/missing, modern processors have performance counters that you can use for exactly this purpose. I have used them extensively for academic research. The easiest way to use them is through perfmon2. Perfmon2 has both a library you can link into your program or a stand-alone program that will monitor an existing program. For example, here's the stand-alone program recording all level 1 data cache read requests and misses:

pfmon -eL1D_CACHE_LD:MESI,L1D_CACHE_LD:I_STATE your_program

For reference, Appendix A of this document (PDF) lists Intel's documentation on what hardware counters are available.

Klox
  • 931
  • 12
  • 21
4

I would try using the valgrind cachegrind tool, it can print out annotated source lines with the number of hits/misses in which cache for that line.

luke
  • 14,518
  • 4
  • 46
  • 57
  • Note, that cachegrind cache misses are not the real cache misses, but simulated one. – osgx Jul 31 '11 at 02:05
1

I don't know if AMD CodeAnalyst can show that level of granularity but it wouldn't hurt to check.

Mike
  • 1,760
  • 2
  • 18
  • 33
  • AMD CodeAnalyst a bit limited on Intel Platform (but the very basic will work). So, for Intel there are Intel Vtune; linux oprofile/perf/perfmon2. – osgx Jul 31 '11 at 02:07
0

Depends on the specific compiler, the OS, and the specific model of processor you're running on. Nothing (that I'm aware of) in the C/C++ language give you access to what's happening at the cache level.

There are various measurement tools, but they would be largely independent of the language.

There are some "rules" for minimizing cache and paging issues, though it would take me some time to think of a reasonably comprehensive list.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151