]>
Commit | Line | Data |
---|---|---|
6039b729 | 1 | /* |
262a0e14 | 2 | * $Id$ |
6039b729 | 3 | * |
b510f3a1 | 4 | * DEBUG: section 05 Socket Functions |
6039b729 | 5 | * |
6 | * SQUID Web Proxy Cache http://www.squid-cache.org/ | |
7 | * ---------------------------------------------------------- | |
8 | * | |
9 | * Squid is the result of efforts by numerous individuals from | |
10 | * the Internet community; see the CONTRIBUTORS file for full | |
11 | * details. Many organizations have provided support for Squid's | |
12 | * development; see the SPONSORS file for full details. Squid is | |
13 | * Copyrighted (C) 2001 by the Regents of the University of | |
14 | * California; see the COPYRIGHT file for full details. Squid | |
15 | * incorporates software developed and/or copyrighted by other | |
16 | * sources; see the CREDITS file for full details. | |
17 | * | |
18 | * This program is free software; you can redistribute it and/or modify | |
19 | * it under the terms of the GNU General Public License as published by | |
20 | * the Free Software Foundation; either version 2 of the License, or | |
21 | * (at your option) any later version. | |
22 | * | |
23 | * This program is distributed in the hope that it will be useful, | |
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
26 | * GNU General Public License for more details. | |
27 | * | |
28 | * You should have received a copy of the GNU General Public License | |
29 | * along with this program; if not, write to the Free Software | |
30 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
31 | * | |
32 | */ | |
33 | ||
34 | /* | |
35 | * The idea for this came from these two websites: | |
36 | * http://www.xmailserver.org/linux-patches/nio-improve.html | |
37 | * http://www.kegel.com/c10k.html | |
38 | * | |
39 | * This is to support the epoll sysctl being added to the linux 2.5 | |
40 | * kernel tree. The new sys_epoll is an event based poller without | |
41 | * most of the fuss of rtsignals. | |
42 | * | |
43 | * -- David Nicklay <dnicklay@web.turner.com> | |
44 | */ | |
45 | ||
46 | /* | |
47 | * XXX Currently not implemented / supported by this module XXX | |
48 | * | |
49 | * - delay pools | |
50 | * - deferred reads | |
51 | * | |
52 | */ | |
53 | ||
d841c88d AJ |
54 | #include "config.h" |
55 | ||
56 | #if USE_EPOLL | |
57 | ||
6039b729 | 58 | #include "squid.h" |
d841c88d | 59 | #include "comm/Loops.h" |
1287b9e1 | 60 | #include "fde.h" |
d841c88d | 61 | #include "mgr/Registration.h" |
348697ca | 62 | #include "SquidTime.h" |
00a7574e | 63 | #include "StatHist.h" |
d841c88d | 64 | #include "Store.h" |
8a02a7f8 | 65 | |
6039b729 | 66 | #define DEBUG_EPOLL 0 |
67 | ||
ad32c661 | 68 | #if HAVE_SYS_EPOLL_H |
6039b729 | 69 | #include <sys/epoll.h> |
ad32c661 | 70 | #endif |
6039b729 | 71 | |
72 | static int kdpfd; | |
73 | static int max_poll_time = 1000; | |
74 | ||
75 | static struct epoll_event *pevents; | |
76 | ||
5acc9f37 | 77 | static void commEPollRegisterWithCacheManager(void); |
6039b729 | 78 | |
79 | ||
80 | /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */ | |
81 | /* Public functions */ | |
82 | ||
83 | ||
84 | /* | |
6039b729 | 85 | * This is a needed exported function which will be called to initialise |
86 | * the network loop code. | |
87 | */ | |
88 | void | |
d841c88d | 89 | Comm::SelectLoopInit(void) |
6039b729 | 90 | { |
6039b729 | 91 | pevents = (struct epoll_event *) xmalloc(SQUID_MAXFD * sizeof(struct epoll_event)); |
92 | ||
93 | if (!pevents) { | |
94 | fatalf("comm_select_init: xmalloc() failed: %s\n",xstrerror()); | |
95 | } | |
96 | ||
97 | kdpfd = epoll_create(SQUID_MAXFD); | |
98 | ||
99 | if (kdpfd < 0) { | |
100 | fatalf("comm_select_init: epoll_create(): %s\n",xstrerror()); | |
101 | } | |
8cb183ec FC |
102 | |
103 | commEPollRegisterWithCacheManager(); | |
6039b729 | 104 | } |
105 | ||
751406fe | 106 | static const char* epolltype_atoi(int x) |
107 | { | |
26ac0430 | 108 | switch (x) { |
751406fe | 109 | |
110 | case EPOLL_CTL_ADD: | |
111 | return "EPOLL_CTL_ADD"; | |
112 | ||
113 | case EPOLL_CTL_DEL: | |
114 | return "EPOLL_CTL_DEL"; | |
115 | ||
116 | case EPOLL_CTL_MOD: | |
117 | return "EPOLL_CTL_MOD"; | |
118 | ||
119 | default: | |
120 | return "UNKNOWN_EPOLLCTL_OP"; | |
121 | } | |
122 | } | |
123 | ||
d841c88d | 124 | /** |
6039b729 | 125 | * This is a needed exported function which will be called to register |
126 | * and deregister interest in a pending IO state for a given FD. | |
6039b729 | 127 | */ |
128 | void | |
d841c88d | 129 | Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout) |
6039b729 | 130 | { |
131 | fde *F = &fd_table[fd]; | |
751406fe | 132 | int epoll_ctl_type = 0; |
6039b729 | 133 | |
134 | struct epoll_event ev; | |
135 | assert(fd >= 0); | |
48e7baac | 136 | debugs(5, 5, HERE << "FD " << fd << ", type=" << type << |
d841c88d AJ |
137 | ", handler=" << handler << ", client_data=" << client_data << |
138 | ", timeout=" << timeout); | |
6039b729 | 139 | |
b4bab919 | 140 | if (RUNNING_ON_VALGRIND) { |
26ac0430 AJ |
141 | /* Keep valgrind happy.. complains about uninitialized bytes otherwise */ |
142 | memset(&ev, 0, sizeof(ev)); | |
b4bab919 | 143 | } |
751406fe | 144 | ev.events = 0; |
145 | ev.data.fd = fd; | |
6039b729 | 146 | |
a1727eb7 | 147 | if (!F->flags.open) { |
148 | epoll_ctl(kdpfd, EPOLL_CTL_DEL, fd, &ev); | |
149 | return; | |
150 | } | |
151 | ||
751406fe | 152 | // If read is an interest |
6039b729 | 153 | |
154 | if (type & COMM_SELECT_READ) { | |
0a080ce3 | 155 | if (handler) { |
26ac0430 AJ |
156 | // Hack to keep the events flowing if there is data immediately ready |
157 | if (F->flags.read_pending) | |
158 | ev.events |= EPOLLOUT; | |
751406fe | 159 | ev.events |= EPOLLIN; |
26ac0430 | 160 | } |
6039b729 | 161 | |
162 | F->read_handler = handler; | |
163 | ||
164 | F->read_data = client_data; | |
751406fe | 165 | |
166 | // Otherwise, use previously stored value | |
167 | } else if (F->epoll_state & EPOLLIN) { | |
168 | ev.events |= EPOLLIN; | |
6039b729 | 169 | } |
170 | ||
751406fe | 171 | // If write is an interest |
6039b729 | 172 | if (type & COMM_SELECT_WRITE) { |
751406fe | 173 | if (handler) |
174 | ev.events |= EPOLLOUT; | |
6039b729 | 175 | |
176 | F->write_handler = handler; | |
177 | ||
178 | F->write_data = client_data; | |
6039b729 | 179 | |
751406fe | 180 | // Otherwise, use previously stored value |
181 | } else if (F->epoll_state & EPOLLOUT) { | |
182 | ev.events |= EPOLLOUT; | |
183 | } | |
6039b729 | 184 | |
751406fe | 185 | if (ev.events) |
186 | ev.events |= EPOLLHUP | EPOLLERR; | |
6039b729 | 187 | |
751406fe | 188 | if (ev.events != F->epoll_state) { |
189 | if (F->epoll_state) // already monitoring something. | |
190 | epoll_ctl_type = ev.events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; | |
191 | else | |
192 | epoll_ctl_type = EPOLL_CTL_ADD; | |
6039b729 | 193 | |
751406fe | 194 | F->epoll_state = ev.events; |
6039b729 | 195 | |
751406fe | 196 | if (epoll_ctl(kdpfd, epoll_ctl_type, fd, &ev) < 0) { |
d841c88d | 197 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "epoll_ctl(," << epolltype_atoi(epoll_ctl_type) << |
bf8fe701 | 198 | ",,): failed on FD " << fd << ": " << xstrerror()); |
6039b729 | 199 | } |
200 | } | |
201 | ||
202 | if (timeout) | |
203 | F->timeout = squid_curtime + timeout; | |
204 | } | |
205 | ||
3a5a4930 | 206 | void |
d841c88d | 207 | Comm::ResetSelect(int fd) |
3a5a4930 | 208 | { |
209 | fde *F = &fd_table[fd]; | |
210 | F->epoll_state = 0; | |
d841c88d | 211 | SetSelect(fd, 0, NULL, NULL, 0); |
3a5a4930 | 212 | } |
213 | ||
610ee341 | 214 | |
215 | static void commIncomingStats(StoreEntry * sentry); | |
216 | ||
5acc9f37 | 217 | static void |
dbeed9ea | 218 | commEPollRegisterWithCacheManager(void) |
610ee341 | 219 | { |
8822ebee | 220 | Mgr::RegisterAction("comm_epoll_incoming", |
d9fc6862 A |
221 | "comm_incoming() stats", |
222 | commIncomingStats, 0, 1); | |
610ee341 | 223 | } |
224 | ||
225 | static void | |
226 | commIncomingStats(StoreEntry * sentry) | |
227 | { | |
228 | StatCounters *f = &statCounter; | |
0a515876 | 229 | storeAppendPrintf(sentry, "Total number of epoll(2) loops: %ld\n", statCounter.select_loops); |
610ee341 | 230 | storeAppendPrintf(sentry, "Histogram of returned filedescriptors\n"); |
231 | statHistDump(&f->select_fds_hist, sentry, statHistIntDumper); | |
232 | } | |
233 | ||
d841c88d | 234 | /** |
6039b729 | 235 | * Check all connections for new connections and input data that is to be |
236 | * processed. Also check for connections with data queued and whether we can | |
237 | * write it out. | |
6039b729 | 238 | * |
239 | * Called to do the new-style IO, courtesy of of squid (like most of this | |
240 | * new IO code). This routine handles the stuff we've hidden in | |
241 | * comm_setselect and fd_table[] and calls callbacks for IO ready | |
242 | * events. | |
243 | */ | |
6039b729 | 244 | comm_err_t |
d841c88d | 245 | Comm::DoSelect(int msec) |
6039b729 | 246 | { |
247 | int num, i,fd; | |
248 | fde *F; | |
249 | PF *hdl; | |
250 | ||
251 | struct epoll_event *cevents; | |
6039b729 | 252 | |
a1727eb7 | 253 | PROF_start(comm_check_incoming); |
254 | ||
6039b729 | 255 | if (msec > max_poll_time) |
256 | msec = max_poll_time; | |
257 | ||
258 | for (;;) { | |
259 | num = epoll_wait(kdpfd, pevents, SQUID_MAXFD, msec); | |
0a515876 | 260 | ++statCounter.select_loops; |
6039b729 | 261 | |
262 | if (num >= 0) | |
263 | break; | |
264 | ||
265 | if (ignoreErrno(errno)) | |
266 | break; | |
267 | ||
268 | getCurrentTime(); | |
269 | ||
a1727eb7 | 270 | PROF_stop(comm_check_incoming); |
271 | ||
6039b729 | 272 | return COMM_ERROR; |
273 | } | |
274 | ||
a1727eb7 | 275 | PROF_stop(comm_check_incoming); |
6039b729 | 276 | getCurrentTime(); |
277 | ||
f30f7998 | 278 | statCounter.select_fds_hist.count(num); |
751406fe | 279 | |
6039b729 | 280 | if (num == 0) |
751406fe | 281 | return COMM_TIMEOUT; /* No error.. */ |
282 | ||
283 | PROF_start(comm_handle_ready_fd); | |
6039b729 | 284 | |
285 | for (i = 0, cevents = pevents; i < num; i++, cevents++) { | |
286 | fd = cevents->data.fd; | |
287 | F = &fd_table[fd]; | |
d841c88d | 288 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "got FD " << fd << " events=" << |
26ac0430 AJ |
289 | std::hex << cevents->events << " monitoring=" << F->epoll_state << |
290 | " F->read_handler=" << F->read_handler << " F->write_handler=" << F->write_handler); | |
751406fe | 291 | |
292 | // TODO: add EPOLLPRI?? | |
6039b729 | 293 | |
0a080ce3 | 294 | if (cevents->events & (EPOLLIN|EPOLLHUP|EPOLLERR) || F->flags.read_pending) { |
a1727eb7 | 295 | if ((hdl = F->read_handler) != NULL) { |
d841c88d | 296 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling read handler on FD " << fd); |
751406fe | 297 | PROF_start(comm_write_handler); |
0a080ce3 | 298 | F->flags.read_pending = 0; |
6039b729 | 299 | F->read_handler = NULL; |
300 | hdl(fd, F->read_data); | |
751406fe | 301 | PROF_stop(comm_write_handler); |
302 | statCounter.select_fds++; | |
303 | } else { | |
d841c88d | 304 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no read handler for FD " << fd); |
751406fe | 305 | // remove interest since no handler exist for this event. |
d841c88d | 306 | SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); |
6039b729 | 307 | } |
308 | } | |
309 | ||
751406fe | 310 | if (cevents->events & (EPOLLOUT|EPOLLHUP|EPOLLERR)) { |
a1727eb7 | 311 | if ((hdl = F->write_handler) != NULL) { |
d841c88d | 312 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling write handler on FD " << fd); |
751406fe | 313 | PROF_start(comm_read_handler); |
6039b729 | 314 | F->write_handler = NULL; |
315 | hdl(fd, F->write_data); | |
751406fe | 316 | PROF_stop(comm_read_handler); |
317 | statCounter.select_fds++; | |
318 | } else { | |
d841c88d | 319 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no write handler for FD " << fd); |
751406fe | 320 | // remove interest since no handler exist for this event. |
d841c88d | 321 | SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0); |
6039b729 | 322 | } |
323 | } | |
324 | } | |
325 | ||
751406fe | 326 | PROF_stop(comm_handle_ready_fd); |
327 | ||
6039b729 | 328 | return COMM_OK; |
329 | } | |
330 | ||
331 | void | |
d841c88d | 332 | Comm::QuickPollRequired(void) |
6039b729 | 333 | { |
768d3d2f | 334 | max_poll_time = 10; |
6039b729 | 335 | } |
336 | ||
8a02a7f8 | 337 | #endif /* USE_EPOLL */ |