]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ModEpoll.cc
514af5caa4eed747a71ff113fc36776dbe5692c8
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 05 Socket Functions */
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
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.
20 * -- David Nicklay <dnicklay@web.turner.com>
24 * XXX Currently not implemented / supported by this module XXX
35 #include "comm/Loops.h"
38 #include "mgr/Registration.h"
39 #include "profiler/Profiler.h"
40 #include "SquidTime.h"
41 #include "StatCounters.h"
49 #include <sys/epoll.h>
52 static int kdpfd
= -1;
53 static int max_poll_time
= 1000;
55 static struct epoll_event
*pevents
;
57 static void commEPollRegisterWithCacheManager(void);
59 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
60 /* Public functions */
63 * This is a needed exported function which will be called to initialise
64 * the network loop code.
67 Comm::SelectLoopInit(void)
69 pevents
= (struct epoll_event
*) xmalloc(SQUID_MAXFD
* sizeof(struct epoll_event
));
73 fatalf("comm_select_init: xmalloc() failed: %s\n", xstrerr(xerrno
));
76 kdpfd
= epoll_create(SQUID_MAXFD
);
80 fatalf("comm_select_init: epoll_create(): %s\n", xstrerr(xerrno
));
83 commEPollRegisterWithCacheManager();
86 static const char* epolltype_atoi(int x
)
91 return "EPOLL_CTL_ADD";
94 return "EPOLL_CTL_DEL";
97 return "EPOLL_CTL_MOD";
100 return "UNKNOWN_EPOLLCTL_OP";
105 * This is a needed exported function which will be called to register
106 * and deregister interest in a pending IO state for a given FD.
109 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
111 fde
*F
= &fd_table
[fd
];
112 int epoll_ctl_type
= 0;
115 debugs(5, 5, HERE
<< "FD " << fd
<< ", type=" << type
<<
116 ", handler=" << handler
<< ", client_data=" << client_data
<<
117 ", timeout=" << timeout
);
119 struct epoll_event ev
;
120 memset(&ev
, 0, sizeof(ev
));
123 if (!F
->flags
.open
) {
124 epoll_ctl(kdpfd
, EPOLL_CTL_DEL
, fd
, &ev
);
128 // If read is an interest
130 if (type
& COMM_SELECT_READ
) {
132 // Hack to keep the events flowing if there is data immediately ready
133 if (F
->flags
.read_pending
)
134 ev
.events
|= EPOLLOUT
;
135 ev
.events
|= EPOLLIN
;
138 F
->read_handler
= handler
;
140 F
->read_data
= client_data
;
142 // Otherwise, use previously stored value
143 } else if (F
->epoll_state
& EPOLLIN
) {
144 ev
.events
|= EPOLLIN
;
147 // If write is an interest
148 if (type
& COMM_SELECT_WRITE
) {
150 ev
.events
|= EPOLLOUT
;
152 F
->write_handler
= handler
;
154 F
->write_data
= client_data
;
156 // Otherwise, use previously stored value
157 } else if (F
->epoll_state
& EPOLLOUT
) {
158 ev
.events
|= EPOLLOUT
;
162 ev
.events
|= EPOLLHUP
| EPOLLERR
;
164 if (ev
.events
!= F
->epoll_state
) {
165 if (F
->epoll_state
) // already monitoring something.
166 epoll_ctl_type
= ev
.events
? EPOLL_CTL_MOD
: EPOLL_CTL_DEL
;
168 epoll_ctl_type
= EPOLL_CTL_ADD
;
170 F
->epoll_state
= ev
.events
;
172 if (epoll_ctl(kdpfd
, epoll_ctl_type
, fd
, &ev
) < 0) {
174 debugs(5, DEBUG_EPOLL
? 0 : 8, "epoll_ctl(," << epolltype_atoi(epoll_ctl_type
) <<
175 ",,): failed on FD " << fd
<< ": " << xstrerr(xerrno
));
180 F
->timeout
= squid_curtime
+ timeout
;
184 Comm::ResetSelect(int fd
)
186 fde
*F
= &fd_table
[fd
];
188 SetSelect(fd
, 0, NULL
, NULL
, 0);
191 static void commIncomingStats(StoreEntry
* sentry
);
194 commEPollRegisterWithCacheManager(void)
196 Mgr::RegisterAction("comm_epoll_incoming",
197 "comm_incoming() stats",
198 commIncomingStats
, 0, 1);
202 commIncomingStats(StoreEntry
* sentry
)
204 StatCounters
*f
= &statCounter
;
205 storeAppendPrintf(sentry
, "Total number of epoll(2) loops: %ld\n", statCounter
.select_loops
);
206 storeAppendPrintf(sentry
, "Histogram of returned filedescriptors\n");
207 f
->select_fds_hist
.dump(sentry
, statHistIntDumper
);
211 * Check all connections for new connections and input data that is to be
212 * processed. Also check for connections with data queued and whether we can
215 * Called to do the new-style IO, courtesy of of squid (like most of this
216 * new IO code). This routine handles the stuff we've hidden in
217 * comm_setselect and fd_table[] and calls callbacks for IO ready
221 Comm::DoSelect(int msec
)
227 struct epoll_event
*cevents
;
229 PROF_start(comm_check_incoming
);
231 if (msec
> max_poll_time
)
232 msec
= max_poll_time
;
235 num
= epoll_wait(kdpfd
, pevents
, SQUID_MAXFD
, msec
);
236 ++ statCounter
.select_loops
;
241 if (ignoreErrno(errno
))
246 PROF_stop(comm_check_incoming
);
248 return Comm::COMM_ERROR
;
251 PROF_stop(comm_check_incoming
);
254 statCounter
.select_fds_hist
.count(num
);
257 return Comm::TIMEOUT
; /* No error.. */
259 PROF_start(comm_handle_ready_fd
);
261 for (i
= 0, cevents
= pevents
; i
< num
; ++i
, ++cevents
) {
262 fd
= cevents
->data
.fd
;
264 debugs(5, DEBUG_EPOLL
? 0 : 8, HERE
<< "got FD " << fd
<< " events=" <<
265 std::hex
<< cevents
->events
<< " monitoring=" << F
->epoll_state
<<
266 " F->read_handler=" << F
->read_handler
<< " F->write_handler=" << F
->write_handler
);
268 // TODO: add EPOLLPRI??
270 if (cevents
->events
& (EPOLLIN
|EPOLLHUP
|EPOLLERR
) || F
->flags
.read_pending
) {
271 if ((hdl
= F
->read_handler
) != NULL
) {
272 debugs(5, DEBUG_EPOLL
? 0 : 8, HERE
<< "Calling read handler on FD " << fd
);
273 PROF_start(comm_write_handler
);
274 F
->flags
.read_pending
= 0;
275 F
->read_handler
= NULL
;
276 hdl(fd
, F
->read_data
);
277 PROF_stop(comm_write_handler
);
278 ++ statCounter
.select_fds
;
280 debugs(5, DEBUG_EPOLL
? 0 : 8, HERE
<< "no read handler for FD " << fd
);
281 // remove interest since no handler exist for this event.
282 SetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
286 if (cevents
->events
& (EPOLLOUT
|EPOLLHUP
|EPOLLERR
)) {
287 if ((hdl
= F
->write_handler
) != NULL
) {
288 debugs(5, DEBUG_EPOLL
? 0 : 8, HERE
<< "Calling write handler on FD " << fd
);
289 PROF_start(comm_read_handler
);
290 F
->write_handler
= NULL
;
291 hdl(fd
, F
->write_data
);
292 PROF_stop(comm_read_handler
);
293 ++ statCounter
.select_fds
;
295 debugs(5, DEBUG_EPOLL
? 0 : 8, HERE
<< "no write handler for FD " << fd
);
296 // remove interest since no handler exist for this event.
297 SetSelect(fd
, COMM_SELECT_WRITE
, NULL
, NULL
, 0);
302 PROF_stop(comm_handle_ready_fd
);
308 Comm::QuickPollRequired(void)
313 #endif /* USE_EPOLL */