-
Notifications
You must be signed in to change notification settings - Fork 0
/
iouringcp.cc
462 lines (392 loc) · 9.88 KB
/
iouringcp.cc
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include "liburing.h"
#include <iostream>
#define QD 64
#define BS (32*1024)
#define GET_LOWER_32BITS(v) ((v) & 0xFFFFFFFF)
static int infd, outfd;
char *infd_path = NULL, *outfd_path = NULL;
int speed_limitation = 0;
char *iuc_program_name = NULL;
bool check_io_uring = false;
char const iuc_short_opts[] = "I:O:S:ah";
const struct option iuc_long_opts[] = {
{"input", 1, NULL, 'I'},
{"output", 1, NULL, 'O'},
{"speed", 1, NULL, 'S'},
{"available", 0, NULL, 'a'},
{"help", 0, NULL, 'h'},
{0, 0, 0, 0}
};
void try_help() {
fprintf(stdout, "Try '%s --help' for more information.\n", iuc_program_name);
exit(1);
}
void help() {
static char const *const help_msg[] = {
"Copy file with io_uring",
"",
" -I, --input set the path of input file",
" -O, --output set the path of output file",
" -S, --speed set the speed limitaion (MB/s)",
" -a, --available check the environment for io_uring",
" -h, --help give this help message",
0
};
char const *const *p = help_msg;
while (*p) {
fprintf(stdout, "%s\n", *p++);
}
}
char* base_name(char *fname) {
char *p;
if ((p = strrchr(fname, '/')) != NULL) {
fname = p + 1;
}
return fname;
}
struct io_data {
int read;
off_t first_offset, offset;
size_t first_len;
struct iovec iov;
bool last_chunk;
size_t actual_size;
int retry;
};
static int setup_context(unsigned entries, struct io_uring *ring)
{
int ret;
ret = io_uring_queue_init(entries, ring, 0);
if (ret < 0) {
fprintf(stderr, "queue_init: %s\n", strerror(-ret));
return -1;
}
return 0;
}
static int get_file_size(int fd, off_t *size)
{
struct stat st;
if (fstat(fd, &st) < 0)
return -1;
if (S_ISREG(st.st_mode)) {
*size = st.st_size;
return 0;
} else if (S_ISBLK(st.st_mode)) {
unsigned long long bytes;
if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
return -1;
*size = bytes;
return 0;
}
return -1;
}
static void queue_prepped(struct io_uring *ring, struct io_data *data)
{
struct io_uring_sqe *sqe;
sqe = io_uring_get_sqe(ring);
assert(sqe);
if (data->read)
io_uring_prep_readv(sqe, infd, &data->iov, 1, data->offset);
else
io_uring_prep_writev(sqe, outfd, &data->iov, 1, data->offset);
io_uring_sqe_set_data(sqe, data);
}
static int queue_read(struct io_uring *ring, off_t size, off_t offset)
{
struct io_uring_sqe *sqe;
struct io_data *data;
void* buffer;
bool last_chunk = false;
data = static_cast<io_data*>(malloc(sizeof(*data)));
if (!data)
return 1;
if (size < BS) {
data->actual_size=size;
size = BS;
last_chunk = true;
}
buffer = aligned_alloc(4096, size);
sqe = io_uring_get_sqe(ring);
if (!sqe) {
free(buffer);
free(data);
return 1;
}
data->read = 1;
data->offset = data->first_offset = offset;
data->iov.iov_base = buffer;
data->iov.iov_len = size;
data->first_len = size;
data->last_chunk = last_chunk;
data->retry = 0;
io_uring_prep_readv(sqe, infd, &data->iov, 1, offset);
io_uring_sqe_set_data(sqe, data);
return 0;
}
static void queue_write(struct io_uring *ring, struct io_data *data)
{
data->read = 0;
data->offset = data->first_offset;
data->retry = 0;
queue_prepped(ring, data);
io_uring_submit(ring);
}
static int copy_file(struct io_uring *ring, off_t insize, struct io_data** last_data)
{
unsigned long reads, writes;
struct io_uring_cqe *cqe;
off_t write_left, offset;
int ret;
off_t speed_size = 0, speed_size_limitation = speed_limitation * 1024 * 1024 / 10;
useconds_t speed_val = 0;
struct timeval speed_time;
struct timeval speed_time_tmp;
write_left = insize;
writes = reads = offset = 0;
if (speed_limitation > 0) {
gettimeofday(&speed_time, NULL);
}
while (insize || write_left) {
unsigned long had_reads;
int got_comp;
if(speed_limitation > 0) {
gettimeofday(&speed_time_tmp, NULL);
speed_val = (speed_time_tmp.tv_sec * 1000000 + speed_time_tmp.tv_usec) - (speed_time.tv_sec * 1000000 + speed_time.tv_usec);
if(speed_val > 100000) {
speed_time.tv_sec = speed_time_tmp.tv_sec;
speed_time.tv_usec = speed_time_tmp.tv_usec;
speed_size = 0;
} else if (speed_size >= speed_size_limitation) {
useconds_t sleep_time = 100000 - speed_val;
usleep(sleep_time);
gettimeofday(&speed_time, NULL);
speed_size = 0;
}
}
/*
* Queue up as many reads as we can
*/
had_reads = reads;
while (insize) {
off_t this_size = insize;
if (reads + writes >= QD)
break;
if (this_size > BS)
this_size = BS;
else if (!this_size)
break;
if (queue_read(ring, this_size, offset))
break;
insize -= this_size;
offset += this_size;
speed_size += this_size;
reads++;
if(speed_limitation > 0 && speed_size >= speed_size_limitation) {
break;
}
}
if (had_reads != reads) {
ret = io_uring_submit(ring);
if (ret < 0) {
fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
break;
}
}
/*
* Queue is full at this point. Find at least one completion.
*/
got_comp = 0;
while (write_left) {
struct io_data *data;
if (!got_comp) {
ret = io_uring_wait_cqe(ring, &cqe);
got_comp = 1;
} else {
ret = io_uring_peek_cqe(ring, &cqe);
if (ret == -EAGAIN) {
cqe = NULL;
ret = 0;
}
}
if (ret < 0) {
fprintf(stderr, "io_uring_peek_cqe: %s\n",
strerror(-ret));
return 1;
}
if (!cqe)
break;
data = static_cast<io_data*>(io_uring_cqe_get_data(cqe));
if (cqe->res < 0) {
if (cqe->res == -EAGAIN) {
queue_prepped(ring, data);
io_uring_submit(ring);
io_uring_cqe_seen(ring, cqe);
continue;
}
fprintf(stderr, "cqe failed: %s\n",
strerror(-cqe->res));
return 1;
} else if (data->last_chunk) {
if ((size_t)cqe->res < data->actual_size) {
data->retry += 1;
if (data->retry < 10) {
queue_prepped(ring, data);
io_uring_submit(ring);
io_uring_cqe_seen(ring, cqe);
} else {
exit(1);
}
continue;
} else {
data->iov.iov_len = data->actual_size;
}
} else if ((size_t)cqe->res != data->iov.iov_len) {
/* Short read/write, adjust and requeue */
data->retry += 1;
if (data->retry < 10) {
queue_prepped(ring, data);
io_uring_submit(ring);
io_uring_cqe_seen(ring, cqe);
} else {
exit(1);
}
continue;
}
/*
* All done. if write, nothing else to do. if read,
* queue up corresponding write.
*/
if (data->read) {
if (data -> last_chunk != true) {
queue_write(ring, data);
writes++;
} else {
*last_data = data;
}
write_left -= data->iov.iov_len;
reads--;
} else {
free(data->iov.iov_base);
free(data);
writes--;
}
io_uring_cqe_seen(ring, cqe);
}
}
/* wait out pending writes */
while (writes) {
struct io_data *data;
ret = io_uring_wait_cqe(ring, &cqe);
if (ret) {
fprintf(stderr, "wait_cqe=%d\n", ret);
return 1;
}
if (cqe->res < 0) {
fprintf(stderr, "write res=%d\n", cqe->res);
return 1;
}
data = static_cast<io_data*>(io_uring_cqe_get_data(cqe));
free(data->iov.iov_base);
free(data);
writes--;
io_uring_cqe_seen(ring, cqe);
}
return 0;
}
int main(int argc, char *argv[])
{
struct io_uring ring;
off_t insize;
int ret;
struct io_data* last_data = NULL;
iuc_program_name = base_name(argv[0]);
while(true) {
int optc;
int long_idx = -1;
char *stop = NULL;
optc = getopt_long(argc, argv, iuc_short_opts, iuc_long_opts, &long_idx);
if (optc < 0) {
break;
}
switch (optc) {
case 'I':
infd_path = optarg;
break;
case 'O':
outfd_path = optarg;
break;
case 'S':
speed_limitation = GET_LOWER_32BITS(strtoul(optarg, &stop, 0));
if (*stop != '\0' || ERANGE == errno || speed_limitation <= 0) {
printf("Error speed limitation: %s\n", optarg);
return 1;
}
break;
case 'a':
check_io_uring = true;
break;
case 'h':
help();
return 0;
break;
default:
try_help();
}
}
if (check_io_uring) {
if (setup_context(QD, &ring)) {
printf("Do not support io_uring\n");
exit(1);
} else {
printf("Support io_uring\n");
io_uring_queue_exit(&ring);
exit(0);
}
}
if (!infd_path || !outfd_path) {
printf("Enter the input file and output file\n");
printf("%s -I infile -O outfile\n", iuc_program_name);
return 1;
}
infd = open(infd_path, O_RDONLY | O_DIRECT);
if (infd < 0) {
perror("open infile");
return 1;
}
outfd = open(outfd_path, O_WRONLY | O_CREAT | O_TRUNC | O_DIRECT, 0644);
if (outfd < 0) {
perror("open outfile");
return 1;
}
if (setup_context(QD, &ring)) {
perror("io_uring set");
return 1;
}
if (get_file_size(infd, &insize)) {
perror("file size");
return 1;
}
ret = copy_file(&ring, insize, &last_data);
close(infd);
close(outfd);
io_uring_queue_exit(&ring);
if (last_data != NULL) {
outfd = open(outfd_path, O_WRONLY);
pwritev(outfd, &last_data->iov, 1, last_data->offset);
close(outfd);
}
return ret;
}