More option help updates
[fio.git] / engines / guasi.c
1 /*
2  * guasi engine
3  *
4  * IO engine using the GUASI library.
5  *
6  * This is currently disabled. To enable it, execute:
7  *
8  * $ export EXTFLAGS="-DFIO_HAVE_GUASI"
9  * $ export EXTLIBS="-lguasi"
10  *
11  * before running make. You'll need the GUASI lib as well:
12  *
13  * http://www.xmailserver.org/guasi-lib.html
14  *
15  */
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <assert.h>
21
22 #include "../fio.h"
23
24 #ifdef FIO_HAVE_GUASI
25
26 #define GFIO_MIN_THREADS 32
27 #ifndef GFIO_MAX_THREADS
28 #define GFIO_MAX_THREADS 2000
29 #endif
30
31 #include <guasi.h>
32 #include <guasi_syscalls.h>
33
34 #ifdef GFIO_DEBUG
35 #define GDBG_PRINT(a) printf a
36 #else
37 #define GDBG_PRINT(a) (void) 0
38 #endif
39
40 struct guasi_data {
41         guasi_t hctx;
42         int max_reqs;
43         guasi_req_t *reqs;
44         struct io_u **io_us;
45         int queued_nr;
46         int reqs_nr;
47 };
48
49 static int fio_guasi_prep(struct thread_data fio_unused *td, struct io_u *io_u)
50 {
51
52         GDBG_PRINT(("fio_guasi_prep(%p)\n", io_u));
53         io_u->greq = NULL;
54
55         return 0;
56 }
57
58 static struct io_u *fio_guasi_event(struct thread_data *td, int event)
59 {
60         struct guasi_data *ld = td->io_ops->data;
61         struct io_u *io_u;
62         struct guasi_reqinfo rinf;
63
64         GDBG_PRINT(("fio_guasi_event(%d)\n", event));
65         if (guasi_req_info(ld->reqs[event], &rinf) < 0) {
66                 log_err("guasi_req_info(%d) FAILED!\n", event);
67                 return NULL;
68         }
69         io_u = rinf.asid;
70         io_u->error = EINPROGRESS;
71         GDBG_PRINT(("fio_guasi_event(%d) -> %p\n", event, io_u));
72         if (rinf.status == GUASI_STATUS_COMPLETE) {
73                 io_u->error = rinf.result;
74                 if (io_u->ddir == DDIR_READ ||
75                     io_u->ddir == DDIR_WRITE) {
76                         io_u->error = 0;
77                         if (rinf.result != (long) io_u->xfer_buflen) {
78                                 if (rinf.result >= 0)
79                                         io_u->resid = io_u->xfer_buflen - rinf.result;
80                                 else
81                                         io_u->error = rinf.error;
82                         }
83                 }
84         }
85
86         return io_u;
87 }
88
89 static int fio_guasi_getevents(struct thread_data *td, int min, int max,
90                                struct timespec *t)
91 {
92         struct guasi_data *ld = td->io_ops->data;
93         int n, r;
94         long timeo = -1;
95
96         GDBG_PRINT(("fio_guasi_getevents(%d, %d)\n", min, max));
97         if (min > ld->max_reqs)
98                 min = ld->max_reqs;
99         if (max > ld->max_reqs)
100                 max = ld->max_reqs;
101         if (t)
102                 timeo = t->tv_sec * 1000L + t->tv_nsec / 1000000L;
103         for (n = 0; n < ld->reqs_nr; n++)
104                 guasi_req_free(ld->reqs[n]);
105         n = 0;
106         do {
107                 r = guasi_fetch(ld->hctx, ld->reqs + n, min - n,
108                                 max - n, timeo);
109                 if (r < 0) {
110                         log_err("guasi_fetch() FAILED! (%d)\n", r);
111                         break;
112                 }
113                 n += r;
114                 if (n >= min)
115                         break;
116         } while (1);
117         ld->reqs_nr = n;
118         GDBG_PRINT(("fio_guasi_getevents() -> %d\n", n));
119
120         return n;
121 }
122
123 static int fio_guasi_queue(struct thread_data *td, struct io_u *io_u)
124 {
125         struct guasi_data *ld = td->io_ops->data;
126
127         GDBG_PRINT(("fio_guasi_queue(%p)\n", io_u));
128         if (ld->queued_nr == (int) td->o.iodepth)
129                 return FIO_Q_BUSY;
130
131         ld->io_us[ld->queued_nr] = io_u;
132         ld->queued_nr++;
133         return FIO_Q_QUEUED;
134 }
135
136 static void fio_guasi_queued(struct thread_data *td, struct io_u **io_us, int nr)
137 {
138         int i;
139         struct io_u *io_u;
140         struct timeval now;
141
142         fio_gettime(&now, NULL);
143         for (i = 0; i < nr; i++) {
144                 io_u = io_us[i];
145                 memcpy(&io_u->issue_time, &now, sizeof(now));
146                 io_u_queued(td, io_u);
147         }
148 }
149
150 static int fio_guasi_commit(struct thread_data *td)
151 {
152         struct guasi_data *ld = td->io_ops->data;
153         int i;
154         struct io_u *io_u;
155         struct fio_file *f;
156
157         GDBG_PRINT(("fio_guasi_commit(%d)\n", ld->queued_nr));
158         for (i = 0; i < ld->queued_nr; i++) {
159                 io_u = ld->io_us[i];
160                 GDBG_PRINT(("fio_guasi_commit(%d) --> %p\n", i, io_u));
161                 f = io_u->file;
162                 io_u->greq = NULL;
163                 if (io_u->ddir == DDIR_READ)
164                         io_u->greq = guasi__pread(ld->hctx, ld, io_u, 0,
165                                                   f->fd, io_u->xfer_buf, io_u->xfer_buflen,
166                                                   io_u->offset);
167                 else if (io_u->ddir == DDIR_WRITE)
168                         io_u->greq = guasi__pwrite(ld->hctx, ld, io_u, 0,
169                                                    f->fd, io_u->xfer_buf, io_u->xfer_buflen,
170                                                    io_u->offset);
171                 else if (io_u->ddir == DDIR_SYNC)
172                         io_u->greq = guasi__fsync(ld->hctx, ld, io_u, 0, f->fd);
173                 else {
174                         log_err("fio_guasi_commit() FAILED: unknow request %d\n",
175                                 io_u->ddir);
176                 }
177                 if (io_u->greq == NULL) {
178                         log_err("fio_guasi_commit() FAILED: submit failed (%s)\n",
179                                 strerror(errno));
180                         return -1;
181                 }
182         }
183         fio_guasi_queued(td, ld->io_us, i);
184         ld->queued_nr = 0;
185         GDBG_PRINT(("fio_guasi_commit() -> %d\n", i));
186
187         return 0;
188 }
189
190 static int fio_guasi_cancel(struct thread_data fio_unused *td,
191                             struct io_u *io_u)
192 {
193         GDBG_PRINT(("fio_guasi_cancel(%p) req=%p\n", io_u, io_u->greq));
194         if (io_u->greq != NULL)
195                 guasi_req_cancel(io_u->greq);
196
197         return 0;
198 }
199
200 static void fio_guasi_cleanup(struct thread_data *td)
201 {
202         struct guasi_data *ld = td->io_ops->data;
203         int n;
204
205         GDBG_PRINT(("fio_guasi_cleanup(%p)\n", ld));
206         if (ld) {
207                 for (n = 0; n < ld->reqs_nr; n++)
208                         guasi_req_free(ld->reqs[n]);
209                 guasi_free(ld->hctx);
210                 free(ld->reqs);
211                 free(ld->io_us);
212                 free(ld);
213                 td->io_ops->data = NULL;
214         }
215         GDBG_PRINT(("fio_guasi_cleanup(%p) DONE\n", ld));
216 }
217
218 static int fio_guasi_init(struct thread_data *td)
219 {
220         int maxthr;
221         struct guasi_data *ld = malloc(sizeof(*ld));
222
223         GDBG_PRINT(("fio_guasi_init(): depth=%d\n", td->o.iodepth));
224         memset(ld, 0, sizeof(*ld));
225         maxthr = td->o.iodepth > GFIO_MIN_THREADS ? td->o.iodepth: GFIO_MIN_THREADS;
226         if (maxthr > GFIO_MAX_THREADS)
227                 maxthr = GFIO_MAX_THREADS;
228         if ((ld->hctx = guasi_create(GFIO_MIN_THREADS, maxthr, 1)) == NULL) {
229                 td_verror(td, errno, "guasi_create");
230                 free(ld);
231                 return 1;
232         }
233         ld->max_reqs = td->o.iodepth;
234         ld->reqs = malloc(ld->max_reqs * sizeof(guasi_req_t));
235         ld->io_us = malloc(ld->max_reqs * sizeof(struct io_u *));
236         memset(ld->io_us, 0, ld->max_reqs * sizeof(struct io_u *));
237         ld->queued_nr = 0;
238         ld->reqs_nr = 0;
239
240         td->io_ops->data = ld;
241         GDBG_PRINT(("fio_guasi_init(): depth=%d -> %p\n", td->o.iodepth, ld));
242
243         return 0;
244 }
245
246 static struct ioengine_ops ioengine = {
247         .name           = "guasi",
248         .version        = FIO_IOOPS_VERSION,
249         .init           = fio_guasi_init,
250         .prep           = fio_guasi_prep,
251         .queue          = fio_guasi_queue,
252         .commit         = fio_guasi_commit,
253         .cancel         = fio_guasi_cancel,
254         .getevents      = fio_guasi_getevents,
255         .event          = fio_guasi_event,
256         .cleanup        = fio_guasi_cleanup,
257         .open_file      = generic_open_file,
258         .close_file     = generic_close_file,
259 };
260
261 #else /* FIO_HAVE_GUASI */
262
263 /*
264  * When we have a proper configure system in place, we simply wont build
265  * and install this io engine. For now install a crippled version that
266  * just complains and fails to load.
267  */
268 static int fio_guasi_init(struct thread_data fio_unused *td)
269 {
270         fprintf(stderr, "fio: guasi not available\n");
271         return 1;
272 }
273
274 static struct ioengine_ops ioengine = {
275         .name           = "guasi",
276         .version        = FIO_IOOPS_VERSION,
277         .init           = fio_guasi_init,
278 };
279
280 #endif
281
282 static void fio_init fio_guasi_register(void)
283 {
284         register_ioengine(&ioengine);
285 }
286
287 static void fio_exit fio_guasi_unregister(void)
288 {
289         unregister_ioengine(&ioengine);
290 }
291