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