ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_linuxaio.c
(Generate patch)

Comparing libev/ev_linuxaio.c (file contents):
Revision 1.1 by root, Thu Jun 20 22:44:59 2019 UTC vs.
Revision 1.9 by root, Sat Jun 22 22:29:38 2019 UTC

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
76static int 77inline_size
78int
77ev_io_setup (unsigned nr_events, aio_context_t *ctx_idp) 79ev_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
82static int 84inline_size
85int
83ev_io_destroy (aio_context_t ctx_id) 86ev_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
88static int 91inline_size
92int
89ev_io_submit (aio_context_t ctx_id, long nr, struct iocb *cbp[]) 93ev_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
94static int 98inline_size
99int
95ev_io_cancel (aio_context_t ctx_id, struct iocb *cbp, struct io_event *result) 100ev_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
100static int 105inline_size
106int
101ev_io_getevents (aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout) 107ev_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
106typedef 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" */
112typedef struct aniocb 116typedef struct aniocb
113{ 117{
114 struct iocb io; 118 struct iocb io;
115 /*int inuse;*/ 119 /*int inuse;*/
116} *ANIOCBP; 120} *ANIOCBP;
117 121
118inline_size 122inline_size
119void 123void
120linuxaio_array_needsize_iocbp (ANIOCBP *base, int count) 124linuxaio_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
138ecb_cold
133static void 139static void
134linuxaio_free_iocbp (EV_P) 140linuxaio_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
142static void 148static void
143linuxaio_modify (EV_P_ int fd, int oev, int nev) 149linuxaio_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);
200static int 205static int
201linuxaio_get_events_from_ring (EV_P) 206linuxaio_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;
304inline_size 311inline_size
305int 312int
306linuxaio_init (EV_P_ int flags) 313linuxaio_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
336inline_size 345inline_size
337void 346void
338linuxaio_fork (EV_P) 347linuxaio_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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines