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