-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrent_warmup.cpp
331 lines (273 loc) · 7.1 KB
/
concurrent_warmup.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "concurrent_warmup.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <thread>
namespace ww {
static size_t max_size = 1024 * 1024;
static size_t total_size = max_size * 2;
std::string ErrStr(int err_no) {
char *e = strerror(errno);
return e ? e : "no error message";
}
size_t Size(const std::string &file_name) {
FILE *fp = fopen(file_name.c_str(), "r");
if (fp == NULL) {
LOG_ERROR << "open file error";
return -1;
}
fseek(fp, 0L, SEEK_END);
size_t res = ftell(fp);
fclose(fp);
return res;
}
struct OutputBuffer {
OutputBuffer() {
ptr = nullptr;
fd = -1;
cur_pos = 0;
};
OutputBuffer(const std::string &file_name, size_t n) {
cur_pos = 0;
ptr = (char *)malloc(n);
if (!ptr) {
LOG_ERROR << "malloc " << n << ", error: " << ErrStr(errno);
}
fd = open(file_name.c_str(), O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
if (fd < 0) {
LOG_ERROR << "open file error: " << file_name;
}
}
~OutputBuffer() {
if (ptr) free(ptr);
if (fd > 0) close(fd);
}
OutputBuffer(OutputBuffer &&other) {
ptr = other.ptr;
fd = other.fd;
cur_pos = other.cur_pos;
other.ptr = nullptr;
other.fd = -1;
other.cur_pos = 0;
}
OutputBuffer &operator=(OutputBuffer &&other) {
ptr = other.ptr;
fd = other.fd;
cur_pos = other.cur_pos;
other.ptr = nullptr;
other.fd = -1;
other.cur_pos = 0;
return *this;
}
OutputBuffer(const OutputBuffer &) = delete;
OutputBuffer &operator=(const OutputBuffer &) = delete;
char *ptr;
int fd;
size_t cur_pos;
};
struct InputBuffer {
InputBuffer() {
ptr = nullptr;
size = 0;
cur_pos = 0;
};
InputBuffer(const std::string file_name) {
ptr = nullptr;
size = 0;
cur_pos = 0;
if (0 == access(file_name.c_str(), R_OK)) {
int fd = open(file_name.c_str(), O_RDONLY);
if (fd < 0) {
LOG_ERROR << "open file failed: " << file_name;
return;
}
size = Size(file_name.c_str());
if (size == 0) {
LOG_ERROR << "empty file: " << file_name;
return;
}
ptr = (char *)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (ptr) {
LOG_INFO << "mmap ok, ptr: " << (void *)ptr << ", size: " << size;
} else {
LOG_ERROR << "mmap error: " << ErrStr(errno);
}
close(fd);
}
}
~InputBuffer() {
if (ptr) {
if (0 == munmap(ptr, size)) {
LOG_INFO << "munmap ok, ptr: " << (void *)ptr << ", size: " << size;
} else {
LOG_ERROR << "munmap error: " << ErrStr(errno)
<< ", ptr: " << (void *)ptr << ", size: " << size;
;
}
}
}
InputBuffer(InputBuffer &&other) {
ptr = other.ptr;
cur_pos = other.cur_pos;
size = other.size;
other.ptr = nullptr;
other.cur_pos = 0;
other.size = 0;
}
InputBuffer &operator=(InputBuffer &&other) {
ptr = other.ptr;
cur_pos = other.cur_pos;
size = other.size;
other.ptr = nullptr;
other.cur_pos = 0;
other.size = 0;
return *this;
}
InputBuffer(const OutputBuffer &) = delete;
InputBuffer &operator=(const OutputBuffer &) = delete;
char *ptr;
size_t size;
size_t cur_pos;
};
size_t WriteN(int fd, const void *vptr, size_t n) {
size_t nleft;
size_t nwritten;
const char *ptr;
ptr = (const char *)vptr;
nleft = n;
while (nleft > 0) {
if ((nwritten = write(fd, ptr, nleft)) <= 0) {
if (nwritten < 0 && errno == EINTR) {
nwritten = 0;
LOG_ERROR << "EINTR error and call write() again, total bytes: " << n
<< ", left: " << nleft;
} else {
LOG_ERROR << "write error: " << ErrStr(errno) << "total bytes: " << n
<< ", left: " << nleft;
return (-1);
}
}
nleft -= nwritten;
ptr += nwritten;
}
return n;
}
thread_local OutputBuffer wt_buf;
thread_local InputBuffer rd_buf;
int WriteToBuf(const void *buf, size_t bytes, bool immediate) {
if (!wt_buf.ptr) {
if (WriteN(wt_buf.fd, buf, bytes) != bytes) {
return -1;
}
return 0;
}
if (wt_buf.cur_pos >= max_size || (immediate && wt_buf.cur_pos > 0)) {
if (WriteN(wt_buf.fd, wt_buf.ptr, wt_buf.cur_pos) != wt_buf.cur_pos) {
return -1;
}
wt_buf.cur_pos = 0;
}
if (buf) {
if (bytes <= max_size) {
memcpy(wt_buf.ptr + wt_buf.cur_pos, buf, bytes);
wt_buf.cur_pos += bytes;
} else {
if (WriteN(wt_buf.fd, buf, bytes) != bytes) {
return -1;
}
}
}
return 0;
}
int ReadFromBuf(void *target, size_t bytes) {
if (!rd_buf.ptr) {
LOG_ERROR << "null read buffer ptr";
return -2;
}
size_t new_pos = rd_buf.cur_pos + bytes;
if (new_pos > rd_buf.size) {
return -1; // end
}
memcpy(target, rd_buf.ptr + rd_buf.cur_pos, bytes);
rd_buf.cur_pos = new_pos;
return 0;
}
int DoWriteTask(WriteTaskFunc write_task_func, const std::string &file_name) {
wt_buf = OutputBuffer(file_name, total_size);
LOG_DEBUG << "wt buffer addr: " << (void *)wt_buf.ptr
<< ", write cur pos addr: " << &wt_buf.cur_pos
<< ", file name: " << file_name;
int res0 = write_task_func();
int res1 = WriteToBuf(NULL, 0, true);
if (res0 != 0 || res1 != 0) {
return -1;
}
return 0;
}
int ReadTask(QuickDeserializeFunc deserialize_func,
const std::string &file_name) {
rd_buf = InputBuffer(file_name);
LOG_DEBUG << "rd buffer addr: " << (void *)rd_buf.ptr
<< ", read cur pos addr: " << &rd_buf.cur_pos
<< ", file name: " << file_name;
return deserialize_func();
}
int ConcurrentWarmup(DeserializeFunc deserialize_func,
const std::string &file_name, int thread_cnt) {
if (thread_cnt <= 0) {
thread_cnt = 2 * std::thread::hardware_concurrency();
}
if (thread_cnt > 100) {
thread_cnt = 100;
}
ThreadPool thread_pool(thread_cnt);
std::vector<std::future<int>> futures;
for (int i = 0; i < 1000; ++i) {
auto split_name = file_name + std::to_string(i);
if (0 == access(split_name.c_str(), R_OK)) {
futures.emplace_back(thread_pool.push(deserialize_func, split_name));
} else {
break;
}
}
int res = 0;
for (auto &future : futures) {
if (future.get() != 0) {
res = -1;
}
}
return res;
}
int ConcurrentWarmup(QuickDeserializeFunc deserialize_func,
const std::string &file_name, int thread_cnt) {
if (thread_cnt <= 0) {
thread_cnt = 2 * std::thread::hardware_concurrency();
}
if (thread_cnt > 100) {
thread_cnt = 100;
}
ThreadPool thread_pool(thread_cnt);
std::vector<std::future<int>> futures;
for (int i = 0; i < 1000; ++i) {
auto split_name = file_name + std::to_string(i);
if (0 == access(split_name.c_str(), R_OK)) {
futures.emplace_back(
thread_pool.push(&ReadTask, deserialize_func, split_name));
} else {
break;
}
}
int res = 0;
for (auto &future : futures) {
if (future.get() != 0) {
res = -1;
}
}
return res;
}
} // namespace ww