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