4 * DEBUG: section 05 Socket Functions
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
38 #include "squid-old.h"
39 #include "comm/Loops.h"
41 #include "mgr/Registration.h"
42 #include "SquidTime.h"
43 #include "StatCounters.h"
47 static int MAX_POLL_TIME
= 1000; /* see also Comm::QuickPollRequired() */
50 #define howmany(x, y) (((x)+((y)-1))/(y))
55 #define FD_MASK_BYTES sizeof(fd_mask)
56 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
59 static int examine_select(fd_set
*, fd_set
*);
60 static int fdIsHttp(int fd
);
61 static int fdIsIcp(int fd
);
62 static int fdIsDns(int fd
);
63 static OBJH commIncomingStats
;
64 static int comm_check_incoming_select_handlers(int nfds
, int *fds
);
65 static void comm_select_dns_incoming(void);
66 static void commUpdateReadBits(int fd
, PF
* handler
);
67 static void commUpdateWriteBits(int fd
, PF
* handler
);
70 static struct timeval zero_tv
;
71 static fd_set global_readfds
;
72 static fd_set global_writefds
;
77 * Automatic tuning for incoming requests:
79 * INCOMING sockets are the ICP and HTTP ports. We need to check these
80 * fairly regularly, but how often? When the load increases, we
81 * want to check the incoming sockets more often. If we have a lot
82 * of incoming ICP, then we need to check these sockets more than
83 * if we just have HTTP.
85 * The variables 'incoming_icp_interval' and 'incoming_http_interval'
86 * determine how many normal I/O events to process before checking
87 * incoming sockets again. Note we store the incoming_interval
88 * multipled by a factor of (2^INCOMING_FACTOR) to have some
89 * pseudo-floating point precision.
91 * The variable 'icp_io_events' and 'http_io_events' counts how many normal
92 * I/O events have been processed since the last check on the incoming
93 * sockets. When io_events > incoming_interval, its time to check incoming
96 * Every time we check incoming sockets, we count how many new messages
97 * or connections were processed. This is used to adjust the
98 * incoming_interval for the next iteration. The new incoming_interval
99 * is calculated as the current incoming_interval plus what we would
100 * like to see as an average number of events minus the number of
101 * events just processed.
103 * incoming_interval = incoming_interval + target_average - number_of_events_processed
105 * There are separate incoming_interval counters for both HTTP and ICP events
107 * You can see the current values of the incoming_interval's, as well as
108 * a histogram of 'incoming_events' by asking the cache manager
109 * for 'comm_incoming', e.g.:
111 * % ./client mgr:comm_incoming
115 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
116 * incoming_interval for both types of sockets. At the
117 * largest value the cache will effectively be idling.
119 * - The higher the INCOMING_FACTOR, the slower the algorithm will
120 * respond to load spikes/increases/decreases in demand. A value
121 * between 3 and 8 is recommended.
124 #define MAX_INCOMING_INTEGER 256
125 #define INCOMING_FACTOR 5
126 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
127 static int icp_io_events
= 0;
128 static int dns_io_events
= 0;
129 static int http_io_events
= 0;
130 static int incoming_icp_interval
= 16 << INCOMING_FACTOR
;
131 static int incoming_dns_interval
= 16 << INCOMING_FACTOR
;
132 static int incoming_http_interval
= 16 << INCOMING_FACTOR
;
133 #define commCheckICPIncoming (++icp_io_events > (incoming_icp_interval>> INCOMING_FACTOR))
134 #define commCheckDNSIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
135 #define commCheckHTTPIncoming (++http_io_events > (incoming_http_interval>> INCOMING_FACTOR))
138 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
140 fde
*F
= &fd_table
[fd
];
142 assert(F
->flags
.open
);
143 debugs(5, 5, HERE
<< "FD " << fd
<< ", type=" << type
<<
144 ", handler=" << handler
<< ", client_data=" << client_data
<<
145 ", timeout=" << timeout
);
147 if (type
& COMM_SELECT_READ
) {
148 F
->read_handler
= handler
;
149 F
->read_data
= client_data
;
150 commUpdateReadBits(fd
, handler
);
153 if (type
& COMM_SELECT_WRITE
) {
154 F
->write_handler
= handler
;
155 F
->write_data
= client_data
;
156 commUpdateWriteBits(fd
, handler
);
160 F
->timeout
= squid_curtime
+ timeout
;
164 Comm::ResetSelect(int fd
)
171 if (fd
== theInIcpConnection
)
174 if (fd
== theOutIcpConnection
)
183 if (fd
== DnsSocketA
)
186 if (fd
== DnsSocketB
)
197 for (j
= 0; j
< NHttpSockets
; j
++) {
198 if (fd
== HttpSockets
[j
])
206 comm_check_incoming_select_handlers(int nfds
, int *fds
)
217 FD_ZERO(&write_mask
);
218 incoming_sockets_accepted
= 0;
220 for (i
= 0; i
< nfds
; i
++) {
223 if (fd_table
[fd
].read_handler
) {
224 FD_SET(fd
, &read_mask
);
230 if (fd_table
[fd
].write_handler
) {
231 FD_SET(fd
, &write_mask
);
243 statCounter
.syscalls
.selects
++;
245 if (select(maxfd
, &read_mask
, &write_mask
, &errfds
, &zero_tv
) < 1)
247 return incoming_sockets_accepted
;
249 for (i
= 0; i
< nfds
; i
++) {
252 if (__WSAFDIsSet(fd_table
[fd
].win32
.handle
, &read_mask
)) {
253 if ((hdl
= fd_table
[fd
].read_handler
) != NULL
) {
254 fd_table
[fd
].read_handler
= NULL
;
255 commUpdateReadBits(fd
, NULL
);
256 hdl(fd
, fd_table
[fd
].read_data
);
258 debugs(5, 1, "comm_select_incoming: FD " << fd
<< " NULL read handler");
262 if (__WSAFDIsSet(fd_table
[fd
].win32
.handle
, &write_mask
)) {
263 if ((hdl
= fd_table
[fd
].write_handler
) != NULL
) {
264 fd_table
[fd
].write_handler
= NULL
;
265 commUpdateWriteBits(fd
, NULL
);
266 hdl(fd
, fd_table
[fd
].write_data
);
268 debugs(5, 1, "comm_select_incoming: FD " << fd
<< " NULL write handler");
273 return incoming_sockets_accepted
;
277 comm_select_icp_incoming(void)
284 if (theInIcpConnection
>= 0)
285 fds
[nfds
++] = theInIcpConnection
;
287 if (theInIcpConnection
!= theOutIcpConnection
)
288 if (theOutIcpConnection
>= 0)
289 fds
[nfds
++] = theOutIcpConnection
;
294 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
296 incoming_icp_interval
+= Config
.comm_incoming
.icp_average
- nevents
;
298 if (incoming_icp_interval
< 0)
299 incoming_icp_interval
= 0;
301 if (incoming_icp_interval
> MAX_INCOMING_INTERVAL
)
302 incoming_icp_interval
= MAX_INCOMING_INTERVAL
;
304 if (nevents
> INCOMING_ICP_MAX
)
305 nevents
= INCOMING_ICP_MAX
;
307 statCounter
.comm_icp_incoming
.count(nevents
);
311 comm_select_http_incoming(void)
314 int fds
[MAXHTTPPORTS
];
319 for (j
= 0; j
< NHttpSockets
; j
++) {
320 if (HttpSockets
[j
] < 0)
323 fds
[nfds
++] = HttpSockets
[j
];
326 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
327 incoming_http_interval
+= Config
.comm_incoming
.http_average
- nevents
;
329 if (incoming_http_interval
< 0)
330 incoming_http_interval
= 0;
332 if (incoming_http_interval
> MAX_INCOMING_INTERVAL
)
333 incoming_http_interval
= MAX_INCOMING_INTERVAL
;
335 if (nevents
> INCOMING_HTTP_MAX
)
336 nevents
= INCOMING_HTTP_MAX
;
338 statCounter
.comm_http_incoming
.count(nevents
);
341 #define DEBUG_FDBITS 0
342 /* Select on all sockets; call handlers for those that are ready. */
344 Comm::DoSelect(int msec
)
355 int callicp
= 0, callhttp
= 0;
362 struct timeval poll_time
;
363 double timeout
= current_dtime
+ (msec
/ 1000.0);
373 start
= current_dtime
;
375 if (commCheckICPIncoming
)
376 comm_select_icp_incoming();
378 if (commCheckDNSIncoming
)
379 comm_select_dns_incoming();
381 if (commCheckHTTPIncoming
)
382 comm_select_http_incoming();
384 callicp
= calldns
= callhttp
= 0;
386 maxfd
= Biggest_FD
+ 1;
388 memcpy(&readfds
, &global_readfds
, sizeof(global_readfds
));
390 memcpy(&writefds
, &global_writefds
, sizeof(global_writefds
));
392 memcpy(&errfds
, &global_writefds
, sizeof(global_writefds
));
394 /* remove stalled FDs, and deal with pending descriptors */
397 FD_ZERO(&pendingfds
);
399 for (j
= 0; j
< (int) readfds
.fd_count
; j
++) {
400 register int readfds_handle
= readfds
.fd_array
[j
];
403 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
404 if ( fd_table
[fd
].win32
.handle
== readfds_handle
) {
405 if (fd_table
[fd
].flags
.open
) {
415 if (__WSAFDIsSet(fd_table
[fd
].win32
.handle
, &readfds
) && fd_table
[fd
].flags
.read_pending
) {
416 FD_SET(fd
, &pendingfds
);
422 for (i
= 0; i
< maxfd
; i
++) {
423 /* Check each open socket for a handler. */
425 if (fd_table
[i
].read_handler
) {
426 assert(__WSAFDIsSet(fd_table
[i
].win32
.handle
, readfds
));
429 if (fd_table
[i
].write_handler
) {
430 assert(__WSAFDIsSet(fd_table
[i
].win32
.handle
, writefds
));
435 if (nreadfds
+ nwritefds
== 0) {
436 assert(shutting_down
);
437 return COMM_SHUTDOWN
;
440 if (msec
> MAX_POLL_TIME
)
441 msec
= MAX_POLL_TIME
;
447 poll_time
.tv_sec
= msec
/ 1000;
448 poll_time
.tv_usec
= (msec
% 1000) * 1000;
449 ++statCounter
.syscalls
.selects
;
450 num
= select(maxfd
, &readfds
, &writefds
, &errfds
, &poll_time
);
451 ++statCounter
.select_loops
;
453 if (num
>= 0 || pending
> 0)
456 if (ignoreErrno(errno
))
459 debugs(5, 0, "comm_select: select failure: " << xstrerror());
461 examine_select(&readfds
, &writefds
);
468 if (num
< 0 && !pending
)
473 debugs(5, num
? 5 : 8, "comm_select: " << num
<< "+" << pending
<< " FDs ready");
475 statCounter
.select_fds_hist
.count(num
);
477 if (num
== 0 && pending
== 0)
480 /* Scan return fd masks for ready descriptors */
481 assert(readfds
.fd_count
<= (unsigned int) Biggest_FD
);
482 assert(pendingfds
.fd_count
<= (unsigned int) Biggest_FD
);
484 for (j
= 0; j
< (int) readfds
.fd_count
; j
++) {
485 register int readfds_handle
= readfds
.fd_array
[j
];
486 register int pendingfds_handle
= pendingfds
.fd_array
[j
];
487 register int osfhandle
;
490 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
491 osfhandle
= fd_table
[fd
].win32
.handle
;
493 if (( osfhandle
== readfds_handle
) ||
494 ( osfhandle
== pendingfds_handle
)) {
495 if (fd_table
[fd
].flags
.open
) {
507 debugs(5, 9, "FD " << fd
<< " bit set for reading");
509 assert(__WSAFDIsSet(fd_table
[fd
].win32
.handle
, readfds
));
529 debugs(5, 6, "comm_select: FD " << fd
<< " ready for reading");
531 if (NULL
== (hdl
= F
->read_handler
))
534 F
->read_handler
= NULL
;
535 F
->flags
.read_pending
= 0;
536 commUpdateReadBits(fd
, NULL
);
537 hdl(fd
, F
->read_data
);
538 statCounter
.select_fds
++;
540 if (commCheckICPIncoming
)
541 comm_select_icp_incoming();
543 if (commCheckDNSIncoming
)
544 comm_select_dns_incoming();
546 if (commCheckHTTPIncoming
)
547 comm_select_http_incoming();
551 assert(errfds
.fd_count
<= (unsigned int) Biggest_FD
);
553 for (j
= 0; j
< (int) errfds
.fd_count
; j
++) {
554 register int errfds_handle
= errfds
.fd_array
[j
];
556 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
557 if ( fd_table
[fd
].win32
.handle
== errfds_handle
)
561 if (fd_table
[fd
].flags
.open
) {
564 if ((hdl
= F
->write_handler
)) {
565 F
->write_handler
= NULL
;
566 commUpdateWriteBits(fd
, NULL
);
567 hdl(fd
, F
->write_data
);
568 statCounter
.select_fds
++;
573 assert(writefds
.fd_count
<= (unsigned int) Biggest_FD
);
575 for (j
= 0; j
< (int) writefds
.fd_count
; j
++) {
576 register int writefds_handle
= writefds
.fd_array
[j
];
579 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
580 if ( fd_table
[fd
].win32
.handle
== writefds_handle
) {
581 if (fd_table
[fd
].flags
.open
) {
593 debugs(5, 9, "FD " << fd
<< " bit set for writing");
595 assert(__WSAFDIsSet(fd_table
[fd
].win32
.handle
, writefds
));
615 debugs(5, 6, "comm_select: FD " << fd
<< " ready for writing");
617 if ((hdl
= F
->write_handler
)) {
618 F
->write_handler
= NULL
;
619 commUpdateWriteBits(fd
, NULL
);
620 hdl(fd
, F
->write_data
);
621 statCounter
.select_fds
++;
623 if (commCheckICPIncoming
)
624 comm_select_icp_incoming();
626 if (commCheckDNSIncoming
)
627 comm_select_dns_incoming();
629 if (commCheckHTTPIncoming
)
630 comm_select_http_incoming();
635 comm_select_icp_incoming();
638 comm_select_dns_incoming();
641 comm_select_http_incoming();
645 statCounter
.select_time
+= (current_dtime
- start
);
648 } while (timeout
> current_dtime
);
649 debugs(5, 8, "comm_select: time out: " << squid_curtime
);
655 comm_select_dns_incoming(void)
662 if (DnsSocketA
< 0 && DnsSocketB
< 0)
666 fds
[nfds
++] = DnsSocketA
;
669 fds
[nfds
++] = DnsSocketB
;
671 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
676 incoming_dns_interval
+= Config
.comm_incoming
.dns_average
- nevents
;
678 if (incoming_dns_interval
< Config
.comm_incoming
.dns_min_poll
)
679 incoming_dns_interval
= Config
.comm_incoming
.dns_min_poll
;
681 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
682 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
684 if (nevents
> INCOMING_DNS_MAX
)
685 nevents
= INCOMING_DNS_MAX
;
687 statCounter
.comm_dns_incoming
.count(nevents
);
691 Comm::SelectLoopInit(void)
695 FD_ZERO(&global_readfds
);
696 FD_ZERO(&global_writefds
);
697 nreadfds
= nwritefds
= 0;
699 Mgr::RegisterAction("comm_select_incoming",
700 "comm_incoming() stats",
701 commIncomingStats
, 0, 1);
705 * examine_select - debug routine.
707 * I spend the day chasing this core dump that occurs when both the client
708 * and the server side of a cache fetch simultaneoulsy abort the
709 * connection. While I haven't really studied the code to figure out how
710 * it happens, the snippet below may prevent the cache from exitting:
712 * Call this from where the select loop fails.
715 examine_select(fd_set
* readfds
, fd_set
* writefds
)
722 AsyncCall::Pointer ch
= NULL
;
726 debugs(5, 0, "examine_select: Examining open file descriptors...");
728 for (fd
= 0; fd
< Squid_MaxFD
; fd
++) {
731 tv
.tv_sec
= tv
.tv_usec
= 0;
733 if (__WSAFDIsSet(fd_table
[fd
].win32
.handle
, readfds
))
735 else if (__WSAFDIsSet(fd_table
[fd
].win32
.handle
, writefds
))
736 FD_SET(fd
, &write_x
);
740 statCounter
.syscalls
.selects
++;
743 if (!fstat(fd
, &sb
)) {
744 debugs(5, 5, "FD " << fd
<< " is valid.");
749 debugs(5, 0, "FD " << fd
<< ": " << xstrerror());
750 debugs(5, 0, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
751 debugs(5, 0, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
] << " called '" << F
->desc
<< "'");
752 debugs(5, 0, "tmout:" << F
->timeoutHandler
<< " read:" << F
->read_handler
<< " write:" << F
->write_handler
);
754 for (ch
= F
->closeHandler
; ch
!= NULL
; ch
= ch
->Next())
755 debugs(5, 0, " close handler: " << ch
);
757 if (F
->closeHandler
!= NULL
) {
758 commCallCloseHandlers(fd
);
759 } else if (F
->timeoutHandler
!= NULL
) {
760 debugs(5, 0, "examine_select: Calling Timeout Handler");
761 ScheduleCallHere(F
->timeoutHandler
);
764 F
->closeHandler
= NULL
;
765 F
->timeoutHandler
= NULL
;
766 F
->read_handler
= NULL
;
767 F
->write_handler
= NULL
;
769 FD_CLR(fd
, writefds
);
777 commIncomingStats(StoreEntry
* sentry
)
779 storeAppendPrintf(sentry
, "Current incoming_icp_interval: %d\n",
780 incoming_icp_interval
>> INCOMING_FACTOR
);
781 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
782 incoming_dns_interval
>> INCOMING_FACTOR
);
783 storeAppendPrintf(sentry
, "Current incoming_http_interval: %d\n",
784 incoming_http_interval
>> INCOMING_FACTOR
);
785 storeAppendPrintf(sentry
, "\n");
786 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
787 storeAppendPrintf(sentry
, "ICP Messages handled per comm_select_icp_incoming() call:\n");
788 statCounter
.comm_icp_incoming
.dump(sentry
, statHistIntDumper
);
789 storeAppendPrintf(sentry
, "DNS Messages handled per comm_select_dns_incoming() call:\n");
790 statCounter
.comm_dns_incoming
.dump(sentry
, statHistIntDumper
);
791 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_select_http_incoming() call:\n");
792 statCounter
.comm_http_incoming
.dump(sentry
, statHistIntDumper
);
796 commUpdateReadBits(int fd
, PF
* handler
)
798 if (handler
&& !__WSAFDIsSet(fd_table
[fd
].win32
.handle
, &global_readfds
)) {
799 FD_SET(fd
, &global_readfds
);
801 } else if (!handler
&& __WSAFDIsSet(fd_table
[fd
].win32
.handle
, &global_readfds
)) {
802 FD_CLR(fd
, &global_readfds
);
808 commUpdateWriteBits(int fd
, PF
* handler
)
810 if (handler
&& !__WSAFDIsSet(fd_table
[fd
].win32
.handle
, &global_writefds
)) {
811 FD_SET(fd
, &global_writefds
);
813 } else if (!handler
&& __WSAFDIsSet(fd_table
[fd
].win32
.handle
, &global_writefds
)) {
814 FD_CLR(fd
, &global_writefds
);
819 /* Called by async-io or diskd to speed up the polling */
821 Comm::QuickPollRequired(void)
826 #endif /* USE_SELECT_WIN32 */