Why C++ fread is so different from Windows ReadFile?
-
2012년 2월 27일 월요일 오전 8:25
Greetings,
I write a piece of code to simulate system I/O perf. However the result is so different between fread (IOPS is 5000+) and Windows ReadFile (IOPS 200 ~ 300) for a 7200RPM HDD (it's 180 ~ 190 Random read IOPS by IOMeter). Could you give me a hint where's the problem?
Thanks.
Nai Yan.Code - Windows ReadFile (with cache)
DWORD WINAPI FileReadThreadEntry(LPVOID lpThreadParameter) { //File access with cache enabled input * in = (input*) lpThreadParameter; LPCTSTR path = (LPCTSTR) in->path; HANDLE fp = CreateFile( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,//|FILE_FLAG_NO_BUFFERING, NULL); unsigned int sPos = in->starting; int * result = in->r; if(fp != NULL) { unsigned long pos; bool bRead; DWORD bNum; for (int i=0; i<length/(threadCount*interval);i++) { pos = i * interval; if ((SetFilePointer(fp,(sPos+pos),NULL,FILE_BEGIN)!=0)) { if ((bRead = ReadFile(fp,&result[sPos/interval + i],512,&bNum,NULL))== true) { InterlockedIncrement(&completeIOs); } else { printf("file read err...\n"); exit(-1); } } } CloseHandle(fp); fp = NULL; } else { printf("File open err... \n"); exit(-1); } }C++ fread code
DWORD WINAPI FileReadThreadEntry(LPVOID lpThreadParameter) { input * in = (input*) lpThreadParameter; LPCTSTR path = (LPCTSTR) in->path; FILE * fp = fopen(path,"rb"); int sPos = in->starting; int * result = in->r; if(fp != NULL) { fpos_t pos; for (int i=0; i<length/(threadCount*interval);i++) { pos = i * interval; fsetpos(fp,&pos); if (fread(&result[sPos/interval + i],512,1,fp) ==1) { InterlockedIncrement(&completeIOs); } else { printf("file read err...\n"); exit(-1); } } fclose(fp); fp = NULL; } else { printf("File open err... \n"); exit(-1); } }
모든 응답
-
2012년 3월 1일 목요일 오후 7:48소유자
Hello,
This forum is for answering questions related to C++ AMP, PPL, ConcRT and other C++ related threading questions.
Since the ask is specifically around the performance of ReadFile, your best to get a response is to post here: http://answers.microsoft.com/en-us/windows/forum
Thanks!
Rahul V. Patil
- 답변으로 제안됨 Zhu, Weirong 2012년 3월 2일 금요일 오후 4:46
- 답변으로 표시됨 DanielMothMicrosoft Employee, Owner 2012년 6월 10일 일요일 오전 6:23

