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.19 by root, Mon Jun 24 19:53:47 2019 UTC vs.
Revision 1.21 by root, Mon Jun 24 20:44:15 2019 UTC

53 * therefore the calculation below will use "exactly" 4kB for the ring buffer 53 * therefore the calculation below will use "exactly" 4kB for the ring buffer
54 */ 54 */
55#define EV_LINUXAIO_DEPTH (128 / 2 - 2 - 1) /* max. number of io events per batch */ 55#define EV_LINUXAIO_DEPTH (128 / 2 - 2 - 1) /* max. number of io events per batch */
56 56
57/*****************************************************************************/ 57/*****************************************************************************/
58/* syscall wrapdadoop */ 58/* syscall wrapdadoop - this section has the raw syscall definitions */
59 59
60#include <sys/syscall.h> /* no glibc wrappers */ 60#include <sys/syscall.h> /* no glibc wrappers */
61 61
62/* aio_abi.h is not versioned in any way, so we cannot test for its existance */ 62/* aio_abi.h is not versioned in any way, so we cannot test for its existance */
63#define IOCB_CMD_POLL 5 63#define IOCB_CMD_POLL 5
80 struct io_event io_events[0]; 80 struct io_event io_events[0];
81}; 81};
82 82
83inline_size 83inline_size
84int 84int
85ev_io_setup (unsigned nr_events, aio_context_t *ctx_idp) 85evsys_io_setup (unsigned nr_events, aio_context_t *ctx_idp)
86{ 86{
87 return syscall (SYS_io_setup, nr_events, ctx_idp); 87 return syscall (SYS_io_setup, nr_events, ctx_idp);
88} 88}
89 89
90inline_size 90inline_size
91int 91int
92ev_io_destroy (aio_context_t ctx_id) 92evsys_io_destroy (aio_context_t ctx_id)
93{ 93{
94 return syscall (SYS_io_destroy, ctx_id); 94 return syscall (SYS_io_destroy, ctx_id);
95} 95}
96 96
97inline_size 97inline_size
98int 98int
99ev_io_submit (aio_context_t ctx_id, long nr, struct iocb *cbp[]) 99evsys_io_submit (aio_context_t ctx_id, long nr, struct iocb *cbp[])
100{ 100{
101 return syscall (SYS_io_submit, ctx_id, nr, cbp); 101 return syscall (SYS_io_submit, ctx_id, nr, cbp);
102} 102}
103 103
104inline_size 104inline_size
105int 105int
106ev_io_cancel (aio_context_t ctx_id, struct iocb *cbp, struct io_event *result) 106evsys_io_cancel (aio_context_t ctx_id, struct iocb *cbp, struct io_event *result)
107{ 107{
108 return syscall (SYS_io_cancel, ctx_id, cbp, result); 108 return syscall (SYS_io_cancel, ctx_id, cbp, result);
109} 109}
110 110
111inline_size 111inline_size
112int 112int
113ev_io_getevents (aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout) 113evsys_io_getevents (aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout)
114{ 114{
115 return syscall (SYS_io_getevents, ctx_id, min_nr, nr, events, timeout); 115 return syscall (SYS_io_getevents, ctx_id, min_nr, nr, events, timeout);
116} 116}
117 117
118/*****************************************************************************/ 118/*****************************************************************************/
164 iocb->io.aio_reqprio = 0; 164 iocb->io.aio_reqprio = 0;
165 } 165 }
166#endif 166#endif
167 167
168 if (iocb->io.aio_buf) 168 if (iocb->io.aio_buf)
169 ev_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); /* always returns an error relevant kernels */ 169 evsys_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); /* always returns an error relevant kernels */
170 170
171 if (nev) 171 if (nev)
172 { 172 {
173 iocb->io.aio_data = fd; 173 iocb->io.aio_data = fd;
174 iocb->io.aio_fildes = fd; 174 iocb->io.aio_fildes = fd;
250 linuxaio_iocbps [fd]->io.aio_buf = 0; 250 linuxaio_iocbps [fd]->io.aio_buf = 0;
251 anfds [fd].events = 0; 251 anfds [fd].events = 0;
252 fd_change (EV_A_ fd, 0); 252 fd_change (EV_A_ fd, 0);
253 253
254 /* feed events, we do not expect or handle POLLNVAL */ 254 /* feed events, we do not expect or handle POLLNVAL */
255 if (expect_false (res & POLLNVAL))
256 fd_kill (EV_A_ fd);
257 else
258 fd_event ( 255 fd_event (
259 EV_A_ 256 EV_A_
260 fd, 257 fd,
261 (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) 258 (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
262 | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) 259 | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
263 ); 260 );
264 261
265 --nr; 262 --nr;
266 ++ev; 263 ++ev;
267 } 264 }
268} 265}
329 EV_RELEASE_CB; 326 EV_RELEASE_CB;
330 327
331 ts.tv_sec = (long)timeout; 328 ts.tv_sec = (long)timeout;
332 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); 329 ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9);
333 330
334 res = ev_io_getevents (linuxaio_ctx, 1, sizeof (ioev) / sizeof (ioev [0]), ioev, &ts); 331 res = evsys_io_getevents (linuxaio_ctx, 1, sizeof (ioev) / sizeof (ioev [0]), ioev, &ts);
335 332
336 EV_ACQUIRE_CB; 333 EV_ACQUIRE_CB;
337 334
338 if (res < 0) 335 if (res < 0)
339 if (errno == EINTR) 336 if (errno == EINTR)
361 for (submitted = 0; submitted < linuxaio_submitcnt; ) 358 for (submitted = 0; submitted < linuxaio_submitcnt; )
362 { 359 {
363#if 0 360#if 0
364 int res; 361 int res;
365 if (linuxaio_submits[submitted]->aio_fildes == backend_fd) 362 if (linuxaio_submits[submitted]->aio_fildes == backend_fd)
366 res = ev_io_submit (linuxaio_ctx, 1, linuxaio_submits + submitted); 363 res = evsys_io_submit (linuxaio_ctx, 1, linuxaio_submits + submitted);
367 else 364 else
368 { res = -1; errno = EINVAL; }; 365 { res = -1; errno = EINVAL; };
369#else 366#else
370 int res = ev_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted); 367 int res = evsys_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted);
371#endif 368#endif
372 369
373 if (expect_false (res < 0)) 370 if (expect_false (res < 0))
374 if (errno == EAGAIN) 371 if (errno == EAGAIN)
375 { 372 {
399 * See https://lore.kernel.org/patchwork/patch/1047453/ to see 396 * See https://lore.kernel.org/patchwork/patch/1047453/ to see
400 * discussion about such a case (ttys) where polling for POLLIN 397 * discussion about such a case (ttys) where polling for POLLIN
401 * fails but POLLIN|POLLOUT works. 398 * fails but POLLIN|POLLOUT works.
402 */ 399 */
403 struct iocb *iocb = linuxaio_submits [submitted]; 400 struct iocb *iocb = linuxaio_submits [submitted];
401
402 linuxaio_rearm_epoll (EV_A_ linuxaio_submits [submitted], EPOLL_CTL_ADD);
403 iocb->aio_reqprio = -1; /* mark iocb as epoll */
404
404 res = 1; /* skip this iocb */ 405 res = 1; /* skip this iocb */
405
406 linuxaio_rearm_epoll (EV_A_ iocb, EPOLL_CTL_ADD);
407 iocb->aio_reqprio = -1; /* mark iocb as epoll */
408 } 406 }
409#endif 407#endif
408 else if (errno == EBADF)
409 {
410 fd_kill (EV_A_ linuxaio_submits [submitted]->aio_fildes);
411
412 res = 1; /* skip this iocb */
413 }
410 else 414 else
411 ev_syserr ("(libev) linuxaio io_submit"); 415 ev_syserr ("(libev) linuxaio io_submit");
412 416
413 submitted += res; 417 submitted += res;
414 } 418 }
435 if (ev_linux_version () < 0x041200) 439 if (ev_linux_version () < 0x041200)
436 return 0; 440 return 0;
437#endif 441#endif
438 442
439 linuxaio_ctx = 0; 443 linuxaio_ctx = 0;
440 if (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) 444 if (evsys_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0)
441 return 0; 445 return 0;
442 446
443#if EPOLL_FALLBACK 447#if EPOLL_FALLBACK
444 backend_fd = ev_epoll_create (); 448 backend_fd = ev_epoll_create ();
445 if (backend_fd < 0) 449 if (backend_fd < 0)
446 { 450 {
447 ev_io_destroy (linuxaio_ctx); 451 evsys_io_destroy (linuxaio_ctx);
448 return 0; 452 return 0;
449 } 453 }
450 454
451 ev_io_init (EV_A_ &linuxaio_epoll_w, linuxaio_epoll_cb, backend_fd, EV_READ); 455 ev_io_init (EV_A_ &linuxaio_epoll_w, linuxaio_epoll_cb, backend_fd, EV_READ);
452 ev_set_priority (&linuxaio_epoll_w, EV_MAXPRI); 456 ev_set_priority (&linuxaio_epoll_w, EV_MAXPRI);
473{ 477{
474#if EPOLL_FALLBACK 478#if EPOLL_FALLBACK
475 close (backend_fd); 479 close (backend_fd);
476#endif 480#endif
477 linuxaio_free_iocbp (EV_A); 481 linuxaio_free_iocbp (EV_A);
478 ev_io_destroy (linuxaio_ctx); 482 evsys_io_destroy (linuxaio_ctx);
479} 483}
480 484
481inline_size 485inline_size
482void 486void
483linuxaio_fork (EV_P) 487linuxaio_fork (EV_P)
485 /* this frees all iocbs, which is very heavy-handed */ 489 /* this frees all iocbs, which is very heavy-handed */
486 linuxaio_destroy (EV_A); 490 linuxaio_destroy (EV_A);
487 linuxaio_submitcnt = 0; /* all pointers were invalidated */ 491 linuxaio_submitcnt = 0; /* all pointers were invalidated */
488 492
489 linuxaio_ctx = 0; 493 linuxaio_ctx = 0;
490 while (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) 494 while (evsys_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0)
491 ev_syserr ("(libev) linuxaio io_setup"); 495 ev_syserr ("(libev) linuxaio io_setup");
492 496
493#if EPOLL_FALLBACK 497#if EPOLL_FALLBACK
494 while ((backend_fd = ev_epoll_create ()) < 0) 498 while ((backend_fd = ev_epoll_create ()) < 0)
495 ev_syserr ("(libev) linuxaio epoll_create"); 499 ev_syserr ("(libev) linuxaio epoll_create");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines