如何清除 CPU L1 和 L2 缓存 [英] How to clear CPU L1 and L2 cache

查看:59
本文介绍了如何清除 CPU L1 和 L2 缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在至强服务器上运行基准测试,并重复执行 2-3 次.我想在重复运行时擦除 L1 和 L2 中的缓存内容.你能提出任何这样做的方法吗?

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 ?

推荐答案

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

这篇关于如何清除 CPU L1 和 L2 缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆