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.6 by root, Fri Jun 21 03:52:29 2019 UTC vs.
Revision 1.9 by root, Sat Jun 22 22:29:38 2019 UTC

42#include <linux/aio_abi.h> 42#include <linux/aio_abi.h>
43 43
44/* we try to fill 4kB pages exactly. 44/* we try to fill 4kB pages exactly.
45 * 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.
46 * 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.
47 * therefore the calculation below will use "exactly" 8kB for the ring buffer 47 * therefore the calculation below will use "exactly" 4kB for the ring buffer
48 */ 48 */
49#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 */
50 50
51/*****************************************************************************/ 51/*****************************************************************************/
52/* syscall wrapdadoop */ 52/* syscall wrapdadoop */
53 53
54#include <sys/syscall.h> /* no glibc wrappers */ 54#include <sys/syscall.h> /* no glibc wrappers */
110} 110}
111 111
112/*****************************************************************************/ 112/*****************************************************************************/
113/* actual backed implementation */ 113/* actual backed implementation */
114 114
115/* we use out own wrapper structure in acse we ever want to do something "clever" */
115typedef struct aniocb 116typedef struct aniocb
116{ 117{
117 struct iocb io; 118 struct iocb io;
118 /*int inuse;*/ 119 /*int inuse;*/
119} *ANIOCBP; 120} *ANIOCBP;
120 121
121inline_size 122inline_size
122void 123void
123linuxaio_array_needsize_iocbp (ANIOCBP *base, int count) 124linuxaio_array_needsize_iocbp (ANIOCBP *base, int count)
124{ 125{
125 /* TODO: quite the overhead to allocate every iocb separately */ 126 /* TODO: quite the overhead to allocate every iocb separately, maybe use our own alocator? */
126 while (count--) 127 while (count--)
127 { 128 {
128 *base = (ANIOCBP)ev_malloc (sizeof (**base)); 129 *base = (ANIOCBP)ev_malloc (sizeof (**base));
129 /* TODO: full zero initialize required? */ 130 /* TODO: full zero initialize required? */
130 memset (*base, 0, sizeof (**base)); 131 memset (*base, 0, sizeof (**base));
131 /* 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 */
132 (*base)->io.aio_lio_opcode = IOCB_CMD_POLL; 133 (*base)->io.aio_lio_opcode = IOCB_CMD_POLL;
133 ++base; 134 ++base;
134 } 135 }
135} 136}
136 137
204static int 205static int
205linuxaio_get_events_from_ring (EV_P) 206linuxaio_get_events_from_ring (EV_P)
206{ 207{
207 struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; 208 struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx;
208 209
209 ECB_MEMORY_FENCE_ACQUIRE;
210
211 unsigned head = ring->head; 210 unsigned head = ring->head;
212 unsigned tail = *(volatile unsigned *)&ring->tail; 211 unsigned tail = *(volatile unsigned *)&ring->tail;
213 212
214 if (head == tail) 213 if (head == tail)
215 return 0; 214 return 0;
216 215
216 /* bail out if the ring buffer doesn't match the expected layout */
217 if (ecb_expect_false (ring->magic != AIO_RING_MAGIC) 217 if (ecb_expect_false (ring->magic != AIO_RING_MAGIC)
218 || ring->incompat_features != AIO_RING_INCOMPAT_FEATURES 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]? */ 219 || ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */
220 return 0; 220 return 0;
221 221
222 ECB_MEMORY_FENCE_ACQUIRE;
223
222 /* parse all available events, but only once, to avoid starvation */ 224 /* parse all available events, but only once, to avoid starvation */
223 if (tail > head) /* normal case around */ 225 if (tail > head) /* normal case around */
224 linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head); 226 linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head);
225 else /* wrapped around */ 227 else /* wrapped around */
226 { 228 {
244 246
245 if (linuxaio_get_events_from_ring (EV_A)) 247 if (linuxaio_get_events_from_ring (EV_A))
246 return; 248 return;
247 249
248 /* 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 */
249 /* this degraded to one event per loop iteration */ 251 /* this degrades to one event per loop iteration */
250 /* if the ring buffer changes layout, but so be it */ 252 /* if the ring buffer changes layout, but so be it */
251 253
252 ts.tv_sec = (long)timeout; 254 ts.tv_sec = (long)timeout;
253 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); 255 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9);
254 256
255 res = ev_io_getevents (linuxaio_ctx, 1, 1, &ioev, &ts); 257 res = ev_io_getevents (linuxaio_ctx, 1, 1, &ioev, &ts);
256 258
257 if (res < 0) 259 if (res < 0)
258 ev_syserr ("(libev) io_getevents"); 260 ev_syserr ("(libev) linuxaio io_getevents");
259 else if (res) 261 else if (res)
260 { 262 {
261 /* 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 */
262 linuxaio_parse_events (EV_A_ &ioev, 1); 264 linuxaio_parse_events (EV_A_ &ioev, 1);
263 linuxaio_get_events_from_ring (EV_A); 265 linuxaio_get_events_from_ring (EV_A);
283 { 285 {
284 /* 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
285 * that the event was queued synchronously during io_submit, and thus 287 * that the event was queued synchronously during io_submit, and thus
286 * the buffer overflowd. 288 * the buffer overflowd.
287 * 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.
288 */ 292 */
289 memmove (linuxaio_submits, linuxaio_submits + submitted, (linuxaio_submitcnt - submitted) * sizeof (*linuxaio_submits)); 293 memmove (linuxaio_submits, linuxaio_submits + submitted, (linuxaio_submitcnt - submitted) * sizeof (*linuxaio_submits));
290 linuxaio_submitcnt -= submitted; 294 linuxaio_submitcnt -= submitted;
291 timeout = 0; 295 timeout = 0;
292 break; 296 break;
293 } 297 }
294 else 298 else
295 /* TODO: we get EAGAIN when the ring buffer is full for some reason */
296 /* TODO: should we always just try next time? */
297 ev_syserr ("(libev) io_submit"); 299 ev_syserr ("(libev) linuxaio io_submit");
298 300
299 submitted += res; 301 submitted += res;
300 } 302 }
301 303
302 linuxaio_submitcnt = 0; 304 linuxaio_submitcnt = 0;
342 344
343inline_size 345inline_size
344void 346void
345linuxaio_fork (EV_P) 347linuxaio_fork (EV_P)
346{ 348{
347 /* TODO: verify and test */
348
349 /* this frees all iocbs, which is very heavy-handed */ 349 /* this frees all iocbs, which is very heavy-handed */
350 linuxaio_destroy (EV_A); 350 linuxaio_destroy (EV_A);
351 linuxaio_submitcnt = 0; /* all pointers were invalidated */ 351 linuxaio_submitcnt = 0; /* all pointers were invalidated */
352 352
353 linuxaio_ctx = 0; 353 linuxaio_ctx = 0;
354 while (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) 354 while (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0)
355 ev_syserr ("(libev) io_setup"); 355 ev_syserr ("(libev) linuxaio io_setup");
356 356
357 fd_rearm_all (EV_A); 357 fd_rearm_all (EV_A);
358} 358}
359 359

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines