]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ModEpoll.cc
MemBuf implements Packable interface
[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 = -1;
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 assert(fd >= 0);
113 debugs(5, 5, HERE << "FD " << fd << ", type=" << type <<
114 ", handler=" << handler << ", client_data=" << client_data <<
115 ", timeout=" << timeout);
116
117 struct epoll_event ev;
118 memset(&ev, 0, sizeof(ev));
119 ev.data.fd = fd;
120
121 if (!F->flags.open) {
122 epoll_ctl(kdpfd, EPOLL_CTL_DEL, fd, &ev);
123 return;
124 }
125
126 // If read is an interest
127
128 if (type & COMM_SELECT_READ) {
129 if (handler) {
130 // Hack to keep the events flowing if there is data immediately ready
131 if (F->flags.read_pending)
132 ev.events |= EPOLLOUT;
133 ev.events |= EPOLLIN;
134 }
135
136 F->read_handler = handler;
137
138 F->read_data = client_data;
139
140 // Otherwise, use previously stored value
141 } else if (F->epoll_state & EPOLLIN) {
142 ev.events |= EPOLLIN;
143 }
144
145 // If write is an interest
146 if (type & COMM_SELECT_WRITE) {
147 if (handler)
148 ev.events |= EPOLLOUT;
149
150 F->write_handler = handler;
151
152 F->write_data = client_data;
153
154 // Otherwise, use previously stored value
155 } else if (F->epoll_state & EPOLLOUT) {
156 ev.events |= EPOLLOUT;
157 }
158
159 if (ev.events)
160 ev.events |= EPOLLHUP | EPOLLERR;
161
162 if (ev.events != F->epoll_state) {
163 if (F->epoll_state) // already monitoring something.
164 epoll_ctl_type = ev.events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;
165 else
166 epoll_ctl_type = EPOLL_CTL_ADD;
167
168 F->epoll_state = ev.events;
169
170 if (epoll_ctl(kdpfd, epoll_ctl_type, fd, &ev) < 0) {
171 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "epoll_ctl(," << epolltype_atoi(epoll_ctl_type) <<
172 ",,): failed on FD " << fd << ": " << xstrerror());
173 }
174 }
175
176 if (timeout)
177 F->timeout = squid_curtime + timeout;
178 }
179
180 void
181 Comm::ResetSelect(int fd)
182 {
183 fde *F = &fd_table[fd];
184 F->epoll_state = 0;
185 SetSelect(fd, 0, NULL, NULL, 0);
186 }
187
188 static void commIncomingStats(StoreEntry * sentry);
189
190 static void
191 commEPollRegisterWithCacheManager(void)
192 {
193 Mgr::RegisterAction("comm_epoll_incoming",
194 "comm_incoming() stats",
195 commIncomingStats, 0, 1);
196 }
197
198 static void
199 commIncomingStats(StoreEntry * sentry)
200 {
201 StatCounters *f = &statCounter;
202 storeAppendPrintf(sentry, "Total number of epoll(2) loops: %ld\n", statCounter.select_loops);
203 storeAppendPrintf(sentry, "Histogram of returned filedescriptors\n");
204 f->select_fds_hist.dump(sentry, statHistIntDumper);
205 }
206
207 /**
208 * Check all connections for new connections and input data that is to be
209 * processed. Also check for connections with data queued and whether we can
210 * write it out.
211 *
212 * Called to do the new-style IO, courtesy of of squid (like most of this
213 * new IO code). This routine handles the stuff we've hidden in
214 * comm_setselect and fd_table[] and calls callbacks for IO ready
215 * events.
216 */
217 Comm::Flag
218 Comm::DoSelect(int msec)
219 {
220 int num, i,fd;
221 fde *F;
222 PF *hdl;
223
224 struct epoll_event *cevents;
225
226 PROF_start(comm_check_incoming);
227
228 if (msec > max_poll_time)
229 msec = max_poll_time;
230
231 for (;;) {
232 num = epoll_wait(kdpfd, pevents, SQUID_MAXFD, msec);
233 ++ statCounter.select_loops;
234
235 if (num >= 0)
236 break;
237
238 if (ignoreErrno(errno))
239 break;
240
241 getCurrentTime();
242
243 PROF_stop(comm_check_incoming);
244
245 return Comm::COMM_ERROR;
246 }
247
248 PROF_stop(comm_check_incoming);
249 getCurrentTime();
250
251 statCounter.select_fds_hist.count(num);
252
253 if (num == 0)
254 return Comm::TIMEOUT; /* No error.. */
255
256 PROF_start(comm_handle_ready_fd);
257
258 for (i = 0, cevents = pevents; i < num; ++i, ++cevents) {
259 fd = cevents->data.fd;
260 F = &fd_table[fd];
261 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "got FD " << fd << " events=" <<
262 std::hex << cevents->events << " monitoring=" << F->epoll_state <<
263 " F->read_handler=" << F->read_handler << " F->write_handler=" << F->write_handler);
264
265 // TODO: add EPOLLPRI??
266
267 if (cevents->events & (EPOLLIN|EPOLLHUP|EPOLLERR) || F->flags.read_pending) {
268 if ((hdl = F->read_handler) != NULL) {
269 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling read handler on FD " << fd);
270 PROF_start(comm_write_handler);
271 F->flags.read_pending = 0;
272 F->read_handler = NULL;
273 hdl(fd, F->read_data);
274 PROF_stop(comm_write_handler);
275 ++ statCounter.select_fds;
276 } else {
277 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no read handler for FD " << fd);
278 // remove interest since no handler exist for this event.
279 SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
280 }
281 }
282
283 if (cevents->events & (EPOLLOUT|EPOLLHUP|EPOLLERR)) {
284 if ((hdl = F->write_handler) != NULL) {
285 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling write handler on FD " << fd);
286 PROF_start(comm_read_handler);
287 F->write_handler = NULL;
288 hdl(fd, F->write_data);
289 PROF_stop(comm_read_handler);
290 ++ statCounter.select_fds;
291 } else {
292 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no write handler for FD " << fd);
293 // remove interest since no handler exist for this event.
294 SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
295 }
296 }
297 }
298
299 PROF_stop(comm_handle_ready_fd);
300
301 return Comm::OK;
302 }
303
304 void
305 Comm::QuickPollRequired(void)
306 {
307 max_poll_time = 10;
308 }
309
310 #endif /* USE_EPOLL */
311