为什么 fsutil.exe 将大文件写入磁盘所需的时间比以编程方式写入的时间要少?
这个问题是根据这个主题: 在案件中创建一个巨大的虚拟文件 。
我刚刚检查了 xp/vista/7 中的 fsutil.exe 将大量虚拟数据写入存储磁盘,与以编程方式相比,写入如此大的文件所需的时间更少
当我尝试在 .net 的帮助下做同样的事情时,它将比 fsutil.exe 花费更多的时间。
注意:我知道 .net 不使用本机代码,因为我刚刚使用本机 api 检查了这个问题,如下所示:
long int size = DiskFree('L' - 64);
const char* full = "fulldisk.dsk";
__try{
Application->ProcessMessages();
HANDLE hf = CreateFile(full,
GENERIC_WRITE,
0,
0,
CREATE_ALWAYS,
0,
0);
SetFilePointer(hf, size, 0, FILE_BEGIN);
SetEndOfFile(hf);
CloseHandle(hf);
}__finally{
ShowMessage("Finished");
exit(0);
答案与 .net 结果相同。
但在 fsutil.exe 的帮助下,它只需要比上面更短的持续时间,或者 .net 方法说它快 2 倍
示例: 使用 .net 写入 400mb 大约需要 40 秒 使用 fsutil.exe 执行相同的操作将需要大约 20 秒或更短的时间。
有什么解释吗? 或者fsutil.exe使用哪个函数有这个意义速度写入?
this question is according to this topic:
creating a huge dummy file in a matter of seconds in c#
I just checked the fsutil.exe in xp/vista/seven to write a huge amount of dummy data into storage disk and it takes less time to write such a big file in comparison to programmaticly way.
When I'm trying to do the same thing with the help of .net it will take considerably more time than fsutil.exe.
note: I know that .net don't use native code because of that I just checked this issue with native api too like following:
long int size = DiskFree('L' - 64);
const char* full = "fulldisk.dsk";
__try{
Application->ProcessMessages();
HANDLE hf = CreateFile(full,
GENERIC_WRITE,
0,
0,
CREATE_ALWAYS,
0,
0);
SetFilePointer(hf, size, 0, FILE_BEGIN);
SetEndOfFile(hf);
CloseHandle(hf);
}__finally{
ShowMessage("Finished");
exit(0);
and the answer was as equal as .net results.
but with the help of fsutil.exe it only takes less duration than above or .net approaches say it is 2 times faster
example :
for writing 400mb with .net it will take ~40 secs
the same amount with fsutil.exe will take around 20secs or less.
is there any explanation about that?
or which function fsutil.exe does use which has this significance speed to write?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道 fsutil 到底在做什么,但我确实知道有两种写入大文件的方法,它们比您上面所做的更快(或者寻找您想要的长度并写入一个零,其具有相同的效果)结果)。
这些方法的问题在于,它们在写入时对文件进行零填充。
您可以通过以下任一方式避免零填充:
I don't know exactly what fsutil is doing, but I do know of two ways to write a large file that are faster than what you've done above (or seeking to the length you want and writing a zero, which has the same result).
The problem with those approaches is that they zero-fill the file at the time you do the write.
You can avoid the zero-fill by either:
我同意最后的评论。我正在尝试使用微过滤器驱动程序并在回调中捕获 IRP_MJ_WRITE IRP。当我从 cmd 行或 win32 应用程序创建或写入文件时,我可以看到写入操作。但是当我使用“fsutil file createnew ...”命令创建文件时,我没有看到任何写入。我在 NTFS 卷上的 win2k8 r2 上看到了这种行为。而且我也不认为(虽然不确定 100%)它是一个稀疏文件。它可能是在 MFT 中设置大小属性而不分配任何簇。 fsutil 会检查可用的可用空间,因此如果文件大小大于磁盘上的可用空间,则会出现错误 1。
我还运行了程序,对文件进行 FSCTL_GET_RETRIEVAL_POINTERS 检测,并获得了文件整个大小的一个范围。但我相信它正在获取所有数据
I agree with the last comment. I was experimenting with a minifilter driver and was capturing IRP_MJ_WRITE IRPs in callback. When I create or write to the file from cmd line or win32 application, I can see writes coming down. But when I created a file using "fsutil file createnew ..." command, I don't see any writes. I'm seeing this behavior on win2k8 r2 on NTFS volume. And I don't think (not sure 100% though) it is a sparse file either. It is probably setting the size properties in MFT without allocating any cluster. fsutil do check the available free space, so if the file size is bigger than free space on disk, you get error 1.
I also ran program sening FSCTL_GET_RETRIEVAL_POINTERS to the file and I got one extent for the entire size of the file. But I believe it is getting all data
这可以用
上述三点可能有一个巨大的重要因素 - 当你考虑一下时,当加载 .NET 代码时,它会被运行时抖动(好吧,如果你有一台速度极快的机器,时间因素就不会那么明显) - 在低端奔腾上,加载速度会很明显)。
它很可能是用 C/C++ 编写的。如果它是用汇编程序编写的,您可能会感到惊讶。
您可以自己检查一下 - 查看可执行文件的文件大小并将其与 .NET 的可执行文件进行比较。您可能会说该文件是压缩的,我对此表示怀疑,因此倾向于排除这一点,微软不会在压缩可执行文件业务方面走得那么远。
希望这能回答您的问题,
此致,
汤姆.
This is something that could be
The above three points could have a huge significant factor - when you think about it, when a .NET code is loaded, it gets jit'ted by the runtime (Ok, the time factor would not be noticeable if you have a blazing fast machine - on a lowly end pentium, it would be noticeable, sluggish loading).
More than likely it could have been written in either C/C++. It may surprise you if it was written in Assembler.
You can check this for yourself - look at the file size of the executable and compare it to a .NET's executable. You might argue that the file is compressed, which I doubt it would be and therefore be inclined to rule this out, Microsoft wouldn't go that far I reckon with the compressing executables business.
Hope this answers your question,
Best regards,
Tom.
fsutil 仅在 NTFS 和 exFAT 上速度快,在 FAT32、FAT16 上速度慢
这是因为某些文件系统具有“初始化大小”概念,因此支持快速文件初始化。这只是保留簇,但不会将它们清零,因为它在文件系统中指出没有数据写入文件,并且有效的读取都会返回填充 00 的缓冲区。
fsutil is only fast on NTFS and exFAT, not on FAT32, FAT16
This is because some file system have an "initialized size" concespt and thus support fast file initialization. This just reserves the clusters but does not zero them out, because it notes in the file system that no data was written to the file and valid reads would all return 00-filled buffers.