且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何清除 CPU L1 和 L2 缓存

更新时间:2021-09-13 22:18:43

尝试通过 CPU(即不通过 DMA)读取重复的大数据.喜欢:

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;
 }

但是取决于服务器,更大的问题可能是磁盘缓存(在内存中)然后是 L1/L2 缓存.在 Linux 上(例如)使用:

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

生成什么都不做的大程序是微不足道的:

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