如何在 x86 Windows 中执行 CPU 缓存刷新? [英] How can I do a CPU cache flush in x86 Windows?

查看:21
本文介绍了如何在 x86 Windows 中执行 CPU 缓存刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在 Windows 中强制刷新 CPU 缓存感兴趣(出于基准测试的原因,我想在 CPU 缓存中没有数据的情况下进行模拟),最好是基本的 C 实现或 Win32 调用.

I am interested in forcing a CPU cache flush in Windows (for benchmarking reasons, I want to emulate starting with no data in CPU cache), preferably a basic C implementation or Win32 call.

是否有一种已知的方法可以通过系统调用或什至像说一个大的memcpy那样偷偷摸摸地做到这一点?

Is there a known way to do this with a system call or even something as sneaky as doing say a large memcpy?

Intel i686 平台(P4 及以上也可以).

Intel i686 platform (P4 and up is okay as well).

推荐答案

幸运的是,显式刷新缓存的方法不止一种.

Fortunately, there is more than one way to explicitly flush the caches.

指令wbinvd"写回修改后的缓存内容并将缓存标记为空.它执行一个总线周期以使外部缓存刷新其数据.不幸的是,这是一个特权指令.但是,如果可以在诸如 DOS 之类的环境下运行测试程序,这就是要走的路.这样做的好处是可以保持操作系统"的缓存占用空间非常小.

The instruction "wbinvd" writes back modified cache content and marks the caches empty. It executes a bus cycle to make external caches flush their data. Unfortunately, it is a privileged instruction. But if it is possible to run the test program under something like DOS, this is the way to go. This has the advantage of keeping the cache footprint of the "OS" very small.

此外,还有invd"指令,它使缓存无效将它们刷新回主内存.这违反了主存和缓存的一致性,因此您必须自己处理.不是很推荐.

Additionally, there is the "invd" instruction, which invalidates caches without flushing them back to main memory. This violates the coherency of main memory and cache, so you have to take care of that by yourself. Not really recommended.

出于基准测试的目的,最简单的解决方案可能是将大内存块复制到标有 WC(写入组合)而不是 WB 的区域.显卡的内存映射区域是一个不错的选择,或者您可以通过 MTRR 寄存器自行将区域标记为 WC.

For benchmarking purposes, the simplest solution is probably copying a large memory block to a region marked with WC (write combining) instead of WB. The memory mapped region of the graphics card is a good candidate, or you can mark a region as WC by yourself via the MTRR registers.

您可以在用于测量时钟周期和性能监控的测试程序中找到一些关于对短例程进行基准测试的资源.

这篇关于如何在 x86 Windows 中执行 CPU 缓存刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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