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

Comparing libev/ev_iouring.c (file contents):
Revision 1.7 by root, Sun Dec 22 15:05:32 2019 UTC vs.
Revision 1.8 by root, Fri Dec 27 16:08:24 2019 UTC

100{ 100{
101 __u8 opcode; 101 __u8 opcode;
102 __u8 flags; 102 __u8 flags;
103 __u16 ioprio; 103 __u16 ioprio;
104 __s32 fd; 104 __s32 fd;
105 union {
105 __u64 off; 106 __u64 off;
107 __u64 addr2;
108 };
106 __u64 addr; 109 __u64 addr;
107 __u32 len; 110 __u32 len;
108 union { 111 union {
109 __kernel_rwf_t rw_flags; 112 __kernel_rwf_t rw_flags;
110 __u32 fsync_flags; 113 __u32 fsync_flags;
111 __u16 poll_events; 114 __u16 poll_events;
112 __u32 sync_range_flags; 115 __u32 sync_range_flags;
113 __u32 msg_flags; 116 __u32 msg_flags;
117 __u32 timeout_flags;
118 __u32 accept_flags;
119 __u32 cancel_flags;
120 __u32 open_flags;
121 __u32 statx_flags;
114 }; 122 };
115 __u64 user_data; 123 __u64 user_data;
116 union { 124 union {
117 __u16 buf_index; 125 __u16 buf_index;
118 __u64 __pad2[3]; 126 __u64 __pad2[3];
155 __u32 sq_entries; 163 __u32 sq_entries;
156 __u32 cq_entries; 164 __u32 cq_entries;
157 __u32 flags; 165 __u32 flags;
158 __u32 sq_thread_cpu; 166 __u32 sq_thread_cpu;
159 __u32 sq_thread_idle; 167 __u32 sq_thread_idle;
168 __u32 features;
160 __u32 resv[5]; 169 __u32 resv[4];
161 struct io_sqring_offsets sq_off; 170 struct io_sqring_offsets sq_off;
162 struct io_cqring_offsets cq_off; 171 struct io_cqring_offsets cq_off;
163}; 172};
164 173
165#define IORING_OP_POLL_ADD 6 174#define IORING_OP_POLL_ADD 6
168#define IORING_ENTER_GETEVENTS 0x01 177#define IORING_ENTER_GETEVENTS 0x01
169 178
170#define IORING_OFF_SQ_RING 0x00000000ULL 179#define IORING_OFF_SQ_RING 0x00000000ULL
171#define IORING_OFF_CQ_RING 0x08000000ULL 180#define IORING_OFF_CQ_RING 0x08000000ULL
172#define IORING_OFF_SQES 0x10000000ULL 181#define IORING_OFF_SQES 0x10000000ULL
182
183#define IORING_FEAT_SINGLE_MMAP 0x1
184#define IORING_FEAT_NODROP 0x2
185#define IORING_FEAT_SUBMIT_STABLE 0x4
173 186
174inline_size 187inline_size
175int 188int
176evsys_io_uring_setup (unsigned entries, struct io_uring_params *params) 189evsys_io_uring_setup (unsigned entries, struct io_uring_params *params)
177{ 190{
240iouring_tfd_cb (EV_P_ struct ev_io *w, int revents) 253iouring_tfd_cb (EV_P_ struct ev_io *w, int revents)
241{ 254{
242 iouring_tfd_to = EV_TSTAMP_HUGE; 255 iouring_tfd_to = EV_TSTAMP_HUGE;
243} 256}
244 257
245static void
246iouring_epoll_cb (EV_P_ struct ev_io *w, int revents)
247{
248 epoll_poll (EV_A_ 0);
249}
250
251/* called for full and partial cleanup */ 258/* called for full and partial cleanup */
252ecb_cold 259ecb_cold
253static int 260static int
254iouring_internal_destroy (EV_P) 261iouring_internal_destroy (EV_P)
255{ 262{
258 265
259 if (iouring_sq_ring != MAP_FAILED) munmap (iouring_sq_ring, iouring_sq_ring_size); 266 if (iouring_sq_ring != MAP_FAILED) munmap (iouring_sq_ring, iouring_sq_ring_size);
260 if (iouring_cq_ring != MAP_FAILED) munmap (iouring_cq_ring, iouring_cq_ring_size); 267 if (iouring_cq_ring != MAP_FAILED) munmap (iouring_cq_ring, iouring_cq_ring_size);
261 if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes , iouring_sqes_size ); 268 if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes , iouring_sqes_size );
262 269
263 if (ev_is_active (&iouring_epoll_w)) ev_ref (EV_A); ev_io_stop (EV_A_ &iouring_epoll_w); 270 if (ev_is_active (&iouring_tfd_w))
264 if (ev_is_active (&iouring_tfd_w )) ev_ref (EV_A); ev_io_stop (EV_A_ &iouring_tfd_w ); 271 {
272 ev_ref (EV_A);
273 ev_io_stop (EV_A_ &iouring_tfd_w);
274 }
265} 275}
266 276
267ecb_cold 277ecb_cold
268static int 278static int
269iouring_internal_init (EV_P) 279iouring_internal_init (EV_P)
346 iouring_internal_destroy (EV_A); 356 iouring_internal_destroy (EV_A);
347 357
348 while (iouring_internal_init (EV_A) < 0) 358 while (iouring_internal_init (EV_A) < 0)
349 ev_syserr ("(libev) io_uring_setup"); 359 ev_syserr ("(libev) io_uring_setup");
350 360
351 /* forking epoll should also effectively unregister all fds from the backend */
352 epoll_fork (EV_A);
353 /* epoll_fork already did this. hopefully */
354 /*fd_rearm_all (EV_A);*/ 361 fd_rearm_all (EV_A);
355
356 ev_io_stop (EV_A_ &iouring_epoll_w);
357 ev_io_set (EV_A_ &iouring_epoll_w, backend_fd, EV_READ);
358 ev_io_start (EV_A_ &iouring_epoll_w);
359 362
360 ev_io_stop (EV_A_ &iouring_tfd_w); 363 ev_io_stop (EV_A_ &iouring_tfd_w);
361 ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ); 364 ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ);
362 ev_io_start (EV_A_ &iouring_tfd_w); 365 ev_io_start (EV_A_ &iouring_tfd_w);
363} 366}
365/*****************************************************************************/ 368/*****************************************************************************/
366 369
367static void 370static void
368iouring_modify (EV_P_ int fd, int oev, int nev) 371iouring_modify (EV_P_ int fd, int oev, int nev)
369{ 372{
370 if (ecb_expect_false (anfds [fd].eflags))
371 {
372 /* we handed this fd over to epoll, so undo this first */
373 /* we do it manually because the optimisations on epoll_modify won't do us any good */
374 epoll_ctl (iouring_fd, EPOLL_CTL_DEL, fd, 0);
375 anfds [fd].eflags = 0;
376 oev = 0;
377 }
378
379 if (oev) 373 if (oev)
380 { 374 {
381 /* we assume the sqe's are all "properly" initialised */ 375 /* we assume the sqe's are all "properly" initialised */
382 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); 376 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
383 sqe->opcode = IORING_OP_POLL_REMOVE; 377 sqe->opcode = IORING_OP_POLL_REMOVE;
450 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) 444 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen))
451 return; 445 return;
452 446
453 if (ecb_expect_false (res < 0)) 447 if (ecb_expect_false (res < 0))
454 { 448 {
455 if (res == -EINVAL) 449 //TODO: EINVAL handling (was something failed with this fd)
456 { 450 //TODO: EBUSY happens when?
457 /* we assume this error code means the fd/poll combination is buggy
458 * and fall back to epoll.
459 * this error code might also indicate a bug, but the kernel doesn't
460 * distinguish between those two conditions, so... sigh...
461 */
462 451
463 epoll_modify (EV_A_ fd, 0, anfds [fd].events);
464 }
465 else if (res == -EBADF) 452 if (res == -EBADF)
466 { 453 {
467 assert (("libev: event loop rejected bad fd", res != -EBADF)); 454 assert (("libev: event loop rejected bad fd", res != -EBADF));
468 fd_kill (EV_A_ fd); 455 fd_kill (EV_A_ fd);
469 } 456 }
470 else 457 else
607 594
608inline_size 595inline_size
609int 596int
610iouring_init (EV_P_ int flags) 597iouring_init (EV_P_ int flags)
611{ 598{
612 if (!epoll_init (EV_A_ 0))
613 return 0;
614
615 iouring_entries = IOURING_INIT_ENTRIES; 599 iouring_entries = IOURING_INIT_ENTRIES;
616 iouring_max_entries = 0; 600 iouring_max_entries = 0;
617 601
618 if (iouring_internal_init (EV_A) < 0) 602 if (iouring_internal_init (EV_A) < 0)
619 { 603 {
620 iouring_internal_destroy (EV_A); 604 iouring_internal_destroy (EV_A);
621 return 0; 605 return 0;
622 } 606 }
623 607
624 ev_io_init (&iouring_epoll_w, iouring_epoll_cb, backend_fd, EV_READ);
625 ev_set_priority (&iouring_epoll_w, EV_MAXPRI);
626
627 ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ); 608 ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ);
628 ev_set_priority (&iouring_tfd_w, EV_MAXPRI); 609 ev_set_priority (&iouring_tfd_w, EV_MINPRI);
629
630 ev_io_start (EV_A_ &iouring_epoll_w);
631 ev_unref (EV_A); /* watcher should not keep loop alive */
632
633 ev_io_start (EV_A_ &iouring_tfd_w); 610 ev_io_start (EV_A_ &iouring_tfd_w);
634 ev_unref (EV_A); /* watcher should not keep loop alive */ 611 ev_unref (EV_A); /* watcher should not keep loop alive */
635 612
636 backend_modify = iouring_modify; 613 backend_modify = iouring_modify;
637 backend_poll = iouring_poll; 614 backend_poll = iouring_poll;
642inline_size 619inline_size
643void 620void
644iouring_destroy (EV_P) 621iouring_destroy (EV_P)
645{ 622{
646 iouring_internal_destroy (EV_A); 623 iouring_internal_destroy (EV_A);
647 epoll_destroy (EV_A);
648} 624}
649 625

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines