unsigned vs signed warnings
[fio.git] / engines / syslet-rw.c
1 /*
2  * syslet engine
3  *
4  * IO engine that does regular pread(2)/pwrite(2) to transfer data, but
5  * with syslets to make the execution async.
6  *
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <assert.h>
13 #include <malloc.h>
14 #include <asm/unistd.h>
15
16 #include "../fio.h"
17 #include "../indirect.h"
18 #include "../syslet.h"
19
20 #ifdef FIO_HAVE_SYSLET
21
22 #ifdef __NR_pread64
23 #define __NR_fio_pread  __NR_pread64
24 #define __NR_fio_pwrite __NR_pwrite64
25 #else
26 #define __NR_fio_pread  __NR_pread
27 #define __NR_fio_pwrite __NR_pwrite
28 #endif
29
30 struct syslet_data {
31         struct io_u **events;
32         unsigned int nr_events;
33         
34         struct syslet_ring *ring;
35         unsigned int ring_mask;
36         void *stack;
37 };
38
39 static void fio_syslet_add_event(struct thread_data *td, struct io_u *io_u)
40 {
41         struct syslet_data *sd = td->io_ops->data;
42
43         assert(sd->nr_events < td->o.iodepth);
44         sd->events[sd->nr_events++] = io_u;
45 }
46
47 static void fio_syslet_add_events(struct thread_data *td, unsigned int nr)
48 {
49         struct syslet_data *sd = td->io_ops->data;
50         unsigned int i, uidx;
51
52         uidx = sd->ring->user_tail;
53         read_barrier();
54
55         for (i = 0; i < nr; i++) {
56                 unsigned int idx = (i + uidx) & sd->ring_mask;
57                 struct syslet_completion *comp = &sd->ring->comp[idx];
58                 struct io_u *io_u = (struct io_u *) (long) comp->caller_data;
59                 long ret;
60
61                 ret = comp->status;
62                 if (ret <= 0) {
63                         io_u->resid = io_u->xfer_buflen;
64                         io_u->error = -ret;
65                 } else {
66                         io_u->resid = io_u->xfer_buflen - ret;
67                         io_u->error = 0;
68                 }
69
70                 fio_syslet_add_event(td, io_u);
71         }
72 }
73
74 static void fio_syslet_wait_for_events(struct thread_data *td)
75 {
76         struct syslet_data *sd = td->io_ops->data;
77         struct syslet_ring *ring = sd->ring;
78
79         do {
80                 unsigned int kh = ring->kernel_head;
81                 int ret;
82
83                 /*
84                  * first reap events that are already completed
85                  */
86                 if (ring->user_tail != kh) {
87                         unsigned int nr = kh - ring->user_tail;
88
89                         fio_syslet_add_events(td, nr);
90                         ring->user_tail = kh;
91                         break;
92                 }
93
94                 /*
95                  * block waiting for at least one event
96                  */
97                 ret = syscall(__NR_syslet_ring_wait, ring, ring->user_tail);
98                 assert(!ret);
99         } while (1);
100 }
101
102 static int fio_syslet_getevents(struct thread_data *td, unsigned int min,
103                                 unsigned int fio_unused max,
104                                 struct timespec fio_unused *t)
105 {
106         struct syslet_data *sd = td->io_ops->data;
107         long ret;
108
109         /*
110          * While we have less events than requested, block waiting for them
111          * (if we have to, there may already be more completed events ready
112          * for us - see fio_syslet_wait_for_events()
113          */
114         while (sd->nr_events < min)
115                 fio_syslet_wait_for_events(td);
116
117         ret = sd->nr_events;
118         sd->nr_events = 0;
119         return ret;
120 }
121
122 static struct io_u *fio_syslet_event(struct thread_data *td, int event)
123 {
124         struct syslet_data *sd = td->io_ops->data;
125
126         return sd->events[event];
127 }
128
129 static void fio_syslet_prep_sync(struct fio_file *f,
130                                  struct indirect_registers *regs)
131 {
132         FILL_IN(*regs, __NR_fsync, (long) f->fd);
133 }
134
135 static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f,
136                                struct indirect_registers *regs)
137 {
138         long nr;
139
140         /*
141          * prepare rw
142          */
143         if (io_u->ddir == DDIR_READ)
144                 nr = __NR_fio_pread;
145         else
146                 nr = __NR_fio_pwrite;
147
148         FILL_IN(*regs, nr, (long) f->fd, (long) io_u->xfer_buf,
149                 (long) io_u->xfer_buflen, (long) io_u->offset);
150 }
151
152 static void fio_syslet_prep(struct io_u *io_u, struct indirect_registers *regs)
153 {
154         struct fio_file *f = io_u->file;
155
156         if (io_u->ddir == DDIR_SYNC)
157                 fio_syslet_prep_sync(f, regs);
158         else
159                 fio_syslet_prep_rw(io_u, f, regs);
160 }
161
162 static void ret_func(void)
163 {
164         syscall(__NR_exit);
165 }
166
167 static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u)
168 {
169         struct syslet_data *sd = td->io_ops->data;
170         union indirect_params params;
171         struct indirect_registers regs;
172         int ret;
173
174         fio_ro_check(td, io_u);
175
176         memset(&params, 0, sizeof(params));
177         fill_syslet_args(&params.syslet, sd->ring, (long)io_u, ret_func, sd->stack);
178
179         fio_syslet_prep(io_u, &regs);
180
181         ret = syscall(__NR_indirect, &regs, &params, sizeof(params), 0);
182         if (ret == (int) io_u->xfer_buflen) {
183                 /*
184                  * completed sync, account. this also catches fsync().
185                  */
186                 return FIO_Q_COMPLETED;
187         } else if (ret < 0) {
188                 /*
189                  * queued for async execution
190                  */
191                 if (errno == ESYSLETPENDING)
192                         return FIO_Q_QUEUED;
193         }
194
195         io_u->error = errno;
196         td_verror(td, io_u->error, "xfer");
197         return FIO_Q_COMPLETED;
198 }
199
200 static int check_syslet_support(struct syslet_data *sd)
201 {
202         union indirect_params params;
203         struct indirect_registers regs;
204         pid_t pid, my_pid = getpid();
205
206         memset(&params, 0, sizeof(params));
207         fill_syslet_args(&params.syslet, sd->ring, 0, ret_func, sd->stack);
208
209         FILL_IN(regs, __NR_getpid);
210
211         pid = syscall(__NR_indirect, &regs, &params, sizeof(params), 0);
212         if (pid == my_pid)
213                 return 0;
214
215         return 1;
216 }
217
218 static void fio_syslet_cleanup(struct thread_data *td)
219 {
220         struct syslet_data *sd = td->io_ops->data;
221
222         if (sd) {
223                 free(sd->events);
224                 free(sd->ring);
225                 free(sd);
226                 td->io_ops->data = NULL;
227         }
228 }
229
230 static int fio_syslet_init(struct thread_data *td)
231 {
232         struct syslet_data *sd;
233         void *ring = NULL, *stack = NULL;
234         unsigned int ring_size, ring_nr;
235
236         sd = malloc(sizeof(*sd));
237         memset(sd, 0, sizeof(*sd));
238
239         sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth);
240         memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth);
241
242         /*
243          * The ring needs to be a power-of-2, so round it up if we have to
244          */
245         ring_nr = td->o.iodepth;
246         if (ring_nr & (ring_nr - 1)) {
247                 int bits = 1;
248
249                 while (ring_nr >>= 1)
250                         bits++;
251
252                 ring_nr = 1 << bits;
253         }
254
255         ring_size = sizeof(struct syslet_ring) +
256                         ring_nr * sizeof(struct syslet_completion);
257         if (posix_memalign(&ring, sizeof(uint64_t), ring_size))
258                 goto err_mem;
259         if (posix_memalign(&stack, page_size, page_size))
260                 goto err_mem;
261
262         sd->ring = ring;
263         sd->ring_mask = ring_nr - 1;
264         sd->stack = stack;
265
266         memset(sd->ring, 0, ring_size);
267         sd->ring->elements = ring_nr;
268
269         if (!check_syslet_support(sd)) {
270                 td->io_ops->data = sd;
271                 return 0;
272         }
273
274         log_err("fio: syslets do not appear to work\n");
275 err_mem:
276         free(sd->events);
277         if (ring)
278                 free(ring);
279         if (stack)
280                 free(stack);
281         free(sd);
282         return 1;
283 }
284
285 static struct ioengine_ops ioengine = {
286         .name           = "syslet-rw",
287         .version        = FIO_IOOPS_VERSION,
288         .init           = fio_syslet_init,
289         .queue          = fio_syslet_queue,
290         .getevents      = fio_syslet_getevents,
291         .event          = fio_syslet_event,
292         .cleanup        = fio_syslet_cleanup,
293         .open_file      = generic_open_file,
294         .close_file     = generic_close_file,
295 };
296
297 #else /* FIO_HAVE_SYSLET */
298
299 /*
300  * When we have a proper configure system in place, we simply wont build
301  * and install this io engine. For now install a crippled version that
302  * just complains and fails to load.
303  */
304 static int fio_syslet_init(struct thread_data fio_unused *td)
305 {
306         fprintf(stderr, "fio: syslet not available\n");
307         return 1;
308 }
309
310 static struct ioengine_ops ioengine = {
311         .name           = "syslet-rw",
312         .version        = FIO_IOOPS_VERSION,
313         .init           = fio_syslet_init,
314 };
315
316 #endif /* FIO_HAVE_SYSLET */
317
318 static void fio_init fio_syslet_register(void)
319 {
320         register_ioengine(&ioengine);
321 }
322
323 static void fio_exit fio_syslet_unregister(void)
324 {
325         unregister_ioengine(&ioengine);
326 }