且构网

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

如何清除CPU L1和L2缓存

更新时间:2022-06-05 22:42:16

尝试通过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