]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ModPoll.cc
Merged from trunk
[thirdparty/squid.git] / src / comm / ModPoll.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 05 Socket Functions
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 #include "squid.h"
34
35 #if USE_POLL
36
37 #include "squid-old.h"
38 #include "anyp/PortCfg.h"
39 #include "comm/Connection.h"
40 #include "comm/Loops.h"
41 #include "fde.h"
42 #include "ICP.h"
43 #include "mgr/Registration.h"
44 #include "SquidTime.h"
45 #include "StatCounters.h"
46 #include "Store.h"
47
48 #if HAVE_POLL_H
49 #include <poll.h>
50 #endif
51
52 /* Needed for poll() on Linux at least */
53 #if USE_POLL
54 #ifndef POLLRDNORM
55 #define POLLRDNORM POLLIN
56 #endif
57 #ifndef POLLWRNORM
58 #define POLLWRNORM POLLOUT
59 #endif
60 #endif
61
62 static int MAX_POLL_TIME = 1000; /* see also Comm::QuickPollRequired() */
63
64 #ifndef howmany
65 #define howmany(x, y) (((x)+((y)-1))/(y))
66 #endif
67 #ifndef NBBY
68 #define NBBY 8
69 #endif
70 #define FD_MASK_BYTES sizeof(fd_mask)
71 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
72
73 /* STATIC */
74 static int fdIsTcpListen(int fd);
75 static int fdIsUdpListen(int fd);
76 static int fdIsDns(int fd);
77 static OBJH commIncomingStats;
78 static int comm_check_incoming_poll_handlers(int nfds, int *fds);
79 static void comm_poll_dns_incoming(void);
80
81 /*
82 * Automatic tuning for incoming requests:
83 *
84 * INCOMING sockets are the ICP and HTTP ports. We need to check these
85 * fairly regularly, but how often? When the load increases, we
86 * want to check the incoming sockets more often. If we have a lot
87 * of incoming ICP, then we need to check these sockets more than
88 * if we just have HTTP.
89 *
90 * The variables 'incoming_icp_interval' and 'incoming_http_interval'
91 * determine how many normal I/O events to process before checking
92 * incoming sockets again. Note we store the incoming_interval
93 * multipled by a factor of (2^INCOMING_FACTOR) to have some
94 * pseudo-floating point precision.
95 *
96 * The variable 'udp_io_events' and 'tcp_io_events' counts how many normal
97 * I/O events have been processed since the last check on the incoming
98 * sockets. When io_events > incoming_interval, its time to check incoming
99 * sockets.
100 *
101 * Every time we check incoming sockets, we count how many new messages
102 * or connections were processed. This is used to adjust the
103 * incoming_interval for the next iteration. The new incoming_interval
104 * is calculated as the current incoming_interval plus what we would
105 * like to see as an average number of events minus the number of
106 * events just processed.
107 *
108 * incoming_interval = incoming_interval + target_average - number_of_events_processed
109 *
110 * There are separate incoming_interval counters for TCP-based, UDP-based, and DNS events
111 *
112 * You can see the current values of the incoming_interval's, as well as
113 * a histogram of 'incoming_events' by asking the cache manager
114 * for 'comm_incoming', e.g.:
115 *
116 * % ./client mgr:comm_poll_incoming
117 *
118 * Caveats:
119 *
120 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
121 * incoming_interval for both types of sockets. At the
122 * largest value the cache will effectively be idling.
123 *
124 * - The higher the INCOMING_FACTOR, the slower the algorithm will
125 * respond to load spikes/increases/decreases in demand. A value
126 * between 3 and 8 is recommended.
127 */
128
129 #define MAX_INCOMING_INTEGER 256
130 #define INCOMING_FACTOR 5
131 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
132 static int udp_io_events = 0; ///< I/O events passed since last UDP receiver socket poll
133 static int dns_io_events = 0; ///< I/O events passed since last DNS socket poll
134 static int tcp_io_events = 0; ///< I/O events passed since last TCP listening socket poll
135 static int incoming_udp_interval = 16 << INCOMING_FACTOR;
136 static int incoming_dns_interval = 16 << INCOMING_FACTOR;
137 static int incoming_tcp_interval = 16 << INCOMING_FACTOR;
138 #define commCheckUdpIncoming (++udp_io_events > (incoming_udp_interval>> INCOMING_FACTOR))
139 #define commCheckDnsIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
140 #define commCheckTcpIncoming (++tcp_io_events > (incoming_tcp_interval>> INCOMING_FACTOR))
141
142
143 void
144 Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout)
145 {
146 fde *F = &fd_table[fd];
147 assert(fd >= 0);
148 assert(F->flags.open);
149 debugs(5, 5, HERE << "FD " << fd << ", type=" << type <<
150 ", handler=" << handler << ", client_data=" << client_data <<
151 ", timeout=" << timeout);
152
153 if (type & COMM_SELECT_READ) {
154 F->read_handler = handler;
155 F->read_data = client_data;
156 }
157
158 if (type & COMM_SELECT_WRITE) {
159 F->write_handler = handler;
160 F->write_data = client_data;
161 }
162
163 if (timeout)
164 F->timeout = squid_curtime + timeout;
165 }
166
167 void
168 Comm::ResetSelect(int fd)
169 {
170 }
171
172 static int
173 fdIsUdpListen(int fd)
174 {
175 if (icpIncomingConn != NULL && icpIncomingConn->fd == fd)
176 return 1;
177
178 if (icpOutgoingConn != NULL && icpOutgoingConn->fd == fd)
179 return 1;
180
181 return 0;
182 }
183
184 static int
185 fdIsDns(int fd)
186 {
187 if (fd == DnsSocketA)
188 return 1;
189
190 if (fd == DnsSocketB)
191 return 1;
192
193 return 0;
194 }
195
196 static int
197 fdIsTcpListen(int fd)
198 {
199 for (const AnyP::PortCfg *s = Config.Sockaddr.http; s; s = s->next) {
200 if (s->listenConn != NULL && s->listenConn->fd == fd)
201 return 1;
202 }
203
204 return 0;
205 }
206
207 static int
208 comm_check_incoming_poll_handlers(int nfds, int *fds)
209 {
210 int i;
211 int fd;
212 PF *hdl = NULL;
213 int npfds;
214
215 struct pollfd pfds[3 + MAXTCPLISTENPORTS];
216 PROF_start(comm_check_incoming);
217 incoming_sockets_accepted = 0;
218
219 for (i = npfds = 0; i < nfds; ++i) {
220 int events;
221 fd = fds[i];
222 events = 0;
223
224 if (fd_table[fd].read_handler)
225 events |= POLLRDNORM;
226
227 if (fd_table[fd].write_handler)
228 events |= POLLWRNORM;
229
230 if (events) {
231 pfds[npfds].fd = fd;
232 pfds[npfds].events = events;
233 pfds[npfds].revents = 0;
234 ++npfds;
235 }
236 }
237
238 if (!nfds) {
239 PROF_stop(comm_check_incoming);
240 return -1;
241 }
242
243 getCurrentTime();
244 ++ statCounter.syscalls.selects;
245
246 if (poll(pfds, npfds, 0) < 1) {
247 PROF_stop(comm_check_incoming);
248 return incoming_sockets_accepted;
249 }
250
251 for (i = 0; i < npfds; ++i) {
252 int revents;
253
254 if (((revents = pfds[i].revents) == 0) || ((fd = pfds[i].fd) == -1))
255 continue;
256
257 if (revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR)) {
258 if ((hdl = fd_table[fd].read_handler)) {
259 fd_table[fd].read_handler = NULL;
260 hdl(fd, fd_table[fd].read_data);
261 } else if (pfds[i].events & POLLRDNORM)
262 debugs(5, 1, "comm_poll_incoming: FD " << fd << " NULL read handler");
263 }
264
265 if (revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR)) {
266 if ((hdl = fd_table[fd].write_handler)) {
267 fd_table[fd].write_handler = NULL;
268 hdl(fd, fd_table[fd].write_data);
269 } else if (pfds[i].events & POLLWRNORM)
270 debugs(5, 1, "comm_poll_incoming: FD " << fd << " NULL write_handler");
271 }
272 }
273
274 PROF_stop(comm_check_incoming);
275 return incoming_sockets_accepted;
276 }
277
278 static void
279 comm_poll_udp_incoming(void)
280 {
281 int nfds = 0;
282 int fds[2];
283 int nevents;
284 udp_io_events = 0;
285
286 if (Comm::IsConnOpen(icpIncomingConn)) {
287 fds[nfds] = icpIncomingConn->fd;
288 ++nfds;
289 }
290
291 if (icpIncomingConn != icpOutgoingConn && Comm::IsConnOpen(icpOutgoingConn)) {
292 fds[nfds] = icpOutgoingConn->fd;
293 ++nfds;
294 }
295
296 if (nfds == 0)
297 return;
298
299 nevents = comm_check_incoming_poll_handlers(nfds, fds);
300
301 incoming_udp_interval += Config.comm_incoming.udp.average - nevents;
302
303 if (incoming_udp_interval < Config.comm_incoming.udp.min_poll)
304 incoming_udp_interval = Config.comm_incoming.udp.min_poll;
305
306 if (incoming_udp_interval > MAX_INCOMING_INTERVAL)
307 incoming_udp_interval = MAX_INCOMING_INTERVAL;
308
309 if (nevents > INCOMING_UDP_MAX)
310 nevents = INCOMING_UDP_MAX;
311
312 statCounter.comm_udp_incoming.count(nevents);
313 }
314
315 static void
316 comm_poll_tcp_incoming(void)
317 {
318 int nfds = 0;
319 int fds[MAXTCPLISTENPORTS];
320 int j;
321 int nevents;
322 tcp_io_events = 0;
323
324 // XXX: only poll sockets that won't be deferred. But how do we identify them?
325
326 for (j = 0; j < NHttpSockets; ++j) {
327 if (HttpSockets[j] < 0)
328 continue;
329
330 fds[nfds] = HttpSockets[j];
331 ++nfds;
332 }
333
334 nevents = comm_check_incoming_poll_handlers(nfds, fds);
335 incoming_tcp_interval = incoming_tcp_interval
336 + Config.comm_incoming.tcp.average - nevents;
337
338 if (incoming_tcp_interval < Config.comm_incoming.tcp.min_poll)
339 incoming_tcp_interval = Config.comm_incoming.tcp.min_poll;
340
341 if (incoming_tcp_interval > MAX_INCOMING_INTERVAL)
342 incoming_tcp_interval = MAX_INCOMING_INTERVAL;
343
344 if (nevents > INCOMING_TCP_MAX)
345 nevents = INCOMING_TCP_MAX;
346
347 statCounter.comm_tcp_incoming.count(nevents);
348 }
349
350 /* poll all sockets; call handlers for those that are ready. */
351 comm_err_t
352 Comm::DoSelect(int msec)
353 {
354 struct pollfd pfds[SQUID_MAXFD];
355
356 PF *hdl = NULL;
357 int fd;
358 int maxfd;
359 unsigned long nfds;
360 unsigned long npending;
361 int num;
362 int calldns = 0, calludp = 0, calltcp = 0;
363 double timeout = current_dtime + (msec / 1000.0);
364
365 do {
366 double start;
367 getCurrentTime();
368 start = current_dtime;
369
370 if (commCheckUdpIncoming)
371 comm_poll_udp_incoming();
372
373 if (commCheckDnsIncoming)
374 comm_poll_dns_incoming();
375
376 if (commCheckTcpIncoming)
377 comm_poll_tcp_incoming();
378
379 PROF_start(comm_poll_prep_pfds);
380
381 calldns = calludp = calltcp = 0;
382
383 nfds = 0;
384
385 npending = 0;
386
387 maxfd = Biggest_FD + 1;
388
389 for (int i = 0; i < maxfd; ++i) {
390 int events;
391 events = 0;
392 /* Check each open socket for a handler. */
393
394 if (fd_table[i].read_handler)
395 events |= POLLRDNORM;
396
397 if (fd_table[i].write_handler)
398 events |= POLLWRNORM;
399
400 if (events) {
401 pfds[nfds].fd = i;
402 pfds[nfds].events = events;
403 pfds[nfds].revents = 0;
404 ++nfds;
405
406 if ((events & POLLRDNORM) && fd_table[i].flags.read_pending)
407 ++npending;
408 }
409 }
410
411 PROF_stop(comm_poll_prep_pfds);
412
413 if (npending)
414 msec = 0;
415
416 if (msec > MAX_POLL_TIME)
417 msec = MAX_POLL_TIME;
418
419 /* nothing to do
420 *
421 * Note that this will only ever trigger when there are no log files
422 * and stdout/err/in are all closed too.
423 */
424 if (nfds == 0 && npending == 0) {
425 if (shutting_down)
426 return COMM_SHUTDOWN;
427 else
428 return COMM_IDLE;
429 }
430
431 for (;;) {
432 PROF_start(comm_poll_normal);
433 ++ statCounter.syscalls.selects;
434 num = poll(pfds, nfds, msec);
435 ++ statCounter.select_loops;
436 PROF_stop(comm_poll_normal);
437
438 if (num >= 0 || npending > 0)
439 break;
440
441 if (ignoreErrno(errno))
442 continue;
443
444 debugs(5, 0, "comm_poll: poll failure: " << xstrerror());
445
446 assert(errno != EINVAL);
447
448 return COMM_ERROR;
449
450 /* NOTREACHED */
451 }
452
453 getCurrentTime();
454
455 debugs(5, num ? 5 : 8, "comm_poll: " << num << "+" << npending << " FDs ready");
456 statCounter.select_fds_hist.count(num);
457
458 if (num == 0 && npending == 0)
459 continue;
460
461 /* scan each socket but the accept socket. Poll this
462 * more frequently to minimize losses due to the 5 connect
463 * limit in SunOS */
464 PROF_start(comm_handle_ready_fd);
465
466 for (size_t loopIndex = 0; loopIndex < nfds; ++loopIndex) {
467 fde *F;
468 int revents = pfds[loopIndex].revents;
469 fd = pfds[loopIndex].fd;
470
471 if (fd == -1)
472 continue;
473
474 if (fd_table[fd].flags.read_pending)
475 revents |= POLLIN;
476
477 if (revents == 0)
478 continue;
479
480 if (fdIsUdpListen(fd)) {
481 calludp = 1;
482 continue;
483 }
484
485 if (fdIsDns(fd)) {
486 calldns = 1;
487 continue;
488 }
489
490 if (fdIsTcpListen(fd)) {
491 calltcp = 1;
492 continue;
493 }
494
495 F = &fd_table[fd];
496
497 if (revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR)) {
498 debugs(5, 6, "comm_poll: FD " << fd << " ready for reading");
499
500 if ((hdl = F->read_handler)) {
501 PROF_start(comm_read_handler);
502 F->read_handler = NULL;
503 F->flags.read_pending = 0;
504 hdl(fd, F->read_data);
505 PROF_stop(comm_read_handler);
506 ++ statCounter.select_fds;
507
508 if (commCheckUdpIncoming)
509 comm_poll_udp_incoming();
510
511 if (commCheckDnsIncoming)
512 comm_poll_dns_incoming();
513
514 if (commCheckTcpIncoming)
515 comm_poll_tcp_incoming();
516 }
517 }
518
519 if (revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR)) {
520 debugs(5, 6, "comm_poll: FD " << fd << " ready for writing");
521
522 if ((hdl = F->write_handler)) {
523 PROF_start(comm_write_handler);
524 F->write_handler = NULL;
525 hdl(fd, F->write_data);
526 PROF_stop(comm_write_handler);
527 ++ statCounter.select_fds;
528
529 if (commCheckUdpIncoming)
530 comm_poll_udp_incoming();
531
532 if (commCheckDnsIncoming)
533 comm_poll_dns_incoming();
534
535 if (commCheckTcpIncoming)
536 comm_poll_tcp_incoming();
537 }
538 }
539
540 if (revents & POLLNVAL) {
541 AsyncCall::Pointer ch;
542 debugs(5, 0, "WARNING: FD " << fd << " has handlers, but it's invalid.");
543 debugs(5, 0, "FD " << fd << " is a " << fdTypeStr[F->type]);
544 debugs(5, 0, "--> " << F->desc);
545 debugs(5, 0, "tmout:" << F->timeoutHandler << "read:" <<
546 F->read_handler << " write:" << F->write_handler);
547
548 for (ch = F->closeHandler; ch != NULL; ch = ch->Next())
549 debugs(5, 0, " close handler: " << ch);
550
551 if (F->closeHandler != NULL) {
552 commCallCloseHandlers(fd);
553 } else if (F->timeoutHandler != NULL) {
554 debugs(5, 0, "comm_poll: Calling Timeout Handler");
555 ScheduleCallHere(F->timeoutHandler);
556 }
557
558 F->closeHandler = NULL;
559 F->timeoutHandler = NULL;
560 F->read_handler = NULL;
561 F->write_handler = NULL;
562
563 if (F->flags.open)
564 fd_close(fd);
565 }
566 }
567
568 PROF_stop(comm_handle_ready_fd);
569
570 if (calludp)
571 comm_poll_udp_incoming();
572
573 if (calldns)
574 comm_poll_dns_incoming();
575
576 if (calltcp)
577 comm_poll_tcp_incoming();
578
579 getCurrentTime();
580
581 statCounter.select_time += (current_dtime - start);
582
583 return COMM_OK;
584 } while (timeout > current_dtime);
585
586 debugs(5, 8, "comm_poll: time out: " << squid_curtime << ".");
587
588 return COMM_TIMEOUT;
589 }
590
591
592 static void
593 comm_poll_dns_incoming(void)
594 {
595 int nfds = 0;
596 int fds[2];
597 int nevents;
598 dns_io_events = 0;
599
600 if (DnsSocketA < 0 && DnsSocketB < 0)
601 return;
602
603 if (DnsSocketA >= 0) {
604 fds[nfds] = DnsSocketA;
605 ++nfds;
606 }
607
608 if (DnsSocketB >= 0) {
609 fds[nfds] = DnsSocketB;
610 ++nfds;
611 }
612
613 nevents = comm_check_incoming_poll_handlers(nfds, fds);
614
615 if (nevents < 0)
616 return;
617
618 incoming_dns_interval += Config.comm_incoming.dns.average - nevents;
619
620 if (incoming_dns_interval < Config.comm_incoming.dns.min_poll)
621 incoming_dns_interval = Config.comm_incoming.dns.min_poll;
622
623 if (incoming_dns_interval > MAX_INCOMING_INTERVAL)
624 incoming_dns_interval = MAX_INCOMING_INTERVAL;
625
626 if (nevents > INCOMING_DNS_MAX)
627 nevents = INCOMING_DNS_MAX;
628
629 statCounter.comm_dns_incoming.count(nevents);
630 }
631
632
633 static void
634 commPollRegisterWithCacheManager(void)
635 {
636 Mgr::RegisterAction("comm_poll_incoming",
637 "comm_incoming() stats",
638 commIncomingStats, 0, 1);
639 }
640
641 void
642 Comm::SelectLoopInit(void)
643 {
644 commPollRegisterWithCacheManager();
645 }
646
647 static void
648 commIncomingStats(StoreEntry * sentry)
649 {
650 storeAppendPrintf(sentry, "Current incoming_udp_interval: %d\n",
651 incoming_udp_interval >> INCOMING_FACTOR);
652 storeAppendPrintf(sentry, "Current incoming_dns_interval: %d\n",
653 incoming_dns_interval >> INCOMING_FACTOR);
654 storeAppendPrintf(sentry, "Current incoming_tcp_interval: %d\n",
655 incoming_tcp_interval >> INCOMING_FACTOR);
656 storeAppendPrintf(sentry, "\n");
657 storeAppendPrintf(sentry, "Histogram of events per incoming socket type\n");
658 storeAppendPrintf(sentry, "ICP Messages handled per comm_poll_udp_incoming() call:\n");
659 statCounter.comm_udp_incoming.dump(sentry, statHistIntDumper);
660 storeAppendPrintf(sentry, "DNS Messages handled per comm_poll_dns_incoming() call:\n");
661 statCounter.comm_dns_incoming.dump(sentry, statHistIntDumper);
662 storeAppendPrintf(sentry, "HTTP Messages handled per comm_poll_tcp_incoming() call:\n");
663 statCounter.comm_tcp_incoming.dump(sentry, statHistIntDumper);
664 }
665
666 /* Called by async-io or diskd to speed up the polling */
667 void
668 Comm::QuickPollRequired(void)
669 {
670 MAX_POLL_TIME = 10;
671 }
672
673 #endif /* USE_POLL */