WBINVD指令用法
我正在尝试在 Linux 上使用 WBINV 指令来清除处理器的 L1 缓存。
以下程序可以编译,但当我尝试运行它时会产生分段错误。
int main() {asm("wbinvd"); return 1;}
我正在使用 gcc 4.4.3 并在 x86 机器上运行 Linux 内核 2.6.32-33。
处理器信息:Intel(R) Core(TM)2 Duo CPU T5270 @ 1.40GHz
我构建的程序如下:
$ gcc
$ ./a.out
Segmentation Failure
有人能告诉我我做错了什么吗?我如何让它运行?
PS:我正在运行一些性能测试,并希望确保处理器缓存的先前内容不会影响结果。
I'm trying to use the WBINV instruction on linux to clear the processor's L1 cache.
The following program compiles, but produces a segmentation fault when I try to run it.
int main() {asm ("wbinvd"); return 1;}
I'm using gcc 4.4.3 and run Linux kernel 2.6.32-33 on my x86 box.
Processor info: Intel(R) Core(TM)2 Duo CPU T5270 @ 1.40GHz
I built the program as follows:
$ gcc
$ ./a.out
Segmentation Fault
Can somebody tell me what I'm doing wrong? How do I get this to run?
P.S: I'm running a few performance tests and want to ensure that the previous content of the processor cache does not influence the results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
引自 英特尔® 64 和 IA-32 架构软件开发人员手册
第 2A 卷和第 2B 卷合并:指令集参考,AZ:
换句话说,只有内核模式代码才被允许执行它。
编辑:以前关于清除缓存的SO讨论:
“C”以编程方式清除L2缓存在 Linux 计算机上
如何在 x86 中执行 CPU 缓存刷新Windows?
如何清除CPU L1和L2缓存
<一个href="https://stackoverflow.com/questions/3443130/how-to-clear-cpu-l1-and-l2-cache">https://stackoverflow.com/questions/3443130/how-to-clear- cpu-l1-和-l2-缓存
Quoting from Intel® 64 and IA-32 Architectures Software Developer's Manual
Combined Volumes 2A and 2B: Instruction Set Reference, A-Z:
In other words only kernel mode code is allowed to execute it.
EDIT: Previous SO discussion on clearing caches:
"C" programmatically clear L2 cache on Linux machines
How can I do a CPU cache flush in x86 Windows?
How to clear CPU L1 and L2 cache
https://stackoverflow.com/questions/3443130/how-to-clear-cpu-l1-and-l2-cache
正如 user786653 所写,
wbinvd
它是一条特权指令,在非内核代码中会出现段错误。您应该避免使用 wbinvd 进行基准测试,因为它会强制所有类型的总线锁定周期、管道序列化并增加从内核到用户空间等的开销,而这在您的实际程序中很可能不会发生。
因此,您的测量不会更准确,它将包含各种伪影。读取二级缓存大小的数据块会产生更好的结果。
您可以阅读测量时钟周期和性能监控的测试程序下的源代码,看看其他人如何发挥作用结果。
As user786653 wrote,
wbinvd
it is an privileged instruction, which segfaults in non-kernel code.You should avoid using
wbinvd
for benchmarking, because it forces all kind of bus locking cycles, pipeline serializing and adds the overhead from kernel to userspace etc., which most likely do not happen in you real world program.Hence your measurement will not be more exact, it will contain all kinds of artifacts. Reading a data chunk in the size of the L2 cache will produce better results.
You can read the source code under Test programs for measuring clock cycles and performance monitoring to see how others got useful results.