对于一些精确的测量,我想从命令行使所有缓存无效/刷新到 RAM(主内存)(以便主程序运行时间评估不受此过程的影响)。我发现了以下内容(第一个和最后一个来自这里):
1. echo 3 > /proc/sys/vm/drop_caches
我可以用以下内容构建一个(预执行的)程序
2. #include <asm/cachectl.h>
int cacheflush(char *addr, int nbytes, int cache);
或者我终于可以做一个
3. 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;
}
我的问题是:对于我需要做的事情,哪个版本是最好的,如果它是#2,我应该给它作为起始地址的地址是什么?我uname -a
的是 Linux 3.2.0-33-generic #52-Ubuntu SMP Thu Oct 18 16:19:45 UTC 2012 i686