Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache Hit/Miss for a value in C/C++ Program

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!

like image 882
SnoopyMe Avatar asked Jul 31 '11 01:07

SnoopyMe


People also ask

How is hit and miss cache calculated?

To calculate a hit ratio, divide the number of cache hits with the sum of the number of cache hits, and the number of cache misses. For example, if you have 51 cache hits and three misses over a period of time, then that would mean you would divide 51 by 54. The result would be a hit ratio of 0.944.

What is a hit or miss in cache?

A cache miss is an event in which a system or application makes a request to retrieve data from a cache, but that specific data is not currently in cache memory. Contrast this to a cache hit, in which the requested data is successfully retrieved from the cache.

How is cache hit rate calculated?

A cache hit ratio is calculated by dividing the number of cache hits by the total number of cache hits and misses, and it measures how effective a cache is at fulfilling requests for content.

What is miss rate in cache memory?

Similarly, the miss rate is the number of total cache misses divided by the total number of memory requests made to the cache. One might also calculate the number of hits or misses on reads or writes only. Clearly, a higher hit rate will generally result in higher performance.


2 Answers

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.

like image 104
Klox Avatar answered Sep 23 '22 16:09

Klox


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.

like image 21
luke Avatar answered Sep 20 '22 16:09

luke