开发者

WriteFile() with WRITE_THROUGH is slow on system disk

On Windows Server 2008 R2 the following very simple program behaves different on system disk and on any other disk. Seems that on system disk FILE_FLAG_WRITE_THROUGH makes something ...

On disk C: (system disk, commodity SATA, no RAID)

$ time ./test.exe

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

On disk E: (another physical SATA disk)

$ 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   buf开发者_如何学编程f[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. What's going on?
  2. Can somebody try to reproduce this on Win Server?


It was weird behavior of Samsung SATA 500GB disk (dying?) with SD15 firmware. We've fixed eventually this with dd utility and new disk. Everything works ok now. Sorry for "false alarm".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜