带有WRITE_THROUGH的WriteFile()在系统磁盘上很慢

时间:2010-07-15 09:49:01

标签: winapi windows-server-2008

在Windows Server 2008 R2上,以下非常简单的程序在系统磁盘和任何其他磁盘上的行为都不同。似乎在系统盘上FILE_FLAG_WRITE_THROUGH会产生一些......

在磁盘C上:(系统磁盘,商品SATA,无RAID)

$ time ./test.exe

real    1m8.281s
user    0m0.000s
sys     0m0.015s

在磁盘E :(另一个物理SATA磁盘)

$ time ./test.exe

real    0m5.445s
user    0m0.000s
sys     0m0.000s

-

#include <stdio.h>
#include <windows.h>

#define BUFFER_SIZE   1024
#define ITERATIONS    10240
#define FILEFLAGS     FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH

BYTE   buff[BUFFER_SIZE];

BOOL SyncWriteFile(HANDLE fd,
                   LPCVOID buf,
                   DWORD to_write) {
    DWORD written = 0, already_written = 0;
 BOOL res = FALSE;

 while(written < to_write) {
  res = WriteFile(fd,
   (BYTE*)buf + written,
   to_write - written,
   &already_written,
   NULL);

  if (FALSE == res)
   return FALSE;
  else
   written += already_written;
 }
    return TRUE;
}

void main() 
{
 HANDLE hFile;
 int i;

 hFile = CreateFile(TEXT("test"),
     GENERIC_READ | GENERIC_WRITE,
     0,
     NULL,
     CREATE_NEW,
     FILEFLAGS,
     NULL);

 if (hFile == INVALID_HANDLE_VALUE) {
    fprintf(stderr, "Could not create test file."); 
    exit(1);
 }

 for(i=0; i<ITERATIONS; i++) {
  SyncWriteFile(hFile, buff, sizeof(buff));
 }

 CloseHandle(hFile);
}
  1. 发生了什么事?
  2. 有人可以尝试在Win Server上重现这个吗?

1 个答案:

答案 0 :(得分:0)