How to clear L1 and L2 processor cache

I run the test on the xeon server and repeat the execution 2-3 times. I would like to erase the contents of the cache in L1 and L2 when repeating runs. Can you suggest any methods for this?

+5
source share
1 answer

Try to read big data regularly through the CPU (i.e. not DMA). How:

int main() { const int size = 20*1024*1024; // Allocate 20M. Set much larger then L2 char *c = (char *)malloc(size); for (int i = 0; i < 0xffff; i++) for (int j = 0; j < size; j++) c[j] = i*j; } 

However, depending on the server, the big problem may be the disk cache (in memory), and then the L1 / L2 cache. On Linux (for example), using:

  sync echo 3 > /proc/sys/vm/drop_caches 

Edit: It is trivial to create a large program that does nothing:

 #!/usr/bin/ruby puts "main:" 200000.times { puts " nop" } puts " xor rax, rax" puts " ret" 

Running several times under different names (code not created with a script) should do the job

+8
source

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


All Articles