开发者

How to clear CPU L1 and L2 cache [duplicate]

开发者 https://www.devze.com 2023-01-11 07:15 出处:网络
This question already h开发者_Python百科as answers here: How can I do a CPU cache flush in x86 Windows?
This question already h开发者_Python百科as answers here: How can I do a CPU cache flush in x86 Windows? (4 answers) Closed 3 years ago.

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 ?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消