Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Native Solaris async IO engine |
| 3 | * |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 8 | #include <signal.h> |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | |
| 11 | #include "../fio.h" |
| 12 | |
| 13 | #ifdef FIO_HAVE_SOLARISAIO |
| 14 | |
| 15 | #include <sys/asynch.h> |
| 16 | |
| 17 | struct solarisaio_data { |
| 18 | struct io_u **aio_events; |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 19 | unsigned int aio_pending; |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 20 | unsigned int nr; |
Jens Axboe | 5cf56c0 | 2008-06-02 13:23:34 +0200 | [diff] [blame] | 21 | unsigned int max_depth; |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | static int fio_solarisaio_cancel(struct thread_data fio_unused *td, |
| 25 | struct io_u *io_u) |
| 26 | { |
| 27 | return aiocancel(&io_u->resultp); |
| 28 | } |
| 29 | |
| 30 | static int fio_solarisaio_prep(struct thread_data fio_unused *td, |
| 31 | struct io_u *io_u) |
| 32 | { |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 33 | struct solarisaio_data *sd = td->io_ops->data; |
| 34 | |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 35 | io_u->resultp.aio_return = AIO_INPROGRESS; |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 36 | io_u->engine_data = sd; |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 40 | static void wait_for_event(struct timeval *tv) |
| 41 | { |
| 42 | struct solarisaio_data *sd; |
| 43 | struct io_u *io_u; |
| 44 | aio_result_t *res; |
| 45 | |
| 46 | res = aiowait(tv); |
| 47 | if (res == (aio_result_t *) -1) { |
| 48 | int err = errno; |
| 49 | |
| 50 | if (err != EINVAL) { |
| 51 | log_err("fio: solarisaio got %d in aiowait\n", err); |
| 52 | exit(err); |
| 53 | } |
| 54 | return; |
| 55 | } else if (!res) |
| 56 | return; |
| 57 | |
| 58 | io_u = container_of(res, struct io_u, resultp); |
| 59 | sd = io_u->engine_data; |
| 60 | |
| 61 | if (io_u->resultp.aio_return >= 0) { |
| 62 | io_u->resid = io_u->xfer_buflen - io_u->resultp.aio_return; |
| 63 | io_u->error = 0; |
| 64 | } else |
Jens Axboe | 983b770 | 2009-08-04 12:49:36 +0200 | [diff] [blame] | 65 | io_u->error = io_u->resultp.aio_errno; |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * For SIGIO, we need a write barrier between the two, so that |
| 69 | * the ->aio_pending store is seen after the ->aio_events store |
| 70 | */ |
| 71 | sd->aio_events[sd->aio_pending] = io_u; |
Jens Axboe | 44c47fe | 2008-06-04 14:31:25 +0200 | [diff] [blame] | 72 | write_barrier(); |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 73 | sd->aio_pending++; |
| 74 | sd->nr--; |
| 75 | } |
| 76 | |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 77 | static int fio_solarisaio_getevents(struct thread_data *td, unsigned int min, |
| 78 | unsigned int max, struct timespec *t) |
| 79 | { |
| 80 | struct solarisaio_data *sd = td->io_ops->data; |
| 81 | struct timeval tv; |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 82 | int ret; |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 83 | |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 84 | if (!min || !t) { |
| 85 | tv.tv_sec = 0; |
| 86 | tv.tv_usec = 0; |
| 87 | } else { |
| 88 | tv.tv_sec = t->tv_sec; |
| 89 | tv.tv_usec = t->tv_nsec / 1000; |
| 90 | } |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 91 | |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 92 | while (sd->aio_pending < min) |
| 93 | wait_for_event(&tv); |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 94 | |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 95 | /* |
Jens Axboe | 44c47fe | 2008-06-04 14:31:25 +0200 | [diff] [blame] | 96 | * should be OK without locking, as int operations should be atomic |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 97 | */ |
| 98 | ret = sd->aio_pending; |
Jens Axboe | 44c47fe | 2008-06-04 14:31:25 +0200 | [diff] [blame] | 99 | sd->aio_pending -= ret; |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 100 | return ret; |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static struct io_u *fio_solarisaio_event(struct thread_data *td, int event) |
| 104 | { |
| 105 | struct solarisaio_data *sd = td->io_ops->data; |
| 106 | |
| 107 | return sd->aio_events[event]; |
| 108 | } |
| 109 | |
| 110 | static int fio_solarisaio_queue(struct thread_data fio_unused *td, |
| 111 | struct io_u *io_u) |
| 112 | { |
| 113 | struct solarisaio_data *sd = td->io_ops->data; |
| 114 | struct fio_file *f = io_u->file; |
| 115 | off_t off; |
| 116 | int ret; |
| 117 | |
| 118 | fio_ro_check(td, io_u); |
| 119 | |
| 120 | if (io_u->ddir == DDIR_SYNC) { |
| 121 | if (sd->nr) |
| 122 | return FIO_Q_BUSY; |
| 123 | if (fsync(f->fd) < 0) |
| 124 | io_u->error = errno; |
| 125 | |
| 126 | return FIO_Q_COMPLETED; |
| 127 | } |
| 128 | |
Jens Axboe | 5f9099e | 2009-06-16 22:40:26 +0200 | [diff] [blame] | 129 | if (io_u->ddir == DDIR_DATASYNC) { |
| 130 | if (sd->nr) |
| 131 | return FIO_Q_BUSY; |
| 132 | if (fdatasync(f->fd) < 0) |
| 133 | io_u->error = errno; |
| 134 | |
| 135 | return FIO_Q_COMPLETED; |
| 136 | } |
| 137 | |
Jens Axboe | 5cf56c0 | 2008-06-02 13:23:34 +0200 | [diff] [blame] | 138 | if (sd->nr == sd->max_depth) |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 139 | return FIO_Q_BUSY; |
| 140 | |
| 141 | off = io_u->offset; |
| 142 | if (io_u->ddir == DDIR_READ) |
| 143 | ret = aioread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, off, |
| 144 | SEEK_SET, &io_u->resultp); |
| 145 | else |
| 146 | ret = aiowrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, off, |
| 147 | SEEK_SET, &io_u->resultp); |
| 148 | if (ret) { |
| 149 | io_u->error = errno; |
| 150 | td_verror(td, io_u->error, "xfer"); |
| 151 | return FIO_Q_COMPLETED; |
| 152 | } |
| 153 | |
| 154 | sd->nr++; |
| 155 | return FIO_Q_QUEUED; |
| 156 | } |
| 157 | |
| 158 | static void fio_solarisaio_cleanup(struct thread_data *td) |
| 159 | { |
| 160 | struct solarisaio_data *sd = td->io_ops->data; |
| 161 | |
| 162 | if (sd) { |
| 163 | free(sd->aio_events); |
| 164 | free(sd); |
| 165 | } |
| 166 | } |
| 167 | |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 168 | /* |
Jens Axboe | 44c47fe | 2008-06-04 14:31:25 +0200 | [diff] [blame] | 169 | * Set USE_SIGNAL_COMPLETIONS to use SIGIO as completion events. |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 170 | */ |
Jens Axboe | da28353 | 2008-06-04 19:57:47 +0200 | [diff] [blame] | 171 | #ifdef USE_SIGNAL_COMPLETIONS |
| 172 | static void fio_solarisaio_sigio(int sig) |
| 173 | { |
| 174 | wait_for_event(NULL); |
| 175 | } |
| 176 | |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 177 | static void fio_solarisaio_init_sigio(void) |
| 178 | { |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 179 | struct sigaction act; |
| 180 | |
| 181 | memset(&act, 0, sizeof(act)); |
| 182 | act.sa_handler = fio_solarisaio_sigio; |
| 183 | act.sa_flags = SA_RESTART; |
| 184 | sigaction(SIGIO, &act, NULL); |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 185 | } |
Jens Axboe | da28353 | 2008-06-04 19:57:47 +0200 | [diff] [blame] | 186 | #endif |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 187 | |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 188 | static int fio_solarisaio_init(struct thread_data *td) |
| 189 | { |
| 190 | struct solarisaio_data *sd = malloc(sizeof(*sd)); |
Jens Axboe | 5cf56c0 | 2008-06-02 13:23:34 +0200 | [diff] [blame] | 191 | unsigned int max_depth; |
| 192 | |
| 193 | max_depth = td->o.iodepth; |
| 194 | if (max_depth > MAXASYNCHIO) { |
| 195 | max_depth = MAXASYNCHIO; |
| 196 | log_info("fio: lower depth to %d due to OS constraints\n", |
| 197 | max_depth); |
| 198 | } |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 199 | |
| 200 | memset(sd, 0, sizeof(*sd)); |
Jens Axboe | 5cf56c0 | 2008-06-02 13:23:34 +0200 | [diff] [blame] | 201 | sd->aio_events = malloc(max_depth * sizeof(struct io_u *)); |
| 202 | memset(sd->aio_events, 0, max_depth * sizeof(struct io_u *)); |
| 203 | sd->max_depth = max_depth; |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 204 | |
Jens Axboe | da28353 | 2008-06-04 19:57:47 +0200 | [diff] [blame] | 205 | #ifdef USE_SIGNAL_COMPLETIONS |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 206 | fio_solarisaio_init_sigio(); |
Jens Axboe | da28353 | 2008-06-04 19:57:47 +0200 | [diff] [blame] | 207 | #endif |
Jens Axboe | 556e831 | 2008-06-02 15:01:16 +0200 | [diff] [blame] | 208 | |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 209 | td->io_ops->data = sd; |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static struct ioengine_ops ioengine = { |
| 214 | .name = "solarisaio", |
| 215 | .version = FIO_IOOPS_VERSION, |
| 216 | .init = fio_solarisaio_init, |
| 217 | .prep = fio_solarisaio_prep, |
| 218 | .queue = fio_solarisaio_queue, |
| 219 | .cancel = fio_solarisaio_cancel, |
| 220 | .getevents = fio_solarisaio_getevents, |
| 221 | .event = fio_solarisaio_event, |
| 222 | .cleanup = fio_solarisaio_cleanup, |
| 223 | .open_file = generic_open_file, |
| 224 | .close_file = generic_close_file, |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 225 | .get_file_size = generic_get_file_size, |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | #else /* FIO_HAVE_SOLARISAIO */ |
| 229 | |
| 230 | /* |
| 231 | * When we have a proper configure system in place, we simply wont build |
| 232 | * and install this io engine. For now install a crippled version that |
| 233 | * just complains and fails to load. |
| 234 | */ |
| 235 | static int fio_solarisaio_init(struct thread_data fio_unused *td) |
| 236 | { |
Jens Axboe | a3edaf7 | 2010-09-26 10:53:40 +0900 | [diff] [blame] | 237 | log_err("fio: solarisaio not available\n"); |
Jens Axboe | 417f006 | 2008-06-02 11:59:30 +0200 | [diff] [blame] | 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | static struct ioengine_ops ioengine = { |
| 242 | .name = "solarisaio", |
| 243 | .version = FIO_IOOPS_VERSION, |
| 244 | .init = fio_solarisaio_init, |
| 245 | }; |
| 246 | |
| 247 | #endif |
| 248 | |
| 249 | static void fio_init fio_solarisaio_register(void) |
| 250 | { |
| 251 | register_ioengine(&ioengine); |
| 252 | } |
| 253 | |
| 254 | static void fio_exit fio_solarisaio_unregister(void) |
| 255 | { |
| 256 | unregister_ioengine(&ioengine); |
| 257 | } |