2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 05 Socket Functions */
14 #include "anyp/PortCfg.h"
15 #include "comm/Connection.h"
16 #include "comm/Loops.h"
19 #include "mgr/Registration.h"
20 #include "SquidTime.h"
21 #include "StatCounters.h"
27 static int MAX_POLL_TIME
= 1000; /* see also Comm::QuickPollRequired() */
30 #define howmany(x, y) (((x)+((y)-1))/(y))
35 #define FD_MASK_BYTES sizeof(fd_mask)
36 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
39 static int examine_select(fd_set
*, fd_set
*);
40 static int fdIsTcpListener(int fd
);
41 static int fdIsUdpListener(int fd
);
42 static int fdIsDns(int fd
);
43 static OBJH commIncomingStats
;
44 static int comm_check_incoming_select_handlers(int nfds
, int *fds
);
45 static void comm_select_dns_incoming(void);
46 static void commUpdateReadBits(int fd
, PF
* handler
);
47 static void commUpdateWriteBits(int fd
, PF
* handler
);
49 static struct timeval zero_tv
;
50 static fd_set global_readfds
;
51 static fd_set global_writefds
;
56 * Automatic tuning for incoming requests:
58 * INCOMING sockets are the ICP and HTTP ports. We need to check these
59 * fairly regularly, but how often? When the load increases, we
60 * want to check the incoming sockets more often. If we have a lot
61 * of incoming ICP, then we need to check these sockets more than
62 * if we just have HTTP.
64 * The variables 'incoming_udp_interval' and 'incoming_tcp_interval'
65 * determine how many normal I/O events to process before checking
66 * incoming sockets again. Note we store the incoming_interval
67 * multipled by a factor of (2^INCOMING_FACTOR) to have some
68 * pseudo-floating point precision.
70 * The variable 'udp_io_events' and 'tcp_io_events' counts how many normal
71 * I/O events have been processed since the last check on the incoming
72 * sockets. When io_events > incoming_interval, its time to check incoming
75 * Every time we check incoming sockets, we count how many new messages
76 * or connections were processed. This is used to adjust the
77 * incoming_interval for the next iteration. The new incoming_interval
78 * is calculated as the current incoming_interval plus what we would
79 * like to see as an average number of events minus the number of
80 * events just processed.
82 * incoming_interval = incoming_interval + target_average - number_of_events_processed
84 * There are separate incoming_interval counters for DNS, UDP and TCP events
86 * You can see the current values of the incoming_interval's, as well as
87 * a histogram of 'incoming_events' by asking the cache manager
88 * for 'comm_incoming', e.g.:
90 * % ./client mgr:comm_incoming
94 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
95 * incoming_interval for both types of sockets. At the
96 * largest value the cache will effectively be idling.
98 * - The higher the INCOMING_FACTOR, the slower the algorithm will
99 * respond to load spikes/increases/decreases in demand. A value
100 * between 3 and 8 is recommended.
103 #define MAX_INCOMING_INTEGER 256
104 #define INCOMING_FACTOR 5
105 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
106 static int udp_io_events
= 0;
107 static int dns_io_events
= 0;
108 static int tcp_io_events
= 0;
109 static int incoming_udp_interval
= 16 << INCOMING_FACTOR
;
110 static int incoming_dns_interval
= 16 << INCOMING_FACTOR
;
111 static int incoming_tcp_interval
= 16 << INCOMING_FACTOR
;
112 #define commCheckUdpIncoming (++udp_io_events > (incoming_udp_interval>> INCOMING_FACTOR))
113 #define commCheckDnsIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
114 #define commCheckTcpIncoming (++tcp_io_events > (incoming_tcp_interval>> INCOMING_FACTOR))
117 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
119 fde
*F
= &fd_table
[fd
];
121 assert(F
->flags
.open
);
122 debugs(5, 5, HERE
<< "FD " << fd
<< ", type=" << type
<<
123 ", handler=" << handler
<< ", client_data=" << client_data
<<
124 ", timeout=" << timeout
);
126 if (type
& COMM_SELECT_READ
) {
127 F
->read_handler
= handler
;
128 F
->read_data
= client_data
;
129 commUpdateReadBits(fd
, handler
);
132 if (type
& COMM_SELECT_WRITE
) {
133 F
->write_handler
= handler
;
134 F
->write_data
= client_data
;
135 commUpdateWriteBits(fd
, handler
);
139 F
->timeout
= squid_curtime
+ timeout
;
143 Comm::ResetSelect(int fd
)
148 fdIsUdpListener(int fd
)
150 if (icpIncomingConn
!= NULL
&& fd
== icpIncomingConn
->fd
)
153 if (icpOutgoingConn
!= NULL
&& fd
== icpOutgoingConn
->fd
)
162 if (fd
== DnsSocketA
)
165 if (fd
== DnsSocketB
)
172 fdIsTcpListener(int fd
)
174 for (AnyP::PortCfgPointer s
= HttpPortList
; s
!= NULL
; s
= s
->next
) {
175 if (s
->listenConn
!= NULL
&& s
->listenConn
->fd
== fd
)
183 comm_check_incoming_select_handlers(int nfds
, int *fds
)
194 FD_ZERO(&write_mask
);
195 incoming_sockets_accepted
= 0;
197 for (i
= 0; i
< nfds
; ++i
) {
200 if (fd_table
[fd
].read_handler
) {
201 FD_SET(fd
, &read_mask
);
207 if (fd_table
[fd
].write_handler
) {
208 FD_SET(fd
, &write_mask
);
220 ++ statCounter
.syscalls
.selects
;
222 if (select(maxfd
, &read_mask
, &write_mask
, &errfds
, &zero_tv
) < 1)
224 return incoming_sockets_accepted
;
226 for (i
= 0; i
< nfds
; ++i
) {
229 if (FD_ISSET(fd
, &read_mask
)) {
230 if ((hdl
= fd_table
[fd
].read_handler
) != NULL
) {
231 fd_table
[fd
].read_handler
= NULL
;
232 commUpdateReadBits(fd
, NULL
);
233 hdl(fd
, fd_table
[fd
].read_data
);
235 debugs(5, DBG_IMPORTANT
, "comm_select_incoming: FD " << fd
<< " NULL read handler");
239 if (FD_ISSET(fd
, &write_mask
)) {
240 if ((hdl
= fd_table
[fd
].write_handler
) != NULL
) {
241 fd_table
[fd
].write_handler
= NULL
;
242 commUpdateWriteBits(fd
, NULL
);
243 hdl(fd
, fd_table
[fd
].write_data
);
245 debugs(5, DBG_IMPORTANT
, "comm_select_incoming: FD " << fd
<< " NULL write handler");
250 return incoming_sockets_accepted
;
254 comm_select_udp_incoming(void)
261 if (Comm::IsConnOpen(icpIncomingConn
)) {
262 fds
[nfds
] = icpIncomingConn
->fd
;
266 if (Comm::IsConnOpen(icpOutgoingConn
) && icpIncomingConn
!= icpOutgoingConn
) {
267 fds
[nfds
] = icpOutgoingConn
->fd
;
274 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
276 incoming_udp_interval
+= Config
.comm_incoming
.udp_average
- nevents
;
278 if (incoming_udp_interval
< 0)
279 incoming_udp_interval
= 0;
281 if (incoming_udp_interval
> MAX_INCOMING_INTERVAL
)
282 incoming_udp_interval
= MAX_INCOMING_INTERVAL
;
284 if (nevents
> INCOMING_UDP_MAX
)
285 nevents
= INCOMING_UDP_MAX
;
287 statCounter
.comm_udp_incoming
.count(nevents
);
291 comm_select_tcp_incoming(void)
294 int fds
[MAXTCPLISTENPORTS
];
298 // XXX: only poll sockets that won't be deferred. But how do we identify them?
300 for (AnyP::PortCfgPointer s
= HttpPortList
; s
!= NULL
; s
= s
->next
) {
301 if (Comm::IsConnOpen(s
->listenConn
)) {
302 fds
[nfds
] = s
->listenConn
->fd
;
307 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
308 incoming_tcp_interval
+= Config
.comm_incoming
.tcp_average
- nevents
;
310 if (incoming_tcp_interval
< 0)
311 incoming_tcp_interval
= 0;
313 if (incoming_tcp_interval
> MAX_INCOMING_INTERVAL
)
314 incoming_tcp_interval
= MAX_INCOMING_INTERVAL
;
316 if (nevents
> INCOMING_TCP_MAX
)
317 nevents
= INCOMING_TCP_MAX
;
319 statCounter
.comm_tcp_incoming
.count(nevents
);
322 #define DEBUG_FDBITS 0
323 /* Select on all sockets; call handlers for those that are ready. */
325 Comm::DoSelect(int msec
)
336 int calldns
= 0, callicp
= 0, callhttp
= 0;
342 struct timeval poll_time
;
343 double timeout
= current_dtime
+ (msec
/ 1000.0);
353 start
= current_dtime
;
355 if (commCheckUdpIncoming
)
356 comm_select_udp_incoming();
358 if (commCheckDnsIncoming
)
359 comm_select_dns_incoming();
361 if (commCheckTcpIncoming
)
362 comm_select_tcp_incoming();
364 calludp
= calldns
= calltcp
= 0;
366 maxfd
= Biggest_FD
+ 1;
368 memcpy(&readfds
, &global_readfds
, sizeof(global_readfds
));
370 memcpy(&writefds
, &global_writefds
, sizeof(global_writefds
));
372 memcpy(&errfds
, &global_writefds
, sizeof(global_writefds
));
374 /* remove stalled FDs, and deal with pending descriptors */
377 FD_ZERO(&pendingfds
);
379 for (j
= 0; j
< (int) readfds
.fd_count
; ++j
) {
380 register int readfds_handle
= readfds
.fd_array
[j
];
383 for ( fd
= Biggest_FD
; fd
; --fd
) {
384 if ( fd_table
[fd
].win32
.handle
== readfds_handle
) {
385 if (fd_table
[fd
].flags
.open
) {
395 if (FD_ISSET(fd
, &readfds
) && fd_table
[fd
].flags
.read_pending
) {
396 FD_SET(fd
, &pendingfds
);
402 for (i
= 0; i
< maxfd
; ++i
) {
403 /* Check each open socket for a handler. */
405 if (fd_table
[i
].read_handler
) {
406 assert(FD_ISSET(i
, readfds
));
409 if (fd_table
[i
].write_handler
) {
410 assert(FD_ISSET(i
, writefds
));
415 if (nreadfds
+ nwritefds
== 0) {
416 assert(shutting_down
);
417 return Comm::SHUTDOWN
;
420 if (msec
> MAX_POLL_TIME
)
421 msec
= MAX_POLL_TIME
;
427 poll_time
.tv_sec
= msec
/ 1000;
428 poll_time
.tv_usec
= (msec
% 1000) * 1000;
429 ++statCounter
.syscalls
.selects
;
430 num
= select(maxfd
, &readfds
, &writefds
, &errfds
, &poll_time
);
431 ++statCounter
.select_loops
;
433 if (num
>= 0 || pending
> 0)
436 if (ignoreErrno(errno
))
439 debugs(5, DBG_CRITICAL
, "comm_select: select failure: " << xstrerror());
441 examine_select(&readfds
, &writefds
);
443 return Comm::COMM_ERROR
;
448 if (num
< 0 && !pending
)
453 debugs(5, num
? 5 : 8, "comm_select: " << num
<< "+" << pending
<< " FDs ready");
455 statCounter
.select_fds_hist
.count(num
);
457 if (num
== 0 && pending
== 0)
460 /* Scan return fd masks for ready descriptors */
461 assert(readfds
.fd_count
<= (unsigned int) Biggest_FD
);
462 assert(pendingfds
.fd_count
<= (unsigned int) Biggest_FD
);
464 for (j
= 0; j
< (int) readfds
.fd_count
; ++j
) {
465 register int readfds_handle
= readfds
.fd_array
[j
];
466 register int pendingfds_handle
= pendingfds
.fd_array
[j
];
467 register int osfhandle
;
470 for ( fd
= Biggest_FD
; fd
; --fd
) {
471 osfhandle
= fd_table
[fd
].win32
.handle
;
473 if (( osfhandle
== readfds_handle
) ||
474 ( osfhandle
== pendingfds_handle
)) {
475 if (fd_table
[fd
].flags
.open
) {
487 debugs(5, 9, "FD " << fd
<< " bit set for reading");
489 assert(FD_ISSET(fd
, readfds
));
493 if (fdIsUdpListener(fd
)) {
503 if (fdIsTcpListener(fd
)) {
509 debugs(5, 6, "comm_select: FD " << fd
<< " ready for reading");
511 if ((hdl
= F
->read_handler
)) {
512 F
->read_handler
= NULL
;
513 F
->flags
.read_pending
= 0;
514 commUpdateReadBits(fd
, NULL
);
515 hdl(fd
, F
->read_data
);
516 ++ statCounter
.select_fds
;
518 if (commCheckUdpIncoming
)
519 comm_select_udp_incoming();
521 if (commCheckDnsIncoming
)
522 comm_select_dns_incoming();
524 if (commCheckTcpIncoming
)
525 comm_select_tcp_incoming();
529 assert(errfds
.fd_count
<= (unsigned int) Biggest_FD
);
531 for (j
= 0; j
< (int) errfds
.fd_count
; ++j
) {
532 register int errfds_handle
= errfds
.fd_array
[j
];
534 for ( fd
= Biggest_FD
; fd
; --fd
) {
535 if ( fd_table
[fd
].win32
.handle
== errfds_handle
)
539 if (fd_table
[fd
].flags
.open
) {
542 if ((hdl
= F
->write_handler
)) {
543 F
->write_handler
= NULL
;
544 commUpdateWriteBits(fd
, NULL
);
545 hdl(fd
, F
->write_data
);
546 ++ statCounter
.select_fds
;
551 assert(writefds
.fd_count
<= (unsigned int) Biggest_FD
);
553 for (j
= 0; j
< (int) writefds
.fd_count
; ++j
) {
554 register int writefds_handle
= writefds
.fd_array
[j
];
557 for ( fd
= Biggest_FD
; fd
; --fd
) {
558 if ( fd_table
[fd
].win32
.handle
== writefds_handle
) {
559 if (fd_table
[fd
].flags
.open
) {
571 debugs(5, 9, "FD " << fd
<< " bit set for writing");
573 assert(FD_ISSET(fd
, writefds
));
577 if (fdIsUdpListener(fd
)) {
587 if (fdIsTcpListener(fd
)) {
593 debugs(5, 6, "comm_select: FD " << fd
<< " ready for writing");
595 if ((hdl
= F
->write_handler
)) {
596 F
->write_handler
= NULL
;
597 commUpdateWriteBits(fd
, NULL
);
598 hdl(fd
, F
->write_data
);
599 ++ statCounter
.select_fds
;
601 if (commCheckUdpIncoming
)
602 comm_select_udp_incoming();
604 if (commCheckDnsIncoming
)
605 comm_select_dns_incoming();
607 if (commCheckTcpIncoming
)
608 comm_select_tcp_incoming();
613 comm_select_udp_incoming();
616 comm_select_dns_incoming();
619 comm_select_tcp_incoming();
623 statCounter
.select_time
+= (current_dtime
- start
);
626 } while (timeout
> current_dtime
);
627 debugs(5, 8, "comm_select: time out: " << squid_curtime
);
629 return Comm::TIMEOUT
;
633 comm_select_dns_incoming(void)
640 if (DnsSocketA
< 0 && DnsSocketB
< 0)
643 if (DnsSocketA
>= 0) {
644 fds
[nfds
] = DnsSocketA
;
648 if (DnsSocketB
>= 0) {
649 fds
[nfds
] = DnsSocketB
;
653 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
658 incoming_dns_interval
+= Config
.comm_incoming
.dns
.average
- nevents
;
660 if (incoming_dns_interval
< Config
.comm_incoming
.dns
.min_poll
)
661 incoming_dns_interval
= Config
.comm_incoming
.dns
.min_poll
;
663 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
664 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
666 if (nevents
> INCOMING_DNS_MAX
)
667 nevents
= INCOMING_DNS_MAX
;
669 statCounter
.comm_dns_incoming
.count(nevents
);
673 Comm::SelectLoopInit(void)
677 FD_ZERO(&global_readfds
);
678 FD_ZERO(&global_writefds
);
679 nreadfds
= nwritefds
= 0;
681 Mgr::RegisterAction("comm_select_incoming",
682 "comm_incoming() stats",
683 commIncomingStats
, 0, 1);
687 * examine_select - debug routine.
689 * I spend the day chasing this core dump that occurs when both the client
690 * and the server side of a cache fetch simultaneoulsy abort the
691 * connection. While I haven't really studied the code to figure out how
692 * it happens, the snippet below may prevent the cache from exitting:
694 * Call this from where the select loop fails.
697 examine_select(fd_set
* readfds
, fd_set
* writefds
)
704 AsyncCall::Pointer ch
= NULL
;
708 debugs(5, DBG_CRITICAL
, "examine_select: Examining open file descriptors...");
710 for (fd
= 0; fd
< Squid_MaxFD
; ++fd
) {
713 tv
.tv_sec
= tv
.tv_usec
= 0;
715 if (FD_ISSET(fd
, readfds
))
717 else if (FD_ISSET(fd
, writefds
))
718 FD_SET(fd
, &write_x
);
722 ++ statCounter
.syscalls
.selects
;
725 if (!fstat(fd
, &sb
)) {
726 debugs(5, 5, "FD " << fd
<< " is valid.");
731 debugs(5, DBG_CRITICAL
, "FD " << fd
<< ": " << xstrerror());
732 debugs(5, DBG_CRITICAL
, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
733 debugs(5, DBG_CRITICAL
, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
] << " called '" << F
->desc
<< "'");
734 debugs(5, DBG_CRITICAL
, "tmout:" << F
->timeoutHandler
<< " read:" << F
->read_handler
<< " write:" << F
->write_handler
);
736 for (ch
= F
->closeHandler
; ch
!= NULL
; ch
= ch
->Next())
737 debugs(5, DBG_CRITICAL
, " close handler: " << ch
);
739 if (F
->closeHandler
!= NULL
) {
740 commCallCloseHandlers(fd
);
741 } else if (F
->timeoutHandler
!= NULL
) {
742 debugs(5, DBG_CRITICAL
, "examine_select: Calling Timeout Handler");
743 ScheduleCallHere(F
->timeoutHandler
);
746 F
->closeHandler
= NULL
;
747 F
->timeoutHandler
= NULL
;
748 F
->read_handler
= NULL
;
749 F
->write_handler
= NULL
;
751 FD_CLR(fd
, writefds
);
758 commIncomingStats(StoreEntry
* sentry
)
760 storeAppendPrintf(sentry
, "Current incoming_udp_interval: %d\n",
761 incoming_udp_interval
>> INCOMING_FACTOR
);
762 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
763 incoming_dns_interval
>> INCOMING_FACTOR
);
764 storeAppendPrintf(sentry
, "Current incoming_tcp_interval: %d\n",
765 incoming_tcp_interval
>> INCOMING_FACTOR
);
766 storeAppendPrintf(sentry
, "\n");
767 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
768 storeAppendPrintf(sentry
, "ICP Messages handled per comm_select_udp_incoming() call:\n");
769 statCounter
.comm_udp_incoming
.dump(sentry
, statHistIntDumper
);
770 storeAppendPrintf(sentry
, "DNS Messages handled per comm_select_dns_incoming() call:\n");
771 statCounter
.comm_dns_incoming
.dump(sentry
, statHistIntDumper
);
772 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_select_tcp_incoming() call:\n");
773 statCounter
.comm_tcp_incoming
.dump(sentry
, statHistIntDumper
);
777 commUpdateReadBits(int fd
, PF
* handler
)
779 if (handler
&& !FD_ISSET(fd
, &global_readfds
)) {
780 FD_SET(fd
, &global_readfds
);
782 } else if (!handler
&& FD_ISSET(fd
, &global_readfds
)) {
783 FD_CLR(fd
, &global_readfds
);
789 commUpdateWriteBits(int fd
, PF
* handler
)
791 if (handler
&& !FD_ISSET(fd
, &global_writefds
)) {
792 FD_SET(fd
, &global_writefds
);
794 } else if (!handler
&& FD_ISSET(fd
, &global_writefds
)) {
795 FD_CLR(fd
, &global_writefds
);
800 /* Called by async-io or diskd to speed up the polling */
802 Comm::QuickPollRequired(void)
807 #endif /* USE_SELECT_WIN32 */