2 * DEBUG: section 05 Socket Functions
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #include "anyp/PortCfg.h"
35 #include "comm/Connection.h"
36 #include "comm/Loops.h"
41 #include "mgr/Registration.h"
42 #include "profiler/Profiler.h"
43 #include "SquidTime.h"
44 #include "StatCounters.h"
54 /* Needed for poll() on Linux at least */
57 #define POLLRDNORM POLLIN
60 #define POLLWRNORM POLLOUT
64 static int MAX_POLL_TIME
= 1000; /* see also Comm::QuickPollRequired() */
67 #define howmany(x, y) (((x)+((y)-1))/(y))
72 #define FD_MASK_BYTES sizeof(fd_mask)
73 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
76 static int fdIsTcpListen(int fd
);
77 static int fdIsUdpListen(int fd
);
78 static int fdIsDns(int fd
);
79 static OBJH commIncomingStats
;
80 static int comm_check_incoming_poll_handlers(int nfds
, int *fds
);
81 static void comm_poll_dns_incoming(void);
84 * Automatic tuning for incoming requests:
86 * INCOMING sockets are the ICP and HTTP ports. We need to check these
87 * fairly regularly, but how often? When the load increases, we
88 * want to check the incoming sockets more often. If we have a lot
89 * of incoming ICP, then we need to check these sockets more than
90 * if we just have HTTP.
92 * The variables 'incoming_icp_interval' and 'incoming_http_interval'
93 * determine how many normal I/O events to process before checking
94 * incoming sockets again. Note we store the incoming_interval
95 * multipled by a factor of (2^INCOMING_FACTOR) to have some
96 * pseudo-floating point precision.
98 * The variable 'udp_io_events' and 'tcp_io_events' counts how many normal
99 * I/O events have been processed since the last check on the incoming
100 * sockets. When io_events > incoming_interval, its time to check incoming
103 * Every time we check incoming sockets, we count how many new messages
104 * or connections were processed. This is used to adjust the
105 * incoming_interval for the next iteration. The new incoming_interval
106 * is calculated as the current incoming_interval plus what we would
107 * like to see as an average number of events minus the number of
108 * events just processed.
110 * incoming_interval = incoming_interval + target_average - number_of_events_processed
112 * There are separate incoming_interval counters for TCP-based, UDP-based, and DNS events
114 * You can see the current values of the incoming_interval's, as well as
115 * a histogram of 'incoming_events' by asking the cache manager
116 * for 'comm_incoming', e.g.:
118 * % ./client mgr:comm_poll_incoming
122 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
123 * incoming_interval for both types of sockets. At the
124 * largest value the cache will effectively be idling.
126 * - The higher the INCOMING_FACTOR, the slower the algorithm will
127 * respond to load spikes/increases/decreases in demand. A value
128 * between 3 and 8 is recommended.
131 #define MAX_INCOMING_INTEGER 256
132 #define INCOMING_FACTOR 5
133 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
134 static int udp_io_events
= 0; ///< I/O events passed since last UDP receiver socket poll
135 static int dns_io_events
= 0; ///< I/O events passed since last DNS socket poll
136 static int tcp_io_events
= 0; ///< I/O events passed since last TCP listening socket poll
137 static int incoming_udp_interval
= 16 << INCOMING_FACTOR
;
138 static int incoming_dns_interval
= 16 << INCOMING_FACTOR
;
139 static int incoming_tcp_interval
= 16 << INCOMING_FACTOR
;
140 #define commCheckUdpIncoming (++udp_io_events > (incoming_udp_interval>> INCOMING_FACTOR))
141 #define commCheckDnsIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
142 #define commCheckTcpIncoming (++tcp_io_events > (incoming_tcp_interval>> INCOMING_FACTOR))
145 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
147 fde
*F
= &fd_table
[fd
];
149 assert(F
->flags
.open
);
150 debugs(5, 5, HERE
<< "FD " << fd
<< ", type=" << type
<<
151 ", handler=" << handler
<< ", client_data=" << client_data
<<
152 ", timeout=" << timeout
);
154 if (type
& COMM_SELECT_READ
) {
155 F
->read_handler
= handler
;
156 F
->read_data
= client_data
;
159 if (type
& COMM_SELECT_WRITE
) {
160 F
->write_handler
= handler
;
161 F
->write_data
= client_data
;
165 F
->timeout
= squid_curtime
+ timeout
;
169 Comm::ResetSelect(int fd
)
174 fdIsUdpListen(int fd
)
176 if (icpIncomingConn
!= NULL
&& icpIncomingConn
->fd
== fd
)
179 if (icpOutgoingConn
!= NULL
&& icpOutgoingConn
->fd
== fd
)
188 if (fd
== DnsSocketA
)
191 if (fd
== DnsSocketB
)
198 fdIsTcpListen(int fd
)
200 for (const AnyP::PortCfg
*s
= Config
.Sockaddr
.http
; s
; s
= s
->next
) {
201 if (s
->listenConn
!= NULL
&& s
->listenConn
->fd
== fd
)
209 comm_check_incoming_poll_handlers(int nfds
, int *fds
)
216 struct pollfd pfds
[3 + MAXTCPLISTENPORTS
];
217 PROF_start(comm_check_incoming
);
218 incoming_sockets_accepted
= 0;
220 for (i
= npfds
= 0; i
< nfds
; ++i
) {
225 if (fd_table
[fd
].read_handler
)
226 events
|= POLLRDNORM
;
228 if (fd_table
[fd
].write_handler
)
229 events
|= POLLWRNORM
;
233 pfds
[npfds
].events
= events
;
234 pfds
[npfds
].revents
= 0;
240 PROF_stop(comm_check_incoming
);
245 ++ statCounter
.syscalls
.selects
;
247 if (poll(pfds
, npfds
, 0) < 1) {
248 PROF_stop(comm_check_incoming
);
249 return incoming_sockets_accepted
;
252 for (i
= 0; i
< npfds
; ++i
) {
255 if (((revents
= pfds
[i
].revents
) == 0) || ((fd
= pfds
[i
].fd
) == -1))
258 if (revents
& (POLLRDNORM
| POLLIN
| POLLHUP
| POLLERR
)) {
259 if ((hdl
= fd_table
[fd
].read_handler
)) {
260 fd_table
[fd
].read_handler
= NULL
;
261 hdl(fd
, fd_table
[fd
].read_data
);
262 } else if (pfds
[i
].events
& POLLRDNORM
)
263 debugs(5, DBG_IMPORTANT
, "comm_poll_incoming: FD " << fd
<< " NULL read handler");
266 if (revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
| POLLERR
)) {
267 if ((hdl
= fd_table
[fd
].write_handler
)) {
268 fd_table
[fd
].write_handler
= NULL
;
269 hdl(fd
, fd_table
[fd
].write_data
);
270 } else if (pfds
[i
].events
& POLLWRNORM
)
271 debugs(5, DBG_IMPORTANT
, "comm_poll_incoming: FD " << fd
<< " NULL write_handler");
275 PROF_stop(comm_check_incoming
);
276 return incoming_sockets_accepted
;
280 comm_poll_udp_incoming(void)
287 if (Comm::IsConnOpen(icpIncomingConn
)) {
288 fds
[nfds
] = icpIncomingConn
->fd
;
292 if (icpIncomingConn
!= icpOutgoingConn
&& Comm::IsConnOpen(icpOutgoingConn
)) {
293 fds
[nfds
] = icpOutgoingConn
->fd
;
300 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
302 incoming_udp_interval
+= Config
.comm_incoming
.udp
.average
- nevents
;
304 if (incoming_udp_interval
< Config
.comm_incoming
.udp
.min_poll
)
305 incoming_udp_interval
= Config
.comm_incoming
.udp
.min_poll
;
307 if (incoming_udp_interval
> MAX_INCOMING_INTERVAL
)
308 incoming_udp_interval
= MAX_INCOMING_INTERVAL
;
310 if (nevents
> INCOMING_UDP_MAX
)
311 nevents
= INCOMING_UDP_MAX
;
313 statCounter
.comm_udp_incoming
.count(nevents
);
317 comm_poll_tcp_incoming(void)
320 int fds
[MAXTCPLISTENPORTS
];
325 // XXX: only poll sockets that won't be deferred. But how do we identify them?
327 for (j
= 0; j
< NHttpSockets
; ++j
) {
328 if (HttpSockets
[j
] < 0)
331 fds
[nfds
] = HttpSockets
[j
];
335 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
336 incoming_tcp_interval
= incoming_tcp_interval
337 + Config
.comm_incoming
.tcp
.average
- nevents
;
339 if (incoming_tcp_interval
< Config
.comm_incoming
.tcp
.min_poll
)
340 incoming_tcp_interval
= Config
.comm_incoming
.tcp
.min_poll
;
342 if (incoming_tcp_interval
> MAX_INCOMING_INTERVAL
)
343 incoming_tcp_interval
= MAX_INCOMING_INTERVAL
;
345 if (nevents
> INCOMING_TCP_MAX
)
346 nevents
= INCOMING_TCP_MAX
;
348 statCounter
.comm_tcp_incoming
.count(nevents
);
351 /* poll all sockets; call handlers for those that are ready. */
353 Comm::DoSelect(int msec
)
355 struct pollfd pfds
[SQUID_MAXFD
];
361 unsigned long npending
;
363 int calldns
= 0, calludp
= 0, calltcp
= 0;
364 double timeout
= current_dtime
+ (msec
/ 1000.0);
369 start
= current_dtime
;
371 if (commCheckUdpIncoming
)
372 comm_poll_udp_incoming();
374 if (commCheckDnsIncoming
)
375 comm_poll_dns_incoming();
377 if (commCheckTcpIncoming
)
378 comm_poll_tcp_incoming();
380 PROF_start(comm_poll_prep_pfds
);
382 calldns
= calludp
= calltcp
= 0;
388 maxfd
= Biggest_FD
+ 1;
390 for (int i
= 0; i
< maxfd
; ++i
) {
393 /* Check each open socket for a handler. */
395 if (fd_table
[i
].read_handler
)
396 events
|= POLLRDNORM
;
398 if (fd_table
[i
].write_handler
)
399 events
|= POLLWRNORM
;
403 pfds
[nfds
].events
= events
;
404 pfds
[nfds
].revents
= 0;
407 if ((events
& POLLRDNORM
) && fd_table
[i
].flags
.read_pending
)
412 PROF_stop(comm_poll_prep_pfds
);
417 if (msec
> MAX_POLL_TIME
)
418 msec
= MAX_POLL_TIME
;
422 * Note that this will only ever trigger when there are no log files
423 * and stdout/err/in are all closed too.
425 if (nfds
== 0 && npending
== 0) {
427 return COMM_SHUTDOWN
;
433 PROF_start(comm_poll_normal
);
434 ++ statCounter
.syscalls
.selects
;
435 num
= poll(pfds
, nfds
, msec
);
436 ++ statCounter
.select_loops
;
437 PROF_stop(comm_poll_normal
);
439 if (num
>= 0 || npending
> 0)
442 if (ignoreErrno(errno
))
445 debugs(5, DBG_CRITICAL
, "comm_poll: poll failure: " << xstrerror());
447 assert(errno
!= EINVAL
);
456 debugs(5, num
? 5 : 8, "comm_poll: " << num
<< "+" << npending
<< " FDs ready");
457 statCounter
.select_fds_hist
.count(num
);
459 if (num
== 0 && npending
== 0)
462 /* scan each socket but the accept socket. Poll this
463 * more frequently to minimize losses due to the 5 connect
465 PROF_start(comm_handle_ready_fd
);
467 for (size_t loopIndex
= 0; loopIndex
< nfds
; ++loopIndex
) {
469 int revents
= pfds
[loopIndex
].revents
;
470 fd
= pfds
[loopIndex
].fd
;
475 if (fd_table
[fd
].flags
.read_pending
)
481 if (fdIsUdpListen(fd
)) {
491 if (fdIsTcpListen(fd
)) {
498 if (revents
& (POLLRDNORM
| POLLIN
| POLLHUP
| POLLERR
)) {
499 debugs(5, 6, "comm_poll: FD " << fd
<< " ready for reading");
501 if ((hdl
= F
->read_handler
)) {
502 PROF_start(comm_read_handler
);
503 F
->read_handler
= NULL
;
504 F
->flags
.read_pending
= 0;
505 hdl(fd
, F
->read_data
);
506 PROF_stop(comm_read_handler
);
507 ++ statCounter
.select_fds
;
509 if (commCheckUdpIncoming
)
510 comm_poll_udp_incoming();
512 if (commCheckDnsIncoming
)
513 comm_poll_dns_incoming();
515 if (commCheckTcpIncoming
)
516 comm_poll_tcp_incoming();
520 if (revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
| POLLERR
)) {
521 debugs(5, 6, "comm_poll: FD " << fd
<< " ready for writing");
523 if ((hdl
= F
->write_handler
)) {
524 PROF_start(comm_write_handler
);
525 F
->write_handler
= NULL
;
526 hdl(fd
, F
->write_data
);
527 PROF_stop(comm_write_handler
);
528 ++ statCounter
.select_fds
;
530 if (commCheckUdpIncoming
)
531 comm_poll_udp_incoming();
533 if (commCheckDnsIncoming
)
534 comm_poll_dns_incoming();
536 if (commCheckTcpIncoming
)
537 comm_poll_tcp_incoming();
541 if (revents
& POLLNVAL
) {
542 AsyncCall::Pointer ch
;
543 debugs(5, DBG_CRITICAL
, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
544 debugs(5, DBG_CRITICAL
, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
]);
545 debugs(5, DBG_CRITICAL
, "--> " << F
->desc
);
546 debugs(5, DBG_CRITICAL
, "tmout:" << F
->timeoutHandler
<< "read:" <<
547 F
->read_handler
<< " write:" << F
->write_handler
);
549 for (ch
= F
->closeHandler
; ch
!= NULL
; ch
= ch
->Next())
550 debugs(5, DBG_CRITICAL
, " close handler: " << ch
);
552 if (F
->closeHandler
!= NULL
) {
553 commCallCloseHandlers(fd
);
554 } else if (F
->timeoutHandler
!= NULL
) {
555 debugs(5, DBG_CRITICAL
, "comm_poll: Calling Timeout Handler");
556 ScheduleCallHere(F
->timeoutHandler
);
559 F
->closeHandler
= NULL
;
560 F
->timeoutHandler
= NULL
;
561 F
->read_handler
= NULL
;
562 F
->write_handler
= NULL
;
569 PROF_stop(comm_handle_ready_fd
);
572 comm_poll_udp_incoming();
575 comm_poll_dns_incoming();
578 comm_poll_tcp_incoming();
582 statCounter
.select_time
+= (current_dtime
- start
);
585 } while (timeout
> current_dtime
);
587 debugs(5, 8, "comm_poll: time out: " << squid_curtime
<< ".");
593 comm_poll_dns_incoming(void)
600 if (DnsSocketA
< 0 && DnsSocketB
< 0)
603 if (DnsSocketA
>= 0) {
604 fds
[nfds
] = DnsSocketA
;
608 if (DnsSocketB
>= 0) {
609 fds
[nfds
] = DnsSocketB
;
613 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
618 incoming_dns_interval
+= Config
.comm_incoming
.dns
.average
- nevents
;
620 if (incoming_dns_interval
< Config
.comm_incoming
.dns
.min_poll
)
621 incoming_dns_interval
= Config
.comm_incoming
.dns
.min_poll
;
623 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
624 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
626 if (nevents
> INCOMING_DNS_MAX
)
627 nevents
= INCOMING_DNS_MAX
;
629 statCounter
.comm_dns_incoming
.count(nevents
);
633 commPollRegisterWithCacheManager(void)
635 Mgr::RegisterAction("comm_poll_incoming",
636 "comm_incoming() stats",
637 commIncomingStats
, 0, 1);
641 Comm::SelectLoopInit(void)
643 commPollRegisterWithCacheManager();
647 commIncomingStats(StoreEntry
* sentry
)
649 storeAppendPrintf(sentry
, "Current incoming_udp_interval: %d\n",
650 incoming_udp_interval
>> INCOMING_FACTOR
);
651 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
652 incoming_dns_interval
>> INCOMING_FACTOR
);
653 storeAppendPrintf(sentry
, "Current incoming_tcp_interval: %d\n",
654 incoming_tcp_interval
>> INCOMING_FACTOR
);
655 storeAppendPrintf(sentry
, "\n");
656 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
657 storeAppendPrintf(sentry
, "ICP Messages handled per comm_poll_udp_incoming() call:\n");
658 statCounter
.comm_udp_incoming
.dump(sentry
, statHistIntDumper
);
659 storeAppendPrintf(sentry
, "DNS Messages handled per comm_poll_dns_incoming() call:\n");
660 statCounter
.comm_dns_incoming
.dump(sentry
, statHistIntDumper
);
661 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_poll_tcp_incoming() call:\n");
662 statCounter
.comm_tcp_incoming
.dump(sentry
, statHistIntDumper
);
665 /* Called by async-io or diskd to speed up the polling */
667 Comm::QuickPollRequired(void)
672 #endif /* USE_POLL */