… | |
… | |
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 4kn 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 |
… | |
… | |
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 |
… | |
… | |
121 | { |
122 | { |
122 | /* TODO: quite the overhead to allocate every iocb separately */ |
123 | /* TODO: quite the overhead to allocate every iocb separately */ |
123 | while (count--) |
124 | while (count--) |
124 | { |
125 | { |
125 | *base = (ANIOCBP)ev_malloc (sizeof (**base)); |
126 | *base = (ANIOCBP)ev_malloc (sizeof (**base)); |
|
|
127 | /* TODO: full zero initialize required? */ |
126 | memset (*base, 0, sizeof (**base)); |
128 | memset (*base, 0, sizeof (**base)); |
127 | /* would be nice to initialize fd/data as well */ |
129 | /* would be nice to initialize fd/data as well */ |
128 | (*base)->io.aio_lio_opcode = IOCB_CMD_POLL; |
130 | (*base)->io.aio_lio_opcode = IOCB_CMD_POLL; |
129 | ++base; |
131 | ++base; |
130 | } |
132 | } |
… | |
… | |
134 | linuxaio_free_iocbp (EV_P) |
136 | linuxaio_free_iocbp (EV_P) |
135 | { |
137 | { |
136 | while (linuxaio_iocbpmax--) |
138 | while (linuxaio_iocbpmax--) |
137 | ev_free (linuxaio_iocbps [linuxaio_iocbpmax]); |
139 | ev_free (linuxaio_iocbps [linuxaio_iocbpmax]); |
138 | |
140 | |
|
|
141 | /* next resize will completely reallocate the array */ |
139 | linuxaio_iocbpmax = 0; |
142 | linuxaio_iocbpmax = 0; |
|
|
143 | linuxaio_submitcnt = 0; /* all pointers invalidated */ |
140 | } |
144 | } |
141 | |
145 | |
142 | static void |
146 | static void |
143 | linuxaio_modify (EV_P_ int fd, int oev, int nev) |
147 | linuxaio_modify (EV_P_ int fd, int oev, int nev) |
144 | { |
148 | { |
145 | /* TODO: full zero initialize required? */ |
|
|
146 | array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
149 | array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
147 | struct aniocb *iocb = linuxaio_iocbps [fd]; |
150 | struct aniocb *iocb = linuxaio_iocbps [fd]; |
148 | |
151 | |
149 | if (iocb->io.aio_buf) |
152 | if (iocb->io.aio_buf) |
150 | ev_io_cancel (linuxaio_ctx, &iocb->io, (void *)0); |
153 | ev_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); /* always returns an error relevant kernels */ |
151 | |
154 | |
152 | if (nev) |
155 | if (nev) |
153 | { |
156 | { |
154 | iocb->io.aio_data = fd; |
157 | iocb->io.aio_data = fd; |
155 | iocb->io.aio_fildes = fd; |
158 | iocb->io.aio_fildes = fd; |
… | |
… | |
171 | while (nr) |
174 | while (nr) |
172 | { |
175 | { |
173 | int fd = ev->data; |
176 | int fd = ev->data; |
174 | int res = ev->res; |
177 | int res = ev->res; |
175 | |
178 | |
176 | assert (("libev: iocb fd must be in-bounds", fd >= 0 && fd < anfdxmax)); |
179 | assert (("libev: iocb fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
177 | |
180 | |
178 | /* linux aio is oneshot: rearm fd */ |
181 | /* linux aio is oneshot: rearm fd */ |
179 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
182 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
180 | anfds [fd].events = 0; |
183 | anfds [fd].events = 0; |
181 | fd_change (EV_A_ fd, 0); |
184 | fd_change (EV_A_ fd, 0); |
… | |
… | |
279 | /* This happens when the ring buffer is full, at least. I assume this means |
282 | /* 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 |
283 | * that the event was queued synchronously during io_submit, and thus |
281 | * the buffer overflowd. |
284 | * the buffer overflowd. |
282 | * In this case, we just try next loop iteration. |
285 | * In this case, we just try next loop iteration. |
283 | */ |
286 | */ |
284 | memcpy (linuxaio_submits, linuxaio_submits + submitted, (linuxaio_submitcnt - submitted) * sizeof (*linuxaio_submits)); |
287 | memmove (linuxaio_submits, linuxaio_submits + submitted, (linuxaio_submitcnt - submitted) * sizeof (*linuxaio_submits)); |
285 | linuxaio_submitcnt -= submitted; |
288 | linuxaio_submitcnt -= submitted; |
286 | timeout = 0; |
289 | timeout = 0; |
287 | break; |
290 | break; |
288 | } |
291 | } |
289 | else |
292 | else |
… | |
… | |
304 | inline_size |
307 | inline_size |
305 | int |
308 | int |
306 | linuxaio_init (EV_P_ int flags) |
309 | linuxaio_init (EV_P_ int flags) |
307 | { |
310 | { |
308 | /* would be great to have a nice test for IOCB_CMD_POLL instead */ |
311 | /* would be great to have a nice test for IOCB_CMD_POLL instead */ |
|
|
312 | /* 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 */ |
313 | if (ev_linux_version () < 0x041200) /* 4.18 introduced IOCB_CMD_POLL */ |
310 | return 0; |
314 | return 0; |
311 | |
315 | |
|
|
316 | linuxaio_ctx = 0; |
312 | if (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
317 | if (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
313 | return 0; |
318 | return 0; |
314 | |
319 | |
315 | backend_modify = linuxaio_modify; |
320 | backend_modify = linuxaio_modify; |
316 | backend_poll = linuxaio_poll; |
321 | backend_poll = linuxaio_poll; |
… | |
… | |
335 | |
340 | |
336 | inline_size |
341 | inline_size |
337 | void |
342 | void |
338 | linuxaio_fork (EV_P) |
343 | linuxaio_fork (EV_P) |
339 | { |
344 | { |
340 | abort ();//D |
345 | /* TODO: verify and test */ |
341 | } |
346 | linuxaio_destroy (EV_A); |
342 | |
347 | |
|
|
348 | linuxaio_ctx = 0; |
|
|
349 | while (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
|
|
350 | ev_syserr ("(libev) io_setup"); |
|
|
351 | |
|
|
352 | fd_rearm_all (EV_A); |
|
|
353 | } |
|
|
354 | |