Commit | Line | Data |
---|---|---|
417f0068 JA |
1 | /* |
2 | * Native Solaris async IO engine | |
3 | * | |
4 | */ | |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | #include <unistd.h> | |
556e831d | 8 | #include <signal.h> |
417f0068 JA |
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; | |
556e831d | 19 | unsigned int aio_pending; |
417f0068 | 20 | unsigned int nr; |
5cf56c03 | 21 | unsigned int max_depth; |
417f0068 JA |
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 | { | |
556e831d JA |
33 | struct solarisaio_data *sd = td->io_ops->data; |
34 | ||
417f0068 | 35 | io_u->resultp.aio_return = AIO_INPROGRESS; |
556e831d | 36 | io_u->engine_data = sd; |
417f0068 JA |
37 | return 0; |
38 | } | |
39 | ||
556e831d JA |
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; | |
44c47feb | 72 | write_barrier(); |
556e831d JA |
73 | sd->aio_pending++; |
74 | sd->nr--; | |
75 | } | |
76 | ||
417f0068 JA |
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; | |
556e831d | 82 | int ret; |
417f0068 | 83 | |
556e831d JA |
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 | } | |
91 | ||
92 | while (sd->aio_pending < min) | |
93 | wait_for_event(&tv); | |
417f0068 | 94 | |
556e831d | 95 | /* |
44c47feb | 96 | * should be OK without locking, as int operations should be atomic |
556e831d JA |
97 | */ |
98 | ret = sd->aio_pending; | |
44c47feb | 99 | sd->aio_pending -= ret; |
556e831d | 100 | return ret; |
417f0068 JA |
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 | ||
5cf56c03 | 129 | if (sd->nr == sd->max_depth) |
417f0068 JA |
130 | return FIO_Q_BUSY; |
131 | ||
132 | off = io_u->offset; | |
133 | if (io_u->ddir == DDIR_READ) | |
134 | ret = aioread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, off, | |
135 | SEEK_SET, &io_u->resultp); | |
136 | else | |
137 | ret = aiowrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, off, | |
138 | SEEK_SET, &io_u->resultp); | |
139 | if (ret) { | |
140 | io_u->error = errno; | |
141 | td_verror(td, io_u->error, "xfer"); | |
142 | return FIO_Q_COMPLETED; | |
143 | } | |
144 | ||
145 | sd->nr++; | |
146 | return FIO_Q_QUEUED; | |
147 | } | |
148 | ||
149 | static void fio_solarisaio_cleanup(struct thread_data *td) | |
150 | { | |
151 | struct solarisaio_data *sd = td->io_ops->data; | |
152 | ||
153 | if (sd) { | |
154 | free(sd->aio_events); | |
155 | free(sd); | |
156 | } | |
157 | } | |
158 | ||
556e831d | 159 | /* |
44c47feb | 160 | * Set USE_SIGNAL_COMPLETIONS to use SIGIO as completion events. |
556e831d | 161 | */ |
da28353c JA |
162 | #ifdef USE_SIGNAL_COMPLETIONS |
163 | static void fio_solarisaio_sigio(int sig) | |
164 | { | |
165 | wait_for_event(NULL); | |
166 | } | |
167 | ||
556e831d JA |
168 | static void fio_solarisaio_init_sigio(void) |
169 | { | |
556e831d JA |
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); | |
556e831d | 176 | } |
da28353c | 177 | #endif |
556e831d | 178 | |
417f0068 JA |
179 | static int fio_solarisaio_init(struct thread_data *td) |
180 | { | |
181 | struct solarisaio_data *sd = malloc(sizeof(*sd)); | |
5cf56c03 JA |
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 | } | |
417f0068 JA |
190 | |
191 | memset(sd, 0, sizeof(*sd)); | |
5cf56c03 JA |
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; | |
417f0068 | 195 | |
da28353c | 196 | #ifdef USE_SIGNAL_COMPLETIONS |
556e831d | 197 | fio_solarisaio_init_sigio(); |
da28353c | 198 | #endif |
556e831d | 199 | |
417f0068 JA |
200 | td->io_ops->data = sd; |
201 | return 0; | |
202 | } | |
203 | ||
204 | static struct ioengine_ops ioengine = { | |
205 | .name = "solarisaio", | |
206 | .version = FIO_IOOPS_VERSION, | |
207 | .init = fio_solarisaio_init, | |
208 | .prep = fio_solarisaio_prep, | |
209 | .queue = fio_solarisaio_queue, | |
210 | .cancel = fio_solarisaio_cancel, | |
211 | .getevents = fio_solarisaio_getevents, | |
212 | .event = fio_solarisaio_event, | |
213 | .cleanup = fio_solarisaio_cleanup, | |
214 | .open_file = generic_open_file, | |
215 | .close_file = generic_close_file, | |
df9c26b1 | 216 | .get_file_size = generic_get_file_size, |
417f0068 JA |
217 | }; |
218 | ||
219 | #else /* FIO_HAVE_SOLARISAIO */ | |
220 | ||
221 | /* | |
222 | * When we have a proper configure system in place, we simply wont build | |
223 | * and install this io engine. For now install a crippled version that | |
224 | * just complains and fails to load. | |
225 | */ | |
226 | static int fio_solarisaio_init(struct thread_data fio_unused *td) | |
227 | { | |
228 | fprintf(stderr, "fio: solarisaio not available\n"); | |
229 | return 1; | |
230 | } | |
231 | ||
232 | static struct ioengine_ops ioengine = { | |
233 | .name = "solarisaio", | |
234 | .version = FIO_IOOPS_VERSION, | |
235 | .init = fio_solarisaio_init, | |
236 | }; | |
237 | ||
238 | #endif | |
239 | ||
240 | static void fio_init fio_solarisaio_register(void) | |
241 | { | |
242 | register_ioengine(&ioengine); | |
243 | } | |
244 | ||
245 | static void fio_exit fio_solarisaio_unregister(void) | |
246 | { | |
247 | unregister_ioengine(&ioengine); | |
248 | } |