-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
140 lines (117 loc) · 4.02 KB
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <ctime>
int32_t g_bytesTransferred;
const std::string getFilePrompt(const std::string& msg)
{
std::string str;
std::cout << msg;
std::cin >> str;
return str;
}
void FileIOCompletionRoutine(DWORD dwErrorCode, DWORD dwNumberOfBytesTransferred, LPOVERLAPPED pol)
{
g_bytesTransferred = dwNumberOfBytesTransferred;
}
void copyFile(const std::string& file, const std::string& dest, const int32_t bufferSize, const int32_t threadsAmount)
{
std::vector<char*>buffers;
for(size_t i = 0; i < threadsAmount; ++i)
buffers.push_back(new char[bufferSize]);
const HANDLE originFileHandle = CreateFileA(file.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
NULL);
if(originFileHandle == INVALID_HANDLE_VALUE)
{
std::cout << "Origin file is not opened. Last error code: " << GetLastError() << std::endl;
return;
}
const HANDLE destFileHandle = CreateFileA(dest.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
NULL);
if(destFileHandle == INVALID_HANDLE_VALUE)
{
std::cout << "Destination file is not opened. Last error code: " << GetLastError() << std::endl;
return;
}
int32_t shift = 0;
int32_t readBytes = 0;
int32_t lastBuffer = -1;
int32_t readLast = -1;
std::vector<OVERLAPPED> overlappeds(threadsAmount);
auto t0 = clock();
do
{
for(size_t i = 0; i < threadsAmount; ++i)
{
overlappeds[i].Offset = (shift + i) * bufferSize;
if(!ReadFileEx(originFileHandle, buffers[i], bufferSize, &overlappeds[i], FileIOCompletionRoutine))
{
std::cout << "Error upon reading! Last error: " << GetLastError();
break;
}
SleepEx(1000, TRUE);
if(lastBuffer == -1 && g_bytesTransferred != bufferSize)
{
lastBuffer = i;
readLast = g_bytesTransferred;
}
}
for(size_t i = 0; i < threadsAmount; ++i)
{
if(lastBuffer == -1 || lastBuffer >= i)
{
overlappeds[i].Offset = (shift + i) * bufferSize;
if(!WriteFileEx(destFileHandle, buffers[i], bufferSize, &overlappeds[i], FileIOCompletionRoutine))
{
std::cout << "Error upon writing! Last error: " << GetLastError();
break;
}
SleepEx(1000, TRUE);
}
}
shift += threadsAmount;
}while(lastBuffer == -1);
auto t1 = clock();
const int32_t fileSize = (shift - threadsAmount + lastBuffer) * bufferSize + readLast;
SetFilePointer(destFileHandle, fileSize, NULL, FILE_BEGIN);
SetEndOfFile(destFileHandle);
CloseHandle(originFileHandle);
CloseHandle(destFileHandle);
std::cout << "Buffer size: " << std::setw(5) << bufferSize << "B " << std::setw(0)
<< "Threads: " << std::setw(2) << threadsAmount << std::setw(0)
<< " Speed: "<< (double)fileSize / 1024 / ((double)(t1 - t0) / CLOCKS_PER_SEC) << "KB/s" << std::endl;
for(char* buffer : buffers)
delete[] buffer;
std::cout << std::endl << "Running file comparator..." << std::endl;
system((std::string("FC ") + file + std::string(" ") + dest).c_str());
}
int main()
{
constexpr int32_t clusterSize = 512;
std::vector<int32_t> bufferSizes{clusterSize};
for(int i = 1; i <= 10; ++i)
bufferSizes.push_back(bufferSizes[bufferSizes.size() - 1] * 2);
std::vector<int32_t> threadsAmounts{1, 2, 4, 8, 12, 16};
const std::string originFile = getFilePrompt("File to copy: ");
const std::string destFile = getFilePrompt("Destination: ");
// checking for the best block size
for(const int32_t bufferSize : bufferSizes)
copyFile(originFile, destFile, bufferSize, 1);
// checking for the best threads amount
for(const int32_t threadsAmount : threadsAmounts)
copyFile(originFile, destFile, 4096, threadsAmount);
system("pause");
return 0;
}