]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm/ModEpoll.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / comm / ModEpoll.cc
CommitLineData
6039b729 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
6039b729 3 *
bbc27441
AJ
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.
6039b729 7 */
8
bbc27441
AJ
9/* DEBUG: section 05 Socket Functions */
10
6039b729 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
f7f3304a 31#include "squid.h"
d841c88d
AJ
32
33#if USE_EPOLL
34
d841c88d 35#include "comm/Loops.h"
1287b9e1 36#include "fde.h"
582c2af2 37#include "globals.h"
d841c88d 38#include "mgr/Registration.h"
582c2af2 39#include "profiler/Profiler.h"
348697ca 40#include "SquidTime.h"
e1656dc4 41#include "StatCounters.h"
00a7574e 42#include "StatHist.h"
d841c88d 43#include "Store.h"
8a02a7f8 44
6039b729 45#define DEBUG_EPOLL 0
46
1a30fdf5 47#include <cerrno>
ad32c661 48#if HAVE_SYS_EPOLL_H
6039b729 49#include <sys/epoll.h>
ad32c661 50#endif
6039b729 51
52static int kdpfd;
53static int max_poll_time = 1000;
54
55static struct epoll_event *pevents;
56
5acc9f37 57static void commEPollRegisterWithCacheManager(void);
6039b729 58
6039b729 59/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
60/* Public functions */
61
6039b729 62/*
6039b729 63 * This is a needed exported function which will be called to initialise
64 * the network loop code.
65 */
66void
d841c88d 67Comm::SelectLoopInit(void)
6039b729 68{
6039b729 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 }
8cb183ec
FC
80
81 commEPollRegisterWithCacheManager();
6039b729 82}
83
751406fe 84static const char* epolltype_atoi(int x)
85{
26ac0430 86 switch (x) {
751406fe 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
d841c88d 102/**
6039b729 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.
6039b729 105 */
106void
d841c88d 107Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout)
6039b729 108{
109 fde *F = &fd_table[fd];
751406fe 110 int epoll_ctl_type = 0;
6039b729 111
112 struct epoll_event ev;
113 assert(fd >= 0);
48e7baac 114 debugs(5, 5, HERE << "FD " << fd << ", type=" << type <<
d841c88d
AJ
115 ", handler=" << handler << ", client_data=" << client_data <<
116 ", timeout=" << timeout);
6039b729 117
b4bab919 118 if (RUNNING_ON_VALGRIND) {
26ac0430
AJ
119 /* Keep valgrind happy.. complains about uninitialized bytes otherwise */
120 memset(&ev, 0, sizeof(ev));
b4bab919 121 }
751406fe 122 ev.events = 0;
123 ev.data.fd = fd;
6039b729 124
a1727eb7 125 if (!F->flags.open) {
126 epoll_ctl(kdpfd, EPOLL_CTL_DEL, fd, &ev);
127 return;
128 }
129
751406fe 130 // If read is an interest
6039b729 131
132 if (type & COMM_SELECT_READ) {
0a080ce3 133 if (handler) {
26ac0430
AJ
134 // Hack to keep the events flowing if there is data immediately ready
135 if (F->flags.read_pending)
136 ev.events |= EPOLLOUT;
751406fe 137 ev.events |= EPOLLIN;
26ac0430 138 }
6039b729 139
140 F->read_handler = handler;
141
142 F->read_data = client_data;
751406fe 143
144 // Otherwise, use previously stored value
145 } else if (F->epoll_state & EPOLLIN) {
146 ev.events |= EPOLLIN;
6039b729 147 }
148
751406fe 149 // If write is an interest
6039b729 150 if (type & COMM_SELECT_WRITE) {
751406fe 151 if (handler)
152 ev.events |= EPOLLOUT;
6039b729 153
154 F->write_handler = handler;
155
156 F->write_data = client_data;
6039b729 157
751406fe 158 // Otherwise, use previously stored value
159 } else if (F->epoll_state & EPOLLOUT) {
160 ev.events |= EPOLLOUT;
161 }
6039b729 162
751406fe 163 if (ev.events)
164 ev.events |= EPOLLHUP | EPOLLERR;
6039b729 165
751406fe 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;
6039b729 171
751406fe 172 F->epoll_state = ev.events;
6039b729 173
751406fe 174 if (epoll_ctl(kdpfd, epoll_ctl_type, fd, &ev) < 0) {
d841c88d 175 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "epoll_ctl(," << epolltype_atoi(epoll_ctl_type) <<
bf8fe701 176 ",,): failed on FD " << fd << ": " << xstrerror());
6039b729 177 }
178 }
179
180 if (timeout)
181 F->timeout = squid_curtime + timeout;
182}
183
3a5a4930 184void
d841c88d 185Comm::ResetSelect(int fd)
3a5a4930 186{
187 fde *F = &fd_table[fd];
188 F->epoll_state = 0;
d841c88d 189 SetSelect(fd, 0, NULL, NULL, 0);
3a5a4930 190}
191
610ee341 192static void commIncomingStats(StoreEntry * sentry);
193
5acc9f37 194static void
dbeed9ea 195commEPollRegisterWithCacheManager(void)
610ee341 196{
8822ebee 197 Mgr::RegisterAction("comm_epoll_incoming",
d9fc6862
A
198 "comm_incoming() stats",
199 commIncomingStats, 0, 1);
610ee341 200}
201
202static void
203commIncomingStats(StoreEntry * sentry)
204{
205 StatCounters *f = &statCounter;
0a515876 206 storeAppendPrintf(sentry, "Total number of epoll(2) loops: %ld\n", statCounter.select_loops);
610ee341 207 storeAppendPrintf(sentry, "Histogram of returned filedescriptors\n");
96886986 208 f->select_fds_hist.dump(sentry, statHistIntDumper);
610ee341 209}
210
d841c88d 211/**
6039b729 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.
6039b729 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 */
c8407295 221Comm::Flag
d841c88d 222Comm::DoSelect(int msec)
6039b729 223{
224 int num, i,fd;
225 fde *F;
226 PF *hdl;
227
228 struct epoll_event *cevents;
6039b729 229
a1727eb7 230 PROF_start(comm_check_incoming);
231
6039b729 232 if (msec > max_poll_time)
233 msec = max_poll_time;
234
235 for (;;) {
236 num = epoll_wait(kdpfd, pevents, SQUID_MAXFD, msec);
098346fd 237 ++ statCounter.select_loops;
6039b729 238
239 if (num >= 0)
240 break;
241
242 if (ignoreErrno(errno))
243 break;
244
245 getCurrentTime();
246
a1727eb7 247 PROF_stop(comm_check_incoming);
248
4ee57cbe 249 return Comm::COMM_ERROR;
6039b729 250 }
251
a1727eb7 252 PROF_stop(comm_check_incoming);
6039b729 253 getCurrentTime();
254
f30f7998 255 statCounter.select_fds_hist.count(num);
751406fe 256
6039b729 257 if (num == 0)
f53969cc 258 return Comm::TIMEOUT; /* No error.. */
751406fe 259
260 PROF_start(comm_handle_ready_fd);
6039b729 261
cbebe602 262 for (i = 0, cevents = pevents; i < num; ++i, ++cevents) {
6039b729 263 fd = cevents->data.fd;
264 F = &fd_table[fd];
d841c88d 265 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "got FD " << fd << " events=" <<
26ac0430
AJ
266 std::hex << cevents->events << " monitoring=" << F->epoll_state <<
267 " F->read_handler=" << F->read_handler << " F->write_handler=" << F->write_handler);
751406fe 268
269 // TODO: add EPOLLPRI??
6039b729 270
0a080ce3 271 if (cevents->events & (EPOLLIN|EPOLLHUP|EPOLLERR) || F->flags.read_pending) {
a1727eb7 272 if ((hdl = F->read_handler) != NULL) {
d841c88d 273 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling read handler on FD " << fd);
751406fe 274 PROF_start(comm_write_handler);
0a080ce3 275 F->flags.read_pending = 0;
6039b729 276 F->read_handler = NULL;
277 hdl(fd, F->read_data);
751406fe 278 PROF_stop(comm_write_handler);
098346fd 279 ++ statCounter.select_fds;
751406fe 280 } else {
d841c88d 281 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no read handler for FD " << fd);
751406fe 282 // remove interest since no handler exist for this event.
d841c88d 283 SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
6039b729 284 }
285 }
286
751406fe 287 if (cevents->events & (EPOLLOUT|EPOLLHUP|EPOLLERR)) {
a1727eb7 288 if ((hdl = F->write_handler) != NULL) {
d841c88d 289 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling write handler on FD " << fd);
751406fe 290 PROF_start(comm_read_handler);
6039b729 291 F->write_handler = NULL;
292 hdl(fd, F->write_data);
751406fe 293 PROF_stop(comm_read_handler);
098346fd 294 ++ statCounter.select_fds;
751406fe 295 } else {
d841c88d 296 debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no write handler for FD " << fd);
751406fe 297 // remove interest since no handler exist for this event.
d841c88d 298 SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
6039b729 299 }
300 }
301 }
302
751406fe 303 PROF_stop(comm_handle_ready_fd);
304
c8407295 305 return Comm::OK;
6039b729 306}
307
308void
d841c88d 309Comm::QuickPollRequired(void)
6039b729 310{
768d3d2f 311 max_poll_time = 10;
6039b729 312}
313
8a02a7f8 314#endif /* USE_EPOLL */
f53969cc 315