Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure a L1, L2 and L3 cache latency using C?

Tags:

arrays

c

caching

I have a general idea about it. This is what I am thinking:

First, find out the size of the L1 cache I will be using. Then create an array (number of byte is large enough to fit within L1 cache), write a program which will access every element of the array. Then create time stamp in every couple of loops.

For latency in L2 cache, I could make the array larger to reach the L2 cache.

But actually I don't know how to start. I don't have a clear idea about how large the array should be for each cache and how to write this C program with the idea above.

Could anybody help me with this C program? Any help will be appreciated!

Thanks a lot!

like image 822
Zip Avatar asked Nov 24 '22 05:11

Zip


1 Answers

You can see the cache sizes using the command in linux :

grep . /sys/devices/system/cpu/cpu1/cache/index*/*

In my case (Intel core i7), it showed L1 D cahe is 32KB so your array size also should be the same; for example say x=32*1024/sizeof(int) then create an array of x integers which occupy exactly 32KB In this case it is int[32*1024/4]

same thing you can apply for L2 and L3 also

Measuring Cache Latencies

You can refer this post which will give you some insight.

like image 184
ANTHONY Avatar answered Jan 26 '23 00:01

ANTHONY