2 * This is only intended for use inside a unicorn worker, nowhere else.
3 * EPOLLEXCLUSIVE somewhat mitigates the thundering herd problem for
4 * mostly idle processes since we can't use blocking accept4.
5 * This is NOT intended for use with multi-threaded servers, nor
6 * single-threaded multi-client ("C10K") servers or anything advanced
7 * like that. This use of epoll is only appropriate for a primitive,
8 * single-client, single-threaded servers like unicorn that need to
9 * support SIGKILL timeouts and parent death detection.
11 #if defined(HAVE_EPOLL_CREATE1)
12 # include <sys/epoll.h>
15 # include <ruby/thread.h>
16 #endif /* __linux__ */
18 #if defined(EPOLLEXCLUSIVE) && defined(HAVE_EPOLL_CREATE1)
19 # define USE_EPOLL (1)
21 # define USE_EPOLL (0)
25 #if defined(HAVE_RB_IO_DESCRIPTOR) /* Ruby 3.1+ */
26 # define my_fileno(io) rb_io_descriptor(io)
28 static int my_fileno(VALUE io
)
31 GetOpenFile(io
, fptr
);
32 rb_io_check_closed(fptr
);
35 #endif /* Ruby <3.1 */
39 * returns IO object if EPOLLEXCLUSIVE works and arms readers
41 static VALUE
prep_readers(VALUE cls
, VALUE readers
)
44 int epfd
= epoll_create1(EPOLL_CLOEXEC
);
47 if (epfd
< 0) rb_sys_fail("epoll_create1");
49 epio
= rb_funcall(cls
, rb_intern("for_fd"), 1, INT2NUM(epfd
));
51 Check_Type(readers
, T_ARRAY
);
52 for (i
= 0; i
< RARRAY_LEN(readers
); i
++) {
55 VALUE io
= rb_ary_entry(readers
, i
);
57 e
.data
.u64
= i
; /* the reason readers shouldn't change */
60 * I wanted to use EPOLLET here, but maintaining our own
61 * equivalent of ep->rdllist in Ruby-space doesn't fit
62 * our design at all (and the kernel already has it's own
63 * code path for doing it). So let the kernel spend
64 * cycles on maintaining level-triggering.
66 e
.events
= EPOLLEXCLUSIVE
| EPOLLIN
;
67 fd
= my_fileno(rb_io_get_io(io
));
68 rc
= epoll_ctl(epfd
, EPOLL_CTL_ADD
, fd
, &e
);
69 if (rc
< 0) rb_sys_fail("epoll_ctl");
73 #endif /* USE_EPOLL */
77 struct epoll_event event
;
82 static void *do_wait(void *ptr
) /* runs w/o GVL */
84 struct ep_wait
*epw
= ptr
;
86 * Linux delivers epoll events in the order received, and using
87 * maxevents=1 ensures we pluck one item off ep->rdllist
88 * at-a-time (c.f. fs/eventpoll.c in linux.git, it's quite
89 * easy-to-understand for anybody familiar with Ruby C).
91 return (void *)(long)epoll_wait(epw
->epfd
, &epw
->event
, 1,
96 /* readers must not change between prepare_readers and get_readers */
98 get_readers(VALUE epio
, VALUE ready
, VALUE readers
, VALUE timeout_msec
)
103 Check_Type(ready
, T_ARRAY
);
104 Check_Type(readers
, T_ARRAY
);
106 epw
.epfd
= my_fileno(epio
);
107 epw
.timeout_msec
= NUM2INT(timeout_msec
);
108 n
= (long)rb_thread_call_without_gvl(do_wait
, &epw
, RUBY_UBF_IO
, NULL
);
110 if (errno
!= EINTR
) rb_sys_fail("epoll_wait");
111 } else if (n
> 0) { /* maxevents is hardcoded to 1 */
112 VALUE obj
= rb_ary_entry(readers
, epw
.event
.data
.u64
);
115 rb_ary_push(ready
, obj
);
116 } /* n == 0 : timeout */
119 #endif /* USE_EPOLL */
121 static void init_epollexclusive(VALUE mUnicorn
)
124 VALUE cWaiter
= rb_define_class_under(mUnicorn
, "Waiter", rb_cIO
);
125 rb_define_singleton_method(cWaiter
, "prep_readers", prep_readers
, 1);
126 rb_define_method(cWaiter
, "get_readers", get_readers
, 3);