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