Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear CPU L1 and L2 cache [duplicate]

Tags:

caching

I'm running a benchmark on xeon server , and i repeat the executions 2-3 times. I'd like to erase the cache contents in L1 and L2 while repeating the runs. Can you suggest any methods for doing so ?

like image 275
Sharat Chandra Avatar asked Aug 09 '10 18:08

Sharat Chandra


1 Answers

Try to read repetitly large data via CPU (i.e. not by DMA). Like:

 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 depend on server a bigger problem may be a disk cache (in memory) then L1/L2 cache. On Linux (for example) drop using:

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

Edit: It is trivial to generate large program which do nothing:

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

Running a few times under different names (code produced not the script) should do the work

like image 83
Maciej Piechotka Avatar answered Oct 06 '22 00:10

Maciej Piechotka