]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ModEpoll.cc
2 * Copyright (C) 1996-2015 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>
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
));
72 fatalf("comm_select_init: xmalloc() failed: %s\n",xstrerror());
75 kdpfd
= epoll_create(SQUID_MAXFD
);
78 fatalf("comm_select_init: epoll_create(): %s\n",xstrerror());
81 commEPollRegisterWithCacheManager();
84 static const char* epolltype_atoi(int x
)
89 return "EPOLL_CTL_ADD";
92 return "EPOLL_CTL_DEL";
95 return "EPOLL_CTL_MOD";
98 return "UNKNOWN_EPOLLCTL_OP";
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.
107 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
109 fde
*F
= &fd_table
[fd
];
110 int epoll_ctl_type
= 0;
112 struct epoll_event ev
;
114 debugs(5, 5, HERE
<< "FD " << fd
<< ", type=" << type
<<
115 ", handler=" << handler
<< ", client_data=" << client_data
<<
116 ", timeout=" << timeout
);
118 if (RUNNING_ON_VALGRIND
) {
119 /* Keep valgrind happy.. complains about uninitialized bytes otherwise */
120 memset(&ev
, 0, sizeof(ev
));
125 if (!F
->flags
.open
) {
126 epoll_ctl(kdpfd
, EPOLL_CTL_DEL
, fd
, &ev
);
130 // If read is an interest
132 if (type
& COMM_SELECT_READ
) {
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
;
140 F
->read_handler
= handler
;
142 F
->read_data
= client_data
;
144 // Otherwise, use previously stored value
145 } else if (F
->epoll_state
& EPOLLIN
) {
146 ev
.events
|= EPOLLIN
;
149 // If write is an interest
150 if (type
& COMM_SELECT_WRITE
) {
152 ev
.events
|= EPOLLOUT
;
154 F
->write_handler
= handler
;
156 F
->write_data
= client_data
;
158 // Otherwise, use previously stored value
159 } else if (F
->epoll_state
& EPOLLOUT
) {
160 ev
.events
|= EPOLLOUT
;
164 ev
.events
|= EPOLLHUP
| EPOLLERR
;
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
;
170 epoll_ctl_type
= EPOLL_CTL_ADD
;
172 F
->epoll_state
= ev
.events
;
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());
181 F
->timeout
= squid_curtime
+ timeout
;
185 Comm::ResetSelect(int fd
)
187 fde
*F
= &fd_table
[fd
];
189 SetSelect(fd
, 0, NULL
, NULL
, 0);
192 static void commIncomingStats(StoreEntry
* sentry
);
195 commEPollRegisterWithCacheManager(void)
197 Mgr::RegisterAction("comm_epoll_incoming",
198 "comm_incoming() stats",
199 commIncomingStats
, 0, 1);
203 commIncomingStats(StoreEntry
* sentry
)
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
);
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
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
222 Comm::DoSelect(int msec
)
228 struct epoll_event
*cevents
;
230 PROF_start(comm_check_incoming
);
232 if (msec
> max_poll_time
)
233 msec
= max_poll_time
;
236 num
= epoll_wait(kdpfd
, pevents
, SQUID_MAXFD
, msec
);
237 ++ statCounter
.select_loops
;
242 if (ignoreErrno(errno
))
247 PROF_stop(comm_check_incoming
);
249 return Comm::COMM_ERROR
;
252 PROF_stop(comm_check_incoming
);
255 statCounter
.select_fds_hist
.count(num
);
258 return Comm::TIMEOUT
; /* No error.. */
260 PROF_start(comm_handle_ready_fd
);
262 for (i
= 0, cevents
= pevents
; i
< num
; ++i
, ++cevents
) {
263 fd
= cevents
->data
.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
);
269 // TODO: add EPOLLPRI??
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
;
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);
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
;
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);
303 PROF_stop(comm_handle_ready_fd
);
309 Comm::QuickPollRequired(void)
314 #endif /* USE_EPOLL */