… | |
… | |
36 | * provisions above, a recipient may use your version of this file under |
36 | * provisions above, a recipient may use your version of this file under |
37 | * either the BSD or the GPL. |
37 | * either the BSD or the GPL. |
38 | */ |
38 | */ |
39 | |
39 | |
40 | #include <sys/time.h> /* actually linux/time.h, but we must assume they are compatible */ |
40 | #include <sys/time.h> /* actually linux/time.h, but we must assume they are compatible */ |
|
|
41 | #include <poll.h> |
41 | #include <linux/aio_abi.h> |
42 | #include <linux/aio_abi.h> |
42 | |
43 | |
43 | /* we try to fill 4kn pages exactly. |
44 | /* we try to fill 4kB pages exactly. |
44 | * the ring buffer header is 32 bytes, every io event is 32 bytes. |
45 | * the ring buffer header is 32 bytes, every io event is 32 bytes. |
45 | * the kernel takes the io event number, doubles it, adds 2, adds the ring buffer |
46 | * the kernel takes the io event number, doubles it, adds 2, adds the ring buffer. |
46 | * so the calculation below will use "exactly" 8kB for the ring buffer |
47 | * therefore the calculation below will use "exactly" 4kB for the ring buffer |
47 | */ |
48 | */ |
48 | #define EV_LINUXAIO_DEPTH (256 / 2 - 2 - 1) /* max. number of io events per batch */ |
49 | #define EV_LINUXAIO_DEPTH (128 / 2 - 2 - 1) /* max. number of io events per batch */ |
49 | |
50 | |
50 | /*****************************************************************************/ |
51 | /*****************************************************************************/ |
51 | /* syscall wrapdadoop */ |
52 | /* syscall wrapdadoop */ |
52 | |
53 | |
53 | #include <sys/syscall.h> /* no glibc wrappers */ |
54 | #include <sys/syscall.h> /* no glibc wrappers */ |
54 | |
55 | |
55 | /* aio_abi.h is not verioned in any way, so we cannot test for its existance */ |
56 | /* aio_abi.h is not versioned in any way, so we cannot test for its existance */ |
56 | #define IOCB_CMD_POLL 5 |
57 | #define IOCB_CMD_POLL 5 |
57 | |
58 | |
58 | /* taken from linux/fs/aio.c */ |
59 | /* taken from linux/fs/aio.c */ |
59 | #define AIO_RING_MAGIC 0xa10a10a1 |
60 | #define AIO_RING_MAGIC 0xa10a10a1 |
60 | #define AIO_RING_INCOMPAT_FEATURES 0 |
61 | #define AIO_RING_INCOMPAT_FEATURES 0 |
… | |
… | |
71 | unsigned header_length; /* size of aio_ring */ |
72 | unsigned header_length; /* size of aio_ring */ |
72 | |
73 | |
73 | struct io_event io_events[0]; |
74 | struct io_event io_events[0]; |
74 | }; |
75 | }; |
75 | |
76 | |
76 | static int |
77 | inline_size |
|
|
78 | int |
77 | ev_io_setup (unsigned nr_events, aio_context_t *ctx_idp) |
79 | ev_io_setup (unsigned nr_events, aio_context_t *ctx_idp) |
78 | { |
80 | { |
79 | return syscall (SYS_io_setup, nr_events, ctx_idp); |
81 | return syscall (SYS_io_setup, nr_events, ctx_idp); |
80 | } |
82 | } |
81 | |
83 | |
82 | static int |
84 | inline_size |
|
|
85 | int |
83 | ev_io_destroy (aio_context_t ctx_id) |
86 | ev_io_destroy (aio_context_t ctx_id) |
84 | { |
87 | { |
85 | return syscall (SYS_io_destroy, ctx_id); |
88 | return syscall (SYS_io_destroy, ctx_id); |
86 | } |
89 | } |
87 | |
90 | |
88 | static int |
91 | inline_size |
|
|
92 | int |
89 | ev_io_submit (aio_context_t ctx_id, long nr, struct iocb *cbp[]) |
93 | ev_io_submit (aio_context_t ctx_id, long nr, struct iocb *cbp[]) |
90 | { |
94 | { |
91 | return syscall (SYS_io_submit, ctx_id, nr, cbp); |
95 | return syscall (SYS_io_submit, ctx_id, nr, cbp); |
92 | } |
96 | } |
93 | |
97 | |
94 | static int |
98 | inline_size |
|
|
99 | int |
95 | ev_io_cancel (aio_context_t ctx_id, struct iocb *cbp, struct io_event *result) |
100 | ev_io_cancel (aio_context_t ctx_id, struct iocb *cbp, struct io_event *result) |
96 | { |
101 | { |
97 | return syscall (SYS_io_cancel, ctx_id, cbp, result); |
102 | return syscall (SYS_io_cancel, ctx_id, cbp, result); |
98 | } |
103 | } |
99 | |
104 | |
100 | static int |
105 | inline_size |
|
|
106 | int |
101 | ev_io_getevents (aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout) |
107 | ev_io_getevents (aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout) |
102 | { |
108 | { |
103 | return syscall (SYS_io_getevents, ctx_id, min_nr, nr, events, timeout); |
109 | return syscall (SYS_io_getevents, ctx_id, min_nr, nr, events, timeout); |
104 | } |
110 | } |
105 | |
|
|
106 | typedef void (*ev_io_cb) (long nr, struct io_event *events); |
|
|
107 | |
111 | |
108 | /*****************************************************************************/ |
112 | /*****************************************************************************/ |
109 | /* actual backed implementation */ |
113 | /* actual backed implementation */ |
110 | |
114 | |
111 | /* two iocbs for every fd, one for read, one for write */ |
115 | /* we use out own wrapper structure in acse we ever want to do something "clever" */ |
112 | typedef struct aniocb |
116 | typedef struct aniocb |
113 | { |
117 | { |
114 | struct iocb io; |
118 | struct iocb io; |
115 | /*int inuse;*/ |
119 | /*int inuse;*/ |
116 | } *ANIOCBP; |
120 | } *ANIOCBP; |
117 | |
121 | |
118 | inline_size |
122 | inline_size |
119 | void |
123 | void |
120 | linuxaio_array_needsize_iocbp (ANIOCBP *base, int count) |
124 | linuxaio_array_needsize_iocbp (ANIOCBP *base, int count) |
121 | { |
125 | { |
122 | /* TODO: quite the overhead to allocate every iocb separately */ |
126 | /* TODO: quite the overhead to allocate every iocb separately, maybe use our own alocator? */ |
123 | while (count--) |
127 | while (count--) |
124 | { |
128 | { |
125 | *base = (ANIOCBP)ev_malloc (sizeof (**base)); |
129 | *base = (ANIOCBP)ev_malloc (sizeof (**base)); |
|
|
130 | /* TODO: full zero initialize required? */ |
126 | memset (*base, 0, sizeof (**base)); |
131 | memset (*base, 0, sizeof (**base)); |
127 | /* would be nice to initialize fd/data as well */ |
132 | /* would be nice to initialize fd/data as well, but array_needsize API doesn't support that */ |
128 | (*base)->io.aio_lio_opcode = IOCB_CMD_POLL; |
133 | (*base)->io.aio_lio_opcode = IOCB_CMD_POLL; |
129 | ++base; |
134 | ++base; |
130 | } |
135 | } |
131 | } |
136 | } |
132 | |
137 | |
|
|
138 | ecb_cold |
133 | static void |
139 | static void |
134 | linuxaio_free_iocbp (EV_P) |
140 | linuxaio_free_iocbp (EV_P) |
135 | { |
141 | { |
136 | while (linuxaio_iocbpmax--) |
142 | while (linuxaio_iocbpmax--) |
137 | ev_free (linuxaio_iocbps [linuxaio_iocbpmax]); |
143 | ev_free (linuxaio_iocbps [linuxaio_iocbpmax]); |
138 | |
144 | |
139 | linuxaio_iocbpmax = 0; |
145 | linuxaio_iocbpmax = 0; /* next resize will completely reallocate the array, at some overhead */ |
140 | } |
146 | } |
141 | |
147 | |
142 | static void |
148 | static void |
143 | linuxaio_modify (EV_P_ int fd, int oev, int nev) |
149 | linuxaio_modify (EV_P_ int fd, int oev, int nev) |
144 | { |
150 | { |
145 | /* TODO: full zero initialize required? */ |
|
|
146 | array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
151 | array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
147 | struct aniocb *iocb = linuxaio_iocbps [fd]; |
152 | struct aniocb *iocb = linuxaio_iocbps [fd]; |
148 | |
153 | |
149 | if (iocb->io.aio_buf) |
154 | if (iocb->io.aio_buf) |
150 | ev_io_cancel (linuxaio_ctx, &iocb->io, (void *)0); |
155 | ev_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); /* always returns an error relevant kernels */ |
151 | |
156 | |
152 | if (nev) |
157 | if (nev) |
153 | { |
158 | { |
154 | iocb->io.aio_data = fd; |
159 | iocb->io.aio_data = fd; |
155 | iocb->io.aio_fildes = fd; |
160 | iocb->io.aio_fildes = fd; |
… | |
… | |
171 | while (nr) |
176 | while (nr) |
172 | { |
177 | { |
173 | int fd = ev->data; |
178 | int fd = ev->data; |
174 | int res = ev->res; |
179 | int res = ev->res; |
175 | |
180 | |
176 | assert (("libev: iocb fd must be in-bounds", fd >= 0 && fd < anfdxmax)); |
181 | assert (("libev: iocb fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
177 | |
182 | |
178 | /* linux aio is oneshot: rearm fd */ |
183 | /* linux aio is oneshot: rearm fd */ |
179 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
184 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
180 | anfds [fd].events = 0; |
185 | anfds [fd].events = 0; |
181 | fd_change (EV_A_ fd, 0); |
186 | fd_change (EV_A_ fd, 0); |
… | |
… | |
200 | static int |
205 | static int |
201 | linuxaio_get_events_from_ring (EV_P) |
206 | linuxaio_get_events_from_ring (EV_P) |
202 | { |
207 | { |
203 | struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; |
208 | struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; |
204 | |
209 | |
205 | ECB_MEMORY_FENCE_ACQUIRE; |
|
|
206 | |
|
|
207 | unsigned head = ring->head; |
210 | unsigned head = ring->head; |
208 | unsigned tail = *(volatile unsigned *)&ring->tail; |
211 | unsigned tail = *(volatile unsigned *)&ring->tail; |
209 | |
212 | |
210 | if (ring->magic != AIO_RING_MAGIC |
213 | if (head == tail) |
211 | || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES |
|
|
212 | || ring->header_length != sizeof (struct aio_ring) /* TODO: or use it to find io_event[0]? */ |
|
|
213 | || head == tail) |
|
|
214 | return 0; |
214 | return 0; |
|
|
215 | |
|
|
216 | /* bail out if the ring buffer doesn't match the expected layout */ |
|
|
217 | if (ecb_expect_false (ring->magic != AIO_RING_MAGIC) |
|
|
218 | || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES |
|
|
219 | || ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */ |
|
|
220 | return 0; |
|
|
221 | |
|
|
222 | ECB_MEMORY_FENCE_ACQUIRE; |
215 | |
223 | |
216 | /* parse all available events, but only once, to avoid starvation */ |
224 | /* parse all available events, but only once, to avoid starvation */ |
217 | if (tail > head) /* normal case around */ |
225 | if (tail > head) /* normal case around */ |
218 | linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head); |
226 | linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head); |
219 | else |
|
|
220 | { |
|
|
221 | /* wrapped around */ |
227 | else /* wrapped around */ |
|
|
228 | { |
222 | linuxaio_parse_events (EV_A_ ring->io_events + head, ring->nr - head); |
229 | linuxaio_parse_events (EV_A_ ring->io_events + head, ring->nr - head); |
223 | linuxaio_parse_events (EV_A_ ring->io_events, tail); |
230 | linuxaio_parse_events (EV_A_ ring->io_events, tail); |
224 | } |
231 | } |
225 | |
232 | |
226 | ring->head = tail; |
233 | ring->head = tail; |
… | |
… | |
239 | |
246 | |
240 | if (linuxaio_get_events_from_ring (EV_A)) |
247 | if (linuxaio_get_events_from_ring (EV_A)) |
241 | return; |
248 | return; |
242 | |
249 | |
243 | /* no events, so wait for at least one, then poll ring buffer again */ |
250 | /* no events, so wait for at least one, then poll ring buffer again */ |
244 | /* this degraded to one event per loop iteration */ |
251 | /* this degrades to one event per loop iteration */ |
245 | /* if the ring buffer changes layout, but so be it */ |
252 | /* if the ring buffer changes layout, but so be it */ |
246 | |
253 | |
247 | ts.tv_sec = (long)timeout; |
254 | ts.tv_sec = (long)timeout; |
248 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
255 | ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
249 | |
256 | |
250 | res = ev_io_getevents (linuxaio_ctx, 1, 1, &ioev, &ts); |
257 | res = ev_io_getevents (linuxaio_ctx, 1, 1, &ioev, &ts); |
251 | |
258 | |
252 | if (res < 0) |
259 | if (res < 0) |
253 | ev_syserr ("(libev) io_getevents"); |
260 | ev_syserr ("(libev) linuxaio io_getevents"); |
254 | else if (res) |
261 | else if (res) |
255 | { |
262 | { |
256 | /* at least one event received, handle it and any remaining ones in the ring buffer */ |
263 | /* at least one event received, handle it and any remaining ones in the ring buffer */ |
257 | linuxaio_parse_events (EV_A_ &ioev, 1); |
264 | linuxaio_parse_events (EV_A_ &ioev, 1); |
258 | linuxaio_get_events_from_ring (EV_A); |
265 | linuxaio_get_events_from_ring (EV_A); |
… | |
… | |
271 | /* which allows us to pinpoint the errornous iocb */ |
278 | /* which allows us to pinpoint the errornous iocb */ |
272 | for (submitted = 0; submitted < linuxaio_submitcnt; ) |
279 | for (submitted = 0; submitted < linuxaio_submitcnt; ) |
273 | { |
280 | { |
274 | int res = ev_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted); |
281 | int res = ev_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted); |
275 | |
282 | |
276 | if (res < 0) |
283 | if (ecb_expect_false (res < 0)) |
277 | if (errno == EAGAIN) |
284 | if (errno == EAGAIN) |
278 | { |
285 | { |
279 | /* This happens when the ring buffer is full, at least. I assume this means |
286 | /* This happens when the ring buffer is full, at least. I assume this means |
280 | * that the event was queued synchronously during io_submit, and thus |
287 | * that the event was queued synchronously during io_submit, and thus |
281 | * the buffer overflowd. |
288 | * the buffer overflowd. |
282 | * In this case, we just try next loop iteration. |
289 | * In this case, we just try next loop iteration. |
|
|
290 | * This should not result in a few fds taking priority, as the interface |
|
|
291 | * is one-shot, and we submit iocb's in a round-robin fashion. |
283 | */ |
292 | */ |
284 | memcpy (linuxaio_submits, linuxaio_submits + submitted, (linuxaio_submitcnt - submitted) * sizeof (*linuxaio_submits)); |
293 | memmove (linuxaio_submits, linuxaio_submits + submitted, (linuxaio_submitcnt - submitted) * sizeof (*linuxaio_submits)); |
285 | linuxaio_submitcnt -= submitted; |
294 | linuxaio_submitcnt -= submitted; |
286 | timeout = 0; |
295 | timeout = 0; |
287 | break; |
296 | break; |
288 | } |
297 | } |
289 | else |
298 | else |
290 | /* TODO: we get EAGAIN when the ring buffer is full for some reason */ |
|
|
291 | /* TODO: should we always just try next time? */ |
|
|
292 | ev_syserr ("(libev) io_submit"); |
299 | ev_syserr ("(libev) linuxaio io_submit"); |
293 | |
300 | |
294 | submitted += res; |
301 | submitted += res; |
295 | } |
302 | } |
296 | |
303 | |
297 | linuxaio_submitcnt = 0; |
304 | linuxaio_submitcnt = 0; |
… | |
… | |
304 | inline_size |
311 | inline_size |
305 | int |
312 | int |
306 | linuxaio_init (EV_P_ int flags) |
313 | linuxaio_init (EV_P_ int flags) |
307 | { |
314 | { |
308 | /* would be great to have a nice test for IOCB_CMD_POLL instead */ |
315 | /* would be great to have a nice test for IOCB_CMD_POLL instead */ |
|
|
316 | /* also: test some semi-common fd types, such as files and ttys in recommended_backends */ |
309 | if (ev_linux_version () < 0x041200) /* 4.18 introduced IOCB_CMD_POLL */ |
317 | if (ev_linux_version () < 0x041200) /* 4.18 introduced IOCB_CMD_POLL */ |
310 | return 0; |
318 | return 0; |
311 | |
319 | |
|
|
320 | linuxaio_ctx = 0; |
312 | if (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
321 | if (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
313 | return 0; |
322 | return 0; |
314 | |
323 | |
315 | backend_modify = linuxaio_modify; |
324 | backend_modify = linuxaio_modify; |
316 | backend_poll = linuxaio_poll; |
325 | backend_poll = linuxaio_poll; |
… | |
… | |
335 | |
344 | |
336 | inline_size |
345 | inline_size |
337 | void |
346 | void |
338 | linuxaio_fork (EV_P) |
347 | linuxaio_fork (EV_P) |
339 | { |
348 | { |
340 | abort ();//D |
349 | /* this frees all iocbs, which is very heavy-handed */ |
341 | } |
350 | linuxaio_destroy (EV_A); |
|
|
351 | linuxaio_submitcnt = 0; /* all pointers were invalidated */ |
342 | |
352 | |
|
|
353 | linuxaio_ctx = 0; |
|
|
354 | while (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
|
|
355 | ev_syserr ("(libev) linuxaio io_setup"); |
|
|
356 | |
|
|
357 | fd_rearm_all (EV_A); |
|
|
358 | } |
|
|
359 | |