Update documentation wrt comment
[fio.git] / engines / syslet-rw.c
CommitLineData
a4f4fdd7
JA
1/*
2 * read/write() engine that uses syslet to be async
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
10
11#include "../fio.h"
12#include "../os.h"
13
14#ifdef FIO_HAVE_SYSLET
15
16struct syslet_data {
17 struct io_u **events;
18 unsigned int nr_events;
19
2ca50be4 20 struct async_head_user *ahu;
a4f4fdd7
JA
21 struct syslet_uatom **ring;
22 unsigned int ring_index;
23};
24
25/*
26 * Inspect the ring to see if we have completed events
27 */
28static void fio_syslet_complete(struct thread_data *td)
29{
30 struct syslet_data *sd = td->io_ops->data;
31
32 do {
33 struct syslet_uatom *atom;
34 struct io_u *io_u;
35 long ret;
36
37 atom = sd->ring[sd->ring_index];
38 if (!atom)
39 break;
40
41 sd->ring[sd->ring_index] = NULL;
42 if (++sd->ring_index == td->iodepth)
43 sd->ring_index = 0;
44
45 io_u = atom->private;
46 ret = *atom->ret_ptr;
47 if (ret > 0)
48 io_u->resid = io_u->xfer_buflen - ret;
49 else if (ret < 0)
50 io_u->error = ret;
51
52 sd->events[sd->nr_events++] = io_u;
53 } while (1);
54}
55
56static int fio_syslet_getevents(struct thread_data *td, int min,
57 int fio_unused max,
58 struct timespec fio_unused *t)
59{
60 struct syslet_data *sd = td->io_ops->data;
61 int get_events;
62 long ret;
63
64 do {
65 fio_syslet_complete(td);
66
67 /*
68 * do we have enough immediate completions?
69 */
70 if (sd->nr_events >= (unsigned int) min)
71 break;
72
73 /*
74 * OK, we need to wait for some events...
75 */
76 get_events = min - sd->nr_events;
77 ret = async_wait(get_events);
78 if (ret < 0)
79 return errno;
80 } while (1);
81
82 ret = sd->nr_events;
83 sd->nr_events = 0;
84 return ret;
85}
86
87static struct io_u *fio_syslet_event(struct thread_data *td, int event)
88{
89 struct syslet_data *sd = td->io_ops->data;
90
91 return sd->events[event];
92}
93
94static void init_atom(struct syslet_uatom *atom, int nr, void *arg0,
a2e1b08a
JA
95 void *arg1, void *arg2, void *arg3, void *ret_ptr,
96 unsigned long flags, void *priv)
a4f4fdd7
JA
97{
98 atom->flags = flags;
99 atom->nr = nr;
100 atom->ret_ptr = ret_ptr;
a2e1b08a 101 atom->next = NULL;
a4f4fdd7
JA
102 atom->arg_ptr[0] = arg0;
103 atom->arg_ptr[1] = arg1;
104 atom->arg_ptr[2] = arg2;
a2e1b08a
JA
105 atom->arg_ptr[3] = arg3;
106 atom->arg_ptr[4] = atom->arg_ptr[5] = NULL;
a4f4fdd7
JA
107 atom->private = priv;
108}
109
110/*
111 * Use seek atom for sync
112 */
113static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f)
114{
a2e1b08a 115 init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL,
7d44a745 116 &io_u->req.ret, 0, io_u);
a4f4fdd7
JA
117}
118
119static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f)
120{
121 int nr;
122
a4f4fdd7
JA
123 /*
124 * prepare rw
125 */
126 if (io_u->ddir == DDIR_READ)
a2e1b08a 127 nr = __NR_pread64;
a4f4fdd7 128 else
a2e1b08a 129 nr = __NR_pwrite64;
a4f4fdd7 130
a2e1b08a 131 init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf,
7d44a745 132 &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u);
a4f4fdd7
JA
133}
134
135static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u)
136{
137 struct fio_file *f = io_u->file;
138
139 if (io_u->ddir == DDIR_SYNC)
140 fio_syslet_prep_sync(io_u, f);
141 else
142 fio_syslet_prep_rw(io_u, f);
143
144 return 0;
145}
146
147static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
148{
149 struct syslet_data *sd = td->io_ops->data;
a4f4fdd7
JA
150 long ret;
151
7d44a745
JA
152 /*
153 * On sync completion, the atom is returned. So on NULL return
154 * it's queued asynchronously.
155 */
156 if (!async_exec(&io_u->req.atom))
36167d82 157 return FIO_Q_QUEUED;
a4f4fdd7
JA
158
159 /*
160 * completed sync
161 */
a2e1b08a 162 ret = io_u->req.ret;
a4f4fdd7
JA
163 if (ret != (long) io_u->xfer_buflen) {
164 if (ret > 0) {
165 io_u->resid = io_u->xfer_buflen - ret;
166 io_u->error = 0;
36167d82 167 return FIO_Q_COMPLETED;
a4f4fdd7
JA
168 } else
169 io_u->error = errno;
170 }
171
172 if (!io_u->error)
173 sd->events[sd->nr_events++] = io_u;
174 else
175 td_verror(td, io_u->error);
176
36167d82 177 return FIO_Q_COMPLETED;
a4f4fdd7
JA
178}
179
db64e9bc 180static int async_head_init(struct syslet_data *sd, unsigned int depth)
a4f4fdd7 181{
a4f4fdd7
JA
182 unsigned long ring_size;
183
2ca50be4
JA
184 sd->ahu = malloc(sizeof(struct async_head_user));
185 memset(sd->ahu, 0, sizeof(struct async_head_user));
186
a4f4fdd7
JA
187 ring_size = sizeof(struct syslet_uatom *) * depth;
188 sd->ring = malloc(ring_size);
189 memset(sd->ring, 0, ring_size);
190
2ca50be4
JA
191 sd->ahu->completion_ring = sd->ring;
192 sd->ahu->ring_size_bytes = ring_size;
193 sd->ahu->max_nr_threads = -1;
a4f4fdd7 194
2ca50be4 195 if (async_register(sd->ahu, sizeof(*sd->ahu)) < 0) {
a4f4fdd7 196 perror("async_register");
db64e9bc
JA
197 fprintf(stderr, "fio: syslet likely not supported\n");
198 free(sd->ring);
2ca50be4 199 free(sd->ahu);
db64e9bc
JA
200 return 1;
201 }
202
203 return 0;
a4f4fdd7
JA
204}
205
2ca50be4 206static void async_head_exit(struct syslet_data *sd)
a4f4fdd7 207{
2ca50be4 208 if (async_unregister(sd->ahu, sizeof(*sd->ahu)) < 0)
a4f4fdd7 209 perror("async_register");
7f059a76
JA
210
211 free(sd->ahu);
212 free(sd->ring);
a4f4fdd7
JA
213}
214
215static void fio_syslet_cleanup(struct thread_data *td)
216{
217 struct syslet_data *sd = td->io_ops->data;
218
219 if (sd) {
2ca50be4 220 async_head_exit(sd);
a4f4fdd7
JA
221 free(sd->events);
222 free(sd);
223 td->io_ops->data = NULL;
224 }
225}
226
227static int fio_syslet_init(struct thread_data *td)
228{
229 struct syslet_data *sd;
230
db64e9bc 231
a4f4fdd7
JA
232 sd = malloc(sizeof(*sd));
233 memset(sd, 0, sizeof(*sd));
234 sd->events = malloc(sizeof(struct io_u *) * td->iodepth);
235 memset(sd->events, 0, sizeof(struct io_u *) * td->iodepth);
db64e9bc
JA
236
237 /*
238 * This will handily fail for kernels where syslet isn't available
239 */
240 if (async_head_init(sd, td->iodepth)) {
241 free(sd->events);
242 free(sd);
243 return 1;
244 }
245
a4f4fdd7 246 td->io_ops->data = sd;
a4f4fdd7
JA
247 return 0;
248}
249
250static struct ioengine_ops ioengine = {
251 .name = "syslet-rw",
252 .version = FIO_IOOPS_VERSION,
253 .init = fio_syslet_init,
254 .prep = fio_syslet_prep,
255 .queue = fio_syslet_queue,
256 .getevents = fio_syslet_getevents,
257 .event = fio_syslet_event,
258 .cleanup = fio_syslet_cleanup,
259};
260
261#else /* FIO_HAVE_SYSLET */
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 */
268static int fio_syslet_init(struct thread_data fio_unused *td)
269{
270 fprintf(stderr, "fio: syslet not available\n");
271 return 1;
272}
273
274static struct ioengine_ops ioengine = {
275 .name = "syslet-rw",
276 .version = FIO_IOOPS_VERSION,
277 .init = fio_syslet_init,
278};
279
280#endif /* FIO_HAVE_SYSLET */
281
282static void fio_init fio_syslet_register(void)
283{
284 register_ioengine(&ioengine);
285}
286
287static void fio_exit fio_syslet_unregister(void)
288{
289 unregister_ioengine(&ioengine);
290}