预读处理

// FileOverlap.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <Windows.h>
 
int cbCount = 0;
BOOL printfDetail = FALSE;
int loop_count = 200;
int successCount = 0;
int failedCount = 0;
 
#define COMPUTE_TIME(fun)  \
{ \
	DWORD begin = GetTickCount();\
	fun;\
	DWORD end = GetTickCount();\
	printf("\r\n %s: loopCount:%d - cbCount:%d - successCount:%d - timeUsed:%d", #fun, loop_count, cbCount, successCount, end - begin);\
}
 
 
void CALLBACK MyFileIOCompletionRoutine(DWORD dwErrorCode,  DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)
{
	if(printfDetail) {
		printf("\r\ndwErrorCode:%d - dwNumber:%d - offset:%d", dwErrorCode, dwNumberOfBytesTransfered, lpOverlapped->Offset);
	}	
	cbCount++;
	if(dwNumberOfBytesTransfered > 0) {
		successCount++;
	}
}
 
void useReadFileEx(LPCTSTR file) 
{
	HANDLE hFile = CreateFile(file, GENERIC_READ, 
		FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN | FILE_FLAG_OVERLAPPED, NULL);
	if(hFile == INVALID_HANDLE_VALUE) {
		printf("bad file");
		return;
	}
 
	OVERLAPPED overlap = {0};
	const DWORD dwStepSize = static_cast<DWORD>(1024 * 1024 * 1);
	const DWORD dwReadSize = dwStepSize - 3072;
	LPVOID buffer = ::VirtualAlloc( NULL, dwStepSize, MEM_COMMIT, PAGE_READWRITE);	
	for(int i = 0; i < loop_count; i++)
	{
		overlap.Offset = (i % 1024) * dwStepSize;
		::ReadFileEx(hFile, buffer, dwReadSize, &overlap, MyFileIOCompletionRoutine);
	}
	if(printfDetail){
		printf("\r\nready wait.....");
	}
	while(loop_count >  cbCount){
		SleepEx(0, TRUE);
	}
	::VirtualFree(buffer, 0, MEM_RELEASE);
	::CloseHandle(hFile);
	if(printfDetail){
		printf("\r\nuseReadFileEx:cbCount:%d ", cbCount);
	}
}
 
void useReadFile(LPCTSTR file)
{
	HANDLE hFile = CreateFile(file, GENERIC_READ, 
		FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN | FILE_FLAG_OVERLAPPED, NULL);
	if(hFile == INVALID_HANDLE_VALUE) {
		printf("bad file");
		return;
	}
	DWORD len = 0;
	OVERLAPPED overlap = {0};
	const DWORD dwStepSize = static_cast<DWORD>(1024 * 1024 * 1);
	const DWORD dwReadSize = dwStepSize - 3072;
	LPVOID buffer = ::VirtualAlloc( NULL, dwStepSize, MEM_COMMIT, PAGE_READWRITE);	
	for(int i = 0; i < loop_count; i++) {		
		overlap.Offset = (i % 1024) * dwStepSize;
		if(!::ReadFile(hFile, buffer, dwReadSize, &len, &overlap)) {
			if(GetLastError() == ERROR_IO_PENDING) {
				GetOverlappedResult(hFile, &overlap, &len, TRUE);
			}
		}
	}
	::VirtualFree(buffer, 0, MEM_RELEASE);
	::CloseHandle(hFile);
}
 
void useMoreReadFileEx(TCHAR (*files)[MAX_PATH], int count)
{
#if 1
	for(int i = 0; i < count; i++) {
		useReadFileEx(files[i]);
	}
#else
	OVERLAPPED overlap = {0};	
	const DWORD dwStepSize = static_cast<DWORD>(1024 * 1024 * 1);
	const DWORD dwReadSize = dwStepSize - 3072;
	LPVOID buffer = ::VirtualAlloc( NULL, dwStepSize, MEM_COMMIT, PAGE_READWRITE);	
	for(int j = 0; j < count; j++) {
		HANDLE hFile = CreateFile(files[j], GENERIC_READ, 
			FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN | FILE_FLAG_OVERLAPPED, NULL);
		if(hFile == INVALID_HANDLE_VALUE) {
			printf("bad file");
			return;
		}
		for(int i = 0; i < loop_count; i++)
		{
			overlap.Offset = (i % 1024) * dwStepSize;
			::ReadFileEx(hFile, buffer, dwReadSize, &overlap, MyFileIOCompletionRoutine);
		}
	}
	SleepEx(0, TRUE);
#endif
}
 
void useMoreReadFile(TCHAR (*files)[MAX_PATH], int count)
{
	for(int i = 0; i < count; i++) {
		useReadFile(files[i]);
	}
}
 
int _tmain(int argc, _TCHAR* argv[])
{
	TCHAR path[MAX_PATH] = _T("c:\\package.zip");
	TCHAR files[10][MAX_PATH];
	int fileCount = 1;
	if(argc < 3) {
		printf("\r\nexample1: 1024 c:\1.zip");
		printf("\r\nexample1: 1024 c:\1.zip 1");
		printf("\r\nexample1: 1024 c:\1.zip 1 2");
		printf("\r\nexample2: 200 c:\cc|1.zip,2.zip");
		printf("\r\nexample2: 200 c:\cc|1.zip,2.zip 1");
		printf("\r\nexample2: 1024 c:\cc|1.zip,2.zip 1 2");
		return 0;
	}
	loop_count = _ttoi(argv[1]);
	_tcscpy(path, argv[2]);
	if(argc >= 3) {
		TCHAR *pmain = _tcstok(path, _T("|"));
		if(pmain != NULL) {
			TCHAR *pfile = _tcstok(NULL,  _T(","));
			int i = 0;
			for(i = 0; i < 10 && pfile != NULL; i++) {
				_stprintf(files[i], _T("%s\\%s"), pmain, pfile);
				pfile = _tcstok(NULL,  _T(","));
			}
			fileCount = i;
		}
	}
 
	if(argc > 3) {
		printfDetail = argc > 4;		
		if(fileCount > 1) {
			COMPUTE_TIME(useMoreReadFileEx(files, fileCount));
		}else{
			COMPUTE_TIME(useReadFileEx(path));
		}		
	}else if(argc == 3) {
		if(fileCount > 1) {
			COMPUTE_TIME(useMoreReadFile(files, fileCount));
		}else{
			COMPUTE_TIME(useReadFile(path));
		}
	}
	getchar();
	return 0;
}