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

I have a general idea about this. That's what I think:

First find out the size of the L1 cache that I will use. Then create an array (the number of bytes is large enough to enter the L1 cache), write a program that will access each element of the array. Then create a time stamp in each pair of loops.

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

But I don’t really know where to start. I have no clear idea of ​​how large the array should be for each cache and how to write this program in C using this idea.

Can someone help me with this program? Any help would be appreciated!

Thanks a lot!

+4
source share
2 answers

There is already an LMbench tool that does just that.

It is an open source tool, so you can even look at the source code and see how it did it.

0
source

Cache sizes can be seen using the linux command:

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

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

the same that you can apply for L2 and L3 also

Cache Latency Measurement

, .

0

Source: https://habr.com/ru/post/1523626/


All Articles