Why C++ fread is so different from Windows ReadFile?

Answered 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);
    	}
    }


모든 응답