root/src/eventer/eventer_epoll_impl.c

Revision 31d42e564259174c10ca8df5d8b206096a29c957, 8.6 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

This pulls the timer stuff into the shared base and consolidates a lot
of repeated code across the different scheduler implementations.

times and fdevents are API exposed now and the console exposes them via:

show eventer debug timers
show eventer debug sockets

(the console stuff need to be cleaned up to support autocomplete)

refs #221

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34 #include "eventer/eventer.h"
35 #include "utils/noit_atomic.h"
36 #include "utils/noit_skiplist.h"
37 #include "utils/noit_log.h"
38
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <sys/epoll.h>
43 #include <signal.h>
44 #include <pthread.h>
45 #include <assert.h>
46
47 struct _eventer_impl eventer_epoll_impl;
48 #define LOCAL_EVENTER eventer_epoll_impl
49 #define LOCAL_EVENTER_foreach_fdevent eventer_epoll_impl_foreach_fdevent
50 #define maxfds LOCAL_EVENTER.maxfds
51 #define master_fds LOCAL_EVENTER.master_fds
52
53 #include "eventer/eventer_impl_private.h"
54
55 static pthread_t master_thread;
56 static int *masks;
57 static int epoll_fd = -1;
58
59 static int eventer_epoll_impl_init() {
60   struct rlimit rlim;
61   int rv;
62
63   /* super init */
64   if((rv = eventer_impl_init()) != 0) return rv;
65
66   master_thread = pthread_self();
67   signal(SIGPIPE, SIG_IGN);
68   epoll_fd = epoll_create(1024);
69   if(epoll_fd == -1) {
70     return -1;
71   }
72   getrlimit(RLIMIT_NOFILE, &rlim);
73   maxfds = rlim.rlim_cur;
74   master_fds = calloc(maxfds, sizeof(*master_fds));
75   masks = calloc(maxfds, sizeof(*masks));
76   return 0;
77 }
78 static int eventer_epoll_impl_propset(const char *key, const char *value) {
79   if(eventer_impl_propset(key, value)) {
80     /* Do our epoll local properties here */
81     return -1;
82   }
83   return 0;
84 }
85 static void eventer_epoll_impl_add(eventer_t e) {
86   struct epoll_event _ev;
87   ev_lock_state_t lockstate;
88   assert(e->mask);
89
90   if(e->mask & EVENTER_ASYNCH) {
91     eventer_add_asynch(NULL, e);
92     return;
93   }
94
95   /* Recurrent delegation */
96   if(e->mask & EVENTER_RECURRENT) {
97     eventer_add_recurrent(e);
98     return;
99   }
100
101   /* Timed events are simple */
102   if(e->mask & EVENTER_TIMER) {
103     eventer_add_timed(e);
104     return;
105   }
106
107   /* file descriptor event */
108   memset(&_ev, 0, sizeof(_ev));
109   _ev.data.fd = e->fd;
110   if(e->mask & EVENTER_READ) _ev.events |= (EPOLLIN|EPOLLPRI);
111   if(e->mask & EVENTER_WRITE) _ev.events |= (EPOLLOUT);
112   if(e->mask & EVENTER_EXCEPTION) _ev.events |= (EPOLLERR|EPOLLHUP);
113
114   lockstate = acquire_master_fd(e->fd);
115   master_fds[e->fd].e = e;
116
117   epoll_ctl(epoll_fd, EPOLL_CTL_ADD, e->fd, &_ev);
118
119   release_master_fd(e->fd, lockstate);
120 }
121 static eventer_t eventer_epoll_impl_remove(eventer_t e) {
122   eventer_t removed = NULL;
123   if(e->mask & EVENTER_ASYNCH) {
124     abort();
125   }
126   if(e->mask & (EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION)) {
127     ev_lock_state_t lockstate;
128     struct epoll_event _ev;
129     memset(&_ev, 0, sizeof(_ev));
130     _ev.data.fd = e->fd;
131     lockstate = acquire_master_fd(e->fd);
132     if(e == master_fds[e->fd].e) {
133       removed = e;
134       master_fds[e->fd].e = NULL;
135       epoll_ctl(epoll_fd, EPOLL_CTL_DEL, e->fd, &_ev);
136     }
137     release_master_fd(e->fd, lockstate);
138   }
139   else if(e->mask & EVENTER_TIMER) {
140     removed = eventer_remove_timed(e);
141   }
142   else if(e->mask & EVENTER_RECURRENT) {
143     removed = eventer_remove_recurrent(e);
144   }
145   else {
146     abort();
147   }
148   return removed;
149 }
150 static void eventer_epoll_impl_update(eventer_t e, int mask) {
151   struct epoll_event _ev;
152   if(e->mask & EVENTER_TIMER) {
153     eventer_update_timed(e,mask);
154     return;
155   }
156   memset(&_ev, 0, sizeof(_ev));
157   _ev.data.fd = e->fd;
158   e->mask = mask;
159   if(e->mask & (EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION)) {
160     if(e->mask & EVENTER_READ) _ev.events |= (EPOLLIN|EPOLLPRI);
161     if(e->mask & EVENTER_WRITE) _ev.events |= (EPOLLOUT);
162     if(e->mask & EVENTER_EXCEPTION) _ev.events |= (EPOLLERR|EPOLLHUP);
163     epoll_ctl(epoll_fd, EPOLL_CTL_MOD, e->fd, &_ev);
164   }
165 }
166 static eventer_t eventer_epoll_impl_remove_fd(int fd) {
167   eventer_t eiq = NULL;
168   ev_lock_state_t lockstate;
169   if(master_fds[fd].e) {
170     struct epoll_event _ev;
171     memset(&_ev, 0, sizeof(_ev));
172     _ev.data.fd = fd;
173     lockstate = acquire_master_fd(fd);
174     eiq = master_fds[fd].e;
175     master_fds[fd].e = NULL;
176     epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &_ev);
177     release_master_fd(fd, lockstate);
178   }
179   return eiq;
180 }
181 static eventer_t eventer_epoll_impl_find_fd(int fd) {
182   return master_fds[fd].e;
183 }
184
185 static void eventer_epoll_impl_trigger(eventer_t e, int mask) {
186   struct timeval __now;
187   int fd, oldmask, newmask;
188   const char *cbname;
189   ev_lock_state_t lockstate;
190
191   fd = e->fd;
192   if(e != master_fds[fd].e) return;
193   lockstate = acquire_master_fd(fd);
194   if(lockstate == EV_ALREADY_OWNED) return;
195   assert(lockstate == EV_OWNED);
196
197   gettimeofday(&__now, NULL);
198   oldmask = e->mask;
199   cbname = eventer_name_for_callback(e->callback);
200   noitLT(eventer_deb, &__now, "epoll: fire on %d/%x to %s(%p)\n",
201          fd, mask, cbname?cbname:"???", e->callback);
202   newmask = e->callback(e, mask, e->closure, &__now);
203
204   if(newmask) {
205     struct epoll_event _ev;
206     memset(&_ev, 0, sizeof(_ev));
207     _ev.data.fd = fd;
208     if(newmask & EVENTER_READ) _ev.events |= (EPOLLIN|EPOLLPRI);
209     if(newmask & EVENTER_WRITE) _ev.events |= (EPOLLOUT);
210     if(newmask & EVENTER_EXCEPTION) _ev.events |= (EPOLLERR|EPOLLHUP);
211     epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &_ev);
212     /* Set our mask */
213     e->mask = newmask;
214   }
215   else {
216     /* see kqueue implementation for details on the next line */
217     if(master_fds[fd].e == e) master_fds[fd].e = NULL;
218     eventer_free(e);
219   }
220   release_master_fd(fd, lockstate);
221 }
222 static int eventer_epoll_impl_loop() {
223   int is_master_thread = 0;
224   struct epoll_event *epev;
225   pthread_t self;
226
227   self = pthread_self();
228   if(pthread_equal(self, master_thread)) is_master_thread = 1;
229
230   epev = malloc(sizeof(*epev) * maxfds);
231
232   while(1) {
233     struct timeval __now, __sleeptime;
234     int fd_cnt = 0;
235
236     __sleeptime = eventer_max_sleeptime;
237
238     eventer_dispatch_timed(&__now, &__sleeptime);
239
240     /* Handle recurrent events */
241     eventer_dispatch_recurrent(&__now);
242
243     /* Now we move on to our fd-based events */
244     fd_cnt = epoll_wait(epoll_fd, epev, maxfds,
245                         __sleeptime.tv_sec * 1000 + __sleeptime.tv_usec / 1000);
246     noitLT(eventer_deb, &__now, "debug: epoll_wait(%d, [], %d) => %d\n", epoll_fd, maxfds, fd_cnt);
247     if(fd_cnt < 0) {
248       noitLT(eventer_err, &__now, "epoll_wait: %s\n", strerror(errno));
249     }
250     else {
251       int idx;
252       /* loop once to clear */
253       for(idx = 0; idx < fd_cnt; idx++) {
254         struct epoll_event *ev;
255         eventer_t e;
256         int fd, mask = 0;
257
258         ev = &epev[idx];
259
260         if(ev->events & (EPOLLIN | EPOLLPRI)) mask |= EVENTER_READ;
261         if(ev->events & (EPOLLOUT)) mask |= EVENTER_WRITE;
262         if(ev->events & (EPOLLERR|EPOLLHUP)) mask |= EVENTER_EXCEPTION;
263
264         fd = ev->data.fd;
265
266         e = master_fds[fd].e;
267         /* It's possible that someone removed the event and freed it
268          * before we got here.
269          */
270         if(!e) continue;
271
272         eventer_epoll_impl_trigger(e, mask);
273       }
274     }
275   }
276   /* NOTREACHED */
277   return 0;
278 }
279
280 struct _eventer_impl eventer_epoll_impl = {
281   "epoll",
282   eventer_epoll_impl_init,
283   eventer_epoll_impl_propset,
284   eventer_epoll_impl_add,
285   eventer_epoll_impl_remove,
286   eventer_epoll_impl_update,
287   eventer_epoll_impl_remove_fd,
288   eventer_epoll_impl_find_fd,
289   eventer_epoll_impl_trigger,
290   eventer_epoll_impl_loop,
291   eventer_epoll_impl_foreach_fdevent,
292   { 0, 200000 },
293   0,
294   NULL
295 };
Note: See TracBrowser for help on using the browser.