]>
Commit | Line | Data |
---|---|---|
6039b729 | 1 | /* |
77b1029d | 2 | * Copyright (C) 1996-2020 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 | ||
ccfbe8f4 | 35 | #include "base/CodeContext.h" |
d841c88d | 36 | #include "comm/Loops.h" |
1287b9e1 | 37 | #include "fde.h" |
582c2af2 | 38 | #include "globals.h" |
d841c88d | 39 | #include "mgr/Registration.h" |
582c2af2 | 40 | #include "profiler/Profiler.h" |
348697ca | 41 | #include "SquidTime.h" |
e1656dc4 | 42 | #include "StatCounters.h" |
00a7574e | 43 | #include "StatHist.h" |
d841c88d | 44 | #include "Store.h" |
8a02a7f8 | 45 | |
6039b729 | 46 | #define DEBUG_EPOLL 0 |
47 | ||
1a30fdf5 | 48 | #include <cerrno> |
ad32c661 | 49 | #if HAVE_SYS_EPOLL_H |
6039b729 | 50 | #include <sys/epoll.h> |
ad32c661 | 51 | #endif |
6039b729 | 52 | |
612fc474 | 53 | static int kdpfd = -1; |
6039b729 | 54 | static int max_poll_time = 1000; |
55 | ||
56 | static struct epoll_event *pevents; | |
57 | ||
5acc9f37 | 58 | static void commEPollRegisterWithCacheManager(void); |
6039b729 | 59 | |
6039b729 | 60 | /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */ |
61 | /* Public functions */ | |
62 | ||
6039b729 | 63 | /* |
6039b729 | 64 | * This is a needed exported function which will be called to initialise |
65 | * the network loop code. | |
66 | */ | |
67 | void | |
d841c88d | 68 | Comm::SelectLoopInit(void) |
6039b729 | 69 | { |
6039b729 | 70 | pevents = (struct epoll_event *) xmalloc(SQUID_MAXFD * sizeof(struct epoll_event)); |
71 | ||
72 | if (!pevents) { | |
b69e9ffa AJ |
73 | int xerrno = errno; |
74 | fatalf("comm_select_init: xmalloc() failed: %s\n", xstrerr(xerrno)); | |
6039b729 | 75 | } |
76 | ||
77 | kdpfd = epoll_create(SQUID_MAXFD); | |
78 | ||
79 | if (kdpfd < 0) { | |
b69e9ffa AJ |
80 | int xerrno = errno; |
81 | fatalf("comm_select_init: epoll_create(): %s\n", xstrerr(xerrno)); | |
6039b729 | 82 | } |
8cb183ec FC |
83 | |
84 | commEPollRegisterWithCacheManager(); | |
6039b729 | 85 | } |
86 | ||
751406fe | 87 | static const char* epolltype_atoi(int x) |
88 | { | |
26ac0430 | 89 | switch (x) { |
751406fe | 90 | |
91 | case EPOLL_CTL_ADD: | |
92 | return "EPOLL_CTL_ADD"; | |
93 | ||
94 | case EPOLL_CTL_DEL: | |
95 | return "EPOLL_CTL_DEL"; | |
96 | ||
97 | case EPOLL_CTL_MOD: | |
98 | return "EPOLL_CTL_MOD"; | |
99 | ||
100 | default: | |
101 | return "UNKNOWN_EPOLLCTL_OP"; | |
102 | } | |
103 | } | |
104 | ||
d841c88d | 105 | /** |
6039b729 | 106 | * This is a needed exported function which will be called to register |
107 | * and deregister interest in a pending IO state for a given FD. | |
6039b729 | 108 | */ |
109 | void | |
d841c88d | 110 | Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout) |
6039b729 | 111 | { |
112 | fde *F = &fd_table[fd]; | |
751406fe | 113 | int epoll_ctl_type = 0; |
6039b729 | 114 | |
6039b729 | 115 | assert(fd >= 0); |
48e7baac | 116 | debugs(5, 5, HERE << "FD " << fd << ", type=" << type << |
d841c88d AJ |
117 | ", handler=" << handler << ", client_data=" << client_data << |
118 | ", timeout=" << timeout); | |
6039b729 | 119 | |
612fc474 AJ |
120 | struct epoll_event ev; |
121 | memset(&ev, 0, sizeof(ev)); | |
751406fe | 122 | ev.data.fd = fd; |
6039b729 | 123 | |
a1727eb7 | 124 | if (!F->flags.open) { |
125 | epoll_ctl(kdpfd, EPOLL_CTL_DEL, fd, &ev); | |
126 | return; | |
127 | } | |
128 | ||
751406fe | 129 | // If read is an interest |
6039b729 | 130 | |
131 | if (type & COMM_SELECT_READ) { | |
0a080ce3 | 132 | if (handler) { |
26ac0430 AJ |
133 | // Hack to keep the events flowing if there is data immediately ready |
134 | if (F->flags.read_pending) | |
135 | ev.events |= EPOLLOUT; | |
751406fe | 136 | ev.events |= EPOLLIN; |
26ac0430 | 137 | } |
6039b729 | 138 | |
139 | F->read_handler = handler; | |
140 | ||
141 | F->read_data = client_data; | |
751406fe | 142 | |
143 | // Otherwise, use previously stored value | |
144 | } else if (F->epoll_state & EPOLLIN) { | |
145 | ev.events |= EPOLLIN; | |
6039b729 | 146 | } |
147 | ||
751406fe | 148 | // If write is an interest |
6039b729 | 149 | if (type & COMM_SELECT_WRITE) { |
751406fe | 150 | if (handler) |
151 | ev.events |= EPOLLOUT; | |
6039b729 | 152 | |
153 | F->write_handler = handler; | |
154 | ||
155 | F->write_data = client_data; | |
6039b729 | 156 | |
751406fe | 157 | // Otherwise, use previously stored value |
158 | } else if (F->epoll_state & EPOLLOUT) { | |
159 | ev.events |= EPOLLOUT; | |
160 | } | |
6039b729 | 161 | |
751406fe | 162 | if (ev.events) |
163 | ev.events |= EPOLLHUP | EPOLLERR; | |
6039b729 | 164 | |
751406fe | 165 | if (ev.events != F->epoll_state) { |
166 | if (F->epoll_state) // already monitoring something. | |
167 | epoll_ctl_type = ev.events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL; | |
168 | else | |
169 | epoll_ctl_type = EPOLL_CTL_ADD; | |
6039b729 | 170 | |
751406fe | 171 | F->epoll_state = ev.events; |
6039b729 | 172 | |
751406fe | 173 | if (epoll_ctl(kdpfd, epoll_ctl_type, fd, &ev) < 0) { |
b69e9ffa AJ |
174 | int xerrno = errno; |
175 | debugs(5, DEBUG_EPOLL ? 0 : 8, "epoll_ctl(," << epolltype_atoi(epoll_ctl_type) << | |
176 | ",,): failed on FD " << fd << ": " << xstrerr(xerrno)); | |
6039b729 | 177 | } |
178 | } | |
179 | ||
180 | if (timeout) | |
181 | F->timeout = squid_curtime + timeout; | |
ccfbe8f4 AR |
182 | |
183 | if (timeout || handler) // all non-cleanup requests | |
184 | F->codeContext = CodeContext::Current(); // TODO: Avoid clearing if set? | |
185 | else if (!ev.events) // full cleanup: no more FD-associated work expected | |
186 | F->codeContext = nullptr; | |
187 | // else: direction-specific/timeout cleanup requests preserve F->codeContext | |
6039b729 | 188 | } |
189 | ||
610ee341 | 190 | static void commIncomingStats(StoreEntry * sentry); |
191 | ||
5acc9f37 | 192 | static void |
dbeed9ea | 193 | commEPollRegisterWithCacheManager(void) |
610ee341 | 194 | { |
8822ebee | 195 | Mgr::RegisterAction("comm_epoll_incoming", |
d9fc6862 A |
196 | "comm_incoming() stats", |
197 | commIncomingStats, 0, 1); | |
610ee341 | 198 | } |
199 | ||
200 | static void | |
201 | commIncomingStats(StoreEntry * sentry) | |
202 | { | |
203 | StatCounters *f = &statCounter; | |
0a515876 | 204 | storeAppendPrintf(sentry, "Total number of epoll(2) loops: %ld\n", statCounter.select_loops); |
610ee341 | 205 | storeAppendPrintf(sentry, "Histogram of returned filedescriptors\n"); |
96886986 | 206 | f->select_fds_hist.dump(sentry, statHistIntDumper); |
610ee341 | 207 | } |
208 | ||
d841c88d | 209 | /** |
6039b729 | 210 | * Check all connections for new connections and input data that is to be |
211 | * processed. Also check for connections with data queued and whether we can | |
212 | * write it out. | |
6039b729 | 213 | * |
214 | * Called to do the new-style IO, courtesy of of squid (like most of this | |
215 | * new IO code). This routine handles the stuff we've hidden in | |
216 | * comm_setselect and fd_table[] and calls callbacks for IO ready | |
217 | * events. | |
218 | */ | |
c8407295 | 219 | Comm::Flag |
d841c88d | 220 | Comm::DoSelect(int msec) |
6039b729 | 221 | { |
222 | int num, i,fd; | |
223 | fde *F; | |
224 | PF *hdl; | |
225 | ||
226 | struct epoll_event *cevents; | |
6039b729 | 227 | |
a1727eb7 | 228 | PROF_start(comm_check_incoming); |
229 | ||
6039b729 | 230 | if (msec > max_poll_time) |
231 | msec = max_poll_time; | |
232 | ||
233 | for (;;) { | |
234 | num = epoll_wait(kdpfd, pevents, SQUID_MAXFD, msec); | |
098346fd | 235 | ++ statCounter.select_loops; |
6039b729 | 236 | |
237 | if (num >= 0) | |
238 | break; | |
239 | ||
240 | if (ignoreErrno(errno)) | |
241 | break; | |
242 | ||
243 | getCurrentTime(); | |
244 | ||
a1727eb7 | 245 | PROF_stop(comm_check_incoming); |
246 | ||
4ee57cbe | 247 | return Comm::COMM_ERROR; |
6039b729 | 248 | } |
249 | ||
a1727eb7 | 250 | PROF_stop(comm_check_incoming); |
6039b729 | 251 | getCurrentTime(); |
252 | ||
f30f7998 | 253 | statCounter.select_fds_hist.count(num); |
751406fe | 254 | |
6039b729 | 255 | if (num == 0) |
f53969cc | 256 | return Comm::TIMEOUT; /* No error.. */ |
751406fe | 257 | |
258 | PROF_start(comm_handle_ready_fd); | |
6039b729 | 259 | |
cbebe602 | 260 | for (i = 0, cevents = pevents; i < num; ++i, ++cevents) { |
6039b729 | 261 | fd = cevents->data.fd; |
262 | F = &fd_table[fd]; | |
ccfbe8f4 | 263 | CodeContext::Reset(F->codeContext); |
d841c88d | 264 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "got FD " << fd << " events=" << |
26ac0430 AJ |
265 | std::hex << cevents->events << " monitoring=" << F->epoll_state << |
266 | " F->read_handler=" << F->read_handler << " F->write_handler=" << F->write_handler); | |
751406fe | 267 | |
268 | // TODO: add EPOLLPRI?? | |
6039b729 | 269 | |
0a080ce3 | 270 | if (cevents->events & (EPOLLIN|EPOLLHUP|EPOLLERR) || F->flags.read_pending) { |
a1727eb7 | 271 | if ((hdl = F->read_handler) != NULL) { |
d841c88d | 272 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling read handler on FD " << fd); |
751406fe | 273 | PROF_start(comm_write_handler); |
6039b729 | 274 | F->read_handler = NULL; |
275 | hdl(fd, F->read_data); | |
751406fe | 276 | PROF_stop(comm_write_handler); |
098346fd | 277 | ++ statCounter.select_fds; |
751406fe | 278 | } else { |
d841c88d | 279 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no read handler for FD " << fd); |
751406fe | 280 | // remove interest since no handler exist for this event. |
d841c88d | 281 | SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); |
6039b729 | 282 | } |
283 | } | |
284 | ||
751406fe | 285 | if (cevents->events & (EPOLLOUT|EPOLLHUP|EPOLLERR)) { |
a1727eb7 | 286 | if ((hdl = F->write_handler) != NULL) { |
d841c88d | 287 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling write handler on FD " << fd); |
751406fe | 288 | PROF_start(comm_read_handler); |
6039b729 | 289 | F->write_handler = NULL; |
290 | hdl(fd, F->write_data); | |
751406fe | 291 | PROF_stop(comm_read_handler); |
098346fd | 292 | ++ statCounter.select_fds; |
751406fe | 293 | } else { |
d841c88d | 294 | debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no write handler for FD " << fd); |
751406fe | 295 | // remove interest since no handler exist for this event. |
d841c88d | 296 | SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0); |
6039b729 | 297 | } |
298 | } | |
299 | } | |
300 | ||
ccfbe8f4 AR |
301 | CodeContext::Reset(); |
302 | ||
751406fe | 303 | PROF_stop(comm_handle_ready_fd); |
304 | ||
c8407295 | 305 | return Comm::OK; |
6039b729 | 306 | } |
307 | ||
308 | void | |
d841c88d | 309 | Comm::QuickPollRequired(void) |
6039b729 | 310 | { |
768d3d2f | 311 | max_poll_time = 10; |
6039b729 | 312 | } |
313 | ||
8a02a7f8 | 314 | #endif /* USE_EPOLL */ |
f53969cc | 315 |