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