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 "SquidConfig.h"
44 #include "SquidTime.h"
45 #include "StatCounters.h"
55 /* Needed for poll() on Linux at least */
58 #define POLLRDNORM POLLIN
61 #define POLLWRNORM POLLOUT
65 static int MAX_POLL_TIME
= 1000; /* see also Comm::QuickPollRequired() */
68 #define howmany(x, y) (((x)+((y)-1))/(y))
73 #define FD_MASK_BYTES sizeof(fd_mask)
74 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
77 static int fdIsTcpListen(int fd
);
78 static int fdIsUdpListen(int fd
);
79 static int fdIsDns(int fd
);
80 static OBJH commIncomingStats
;
81 static int comm_check_incoming_poll_handlers(int nfds
, int *fds
);
82 static void comm_poll_dns_incoming(void);
85 * Automatic tuning for incoming requests:
87 * INCOMING sockets are the ICP and HTTP ports. We need to check these
88 * fairly regularly, but how often? When the load increases, we
89 * want to check the incoming sockets more often. If we have a lot
90 * of incoming ICP, then we need to check these sockets more than
91 * if we just have HTTP.
93 * The variables 'incoming_icp_interval' and 'incoming_http_interval'
94 * determine how many normal I/O events to process before checking
95 * incoming sockets again. Note we store the incoming_interval
96 * multipled by a factor of (2^INCOMING_FACTOR) to have some
97 * pseudo-floating point precision.
99 * The variable 'udp_io_events' and 'tcp_io_events' counts how many normal
100 * I/O events have been processed since the last check on the incoming
101 * sockets. When io_events > incoming_interval, its time to check incoming
104 * Every time we check incoming sockets, we count how many new messages
105 * or connections were processed. This is used to adjust the
106 * incoming_interval for the next iteration. The new incoming_interval
107 * is calculated as the current incoming_interval plus what we would
108 * like to see as an average number of events minus the number of
109 * events just processed.
111 * incoming_interval = incoming_interval + target_average - number_of_events_processed
113 * There are separate incoming_interval counters for TCP-based, UDP-based, and DNS events
115 * You can see the current values of the incoming_interval's, as well as
116 * a histogram of 'incoming_events' by asking the cache manager
117 * for 'comm_incoming', e.g.:
119 * % ./client mgr:comm_poll_incoming
123 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
124 * incoming_interval for both types of sockets. At the
125 * largest value the cache will effectively be idling.
127 * - The higher the INCOMING_FACTOR, the slower the algorithm will
128 * respond to load spikes/increases/decreases in demand. A value
129 * between 3 and 8 is recommended.
132 #define MAX_INCOMING_INTEGER 256
133 #define INCOMING_FACTOR 5
134 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
135 static int udp_io_events
= 0; ///< I/O events passed since last UDP receiver socket poll
136 static int dns_io_events
= 0; ///< I/O events passed since last DNS socket poll
137 static int tcp_io_events
= 0; ///< I/O events passed since last TCP listening socket poll
138 static int incoming_udp_interval
= 16 << INCOMING_FACTOR
;
139 static int incoming_dns_interval
= 16 << INCOMING_FACTOR
;
140 static int incoming_tcp_interval
= 16 << INCOMING_FACTOR
;
141 #define commCheckUdpIncoming (++udp_io_events > (incoming_udp_interval>> INCOMING_FACTOR))
142 #define commCheckDnsIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
143 #define commCheckTcpIncoming (++tcp_io_events > (incoming_tcp_interval>> INCOMING_FACTOR))
146 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
148 fde
*F
= &fd_table
[fd
];
150 assert(F
->flags
.open
);
151 debugs(5, 5, HERE
<< "FD " << fd
<< ", type=" << type
<<
152 ", handler=" << handler
<< ", client_data=" << client_data
<<
153 ", timeout=" << timeout
);
155 if (type
& COMM_SELECT_READ
) {
156 F
->read_handler
= handler
;
157 F
->read_data
= client_data
;
160 if (type
& COMM_SELECT_WRITE
) {
161 F
->write_handler
= handler
;
162 F
->write_data
= client_data
;
166 F
->timeout
= squid_curtime
+ timeout
;
170 Comm::ResetSelect(int fd
)
175 fdIsUdpListen(int fd
)
177 if (icpIncomingConn
!= NULL
&& icpIncomingConn
->fd
== fd
)
180 if (icpOutgoingConn
!= NULL
&& icpOutgoingConn
->fd
== fd
)
189 if (fd
== DnsSocketA
)
192 if (fd
== DnsSocketB
)
199 fdIsTcpListen(int fd
)
201 for (const AnyP::PortCfg
*s
= Config
.Sockaddr
.http
; s
; s
= s
->next
) {
202 if (s
->listenConn
!= NULL
&& s
->listenConn
->fd
== fd
)
210 comm_check_incoming_poll_handlers(int nfds
, int *fds
)
217 struct pollfd pfds
[3 + MAXTCPLISTENPORTS
];
218 PROF_start(comm_check_incoming
);
219 incoming_sockets_accepted
= 0;
221 for (i
= npfds
= 0; i
< nfds
; ++i
) {
226 if (fd_table
[fd
].read_handler
)
227 events
|= POLLRDNORM
;
229 if (fd_table
[fd
].write_handler
)
230 events
|= POLLWRNORM
;
234 pfds
[npfds
].events
= events
;
235 pfds
[npfds
].revents
= 0;
241 PROF_stop(comm_check_incoming
);
246 ++ statCounter
.syscalls
.selects
;
248 if (poll(pfds
, npfds
, 0) < 1) {
249 PROF_stop(comm_check_incoming
);
250 return incoming_sockets_accepted
;
253 for (i
= 0; i
< npfds
; ++i
) {
256 if (((revents
= pfds
[i
].revents
) == 0) || ((fd
= pfds
[i
].fd
) == -1))
259 if (revents
& (POLLRDNORM
| POLLIN
| POLLHUP
| POLLERR
)) {
260 if ((hdl
= fd_table
[fd
].read_handler
)) {
261 fd_table
[fd
].read_handler
= NULL
;
262 hdl(fd
, fd_table
[fd
].read_data
);
263 } else if (pfds
[i
].events
& POLLRDNORM
)
264 debugs(5, DBG_IMPORTANT
, "comm_poll_incoming: FD " << fd
<< " NULL read handler");
267 if (revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
| POLLERR
)) {
268 if ((hdl
= fd_table
[fd
].write_handler
)) {
269 fd_table
[fd
].write_handler
= NULL
;
270 hdl(fd
, fd_table
[fd
].write_data
);
271 } else if (pfds
[i
].events
& POLLWRNORM
)
272 debugs(5, DBG_IMPORTANT
, "comm_poll_incoming: FD " << fd
<< " NULL write_handler");
276 PROF_stop(comm_check_incoming
);
277 return incoming_sockets_accepted
;
281 comm_poll_udp_incoming(void)
288 if (Comm::IsConnOpen(icpIncomingConn
)) {
289 fds
[nfds
] = icpIncomingConn
->fd
;
293 if (icpIncomingConn
!= icpOutgoingConn
&& Comm::IsConnOpen(icpOutgoingConn
)) {
294 fds
[nfds
] = icpOutgoingConn
->fd
;
301 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
303 incoming_udp_interval
+= Config
.comm_incoming
.udp
.average
- nevents
;
305 if (incoming_udp_interval
< Config
.comm_incoming
.udp
.min_poll
)
306 incoming_udp_interval
= Config
.comm_incoming
.udp
.min_poll
;
308 if (incoming_udp_interval
> MAX_INCOMING_INTERVAL
)
309 incoming_udp_interval
= MAX_INCOMING_INTERVAL
;
311 if (nevents
> INCOMING_UDP_MAX
)
312 nevents
= INCOMING_UDP_MAX
;
314 statCounter
.comm_udp_incoming
.count(nevents
);
318 comm_poll_tcp_incoming(void)
321 int fds
[MAXTCPLISTENPORTS
];
326 // XXX: only poll sockets that won't be deferred. But how do we identify them?
328 for (j
= 0; j
< NHttpSockets
; ++j
) {
329 if (HttpSockets
[j
] < 0)
332 fds
[nfds
] = HttpSockets
[j
];
336 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
337 incoming_tcp_interval
= incoming_tcp_interval
338 + Config
.comm_incoming
.tcp
.average
- nevents
;
340 if (incoming_tcp_interval
< Config
.comm_incoming
.tcp
.min_poll
)
341 incoming_tcp_interval
= Config
.comm_incoming
.tcp
.min_poll
;
343 if (incoming_tcp_interval
> MAX_INCOMING_INTERVAL
)
344 incoming_tcp_interval
= MAX_INCOMING_INTERVAL
;
346 if (nevents
> INCOMING_TCP_MAX
)
347 nevents
= INCOMING_TCP_MAX
;
349 statCounter
.comm_tcp_incoming
.count(nevents
);
352 /* poll all sockets; call handlers for those that are ready. */
354 Comm::DoSelect(int msec
)
356 struct pollfd pfds
[SQUID_MAXFD
];
362 unsigned long npending
;
364 int calldns
= 0, calludp
= 0, calltcp
= 0;
365 double timeout
= current_dtime
+ (msec
/ 1000.0);
370 start
= current_dtime
;
372 if (commCheckUdpIncoming
)
373 comm_poll_udp_incoming();
375 if (commCheckDnsIncoming
)
376 comm_poll_dns_incoming();
378 if (commCheckTcpIncoming
)
379 comm_poll_tcp_incoming();
381 PROF_start(comm_poll_prep_pfds
);
383 calldns
= calludp
= calltcp
= 0;
389 maxfd
= Biggest_FD
+ 1;
391 for (int i
= 0; i
< maxfd
; ++i
) {
394 /* Check each open socket for a handler. */
396 if (fd_table
[i
].read_handler
)
397 events
|= POLLRDNORM
;
399 if (fd_table
[i
].write_handler
)
400 events
|= POLLWRNORM
;
404 pfds
[nfds
].events
= events
;
405 pfds
[nfds
].revents
= 0;
408 if ((events
& POLLRDNORM
) && fd_table
[i
].flags
.read_pending
)
413 PROF_stop(comm_poll_prep_pfds
);
418 if (msec
> MAX_POLL_TIME
)
419 msec
= MAX_POLL_TIME
;
423 * Note that this will only ever trigger when there are no log files
424 * and stdout/err/in are all closed too.
426 if (nfds
== 0 && npending
== 0) {
428 return COMM_SHUTDOWN
;
434 PROF_start(comm_poll_normal
);
435 ++ statCounter
.syscalls
.selects
;
436 num
= poll(pfds
, nfds
, msec
);
437 ++ statCounter
.select_loops
;
438 PROF_stop(comm_poll_normal
);
440 if (num
>= 0 || npending
> 0)
443 if (ignoreErrno(errno
))
446 debugs(5, DBG_CRITICAL
, "comm_poll: poll failure: " << xstrerror());
448 assert(errno
!= EINVAL
);
457 debugs(5, num
? 5 : 8, "comm_poll: " << num
<< "+" << npending
<< " FDs ready");
458 statCounter
.select_fds_hist
.count(num
);
460 if (num
== 0 && npending
== 0)
463 /* scan each socket but the accept socket. Poll this
464 * more frequently to minimize losses due to the 5 connect
466 PROF_start(comm_handle_ready_fd
);
468 for (size_t loopIndex
= 0; loopIndex
< nfds
; ++loopIndex
) {
470 int revents
= pfds
[loopIndex
].revents
;
471 fd
= pfds
[loopIndex
].fd
;
476 if (fd_table
[fd
].flags
.read_pending
)
482 if (fdIsUdpListen(fd
)) {
492 if (fdIsTcpListen(fd
)) {
499 if (revents
& (POLLRDNORM
| POLLIN
| POLLHUP
| POLLERR
)) {
500 debugs(5, 6, "comm_poll: FD " << fd
<< " ready for reading");
502 if ((hdl
= F
->read_handler
)) {
503 PROF_start(comm_read_handler
);
504 F
->read_handler
= NULL
;
505 F
->flags
.read_pending
= false;
506 hdl(fd
, F
->read_data
);
507 PROF_stop(comm_read_handler
);
508 ++ statCounter
.select_fds
;
510 if (commCheckUdpIncoming
)
511 comm_poll_udp_incoming();
513 if (commCheckDnsIncoming
)
514 comm_poll_dns_incoming();
516 if (commCheckTcpIncoming
)
517 comm_poll_tcp_incoming();
521 if (revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
| POLLERR
)) {
522 debugs(5, 6, "comm_poll: FD " << fd
<< " ready for writing");
524 if ((hdl
= F
->write_handler
)) {
525 PROF_start(comm_write_handler
);
526 F
->write_handler
= NULL
;
527 hdl(fd
, F
->write_data
);
528 PROF_stop(comm_write_handler
);
529 ++ statCounter
.select_fds
;
531 if (commCheckUdpIncoming
)
532 comm_poll_udp_incoming();
534 if (commCheckDnsIncoming
)
535 comm_poll_dns_incoming();
537 if (commCheckTcpIncoming
)
538 comm_poll_tcp_incoming();
542 if (revents
& POLLNVAL
) {
543 AsyncCall::Pointer ch
;
544 debugs(5, DBG_CRITICAL
, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
545 debugs(5, DBG_CRITICAL
, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
]);
546 debugs(5, DBG_CRITICAL
, "--> " << F
->desc
);
547 debugs(5, DBG_CRITICAL
, "tmout:" << F
->timeoutHandler
<< "read:" <<
548 F
->read_handler
<< " write:" << F
->write_handler
);
550 for (ch
= F
->closeHandler
; ch
!= NULL
; ch
= ch
->Next())
551 debugs(5, DBG_CRITICAL
, " close handler: " << ch
);
553 if (F
->closeHandler
!= NULL
) {
554 commCallCloseHandlers(fd
);
555 } else if (F
->timeoutHandler
!= NULL
) {
556 debugs(5, DBG_CRITICAL
, "comm_poll: Calling Timeout Handler");
557 ScheduleCallHere(F
->timeoutHandler
);
560 F
->closeHandler
= NULL
;
561 F
->timeoutHandler
= NULL
;
562 F
->read_handler
= NULL
;
563 F
->write_handler
= NULL
;
570 PROF_stop(comm_handle_ready_fd
);
573 comm_poll_udp_incoming();
576 comm_poll_dns_incoming();
579 comm_poll_tcp_incoming();
583 statCounter
.select_time
+= (current_dtime
- start
);
586 } while (timeout
> current_dtime
);
588 debugs(5, 8, "comm_poll: time out: " << squid_curtime
<< ".");
594 comm_poll_dns_incoming(void)
601 if (DnsSocketA
< 0 && DnsSocketB
< 0)
604 if (DnsSocketA
>= 0) {
605 fds
[nfds
] = DnsSocketA
;
609 if (DnsSocketB
>= 0) {
610 fds
[nfds
] = DnsSocketB
;
614 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
619 incoming_dns_interval
+= Config
.comm_incoming
.dns
.average
- nevents
;
621 if (incoming_dns_interval
< Config
.comm_incoming
.dns
.min_poll
)
622 incoming_dns_interval
= Config
.comm_incoming
.dns
.min_poll
;
624 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
625 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
627 if (nevents
> INCOMING_DNS_MAX
)
628 nevents
= INCOMING_DNS_MAX
;
630 statCounter
.comm_dns_incoming
.count(nevents
);
634 commPollRegisterWithCacheManager(void)
636 Mgr::RegisterAction("comm_poll_incoming",
637 "comm_incoming() stats",
638 commIncomingStats
, 0, 1);
642 Comm::SelectLoopInit(void)
644 commPollRegisterWithCacheManager();
648 commIncomingStats(StoreEntry
* sentry
)
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
);
666 /* Called by async-io or diskd to speed up the polling */
668 Comm::QuickPollRequired(void)
673 #endif /* USE_POLL */