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