]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ModEpoll.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / comm / ModEpoll.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 05 Socket Functions
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
32 */
33
34 /*
35 * The idea for this came from these two websites:
36 * http://www.xmailserver.org/linux-patches/nio-improve.html
37 * http://www.kegel.com/c10k.html
38 *
39 * This is to support the epoll sysctl being added to the linux 2.5
40 * kernel tree. The new sys_epoll is an event based poller without
41 * most of the fuss of rtsignals.
42 *
43 * -- David Nicklay <dnicklay@web.turner.com>
44 */
45
46 /*
47 * XXX Currently not implemented / supported by this module XXX
48 *
49 * - delay pools
50 * - deferred reads
51 *
52 */
53
54 #include "squid.h"
55
56 #if USE_EPOLL
57
58 #include "comm/Loops.h"
59 #include "fde.h"
60 #include "globals.h"
61 #include "mgr/Registration.h"
62 #include "profiler/Profiler.h"
63 #include "SquidTime.h"
64 #include "StatCounters.h"
65 #include "StatHist.h"
66 #include "Store.h"
67
68 #define DEBUG_EPOLL 0
69
70 #if HAVE_SYS_EPOLL_H
71 #include <sys/epoll.h>
72 #endif
73 #if HAVE_ERRNO_H
74 #include <errno.h>
75 #endif
76
77 static int kdpfd;
78 static int max_poll_time = 1000;
79
80 static struct epoll_event *pevents;
81
82 static void commEPollRegisterWithCacheManager(void);
83
84 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
85 /* Public functions */
86
87 /*
88 * This is a needed exported function which will be called to initialise
89 * the network loop code.
90 */
91 void
92 Comm::SelectLoopInit(void)
93 {
94 pevents = (struct epoll_event *) xmalloc(SQUID_MAXFD * sizeof(struct epoll_event));
95
96 if (!pevents) {
97 fatalf("comm_select_init: xmalloc() failed: %s\n",xstrerror());
98 }
99
100 kdpfd = epoll_create(SQUID_MAXFD);
101
102 if (kdpfd < 0) {
103 fatalf("comm_select_init: epoll_create(): %s\n",xstrerror());
104 }
105
106 commEPollRegisterWithCacheManager();
107 }
108
109 static const char* epolltype_atoi(int x)
110 {
111 switch (x) {
112
113 case EPOLL_CTL_ADD:
114 return "EPOLL_CTL_ADD";
115
116 case EPOLL_CTL_DEL:
117 return "EPOLL_CTL_DEL";
118
119 case EPOLL_CTL_MOD:
120 return "EPOLL_CTL_MOD";
121
122 default:
123 return "UNKNOWN_EPOLLCTL_OP";
124 }
125 }
126
127 /**
128 * This is a needed exported function which will be called to register
129 * and deregister interest in a pending IO state for a given FD.
130 */
131 void
132 Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout)
133 {
134 fde *F = &fd_table[fd];
135 int epoll_ctl_type = 0;
136
137 struct epoll_event ev;
138 assert(fd >= 0);
139 debugs(5, 5, HERE << "FD " << fd << ", type=" << type <<
140 ", handler=" << handler << ", client_data=" << client_data <<
141 ", timeout=" << timeout);
142
143 if (RUNNING_ON_VALGRIND) {
144 /* Keep valgrind happy.. complains about uninitialized bytes otherwise */
145 memset(&ev, 0, sizeof(ev));
146 }
147 ev.events = 0;
148 ev.data.fd = fd;
149
150 if (!F->flags.open) {
151 epoll_ctl(kdpfd, EPOLL_CTL_DEL, fd, &ev);
152 return;
153 }
154
155 // If read is an interest
156
157 if (type & COMM_SELECT_READ) {
158 if (handler) {
159 // Hack to keep the events flowing if there is data immediately ready
160 if (F->flags.read_pending)
161 ev.events |= EPOLLOUT;
162 ev.events |= EPOLLIN;
163 }
164
165 F->read_handler = handler;
166
167 F->read_data = client_data;
168
169 // Otherwise, use previously stored value
170 } else if (F->epoll_state & EPOLLIN) {
171 ev.events |= EPOLLIN;
172 }
173
174 // If write is an interest
175 if (type & COMM_SELECT_WRITE) {
176 if (handler)
177 ev.events |= EPOLLOUT;
178
179 F->write_handler = handler;
180
181 F->write_data = client_data;
182
183 // Otherwise, use previously stored value
184 } else if (F->epoll_state & EPOLLOUT) {
185 ev.events |= EPOLLOUT;
186 }
187
188 if (ev.events)
189 ev.events |= EPOLLHUP | EPOLLERR;
190
191 if (ev.events != F->epoll_state) {
192 if (F->epoll_state) // already monitoring something.
193 epoll_ctl_type = ev.events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;
194 else
195 epoll_ctl_type = EPOLL_CTL_ADD;
196
197 F->epoll_state = ev.events;
198
199 if (epoll_ctl(kdpfd, epoll_ctl_type, fd, &ev) < 0) {
200 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "epoll_ctl(," << epolltype_atoi(epoll_ctl_type) <<
201 ",,): failed on FD " << fd << ": " << xstrerror());
202 }
203 }
204
205 if (timeout)
206 F->timeout = squid_curtime + timeout;
207 }
208
209 void
210 Comm::ResetSelect(int fd)
211 {
212 fde *F = &fd_table[fd];
213 F->epoll_state = 0;
214 SetSelect(fd, 0, NULL, NULL, 0);
215 }
216
217 static void commIncomingStats(StoreEntry * sentry);
218
219 static void
220 commEPollRegisterWithCacheManager(void)
221 {
222 Mgr::RegisterAction("comm_epoll_incoming",
223 "comm_incoming() stats",
224 commIncomingStats, 0, 1);
225 }
226
227 static void
228 commIncomingStats(StoreEntry * sentry)
229 {
230 StatCounters *f = &statCounter;
231 storeAppendPrintf(sentry, "Total number of epoll(2) loops: %ld\n", statCounter.select_loops);
232 storeAppendPrintf(sentry, "Histogram of returned filedescriptors\n");
233 f->select_fds_hist.dump(sentry, statHistIntDumper);
234 }
235
236 /**
237 * Check all connections for new connections and input data that is to be
238 * processed. Also check for connections with data queued and whether we can
239 * write it out.
240 *
241 * Called to do the new-style IO, courtesy of of squid (like most of this
242 * new IO code). This routine handles the stuff we've hidden in
243 * comm_setselect and fd_table[] and calls callbacks for IO ready
244 * events.
245 */
246 comm_err_t
247 Comm::DoSelect(int msec)
248 {
249 int num, i,fd;
250 fde *F;
251 PF *hdl;
252
253 struct epoll_event *cevents;
254
255 PROF_start(comm_check_incoming);
256
257 if (msec > max_poll_time)
258 msec = max_poll_time;
259
260 for (;;) {
261 num = epoll_wait(kdpfd, pevents, SQUID_MAXFD, msec);
262 ++ statCounter.select_loops;
263
264 if (num >= 0)
265 break;
266
267 if (ignoreErrno(errno))
268 break;
269
270 getCurrentTime();
271
272 PROF_stop(comm_check_incoming);
273
274 return COMM_ERROR;
275 }
276
277 PROF_stop(comm_check_incoming);
278 getCurrentTime();
279
280 statCounter.select_fds_hist.count(num);
281
282 if (num == 0)
283 return COMM_TIMEOUT; /* No error.. */
284
285 PROF_start(comm_handle_ready_fd);
286
287 for (i = 0, cevents = pevents; i < num; ++i, ++cevents) {
288 fd = cevents->data.fd;
289 F = &fd_table[fd];
290 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "got FD " << fd << " events=" <<
291 std::hex << cevents->events << " monitoring=" << F->epoll_state <<
292 " F->read_handler=" << F->read_handler << " F->write_handler=" << F->write_handler);
293
294 // TODO: add EPOLLPRI??
295
296 if (cevents->events & (EPOLLIN|EPOLLHUP|EPOLLERR) || F->flags.read_pending) {
297 if ((hdl = F->read_handler) != NULL) {
298 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling read handler on FD " << fd);
299 PROF_start(comm_write_handler);
300 F->flags.read_pending = 0;
301 F->read_handler = NULL;
302 hdl(fd, F->read_data);
303 PROF_stop(comm_write_handler);
304 ++ statCounter.select_fds;
305 } else {
306 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no read handler for FD " << fd);
307 // remove interest since no handler exist for this event.
308 SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
309 }
310 }
311
312 if (cevents->events & (EPOLLOUT|EPOLLHUP|EPOLLERR)) {
313 if ((hdl = F->write_handler) != NULL) {
314 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling write handler on FD " << fd);
315 PROF_start(comm_read_handler);
316 F->write_handler = NULL;
317 hdl(fd, F->write_data);
318 PROF_stop(comm_read_handler);
319 ++ statCounter.select_fds;
320 } else {
321 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no write handler for FD " << fd);
322 // remove interest since no handler exist for this event.
323 SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
324 }
325 }
326 }
327
328 PROF_stop(comm_handle_ready_fd);
329
330 return COMM_OK;
331 }
332
333 void
334 Comm::QuickPollRequired(void)
335 {
336 max_poll_time = 10;
337 }
338
339 #endif /* USE_EPOLL */