2 * Copyright (C) 1996-2015 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
)
192 FD_ZERO(&write_mask
);
193 incoming_sockets_accepted
= 0;
195 for (i
= 0; i
< nfds
; ++i
) {
198 if (fd_table
[fd
].read_handler
) {
199 FD_SET(fd
, &read_mask
);
205 if (fd_table
[fd
].write_handler
) {
206 FD_SET(fd
, &write_mask
);
218 ++ statCounter
.syscalls
.selects
;
220 if (select(maxfd
, &read_mask
, &write_mask
, NULL
, &zero_tv
) < 1)
221 return incoming_sockets_accepted
;
223 for (i
= 0; i
< nfds
; ++i
) {
226 if (FD_ISSET(fd
, &read_mask
)) {
227 if ((hdl
= fd_table
[fd
].read_handler
) != NULL
) {
228 fd_table
[fd
].read_handler
= NULL
;
229 commUpdateReadBits(fd
, NULL
);
230 hdl(fd
, fd_table
[fd
].read_data
);
232 debugs(5, DBG_IMPORTANT
, "comm_select_incoming: FD " << fd
<< " NULL read handler");
236 if (FD_ISSET(fd
, &write_mask
)) {
237 if ((hdl
= fd_table
[fd
].write_handler
) != NULL
) {
238 fd_table
[fd
].write_handler
= NULL
;
239 commUpdateWriteBits(fd
, NULL
);
240 hdl(fd
, fd_table
[fd
].write_data
);
242 debugs(5, DBG_IMPORTANT
, "comm_select_incoming: FD " << fd
<< " NULL write handler");
247 return incoming_sockets_accepted
;
251 comm_select_udp_incoming(void)
258 if (Comm::IsConnOpen(icpIncomingConn
)) {
259 fds
[nfds
] = icpIncomingConn
->fd
;
263 if (Comm::IsConnOpen(icpOutgoingConn
) && icpIncomingConn
!= icpOutgoingConn
) {
264 fds
[nfds
] = icpOutgoingConn
->fd
;
271 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
273 incoming_udp_interval
+= Config
.comm_incoming
.udp
.average
- nevents
;
275 if (incoming_udp_interval
< 0)
276 incoming_udp_interval
= 0;
278 if (incoming_udp_interval
> MAX_INCOMING_INTERVAL
)
279 incoming_udp_interval
= MAX_INCOMING_INTERVAL
;
281 if (nevents
> INCOMING_UDP_MAX
)
282 nevents
= INCOMING_UDP_MAX
;
284 statCounter
.comm_udp_incoming
.count(nevents
);
288 comm_select_tcp_incoming(void)
291 int fds
[MAXTCPLISTENPORTS
];
295 // XXX: only poll sockets that won't be deferred. But how do we identify them?
297 for (AnyP::PortCfgPointer s
= HttpPortList
; s
!= NULL
; s
= s
->next
) {
298 if (Comm::IsConnOpen(s
->listenConn
)) {
299 fds
[nfds
] = s
->listenConn
->fd
;
304 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
305 incoming_tcp_interval
+= Config
.comm_incoming
.tcp
.average
- nevents
;
307 if (incoming_tcp_interval
< 0)
308 incoming_tcp_interval
= 0;
310 if (incoming_tcp_interval
> MAX_INCOMING_INTERVAL
)
311 incoming_tcp_interval
= MAX_INCOMING_INTERVAL
;
313 if (nevents
> INCOMING_TCP_MAX
)
314 nevents
= INCOMING_TCP_MAX
;
316 statCounter
.comm_tcp_incoming
.count(nevents
);
319 #define DEBUG_FDBITS 0
320 /* Select on all sockets; call handlers for those that are ready. */
322 Comm::DoSelect(int msec
)
333 int calldns
= 0, calludp
= 0, calltcp
= 0;
339 struct timeval poll_time
;
340 double timeout
= current_dtime
+ (msec
/ 1000.0);
350 start
= current_dtime
;
352 if (commCheckUdpIncoming
)
353 comm_select_udp_incoming();
355 if (commCheckDnsIncoming
)
356 comm_select_dns_incoming();
358 if (commCheckTcpIncoming
)
359 comm_select_tcp_incoming();
361 calldns
= calludp
= calltcp
= 0;
363 maxfd
= Biggest_FD
+ 1;
365 memcpy(&readfds
, &global_readfds
, sizeof(global_readfds
));
367 memcpy(&writefds
, &global_writefds
, sizeof(global_writefds
));
369 memcpy(&errfds
, &global_writefds
, sizeof(global_writefds
));
371 /* remove stalled FDs, and deal with pending descriptors */
374 FD_ZERO(&pendingfds
);
376 for (j
= 0; j
< (int) readfds
.fd_count
; ++j
) {
377 register int readfds_handle
= readfds
.fd_array
[j
];
380 for ( fd
= Biggest_FD
; fd
; --fd
) {
381 if ( fd_table
[fd
].win32
.handle
== readfds_handle
) {
382 if (fd_table
[fd
].flags
.open
) {
392 if (FD_ISSET(fd
, &readfds
) && fd_table
[fd
].flags
.read_pending
) {
393 FD_SET(fd
, &pendingfds
);
399 for (i
= 0; i
< maxfd
; ++i
) {
400 /* Check each open socket for a handler. */
402 if (fd_table
[i
].read_handler
) {
403 assert(FD_ISSET(i
, readfds
));
406 if (fd_table
[i
].write_handler
) {
407 assert(FD_ISSET(i
, writefds
));
412 if (nreadfds
+ nwritefds
== 0) {
413 assert(shutting_down
);
414 return Comm::SHUTDOWN
;
417 if (msec
> MAX_POLL_TIME
)
418 msec
= MAX_POLL_TIME
;
424 poll_time
.tv_sec
= msec
/ 1000;
425 poll_time
.tv_usec
= (msec
% 1000) * 1000;
426 ++ statCounter
.syscalls
.selects
;
427 num
= select(maxfd
, &readfds
, &writefds
, &errfds
, &poll_time
);
428 ++ statCounter
.select_loops
;
430 if (num
>= 0 || pending
> 0)
433 if (ignoreErrno(errno
))
436 debugs(5, DBG_CRITICAL
, "comm_select: select failure: " << xstrerror());
438 examine_select(&readfds
, &writefds
);
440 return Comm::COMM_ERROR
;
445 if (num
< 0 && !pending
)
450 debugs(5, num
? 5 : 8, "comm_select: " << num
<< "+" << pending
<< " FDs ready");
452 statCounter
.select_fds_hist
.count(num
);
454 if (num
== 0 && pending
== 0)
457 /* Scan return fd masks for ready descriptors */
458 assert(readfds
.fd_count
<= (unsigned int) Biggest_FD
);
459 assert(pendingfds
.fd_count
<= (unsigned int) Biggest_FD
);
461 for (j
= 0; j
< (int) readfds
.fd_count
; ++j
) {
462 register int readfds_handle
= readfds
.fd_array
[j
];
463 register int pendingfds_handle
= pendingfds
.fd_array
[j
];
464 register int osfhandle
;
467 for ( fd
= Biggest_FD
; fd
; --fd
) {
468 osfhandle
= fd_table
[fd
].win32
.handle
;
470 if (( osfhandle
== readfds_handle
) ||
471 ( osfhandle
== pendingfds_handle
)) {
472 if (fd_table
[fd
].flags
.open
) {
484 debugs(5, 9, "FD " << fd
<< " bit set for reading");
486 assert(FD_ISSET(fd
, readfds
));
490 if (fdIsUdpListener(fd
)) {
500 if (fdIsTcpListener(fd
)) {
506 debugs(5, 6, "comm_select: FD " << fd
<< " ready for reading");
508 if ((hdl
= F
->read_handler
)) {
509 F
->read_handler
= NULL
;
510 F
->flags
.read_pending
= 0;
511 commUpdateReadBits(fd
, NULL
);
512 hdl(fd
, F
->read_data
);
513 ++ statCounter
.select_fds
;
515 if (commCheckUdpIncoming
)
516 comm_select_udp_incoming();
518 if (commCheckDnsIncoming
)
519 comm_select_dns_incoming();
521 if (commCheckTcpIncoming
)
522 comm_select_tcp_incoming();
526 assert(errfds
.fd_count
<= (unsigned int) Biggest_FD
);
528 for (j
= 0; j
< (int) errfds
.fd_count
; ++j
) {
529 register int errfds_handle
= errfds
.fd_array
[j
];
531 for ( fd
= Biggest_FD
; fd
; --fd
) {
532 if ( fd_table
[fd
].win32
.handle
== errfds_handle
)
536 if (fd_table
[fd
].flags
.open
) {
539 if ((hdl
= F
->write_handler
)) {
540 F
->write_handler
= NULL
;
541 commUpdateWriteBits(fd
, NULL
);
542 hdl(fd
, F
->write_data
);
543 ++ statCounter
.select_fds
;
548 assert(writefds
.fd_count
<= (unsigned int) Biggest_FD
);
550 for (j
= 0; j
< (int) writefds
.fd_count
; ++j
) {
551 register int writefds_handle
= writefds
.fd_array
[j
];
554 for ( fd
= Biggest_FD
; fd
; --fd
) {
555 if ( fd_table
[fd
].win32
.handle
== writefds_handle
) {
556 if (fd_table
[fd
].flags
.open
) {
568 debugs(5, 9, "FD " << fd
<< " bit set for writing");
570 assert(FD_ISSET(fd
, writefds
));
574 if (fdIsUdpListener(fd
)) {
584 if (fdIsTcpListener(fd
)) {
590 debugs(5, 6, "comm_select: FD " << fd
<< " ready for writing");
592 if ((hdl
= F
->write_handler
)) {
593 F
->write_handler
= NULL
;
594 commUpdateWriteBits(fd
, NULL
);
595 hdl(fd
, F
->write_data
);
596 ++ statCounter
.select_fds
;
598 if (commCheckUdpIncoming
)
599 comm_select_udp_incoming();
601 if (commCheckDnsIncoming
)
602 comm_select_dns_incoming();
604 if (commCheckTcpIncoming
)
605 comm_select_tcp_incoming();
610 comm_select_udp_incoming();
613 comm_select_dns_incoming();
616 comm_select_tcp_incoming();
620 statCounter
.select_time
+= (current_dtime
- start
);
623 } while (timeout
> current_dtime
);
624 debugs(5, 8, "comm_select: time out: " << squid_curtime
);
626 return Comm::TIMEOUT
;
630 comm_select_dns_incoming(void)
637 if (DnsSocketA
< 0 && DnsSocketB
< 0)
640 if (DnsSocketA
>= 0) {
641 fds
[nfds
] = DnsSocketA
;
645 if (DnsSocketB
>= 0) {
646 fds
[nfds
] = DnsSocketB
;
650 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
655 incoming_dns_interval
+= Config
.comm_incoming
.dns
.average
- nevents
;
657 if (incoming_dns_interval
< Config
.comm_incoming
.dns
.min_poll
)
658 incoming_dns_interval
= Config
.comm_incoming
.dns
.min_poll
;
660 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
661 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
663 if (nevents
> INCOMING_DNS_MAX
)
664 nevents
= INCOMING_DNS_MAX
;
666 statCounter
.comm_dns_incoming
.count(nevents
);
670 Comm::SelectLoopInit(void)
674 FD_ZERO(&global_readfds
);
675 FD_ZERO(&global_writefds
);
676 nreadfds
= nwritefds
= 0;
678 Mgr::RegisterAction("comm_select_incoming",
679 "comm_incoming() stats",
680 commIncomingStats
, 0, 1);
684 * examine_select - debug routine.
686 * I spend the day chasing this core dump that occurs when both the client
687 * and the server side of a cache fetch simultaneoulsy abort the
688 * connection. While I haven't really studied the code to figure out how
689 * it happens, the snippet below may prevent the cache from exitting:
691 * Call this from where the select loop fails.
694 examine_select(fd_set
* readfds
, fd_set
* writefds
)
701 AsyncCall::Pointer ch
= NULL
;
705 debugs(5, DBG_CRITICAL
, "examine_select: Examining open file descriptors...");
707 for (fd
= 0; fd
< Squid_MaxFD
; ++fd
) {
710 tv
.tv_sec
= tv
.tv_usec
= 0;
712 if (FD_ISSET(fd
, readfds
))
714 else if (FD_ISSET(fd
, writefds
))
715 FD_SET(fd
, &write_x
);
719 ++ statCounter
.syscalls
.selects
;
722 if (!fstat(fd
, &sb
)) {
723 debugs(5, 5, "FD " << fd
<< " is valid.");
728 debugs(5, DBG_CRITICAL
, "FD " << fd
<< ": " << xstrerror());
729 debugs(5, DBG_CRITICAL
, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
730 debugs(5, DBG_CRITICAL
, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
] << " called '" << F
->desc
<< "'");
731 debugs(5, DBG_CRITICAL
, "tmout:" << F
->timeoutHandler
<< " read:" << F
->read_handler
<< " write:" << F
->write_handler
);
733 for (ch
= F
->closeHandler
; ch
!= NULL
; ch
= ch
->Next())
734 debugs(5, DBG_CRITICAL
, " close handler: " << ch
);
736 if (F
->closeHandler
!= NULL
) {
737 commCallCloseHandlers(fd
);
738 } else if (F
->timeoutHandler
!= NULL
) {
739 debugs(5, DBG_CRITICAL
, "examine_select: Calling Timeout Handler");
740 ScheduleCallHere(F
->timeoutHandler
);
743 F
->closeHandler
= NULL
;
744 F
->timeoutHandler
= NULL
;
745 F
->read_handler
= NULL
;
746 F
->write_handler
= NULL
;
748 FD_CLR(fd
, writefds
);
755 commIncomingStats(StoreEntry
* sentry
)
757 storeAppendPrintf(sentry
, "Current incoming_udp_interval: %d\n",
758 incoming_udp_interval
>> INCOMING_FACTOR
);
759 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
760 incoming_dns_interval
>> INCOMING_FACTOR
);
761 storeAppendPrintf(sentry
, "Current incoming_tcp_interval: %d\n",
762 incoming_tcp_interval
>> INCOMING_FACTOR
);
763 storeAppendPrintf(sentry
, "\n");
764 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
765 storeAppendPrintf(sentry
, "ICP Messages handled per comm_select_udp_incoming() call:\n");
766 statCounter
.comm_udp_incoming
.dump(sentry
, statHistIntDumper
);
767 storeAppendPrintf(sentry
, "DNS Messages handled per comm_select_dns_incoming() call:\n");
768 statCounter
.comm_dns_incoming
.dump(sentry
, statHistIntDumper
);
769 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_select_tcp_incoming() call:\n");
770 statCounter
.comm_tcp_incoming
.dump(sentry
, statHistIntDumper
);
774 commUpdateReadBits(int fd
, PF
* handler
)
776 if (handler
&& !FD_ISSET(fd
, &global_readfds
)) {
777 FD_SET(fd
, &global_readfds
);
779 } else if (!handler
&& FD_ISSET(fd
, &global_readfds
)) {
780 FD_CLR(fd
, &global_readfds
);
786 commUpdateWriteBits(int fd
, PF
* handler
)
788 if (handler
&& !FD_ISSET(fd
, &global_writefds
)) {
789 FD_SET(fd
, &global_writefds
);
791 } else if (!handler
&& FD_ISSET(fd
, &global_writefds
)) {
792 FD_CLR(fd
, &global_writefds
);
797 /* Called by async-io or diskd to speed up the polling */
799 Comm::QuickPollRequired(void)
804 #endif /* USE_SELECT_WIN32 */