-
Notifications
You must be signed in to change notification settings - Fork 4
/
aio-test.c
339 lines (278 loc) · 7.24 KB
/
aio-test.c
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
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <linux/aio_abi.h>
#include "ccan/list/list.h"
#include "ccan/minmax/minmax.h"
// default values
#define OP_SIZE 512
#define NOPS 100000
#define WR_P 0
#define QUEUE_DEPTH 32
struct conf {
size_t op_size;
size_t nops;
unsigned wr_p;
char *filename;
size_t queue_depth;
bool buffered;
bool drop_caches;
};
struct io_op {
char *buff;
struct iocb iocb;
struct list_node lnode;
};
struct io_op_slab {
struct list_head io_ops;
size_t total_nops;
};
static inline long
io_setup(unsigned maxevents, aio_context_t *ctx) {
return syscall(SYS_io_setup, maxevents, ctx);
}
static inline long
io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp) {
return syscall(SYS_io_submit, ctx, nr, iocbpp);
}
static inline long
io_getevents(aio_context_t ctx, long min_nr, long nr,
struct io_event *events, struct timespec *timeout) {
return syscall(SYS_io_getevents, ctx, min_nr, nr, events, timeout);
}
static inline int
io_destroy(aio_context_t ctx) {
return syscall(SYS_io_destroy, ctx);
}
static void
drop_caches(void) {
int fd;
const char fname[] = "/proc/sys/vm/drop_caches";
char data[16];
snprintf(data, sizeof(data), "%d\n", 3);
if ((fd = open(fname, O_WRONLY)) == -1) {
fprintf(stderr, "Failed to drop caches: open %s: %s (requires root)\n", fname, strerror(errno));
exit(1);
}
size_t data_len = strnlen(data, sizeof(data));
if (write(fd, data, data_len) != data_len) {
fprintf(stderr, "Failed to drop caches: write %s: %s\n", fname, strerror(errno));
exit(1);
}
close(fd);
return;
}
static struct io_op *
alloc_io_op(size_t buff_size) {
int err;
struct io_op *ret = malloc(sizeof(*ret));
if (!ret)
return NULL;
err = posix_memalign((void **)&ret->buff, 4096, buff_size);
if (err) {
free(ret);
return NULL;
}
return ret;
}
static void
free_io_op(struct io_op *io) {
free(io->buff);
free(io);
}
static void
io_op_slab_init(struct io_op_slab *slab, size_t nops, size_t buff_size) {
list_head_init(&slab->io_ops);
for (size_t i=0; i<nops; i++) {
struct io_op *op = alloc_io_op(buff_size);
if (!op) {
fprintf(stderr, "alloc_io_op failed\n");
abort();
}
list_add_tail(&slab->io_ops, &op->lnode);
}
slab->total_nops = nops;
}
static void
io_op_slab_destroy(struct io_op_slab *slab) {
size_t nops = 0;
struct io_op *op;
while ((op = list_pop(&slab->io_ops, struct io_op, lnode))) {
free_io_op(op);
nops++;
}
if (nops != slab->total_nops)
fprintf(stderr, "Error: leaked io ops (freed:%zd, total:%zd)\n", nops, slab->total_nops);
slab->total_nops = 0;
}
static struct io_op *
get_io_op(struct io_op_slab *slab) {
return list_pop(&slab->io_ops, struct io_op, lnode);
}
static void
put_io_op(struct io_op_slab *slab, struct io_op *op) {
list_add(&slab->io_ops, &op->lnode);
}
static inline void
io_op_fill_iocb(struct io_op *op, int fd, size_t total_size, struct conf *cnf) {
size_t nblocks = total_size / cnf->op_size;
size_t block = rand() % nblocks;
size_t offset = block*cnf->op_size;
unsigned wr_p = 1 + (rand() % 100);
memset(&op->iocb, 0, sizeof(op->iocb));
op->iocb.aio_buf = (uintptr_t)op->buff;
op->iocb.aio_data = (uintptr_t)op;
op->iocb.aio_fildes = fd;
op->iocb.aio_lio_opcode = (wr_p <= cnf->wr_p) ? IOCB_CMD_PWRITE : IOCB_CMD_PREAD;
op->iocb.aio_reqprio = 0;
op->iocb.aio_nbytes = cnf->op_size;
op->iocb.aio_offset = offset;
}
static void
print_help(FILE *f, char *p) {
fprintf(f,"Usage: %s [-o op_size] [-n nops] [-w write_percentage] -q queue_depth] [-b (for buffered IO)] [-d (drop caches)] -f filename\n", p);
}
static void
parse_conf(struct conf *cnf, int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, "ho:n:w:f:c:q:bd")) != -1) {
switch (opt) {
case 'o':
cnf->op_size = atol(optarg);
break;
case 'n':
cnf->nops = atol(optarg);
break;
case 'w':
cnf->wr_p = atol(optarg);
break;
case 'f':
cnf->filename = optarg;
break;
case 'q':
cnf->queue_depth = atol(optarg);
break;
case 'b':
cnf->buffered = true;
break;
case 'd':
cnf->drop_caches = true;
break;
case 'h':
print_help(stdout, argv[0]);
exit(0);
default:
print_help(stderr, argv[0]);
exit(1);
}
}
}
static int
open_file(struct conf *cnf) {
int fd, oflags;
if (cnf->wr_p == 0) {
oflags = O_RDONLY;
} else if (cnf->wr_p == 100) {
oflags = O_WRONLY;
} else if (cnf->wr_p < 100) {
oflags = O_RDWR;
} else {
fprintf(stderr, "percentage %u is >100\n", cnf->wr_p);
exit(1);
}
if (!cnf->buffered)
oflags |= O_DIRECT;
if ((fd = open(cnf->filename, oflags, S_IRUSR | S_IWUSR)) == -1) {
perror(cnf->filename);
exit(1);
}
return fd;
}
int main(int argc, char *argv[])
{
struct conf cnf = {
.filename = NULL,
.op_size = OP_SIZE,
.nops = NOPS,
.wr_p = WR_P,
.queue_depth = QUEUE_DEPTH,
.buffered = false,
.drop_caches = false,
};
parse_conf(&cnf, argc, argv);
if (!cnf.filename) {
print_help(stderr, argv[0]);
exit(1);
}
printf("CONF: filename:%s op_size:%zd nops:%zd wr_p:%u queue_depth:%zd buffered:%u drop_caches:%u\n",
cnf.filename, cnf.op_size, cnf.nops, cnf.wr_p, cnf.queue_depth, cnf.buffered, cnf.drop_caches);
int fd;
struct stat st;
aio_context_t ioctx = 0;
struct io_op_slab slab;
fd = open_file(&cnf);
if (fstat(fd, &st) == -1) {
perror("fstat");
exit(1);
}
assert(st.st_size >= cnf.op_size);
if (io_setup(cnf.queue_depth, &ioctx) < 0) {
perror("io_setup");
exit(1);
}
io_op_slab_init(&slab, cnf.queue_depth, cnf.op_size);
if (cnf.drop_caches)
drop_caches();
struct iocb *iocb_ptrs[cnf.queue_depth];
struct io_event io_events[cnf.queue_depth];
size_t submitted = 0, completed = 0;
while (completed < cnf.nops) {
long ret;
assert(submitted >= completed);
size_t in_flight = submitted - completed;
assert(in_flight <= cnf.queue_depth);
size_t to_submit = min(cnf.queue_depth - in_flight, cnf.nops - submitted);
for (size_t i=0; i<to_submit; i++) {
struct io_op *op = get_io_op(&slab);
assert(op);
io_op_fill_iocb(op, fd, st.st_size, &cnf);
iocb_ptrs[i] = &op->iocb;
}
ret = io_submit(ioctx, to_submit, iocb_ptrs);
if (ret < 0) {
perror("io_submit");
exit(1);
} else if (ret != to_submit) {
fprintf(stderr, "Partial success (%zd instead of %zd). Bailing out\n", ret, to_submit);
exit(1);
}
submitted += to_submit;
ret = io_getevents(ioctx, 0 /* min */, submitted - completed, io_events, NULL);
if (ret < 0) {
perror("io_getevents");
exit(1);
}
size_t to_complete = ret;
for (size_t i=0; i<to_complete; i++) {
struct io_event *ev = &io_events[i];
if (ev->res2 != 0 || ev->res != cnf.op_size)
fprintf(stderr, "******************** Event returned with res=%lld res2=%lld\n", ev->res, ev->res2);
struct io_op *op = (void *)ev->data;
put_io_op(&slab, op);
}
completed += to_complete;
printf("in_flight:%zd submitted:%zd (total:%zd) completed:%zd (total:%zd)\n", submitted - completed, to_submit, submitted, to_complete, completed);
}
io_op_slab_destroy(&slab);
io_destroy(ioctx);
close(fd);
return 0;
}