… | |
… | |
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" 8kB 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 (256 / 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 */ |
|
|
112 | typedef struct aniocb |
115 | typedef struct aniocb |
113 | { |
116 | { |
114 | struct iocb io; |
117 | struct iocb io; |
115 | /*int inuse;*/ |
118 | /*int inuse;*/ |
116 | } *ANIOCBP; |
119 | } *ANIOCBP; |
… | |
… | |
121 | { |
124 | { |
122 | /* TODO: quite the overhead to allocate every iocb separately */ |
125 | /* TODO: quite the overhead to allocate every iocb separately */ |
123 | while (count--) |
126 | while (count--) |
124 | { |
127 | { |
125 | *base = (ANIOCBP)ev_malloc (sizeof (**base)); |
128 | *base = (ANIOCBP)ev_malloc (sizeof (**base)); |
|
|
129 | /* TODO: full zero initialize required? */ |
126 | memset (*base, 0, sizeof (**base)); |
130 | memset (*base, 0, sizeof (**base)); |
127 | /* would be nice to initialize fd/data as well */ |
131 | /* would be nice to initialize fd/data as well */ |
128 | (*base)->io.aio_lio_opcode = IOCB_CMD_POLL; |
132 | (*base)->io.aio_lio_opcode = IOCB_CMD_POLL; |
129 | ++base; |
133 | ++base; |
130 | } |
134 | } |
131 | } |
135 | } |
132 | |
136 | |
|
|
137 | ecb_cold |
133 | static void |
138 | static void |
134 | linuxaio_free_iocbp (EV_P) |
139 | linuxaio_free_iocbp (EV_P) |
135 | { |
140 | { |
136 | while (linuxaio_iocbpmax--) |
141 | while (linuxaio_iocbpmax--) |
137 | ev_free (linuxaio_iocbps [linuxaio_iocbpmax]); |
142 | ev_free (linuxaio_iocbps [linuxaio_iocbpmax]); |
138 | |
143 | |
139 | linuxaio_iocbpmax = 0; |
144 | linuxaio_iocbpmax = 0; /* next resize will completely reallocate the array, at some overhead */ |
140 | } |
145 | } |
141 | |
146 | |
142 | static void |
147 | static void |
143 | linuxaio_modify (EV_P_ int fd, int oev, int nev) |
148 | linuxaio_modify (EV_P_ int fd, int oev, int nev) |
144 | { |
149 | { |
145 | /* TODO: full zero initialize required? */ |
|
|
146 | array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
150 | array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
147 | struct aniocb *iocb = linuxaio_iocbps [fd]; |
151 | struct aniocb *iocb = linuxaio_iocbps [fd]; |
148 | |
152 | |
149 | if (iocb->io.aio_buf) |
153 | if (iocb->io.aio_buf) |
150 | ev_io_cancel (linuxaio_ctx, &iocb->io, (void *)0); |
154 | ev_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); /* always returns an error relevant kernels */ |
151 | |
155 | |
152 | if (nev) |
156 | if (nev) |
153 | { |
157 | { |
154 | iocb->io.aio_data = fd; |
158 | iocb->io.aio_data = fd; |
155 | iocb->io.aio_fildes = fd; |
159 | iocb->io.aio_fildes = fd; |
… | |
… | |
171 | while (nr) |
175 | while (nr) |
172 | { |
176 | { |
173 | int fd = ev->data; |
177 | int fd = ev->data; |
174 | int res = ev->res; |
178 | int res = ev->res; |
175 | |
179 | |
176 | assert (("libev: iocb fd must be in-bounds", fd >= 0 && fd < anfdxmax)); |
180 | assert (("libev: iocb fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
177 | |
181 | |
178 | /* linux aio is oneshot: rearm fd */ |
182 | /* linux aio is oneshot: rearm fd */ |
179 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
183 | linuxaio_iocbps [fd]->io.aio_buf = 0; |
180 | anfds [fd].events = 0; |
184 | anfds [fd].events = 0; |
181 | fd_change (EV_A_ fd, 0); |
185 | fd_change (EV_A_ fd, 0); |
… | |
… | |
205 | ECB_MEMORY_FENCE_ACQUIRE; |
209 | ECB_MEMORY_FENCE_ACQUIRE; |
206 | |
210 | |
207 | unsigned head = ring->head; |
211 | unsigned head = ring->head; |
208 | unsigned tail = *(volatile unsigned *)&ring->tail; |
212 | unsigned tail = *(volatile unsigned *)&ring->tail; |
209 | |
213 | |
|
|
214 | if (head == tail) |
|
|
215 | return 0; |
|
|
216 | |
210 | if (ring->magic != AIO_RING_MAGIC |
217 | if (ecb_expect_false (ring->magic != AIO_RING_MAGIC) |
211 | || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES |
218 | || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES |
212 | || ring->header_length != sizeof (struct aio_ring) /* TODO: or use it to find io_event[0]? */ |
219 | || ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */ |
213 | || head == tail) |
|
|
214 | return 0; |
220 | return 0; |
215 | |
221 | |
216 | /* parse all available events, but only once, to avoid starvation */ |
222 | /* parse all available events, but only once, to avoid starvation */ |
217 | if (tail > head) /* normal case around */ |
223 | if (tail > head) /* normal case around */ |
218 | linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head); |
224 | linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head); |
219 | else |
|
|
220 | { |
|
|
221 | /* wrapped around */ |
225 | else /* wrapped around */ |
|
|
226 | { |
222 | linuxaio_parse_events (EV_A_ ring->io_events + head, ring->nr - head); |
227 | linuxaio_parse_events (EV_A_ ring->io_events + head, ring->nr - head); |
223 | linuxaio_parse_events (EV_A_ ring->io_events, tail); |
228 | linuxaio_parse_events (EV_A_ ring->io_events, tail); |
224 | } |
229 | } |
225 | |
230 | |
226 | ring->head = tail; |
231 | ring->head = tail; |
… | |
… | |
271 | /* which allows us to pinpoint the errornous iocb */ |
276 | /* which allows us to pinpoint the errornous iocb */ |
272 | for (submitted = 0; submitted < linuxaio_submitcnt; ) |
277 | for (submitted = 0; submitted < linuxaio_submitcnt; ) |
273 | { |
278 | { |
274 | int res = ev_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted); |
279 | int res = ev_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted); |
275 | |
280 | |
276 | if (res < 0) |
281 | if (ecb_expect_false (res < 0)) |
277 | if (errno == EAGAIN) |
282 | if (errno == EAGAIN) |
278 | { |
283 | { |
279 | /* This happens when the ring buffer is full, at least. I assume this means |
284 | /* 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 |
285 | * that the event was queued synchronously during io_submit, and thus |
281 | * the buffer overflowd. |
286 | * the buffer overflowd. |
282 | * In this case, we just try next loop iteration. |
287 | * In this case, we just try next loop iteration. |
283 | */ |
288 | */ |
284 | memcpy (linuxaio_submits, linuxaio_submits + submitted, (linuxaio_submitcnt - submitted) * sizeof (*linuxaio_submits)); |
289 | memmove (linuxaio_submits, linuxaio_submits + submitted, (linuxaio_submitcnt - submitted) * sizeof (*linuxaio_submits)); |
285 | linuxaio_submitcnt -= submitted; |
290 | linuxaio_submitcnt -= submitted; |
286 | timeout = 0; |
291 | timeout = 0; |
287 | break; |
292 | break; |
288 | } |
293 | } |
289 | else |
294 | else |
… | |
… | |
304 | inline_size |
309 | inline_size |
305 | int |
310 | int |
306 | linuxaio_init (EV_P_ int flags) |
311 | linuxaio_init (EV_P_ int flags) |
307 | { |
312 | { |
308 | /* would be great to have a nice test for IOCB_CMD_POLL instead */ |
313 | /* would be great to have a nice test for IOCB_CMD_POLL instead */ |
|
|
314 | /* 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 */ |
315 | if (ev_linux_version () < 0x041200) /* 4.18 introduced IOCB_CMD_POLL */ |
310 | return 0; |
316 | return 0; |
311 | |
317 | |
|
|
318 | linuxaio_ctx = 0; |
312 | if (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
319 | if (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
313 | return 0; |
320 | return 0; |
314 | |
321 | |
315 | backend_modify = linuxaio_modify; |
322 | backend_modify = linuxaio_modify; |
316 | backend_poll = linuxaio_poll; |
323 | backend_poll = linuxaio_poll; |
… | |
… | |
335 | |
342 | |
336 | inline_size |
343 | inline_size |
337 | void |
344 | void |
338 | linuxaio_fork (EV_P) |
345 | linuxaio_fork (EV_P) |
339 | { |
346 | { |
340 | abort ();//D |
347 | /* TODO: verify and test */ |
341 | } |
|
|
342 | |
348 | |
|
|
349 | /* this frees all iocbs, which is very heavy-handed */ |
|
|
350 | linuxaio_destroy (EV_A); |
|
|
351 | linuxaio_submitcnt = 0; /* all pointers were invalidated */ |
|
|
352 | |
|
|
353 | linuxaio_ctx = 0; |
|
|
354 | while (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
|
|
355 | ev_syserr ("(libev) io_setup"); |
|
|
356 | |
|
|
357 | fd_rearm_all (EV_A); |
|
|
358 | } |
|
|
359 | |