Manuel Mena
Manuel Mena

Reputation: 150

Knowing cache behaviour for C program

Is there any way to know the cache hit rate or some behaviour of the cache memory when I run my C program?

I'm trying to compare how the cache is acting with 2 different implementations of the function.

Upvotes: 3

Views: 221

Answers (1)

romeric
romeric

Reputation: 2385

Yes, there are tools that let you do this, but as one would expect they are platform/architecture specific. They may also have compiler restriction:

  1. Cachegrind: Cache and branch-prediction profiler. This is probably the closest to what you are looking for. You can use it through valgrind (assuming a Linux kernel)

    valgrind --tool=cachegrind your_program

It has a few more options for fine-tuning. Have a look at the documentation. A typical dump/output in Cachegrind would look like (stripped from the website)

==31751== I   refs:      27,742,716
==31751== I1  misses:           276
==31751== LLi misses:           275
==31751== I1  miss rate:        0.0%
==31751== LLi miss rate:        0.0%
==31751== 
==31751== D   refs:      15,430,290  (10,955,517 rd + 4,474,773 wr)
==31751== D1  misses:        41,185  (    21,905 rd +    19,280 wr)
==31751== LLd misses:        23,085  (     3,987 rd +    19,098 wr)
==31751== D1  miss rate:        0.2% (       0.1%   +       0.4%)
==31751== LLd miss rate:        0.1% (       0.0%   +       0.4%)
==31751== 
==31751== LL misses:         23,360  (     4,262 rd +    19,098 wr)
==31751== LL miss rate:         0.0% (       0.0%   +       0.4%)

For Windows there is also the WinCachegrind

  1. gem5: A modular platform for computer-system architecture research. Works on ARM, X86, Alpha, SPARC. It also supports some specific GPU models. Not sure if it supports non-POSIX platforms.
  2. CACTI: An integrated cache access time, cycle time, area, leakage, and dynamic power model for uniform and non-uniform cache architectures.
  3. MARSSx86: Micro-architectural and system simulator for x86-based systems.

Upvotes: 5

Related Questions