dfaa375a1e0aa6a06c91d97a6e54d93d9fb87041
[fio.git] / engines / solarisaio.c
1 /*
2  * Native Solaris async IO engine
3  *
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <signal.h>
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;
19         unsigned int aio_pending;
20         unsigned int nr;
21         unsigned int max_depth;
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 {
33         struct solarisaio_data *sd = td->io_ops->data;
34
35         io_u->resultp.aio_return = AIO_INPROGRESS;
36         io_u->engine_data = sd;
37         return 0;
38 }
39
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
65                 io_u->error = io_u->resultp.aio_return;
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;
72         write_barrier();
73         sd->aio_pending++;
74         sd->nr--;
75 }
76
77 static void fio_solarisaio_sigio(int sig)
78 {
79         wait_for_event(NULL);
80 }
81
82 static int fio_solarisaio_getevents(struct thread_data *td, unsigned int min,
83                                     unsigned int max, struct timespec *t)
84 {
85         struct solarisaio_data *sd = td->io_ops->data;
86         struct timeval tv;
87         int ret;
88
89         if (!min || !t) {
90                 tv.tv_sec = 0;
91                 tv.tv_usec = 0;
92         } else {
93                 tv.tv_sec = t->tv_sec;
94                 tv.tv_usec = t->tv_nsec / 1000;
95         }
96
97         while (sd->aio_pending < min)
98                 wait_for_event(&tv);
99
100         /*
101          * should be OK without locking, as int operations should be atomic
102          */
103         ret = sd->aio_pending;
104         sd->aio_pending -= ret;
105         return ret;
106 }
107
108 static struct io_u *fio_solarisaio_event(struct thread_data *td, int event)
109 {
110         struct solarisaio_data *sd = td->io_ops->data;
111
112         return sd->aio_events[event];
113 }
114
115 static int fio_solarisaio_queue(struct thread_data fio_unused *td,
116                               struct io_u *io_u)
117 {
118         struct solarisaio_data *sd = td->io_ops->data;
119         struct fio_file *f = io_u->file;
120         off_t off;
121         int ret;
122
123         fio_ro_check(td, io_u);
124
125         if (io_u->ddir == DDIR_SYNC) {
126                 if (sd->nr)
127                         return FIO_Q_BUSY;
128                 if (fsync(f->fd) < 0)
129                         io_u->error = errno;
130
131                 return FIO_Q_COMPLETED;
132         }
133
134         if (sd->nr == sd->max_depth)
135                 return FIO_Q_BUSY;
136
137         off = io_u->offset;
138         if (io_u->ddir == DDIR_READ)
139                 ret = aioread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, off,
140                                         SEEK_SET, &io_u->resultp);
141         else
142                 ret = aiowrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, off,
143                                         SEEK_SET, &io_u->resultp);
144         if (ret) {
145                 io_u->error = errno;
146                 td_verror(td, io_u->error, "xfer");
147                 return FIO_Q_COMPLETED;
148         }
149
150         sd->nr++;
151         return FIO_Q_QUEUED;
152 }
153
154 static void fio_solarisaio_cleanup(struct thread_data *td)
155 {
156         struct solarisaio_data *sd = td->io_ops->data;
157
158         if (sd) {
159                 free(sd->aio_events);
160                 free(sd);
161         }
162 }
163
164 /*
165  * Set USE_SIGNAL_COMPLETIONS to use SIGIO as completion events.
166  */
167 static void fio_solarisaio_init_sigio(void)
168 {
169 #ifdef USE_SIGNAL_COMPLETIONS
170         struct sigaction act;
171
172         memset(&act, 0, sizeof(act));
173         act.sa_handler = fio_solarisaio_sigio;
174         act.sa_flags = SA_RESTART;
175         sigaction(SIGIO, &act, NULL);
176 #endif
177 }
178
179 static int fio_solarisaio_init(struct thread_data *td)
180 {
181         struct solarisaio_data *sd = malloc(sizeof(*sd));
182         unsigned int max_depth;
183
184         max_depth = td->o.iodepth;
185         if (max_depth > MAXASYNCHIO) {
186                 max_depth = MAXASYNCHIO;
187                 log_info("fio: lower depth to %d due to OS constraints\n",
188                                                         max_depth);
189         }
190
191         memset(sd, 0, sizeof(*sd));
192         sd->aio_events = malloc(max_depth * sizeof(struct io_u *));
193         memset(sd->aio_events, 0, max_depth * sizeof(struct io_u *));
194         sd->max_depth = max_depth;
195
196         fio_solarisaio_init_sigio();
197
198         td->io_ops->data = sd;
199         return 0;
200 }
201
202 static struct ioengine_ops ioengine = {
203         .name           = "solarisaio",
204         .version        = FIO_IOOPS_VERSION,
205         .init           = fio_solarisaio_init,
206         .prep           = fio_solarisaio_prep,
207         .queue          = fio_solarisaio_queue,
208         .cancel         = fio_solarisaio_cancel,
209         .getevents      = fio_solarisaio_getevents,
210         .event          = fio_solarisaio_event,
211         .cleanup        = fio_solarisaio_cleanup,
212         .open_file      = generic_open_file,
213         .close_file     = generic_close_file,
214 };
215
216 #else /* FIO_HAVE_SOLARISAIO */
217
218 /*
219  * When we have a proper configure system in place, we simply wont build
220  * and install this io engine. For now install a crippled version that
221  * just complains and fails to load.
222  */
223 static int fio_solarisaio_init(struct thread_data fio_unused *td)
224 {
225         fprintf(stderr, "fio: solarisaio not available\n");
226         return 1;
227 }
228
229 static struct ioengine_ops ioengine = {
230         .name           = "solarisaio",
231         .version        = FIO_IOOPS_VERSION,
232         .init           = fio_solarisaio_init,
233 };
234
235 #endif
236
237 static void fio_init fio_solarisaio_register(void)
238 {
239         register_ioengine(&ioengine);
240 }
241
242 static void fio_exit fio_solarisaio_unregister(void)
243 {
244         unregister_ioengine(&ioengine);
245 }