blob: 5e394e3e477f0961ad65c8f65a619b1151c30ee6 [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
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
15struct libaio_data {
16 io_context_t aio_ctx;
17 struct io_event *aio_events;
18};
19
Jens Axboe7a16dd02006-10-18 17:21:58 +020020static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020021{
Jens Axboe53cdc682006-10-18 11:50:58 +020022 struct fio_file *f = io_u->file;
23
Jens Axboe2866c822006-10-09 15:57:48 +020024 if (io_u->ddir == DDIR_READ)
Jens Axboe53cdc682006-10-18 11:50:58 +020025 io_prep_pread(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020026 else if (io_u->ddir == DDIR_WRITE)
Jens Axboe53cdc682006-10-18 11:50:58 +020027 io_prep_pwrite(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020028 else if (io_u->ddir == DDIR_SYNC)
29 io_prep_fsync(&io_u->iocb, f->fd);
30 else
31 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +020032
33 return 0;
34}
35
36static struct io_u *fio_libaio_event(struct thread_data *td, int event)
37{
38 struct libaio_data *ld = td->io_ops->data;
39
40 return ev_to_iou(ld->aio_events + event);
41}
42
43static int fio_libaio_getevents(struct thread_data *td, int min, int max,
44 struct timespec *t)
45{
46 struct libaio_data *ld = td->io_ops->data;
47 long r;
48
49 do {
50 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
51 if (r == -EAGAIN) {
52 usleep(100);
53 continue;
54 } else if (r == -EINTR)
55 continue;
Jens Axboe84585002006-10-19 20:26:22 +020056 else if (r != 0)
Jens Axboe2866c822006-10-09 15:57:48 +020057 break;
58 } while (1);
59
Jens Axboe87dc1ab2006-10-24 14:41:26 +020060 return (int) -r;
Jens Axboe2866c822006-10-09 15:57:48 +020061}
62
63static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
64{
65 struct libaio_data *ld = td->io_ops->data;
66 struct iocb *iocb = &io_u->iocb;
67 long ret;
68
69 do {
70 ret = io_submit(ld->aio_ctx, 1, &iocb);
71 if (ret == 1)
72 return 0;
Jens Axboe84585002006-10-19 20:26:22 +020073 else if (ret == -EAGAIN || !ret)
Jens Axboe2866c822006-10-09 15:57:48 +020074 usleep(100);
75 else if (ret == -EINTR)
76 continue;
77 else
78 break;
79 } while (1);
80
Jens Axboe84585002006-10-19 20:26:22 +020081 assert(ret);
Jens Axboe2866c822006-10-09 15:57:48 +020082
Jens Axboe87dc1ab2006-10-24 14:41:26 +020083 return (int) -ret;
Jens Axboe2866c822006-10-09 15:57:48 +020084}
85
86static 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
93static 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
107static 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));
Jens Axboe84585002006-10-19 20:26:22 +0200118 memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
Jens Axboe2866c822006-10-09 15:57:48 +0200119 td->io_ops->data = ld;
120 return 0;
121}
122
123struct 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,
Jens Axboe2866c822006-10-09 15:57:48 +0200133};