如何使文件系统缓存失效?
我想测量/优化应用程序的“冷启动”启动性能,如果不实际重新启动就很难做到这一点,这显然不是一个理想的解决方案。
有没有一种方法可以使整个系统的文件缓存无效,以便映射页面访问实际上导致磁盘访问,以便我可以测量程序启动所需的时间?
信息:
我非常需要 FSCTL_DISMOUNT_VOLUME
的功能,但是对于系统卷。
I want to measure/optimize the "cold boot" startup performance of an application, and it's difficult to do this without an actual reboot, which is obviously not an ideal solution.
Is there a way I could invalidate entire system's file cache, so that mapped page accesses actually cause a disk access, so that I can measure the time my program takes to start up?
Information:
I pretty much need FSCTL_DISMOUNT_VOLUME
's functionality, but for the system volume.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
至少在 Windows 7 上,尝试在没有
FILE_SHARE_WRITE
共享权限的情况下打开卷句柄似乎会导致文件系统缓存失效,即使创建失败。因此,我编写了一个程序,只需调用 CreateFile 即可达到此目的。
从此处的 Base64 版本下载程序*:
Source
* 只是为了好玩,看看您是否可以通过反汇编来弄清楚可执行文件的用途。这不是典型的可执行文件。 :)
At least on Windows 7, it seems that attempting to open a volume handle without
FILE_SHARE_WRITE
sharing permissions causes the file system cache to be invalidated, even if the creation fails.Thus I made a program that simply calls
CreateFile
to this end.Download the program* from its Base64 version here:
Source
* Just for fun, see if you can figure out what the executable does by disassembling it. It's not your typical executable. :)
我编写了一个简单的命令行实用程序来执行此操作: FlushFileCache
它依赖于未记录的 NtSetSystemInformation 函数,并且还可以刷新各种其他内存池。
I've written a simple command-line utility to do that: FlushFileCache
It relies on the undocumented NtSetSystemInformation functions, and can flush the various other memory pools as well.
此解决方案效果很好:清除文件缓存以重复性能测试
更具体地说,我正在这样做:
This solution worked great: Clear file cache to repeat performance testing
More specifically, I'm doing this:
大卫所说的。创建一个大文件,无论您需要多少 GB,并且每次您想要重置文件缓存时,请创建该文件的副本。然后确保删除旧文件。
因此,创建 BIGFILE1.DAT,将其复制到 BIGFILE2.DAT,然后删除 BIGFILE1.DAT(这会将其从磁盘和缓存中删除)。下次,只需颠倒该过程即可。
附录:
嗯,另一个选择是获取映射的文件,并将它们复制到新文件,删除旧文件,然后将新文件重命名回旧文件。缓存由文件支持。如果文件“消失”,缓存也会消失。
如果您可以识别这些文件,并且它们不被系统/其他正在运行的程序共享,那么编写脚本应该很简单,并且理想情况下,运行速度比复制 6 GB 的文件要快。
What David said. Create a large file, however many GB you need, and each time you want to reset your file cache, make a copy of the file. Then make sure you delete the old file.
So, create BIGFILE1.DAT, copy it to BIGFILE2.DAT, and then delete BIGFILE1.DAT (which removes it from the disk and the cache). Next time, just reverse the process.
Addenda:
Well, the other option is to take the files that are mapped, and copy them to new files, delete the old ones, and rename the new files back to the old ones. The cache is backed by a file. If the file "goes away" so does the cache.
If you can identify these files, and they're not shared by the system/other running programs, this should be simple to script and, ideally, run faster than copy 6 GB of files around.
您可以使用 VM 并在 VM 启动后立即拍摄快照。从快照恢复将比重新启动更快。
You can use a VM and take a snapshot right after the VM boots. Resuming from a snapshot will be faster than a reboot.