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.6 by root, Fri Jun 21 03:52:29 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" 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
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 */
112typedef struct aniocb 115typedef 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
137ecb_cold
133static void 138static void
134linuxaio_free_iocbp (EV_P) 139linuxaio_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
142static void 147static void
143linuxaio_modify (EV_P_ int fd, int oev, int nev) 148linuxaio_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
304inline_size 309inline_size
305int 310int
306linuxaio_init (EV_P_ int flags) 311linuxaio_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
336inline_size 343inline_size
337void 344void
338linuxaio_fork (EV_P) 345linuxaio_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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines