Reputation: 150
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
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:
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
Upvotes: 5