Avoid "ts" going out of scope.
[fio.git] / engines / guasi.c
CommitLineData
609342ff
DL
1/*
2 * guasi engine
3 *
4 * IO engine using the GUASI library.
5 *
46eaa157
DL
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:
609342ff
DL
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
41struct guasi_data {
42 guasi_t hctx;
43 int max_reqs;
44 guasi_req_t *reqs;
45 struct io_u **io_us;
46eaa157 46 int queued_nr;
609342ff
DL
47 int reqs_nr;
48};
49
50static int fio_guasi_prep(struct thread_data fio_unused *td, struct io_u *io_u)
51{
609342ff
DL
52
53 GDBG_PRINT(("fio_guasi_prep(%p)\n", io_u));
46eaa157 54 io_u->greq = NULL;
609342ff
DL
55
56 return 0;
57}
58
59static 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;
66b83d9a 71 io_u->error = EINPROGRESS;
609342ff 72 GDBG_PRINT(("fio_guasi_event(%d) -> %p\n", event, io_u));
66b83d9a 73 if (rinf.status == GUASI_STATUS_COMPLETE) {
609342ff 74 io_u->error = rinf.result;
66b83d9a
DL
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 }
609342ff
DL
86
87 return io_u;
88}
89
90static 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;
46eaa157 94 int n, r;
609342ff
DL
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;
46eaa157
DL
104 for (n = 0; n < ld->reqs_nr; n++)
105 guasi_req_free(ld->reqs[n]);
106 n = 0;
609342ff
DL
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);
46eaa157 115 ld->reqs_nr = n;
609342ff
DL
116 GDBG_PRINT(("fio_guasi_getevents() -> %d\n", n));
117
118 return n;
119}
120
121static 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));
46eaa157 126 if (ld->queued_nr == (int) td->o.iodepth)
609342ff
DL
127 return FIO_Q_BUSY;
128
46eaa157
DL
129 ld->io_us[ld->queued_nr] = io_u;
130 ld->queued_nr++;
609342ff
DL
131 return FIO_Q_QUEUED;
132}
133
134static 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
145static 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"));
46eaa157 153 for (i = 0; i < ld->queued_nr; i++) {
609342ff
DL
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 {
66b83d9a
DL
168 fprintf(stderr, "fio_guasi_commit() FAILED: unknow request %d\n",
169 io_u->ddir);
609342ff
DL
170 }
171 if (io_u->greq != NULL)
172 fio_guasi_queued(td, ld->io_us, i);
46eaa157
DL
173 else {
174 perror("guasi submit");
175 fprintf(stderr, "fio_guasi_commit() FAILED: submit failed\n");
176 return -1;
177 }
609342ff 178 }
46eaa157 179 ld->queued_nr = 0;
609342ff
DL
180 GDBG_PRINT(("fio_guasi_commit() -> %d\n", i));
181
182 return 0;
183}
184
185static 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);
46eaa157
DL
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);
609342ff 193
46eaa157 194 return 0;
609342ff
DL
195}
196
197static void fio_guasi_cleanup(struct thread_data *td)
198{
199 struct guasi_data *ld = td->io_ops->data;
200
46eaa157 201 GDBG_PRINT(("fio_guasi_cleanup(%p)\n", ld));
609342ff
DL
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 }
46eaa157 209 GDBG_PRINT(("fio_guasi_cleanup(%p) DONE\n", ld));
609342ff
DL
210}
211
212static int fio_guasi_init(struct thread_data *td)
213{
214 int maxthr;
215 struct guasi_data *ld = malloc(sizeof(*ld));
216
46eaa157 217 GDBG_PRINT(("fio_guasi_init(): depth=%d\n", td->o.iodepth));
609342ff 218 memset(ld, 0, sizeof(*ld));
46eaa157 219 maxthr = td->o.iodepth > GFIO_MIN_THREADS ? td->o.iodepth: GFIO_MIN_THREADS;
609342ff
DL
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 }
46eaa157 225 ld->max_reqs = td->o.iodepth;
609342ff
DL
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 *));
46eaa157 229 ld->queued_nr = 0;
609342ff
DL
230 ld->reqs_nr = 0;
231
232 td->io_ops->data = ld;
46eaa157 233 GDBG_PRINT(("fio_guasi_init(): depth=%d -> %p\n", td->o.iodepth, ld));
609342ff
DL
234
235 return 0;
236}
237
238static 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 */
260static int fio_guasi_init(struct thread_data fio_unused *td)
261{
262 fprintf(stderr, "fio: guasi not available\n");
263 return 1;
264}
265
266static struct ioengine_ops ioengine = {
267 .name = "guasi",
268 .version = FIO_IOOPS_VERSION,
269 .init = fio_guasi_init,
270};
271
272#endif
273
274static void fio_init fio_guasi_register(void)
275{
276 register_ioengine(&ioengine);
277}
278
279static void fio_exit fio_guasi_unregister(void)
280{
281 unregister_ioengine(&ioengine);
282}
283