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