ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/s_bsd_epoll.c
Revision: 8533
Committed: Sun Apr 29 16:43:06 2018 UTC (7 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 4946 byte(s)
Log Message:
- s_bsd_epoll.c: get rid of the fixed size ep_fdlist and add support for a growing epoll_event array. Some code derived from libevent.

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 2005-2018 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file s_bsd_epoll.c
23 * \brief Linux epoll() compatible network routines.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_EPOLL
29 #include "fdlist.h"
30 #include "ircd.h"
31 #include "s_bsd.h"
32 #include "log.h"
33 #include "memory.h"
34 #include <sys/epoll.h>
35 #include <sys/syscall.h>
36
37 enum
38 {
39 INITIAL_NEVENT = 16,
40 MAXIMUM_NEVENT = 4096
41 };
42
43 struct epollop
44 {
45 struct epoll_event *events;
46 int nevents;
47 int fd;
48 };
49
50 static struct epollop *epollop;
51
52
53 /*
54 * comm_select_init
55 *
56 * This is a needed exported function which will be called to initialise
57 * the network loop code.
58 */
59 void
60 comm_select_init(void)
61 {
62 int fd = epoll_create1(EPOLL_CLOEXEC);
63 if (fd < 0)
64 {
65 ilog(LOG_TYPE_IRCD, "comm_select_init: couldn't open epoll fd: %s",
66 strerror(errno));
67 exit(EXIT_FAILURE); /* Whee! */
68 }
69
70 fd_open(fd, 0, "epoll file descriptor");
71
72 epollop = xcalloc(sizeof(*epollop));
73 epollop->fd = fd;
74 epollop->nevents = INITIAL_NEVENT;
75 epollop->events = xcalloc(epollop->nevents * sizeof(*epollop->events));
76 }
77
78 /*
79 * comm_setselect
80 *
81 * This is a needed exported function which will be called to register
82 * and deregister interest in a pending IO state for a given FD.
83 */
84 void
85 comm_setselect(fde_t *F, unsigned int type, void (*handler)(fde_t *, void *),
86 void *client_data, uintmax_t timeout)
87 {
88 int new_events, op;
89 struct epoll_event ep_event;
90
91 assert(F->flags.open);
92
93 if ((type & COMM_SELECT_READ))
94 {
95 F->read_handler = handler;
96 F->read_data = client_data;
97 }
98
99 if ((type & COMM_SELECT_WRITE))
100 {
101 F->write_handler = handler;
102 F->write_data = client_data;
103 }
104
105 new_events = (F->read_handler ? EPOLLIN : 0) |
106 (F->write_handler ? EPOLLOUT : 0);
107
108 if (timeout)
109 {
110 F->timeout = CurrentTime + (timeout / 1000);
111 F->timeout_handler = handler;
112 F->timeout_data = client_data;
113 }
114
115 if (new_events != F->evcache)
116 {
117 if (new_events == 0)
118 op = EPOLL_CTL_DEL;
119 else if (F->evcache == 0)
120 op = EPOLL_CTL_ADD;
121 else
122 op = EPOLL_CTL_MOD;
123
124 memset(&ep_event, 0, sizeof(ep_event));
125 ep_event.events = F->evcache = new_events;
126 ep_event.data.ptr = F;
127
128 if (epoll_ctl(epollop->fd, op, F->fd, &ep_event))
129 {
130 ilog(LOG_TYPE_IRCD, "comm_setselect: epoll_ctl() failed: %s", strerror(errno));
131 abort();
132 }
133 }
134 }
135
136 /*
137 * comm_select()
138 *
139 * Called to do the new-style IO, courtesy of of squid (like most of this
140 * new IO code). This routine handles the stuff we've hidden in
141 * comm_setselect and fd_table[] and calls callbacks for IO ready
142 * events.
143 */
144 void
145 comm_select(void)
146 {
147 int num;
148 void (*hdl)(fde_t *, void *);
149
150 num = epoll_wait(epollop->fd, epollop->events, epollop->nevents, SELECT_DELAY);
151 assert(num <= epollop->nevents);
152
153 set_time();
154
155 if (num < 0)
156 {
157 const struct timespec req = { .tv_sec = 0, .tv_nsec = 50000000 };
158 nanosleep(&req, NULL); /* Avoid 99% CPU in comm_select */
159 return;
160 }
161
162 for (int i = 0; i < num; ++i)
163 {
164 fde_t *F = epollop->events[i].data.ptr;
165
166 if (F->flags.open == 0)
167 continue;
168
169 if ((epollop->events[i].events & (EPOLLIN | EPOLLHUP | EPOLLERR)))
170 {
171 if ((hdl = F->read_handler))
172 {
173 F->read_handler = NULL;
174 hdl(F, F->read_data);
175
176 if (F->flags.open == 0)
177 continue;
178 }
179 }
180
181 if ((epollop->events[i].events & (EPOLLOUT | EPOLLHUP | EPOLLERR)))
182 {
183 if ((hdl = F->write_handler))
184 {
185 F->write_handler = NULL;
186 hdl(F, F->write_data);
187
188 if (F->flags.open == 0)
189 continue;
190 }
191 }
192
193 comm_setselect(F, 0, NULL, NULL, 0);
194 }
195
196 if (num == epollop->nevents && epollop->nevents < MAXIMUM_NEVENT)
197 {
198 /*
199 * We used all of the event space this time. We should be
200 * ready for more events next time.
201 */
202 int new_nevents = epollop->nevents * 2;
203 struct epoll_event *new_events = xrealloc(epollop->events, new_nevents * sizeof(*epollop->events));
204
205 epollop->events = new_events;
206 epollop->nevents = new_nevents;
207 }
208 }
209 #endif

Properties

Name Value
svn:eol-style native
svn:keywords Id