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