Commit | Line | Data |
---|---|---|
2866c82d JA |
1 | /* |
2 | * native linux aio io engine | |
3 | * | |
4 | */ | |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | #include <unistd.h> | |
8 | #include <errno.h> | |
9 | #include <assert.h> | |
10 | #include "fio.h" | |
11 | #include "os.h" | |
12 | ||
13 | #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj) | |
14 | ||
15 | struct libaio_data { | |
16 | io_context_t aio_ctx; | |
17 | struct io_event *aio_events; | |
18 | }; | |
19 | ||
53cdc686 | 20 | static int fio_libaio_sync(struct thread_data *td, struct fio_file *f) |
2866c82d | 21 | { |
53cdc686 | 22 | return fsync(f->fd); |
2866c82d JA |
23 | } |
24 | ||
25 | static int fio_libaio_prep(struct thread_data *td, struct io_u *io_u) | |
26 | { | |
53cdc686 JA |
27 | struct fio_file *f = io_u->file; |
28 | ||
2866c82d | 29 | if (io_u->ddir == DDIR_READ) |
53cdc686 | 30 | io_prep_pread(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset); |
2866c82d | 31 | else |
53cdc686 | 32 | io_prep_pwrite(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset); |
2866c82d JA |
33 | |
34 | return 0; | |
35 | } | |
36 | ||
37 | static struct io_u *fio_libaio_event(struct thread_data *td, int event) | |
38 | { | |
39 | struct libaio_data *ld = td->io_ops->data; | |
40 | ||
41 | return ev_to_iou(ld->aio_events + event); | |
42 | } | |
43 | ||
44 | static int fio_libaio_getevents(struct thread_data *td, int min, int max, | |
45 | struct timespec *t) | |
46 | { | |
47 | struct libaio_data *ld = td->io_ops->data; | |
48 | long r; | |
49 | ||
50 | do { | |
51 | r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t); | |
52 | if (r == -EAGAIN) { | |
53 | usleep(100); | |
54 | continue; | |
55 | } else if (r == -EINTR) | |
56 | continue; | |
57 | else | |
58 | break; | |
59 | } while (1); | |
60 | ||
61 | return (int) r; | |
62 | } | |
63 | ||
64 | static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u) | |
65 | { | |
66 | struct libaio_data *ld = td->io_ops->data; | |
67 | struct iocb *iocb = &io_u->iocb; | |
68 | long ret; | |
69 | ||
70 | do { | |
71 | ret = io_submit(ld->aio_ctx, 1, &iocb); | |
72 | if (ret == 1) | |
73 | return 0; | |
74 | else if (ret == -EAGAIN) | |
75 | usleep(100); | |
76 | else if (ret == -EINTR) | |
77 | continue; | |
78 | else | |
79 | break; | |
80 | } while (1); | |
81 | ||
82 | return (int) ret; | |
83 | ||
84 | } | |
85 | ||
86 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) | |
87 | { | |
88 | struct libaio_data *ld = td->io_ops->data; | |
89 | ||
90 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); | |
91 | } | |
92 | ||
93 | static void fio_libaio_cleanup(struct thread_data *td) | |
94 | { | |
95 | struct libaio_data *ld = td->io_ops->data; | |
96 | ||
97 | if (ld) { | |
98 | io_destroy(ld->aio_ctx); | |
99 | if (ld->aio_events) | |
100 | free(ld->aio_events); | |
101 | ||
102 | free(ld); | |
103 | td->io_ops->data = NULL; | |
104 | } | |
105 | } | |
106 | ||
107 | static int fio_libaio_init(struct thread_data *td) | |
108 | { | |
109 | struct libaio_data *ld = malloc(sizeof(*ld)); | |
110 | ||
111 | memset(ld, 0, sizeof(*ld)); | |
112 | if (io_queue_init(td->iodepth, &ld->aio_ctx)) { | |
113 | td_verror(td, errno); | |
114 | return 1; | |
115 | } | |
116 | ||
117 | ld->aio_events = malloc(td->iodepth * sizeof(struct io_event)); | |
118 | td->io_ops->data = ld; | |
119 | return 0; | |
120 | } | |
121 | ||
122 | struct ioengine_ops ioengine = { | |
123 | .name = "libaio", | |
124 | .version = FIO_IOOPS_VERSION, | |
125 | .init = fio_libaio_init, | |
126 | .prep = fio_libaio_prep, | |
127 | .queue = fio_libaio_queue, | |
128 | .cancel = fio_libaio_cancel, | |
129 | .getevents = fio_libaio_getevents, | |
130 | .event = fio_libaio_event, | |
131 | .cleanup = fio_libaio_cleanup, | |
132 | .sync = fio_libaio_sync, | |
133 | }; |