Is there a way to disable CPU cache (L1/L2) on a Linux system?

China☆狼群 提交于 2019-11-29 14:40:54

See this 2012 thread, someone posted a tiny kernel module source to disable cache through asm.

http://www.linuxquestions.org/questions/linux-kernel-70/disabling-cpu-caches-936077/

If disabling the cache is really necessary, then so be it.

Otherwise, to know how much time a process takes in terms of user or system "cycles", then I would recommend the getrusage() function.

struct rusage usage;
getrusage(RUSAGE_SELF, &usage);

You can call it before/after your loop/test and subtracted the values to get a good idea of how much time your process took, even if many other processes run in parallel on the same machine. The main problem you'd get is if your process start swapping. In that case your timings will be off.

double user_usage = usage.ru_utime.tv_sec + usage.ru_utime.tv_usec / 1000000.0;
double system_uage = usage.ru_stime.tv_sec + usage.ru_stime.tv_usec / 1000000.0;

This is really precise from my own experience. To increase precision, you could be root when running your test and give it a negative priority (-1 or -2 is enough.) Then it won't be swapped out until you call a function that may require it.

Of course, you still get the effect of the cache... assuming you do not handle very large amount of data with code that goes on and on (opposed to having a loop).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!