engines/glfs_async: add trim support
[fio.git] / engines / glusterfs_async.c
CommitLineData
cc47f094 1/*
2 * glusterfs engine
3 *
4 * IO engine using Glusterfs's gfapi async interface
5 *
6 */
7#include "gfapi.h"
88db2717 8#define NOT_YET 1
cc47f094 9struct fio_gf_iou {
10 struct io_u *io_u;
11 int io_complete;
12};
6fa14b99 13static ulong cb_count = 0, issued = 0;
cc47f094 14
15static struct io_u *fio_gf_event(struct thread_data *td, int event)
16{
17 struct gf_data *gf_data = td->io_ops->data;
18 dprint(FD_IO, "%s\n", __FUNCTION__);
19 return gf_data->aio_events[event];
20}
21
22static int fio_gf_getevents(struct thread_data *td, unsigned int min,
1f440ece 23 unsigned int max, const struct timespec *t)
cc47f094 24{
25 struct gf_data *g = td->io_ops->data;
26 unsigned int events = 0;
27 struct io_u *io_u;
28 int i = 0;
29 struct fio_gf_iou *io = NULL;
30
31 dprint(FD_IO, "%s\n", __FUNCTION__);
32 do {
33 io_u_qiter(&td->io_u_all, io_u, i) {
34 if (!(io_u->flags & IO_U_F_FLIGHT))
35 continue;
36
37 io = (struct fio_gf_iou *)io_u->engine_data;
38
39 if (io && io->io_complete) {
40 io->io_complete = 0;
41 g->aio_events[events] = io_u;
42 events++;
88db2717 43
b29c813f
JA
44 if (events >= max)
45 break;
cc47f094 46 }
47
48 }
49 if (events < min)
6fa14b99 50 usleep(100);
cc47f094 51 else
52 break;
53
54 } while (1);
55
56 return events;
57}
58
88db2717 59static void fio_gf_io_u_free(struct thread_data *td, struct io_u *io_u)
cc47f094 60{
88db2717 61 struct fio_gf_iou *io = io_u->engine_data;
62
63 if (io) {
b29c813f
JA
64 if (io->io_complete) {
65 log_err("incomplete IO found.\n");
66 }
88db2717 67 io_u->engine_data = NULL;
68 free(io);
69 }
7d4a8e7e 70 log_err("issued %lu finished %lu\n", issued, cb_count);
88db2717 71}
cc47f094 72
88db2717 73static int fio_gf_io_u_init(struct thread_data *td, struct io_u *io_u)
74{
75 struct fio_gf_iou *io = NULL;
cc47f094 76
88db2717 77 dprint(FD_FILE, "%s\n", __FUNCTION__);
78
b29c813f
JA
79 if (!io_u->engine_data) {
80 io = malloc(sizeof(struct fio_gf_iou));
81 if (!io) {
82 td_verror(td, errno, "malloc");
83 return 1;
84 }
85 io->io_complete = 0;
86 io->io_u = io_u;
87 io_u->engine_data = io;
88 }
cc47f094 89 return 0;
90}
91
b29c813f 92static void gf_async_cb(glfs_fd_t * fd, ssize_t ret, void *data)
cc47f094 93{
94 struct io_u *io_u = (struct io_u *)data;
b29c813f 95 struct fio_gf_iou *iou = (struct fio_gf_iou *)io_u->engine_data;
cc47f094 96
b29c813f
JA
97 dprint(FD_IO, "%s ret %lu\n", __FUNCTION__, ret);
98 iou->io_complete = 1;
99 cb_count++;
cc47f094 100}
101
b29c813f
JA
102static int fio_gf_async_queue(struct thread_data fio_unused * td,
103 struct io_u *io_u)
cc47f094 104{
105 struct gf_data *g = td->io_ops->data;
b29c813f 106 int r = 0;
cc47f094 107
b29c813f
JA
108 dprint(FD_IO, "%s op %s\n", __FUNCTION__,
109 io_u->ddir == DDIR_READ ? "read" : io_u->ddir ==
110 DDIR_WRITE ? "write" : io_u->ddir ==
111 DDIR_SYNC ? "sync" : "unknown");
88db2717 112
cc47f094 113 fio_ro_check(td, io_u);
114
115 if (io_u->ddir == DDIR_READ)
b29c813f
JA
116 r = glfs_pread_async(g->fd, io_u->xfer_buf, io_u->xfer_buflen,
117 io_u->offset, 0, gf_async_cb,
118 (void *)io_u);
cc47f094 119 else if (io_u->ddir == DDIR_WRITE)
b29c813f
JA
120 r = glfs_pwrite_async(g->fd, io_u->xfer_buf, io_u->xfer_buflen,
121 io_u->offset, 0, gf_async_cb,
122 (void *)io_u);
6876c98c
JA
123#if defined(CONFIG_GF_TRIM)
124 else if (io_u->ddir == DDIR_TRIM)
125 r = glfs_discard_async(g->fd, io_u->offset, io_u->xfer_buflen,
126 gf_async_cb, io_u);
127#endif
b29c813f
JA
128 else if (io_u->ddir == DDIR_SYNC) {
129 r = glfs_fsync_async(g->fd, gf_async_cb, (void *)io_u);
130 } else {
131 log_err("unsupported operation.\n");
132 io_u->error = -EINVAL;
133 goto failed;
134 }
135 if (r) {
136 log_err("glfs failed.\n");
137 io_u->error = r;
138 goto failed;
139 }
140 issued++;
cc47f094 141 return FIO_Q_QUEUED;
142
143failed:
144 io_u->error = r;
145 td_verror(td, io_u->error, "xfer");
146 return FIO_Q_COMPLETED;
147}
148
88db2717 149int fio_gf_async_setup(struct thread_data *td)
150{
151 int r = 0;
152 struct gf_data *g = NULL;
7d4a8e7e 153
88db2717 154#if defined(NOT_YET)
7d4a8e7e 155 log_err("the async interface is still very experimental...\n");
88db2717 156#endif
b29c813f
JA
157 r = fio_gf_setup(td);
158 if (r) {
159 return r;
160 }
6fa14b99 161 td->o.use_thread = 1;
b29c813f
JA
162 g = td->io_ops->data;
163 g->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
164 if (!g->aio_events) {
165 r = -ENOMEM;
166 fio_gf_cleanup(td);
167 return r;
168 }
88db2717 169
170 memset(g->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
171
b29c813f 172 return r;
88db2717 173
174}
175
176static int fio_gf_async_prep(struct thread_data *td, struct io_u *io_u)
177{
178 dprint(FD_FILE, "%s\n", __FUNCTION__);
179
180 if (!ddir_rw(io_u->ddir))
181 return 0;
182
183 return 0;
184}
cc47f094 185
186static struct ioengine_ops ioengine = {
b29c813f
JA
187 .name = "gfapi_async",
188 .version = FIO_IOOPS_VERSION,
189 .init = fio_gf_async_setup,
190 .cleanup = fio_gf_cleanup,
191 .prep = fio_gf_async_prep,
192 .queue = fio_gf_async_queue,
193 .open_file = fio_gf_open_file,
194 .close_file = fio_gf_close_file,
38ef9c90 195 .unlink_file = fio_gf_unlink_file,
b29c813f
JA
196 .get_file_size = fio_gf_get_file_size,
197 .getevents = fio_gf_getevents,
198 .event = fio_gf_event,
199 .io_u_init = fio_gf_io_u_init,
200 .io_u_free = fio_gf_io_u_free,
201 .options = gfapi_options,
cc47f094 202 .option_struct_size = sizeof(struct gf_options),
b29c813f 203 .flags = FIO_DISKLESSIO,
cc47f094 204};
205
206static void fio_init fio_gf_register(void)
207{
b29c813f 208 register_ioengine(&ioengine);
cc47f094 209}
210
211static void fio_exit fio_gf_unregister(void)
212{
b29c813f 213 unregister_ioengine(&ioengine);
cc47f094 214}