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 "comm/Connection.h"
39 #include "comm/Loops.h"
41 #include "mgr/Registration.h"
42 #include "SquidTime.h"
43 #include "StatCounters.h"
48 static int MAX_POLL_TIME
= 1000; /* see also Comm::QuickPollRequired() */
51 #define howmany(x, y) (((x)+((y)-1))/(y))
56 #define FD_MASK_BYTES sizeof(fd_mask)
57 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
60 static int examine_select(fd_set
*, fd_set
*);
61 static int fdIsHttp(int fd
);
62 static int fdIsIcp(int fd
);
63 static int fdIsDns(int fd
);
64 static OBJH commIncomingStats
;
65 static int comm_check_incoming_select_handlers(int nfds
, int *fds
);
66 static void comm_select_dns_incoming(void);
67 static void commUpdateReadBits(int fd
, PF
* handler
);
68 static void commUpdateWriteBits(int fd
, PF
* handler
);
71 static struct timeval zero_tv
;
72 static fd_set global_readfds
;
73 static fd_set global_writefds
;
78 * Automatic tuning for incoming requests:
80 * INCOMING sockets are the ICP and HTTP ports. We need to check these
81 * fairly regularly, but how often? When the load increases, we
82 * want to check the incoming sockets more often. If we have a lot
83 * of incoming ICP, then we need to check these sockets more than
84 * if we just have HTTP.
86 * The variables 'incoming_icp_interval' and 'incoming_http_interval'
87 * determine how many normal I/O events to process before checking
88 * incoming sockets again. Note we store the incoming_interval
89 * multipled by a factor of (2^INCOMING_FACTOR) to have some
90 * pseudo-floating point precision.
92 * The variable 'icp_io_events' and 'http_io_events' counts how many normal
93 * I/O events have been processed since the last check on the incoming
94 * sockets. When io_events > incoming_interval, its time to check incoming
97 * Every time we check incoming sockets, we count how many new messages
98 * or connections were processed. This is used to adjust the
99 * incoming_interval for the next iteration. The new incoming_interval
100 * is calculated as the current incoming_interval plus what we would
101 * like to see as an average number of events minus the number of
102 * events just processed.
104 * incoming_interval = incoming_interval + target_average - number_of_events_processed
106 * There are separate incoming_interval counters for both HTTP and ICP events
108 * You can see the current values of the incoming_interval's, as well as
109 * a histogram of 'incoming_events' by asking the cache manager
110 * for 'comm_incoming', e.g.:
112 * % ./client mgr:comm_incoming
116 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
117 * incoming_interval for both types of sockets. At the
118 * largest value the cache will effectively be idling.
120 * - The higher the INCOMING_FACTOR, the slower the algorithm will
121 * respond to load spikes/increases/decreases in demand. A value
122 * between 3 and 8 is recommended.
125 #define MAX_INCOMING_INTEGER 256
126 #define INCOMING_FACTOR 5
127 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
128 static int icp_io_events
= 0;
129 static int dns_io_events
= 0;
130 static int http_io_events
= 0;
131 static int incoming_icp_interval
= 16 << INCOMING_FACTOR
;
132 static int incoming_dns_interval
= 16 << INCOMING_FACTOR
;
133 static int incoming_http_interval
= 16 << INCOMING_FACTOR
;
134 #define commCheckICPIncoming (++icp_io_events > (incoming_icp_interval>> INCOMING_FACTOR))
135 #define commCheckDNSIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
136 #define commCheckHTTPIncoming (++http_io_events > (incoming_http_interval>> INCOMING_FACTOR))
139 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
141 fde
*F
= &fd_table
[fd
];
143 assert(F
->flags
.open
);
144 debugs(5, 5, HERE
<< "FD " << fd
<< ", type=" << type
<<
145 ", handler=" << handler
<< ", client_data=" << client_data
<<
146 ", timeout=" << timeout
);
148 if (type
& COMM_SELECT_READ
) {
149 F
->read_handler
= handler
;
150 F
->read_data
= client_data
;
151 commUpdateReadBits(fd
, handler
);
154 if (type
& COMM_SELECT_WRITE
) {
155 F
->write_handler
= handler
;
156 F
->write_data
= client_data
;
157 commUpdateWriteBits(fd
, handler
);
161 F
->timeout
= squid_curtime
+ timeout
;
165 Comm::ResetSelect(int fd
)
172 if (icpIncomingConn
!= NULL
&& fd
== icpIncomingConn
->fd
)
175 if (icpOutgoingConn
!= NULL
&& fd
== icpOutgoingConn
->fd
)
184 if (fd
== DnsSocketA
)
187 if (fd
== DnsSocketB
)
198 for (j
= 0; j
< NHttpSockets
; j
++) {
199 if (fd
== HttpSockets
[j
])
207 comm_check_incoming_select_handlers(int nfds
, int *fds
)
216 FD_ZERO(&write_mask
);
217 incoming_sockets_accepted
= 0;
219 for (i
= 0; i
< nfds
; i
++) {
222 if (fd_table
[fd
].read_handler
) {
223 FD_SET(fd
, &read_mask
);
229 if (fd_table
[fd
].write_handler
) {
230 FD_SET(fd
, &write_mask
);
242 statCounter
.syscalls
.selects
++;
244 if (select(maxfd
, &read_mask
, &write_mask
, NULL
, &zero_tv
) < 1)
245 return incoming_sockets_accepted
;
247 for (i
= 0; i
< nfds
; i
++) {
250 if (FD_ISSET(fd
, &read_mask
)) {
251 if ((hdl
= fd_table
[fd
].read_handler
) != NULL
) {
252 fd_table
[fd
].read_handler
= NULL
;
253 commUpdateReadBits(fd
, NULL
);
254 hdl(fd
, fd_table
[fd
].read_data
);
256 debugs(5, 1, "comm_select_incoming: FD " << fd
<< " NULL read handler");
260 if (FD_ISSET(fd
, &write_mask
)) {
261 if ((hdl
= fd_table
[fd
].write_handler
) != NULL
) {
262 fd_table
[fd
].write_handler
= NULL
;
263 commUpdateWriteBits(fd
, NULL
);
264 hdl(fd
, fd_table
[fd
].write_data
);
266 debugs(5, 1, "comm_select_incoming: FD " << fd
<< " NULL write handler");
271 return incoming_sockets_accepted
;
275 comm_select_icp_incoming(void)
282 if (Comm::IsConnOpen(icpIncomingConn
))
283 fds
[nfds
++] = icpIncomingConn
->fd
;
285 if (Comm::IsConnOpen(icpOutgoingConn
) && icpIncomingConn
!= icpOutgoingConn
)
286 fds
[nfds
++] = icpOutgoingConn
->fd
;
291 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
293 incoming_icp_interval
+= Config
.comm_incoming
.icp_average
- nevents
;
295 if (incoming_icp_interval
< 0)
296 incoming_icp_interval
= 0;
298 if (incoming_icp_interval
> MAX_INCOMING_INTERVAL
)
299 incoming_icp_interval
= MAX_INCOMING_INTERVAL
;
301 if (nevents
> INCOMING_ICP_MAX
)
302 nevents
= INCOMING_ICP_MAX
;
304 statCounter
.comm_icp_incoming
.count(nevents
);
308 comm_select_http_incoming(void)
311 int fds
[MAXHTTPPORTS
];
316 for (j
= 0; j
< NHttpSockets
; j
++) {
317 if (HttpSockets
[j
] < 0)
320 fds
[nfds
++] = HttpSockets
[j
];
323 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
324 incoming_http_interval
+= Config
.comm_incoming
.http_average
- nevents
;
326 if (incoming_http_interval
< 0)
327 incoming_http_interval
= 0;
329 if (incoming_http_interval
> MAX_INCOMING_INTERVAL
)
330 incoming_http_interval
= MAX_INCOMING_INTERVAL
;
332 if (nevents
> INCOMING_HTTP_MAX
)
333 nevents
= INCOMING_HTTP_MAX
;
335 statCounter
.comm_http_incoming
.count(nevents
);
338 #define DEBUG_FDBITS 0
339 /* Select on all sockets; call handlers for those that are ready. */
341 Comm::DoSelect(int msec
)
352 int callicp
= 0, callhttp
= 0;
366 struct timeval poll_time
;
367 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
,
389 howmany(maxfd
, FD_MASK_BITS
) * FD_MASK_BYTES
);
391 memcpy(&writefds
, &global_writefds
,
392 howmany(maxfd
, FD_MASK_BITS
) * FD_MASK_BYTES
);
394 /* remove stalled FDs, and deal with pending descriptors */
397 FD_ZERO(&pendingfds
);
399 maxindex
= howmany(maxfd
, FD_MASK_BITS
);
401 fdsp
= (fd_mask
*) & readfds
;
403 for (j
= 0; j
< maxindex
; j
++) {
404 if ((tmask
= fdsp
[j
]) == 0)
405 continue; /* no bits here */
407 for (k
= 0; k
< FD_MASK_BITS
; k
++) {
408 if (!EBIT_TEST(tmask
, k
))
411 /* Found a set bit */
412 fd
= (j
* FD_MASK_BITS
) + k
;
414 if (FD_ISSET(fd
, &readfds
) && fd_table
[fd
].flags
.read_pending
) {
415 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(FD_ISSET(i
, &readfds
));
429 if (fd_table
[i
].write_handler
) {
430 assert(FD_ISSET(i
, &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
, NULL
, &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 fdsp
= (fd_mask
*) & readfds
;
483 pfdsp
= (fd_mask
*) & pendingfds
;
485 maxindex
= howmany(maxfd
, FD_MASK_BITS
);
487 for (j
= 0; j
< maxindex
; j
++) {
488 if ((tmask
= (fdsp
[j
] | pfdsp
[j
])) == 0)
489 continue; /* no bits here */
491 for (k
= 0; k
< FD_MASK_BITS
; k
++) {
493 break; /* no more bits left */
495 if (!EBIT_TEST(tmask
, k
))
498 /* Found a set bit */
499 fd
= (j
* FD_MASK_BITS
) + k
;
501 EBIT_CLR(tmask
, k
); /* this will be done */
505 debugs(5, 9, "FD " << fd
<< " bit set for reading");
507 assert(FD_ISSET(fd
, &readfds
));
527 debugs(5, 6, "comm_select: FD " << fd
<< " ready for reading");
529 if (NULL
== (hdl
= F
->read_handler
))
532 F
->read_handler
= NULL
;
533 F
->flags
.read_pending
= 0;
534 commUpdateReadBits(fd
, NULL
);
535 hdl(fd
, F
->read_data
);
536 statCounter
.select_fds
++;
538 if (commCheckICPIncoming
)
539 comm_select_icp_incoming();
541 if (commCheckDNSIncoming
)
542 comm_select_dns_incoming();
544 if (commCheckHTTPIncoming
)
545 comm_select_http_incoming();
550 fdsp
= (fd_mask
*) & writefds
;
552 for (j
= 0; j
< maxindex
; j
++) {
553 if ((tmask
= fdsp
[j
]) == 0)
554 continue; /* no bits here */
556 for (k
= 0; k
< FD_MASK_BITS
; k
++) {
558 break; /* no more bits left */
560 if (!EBIT_TEST(tmask
, k
))
563 /* Found a set bit */
564 fd
= (j
* FD_MASK_BITS
) + k
;
566 EBIT_CLR(tmask
, k
); /* this will be done */
570 debugs(5, 9, "FD " << fd
<< " bit set for writing");
572 assert(FD_ISSET(fd
, &writefds
));
592 debugs(5, 6, "comm_select: FD " << fd
<< " ready for writing");
594 if ((hdl
= F
->write_handler
)) {
595 F
->write_handler
= NULL
;
596 commUpdateWriteBits(fd
, NULL
);
597 hdl(fd
, F
->write_data
);
598 statCounter
.select_fds
++;
600 if (commCheckICPIncoming
)
601 comm_select_icp_incoming();
603 if (commCheckDNSIncoming
)
604 comm_select_dns_incoming();
606 if (commCheckHTTPIncoming
)
607 comm_select_http_incoming();
613 comm_select_icp_incoming();
616 comm_select_dns_incoming();
619 comm_select_http_incoming();
623 statCounter
.select_time
+= (current_dtime
- start
);
626 } while (timeout
> current_dtime
);
627 debugs(5, 8, "comm_select: time out: " << squid_curtime
);
633 comm_select_dns_incoming(void)
640 if (DnsSocketA
< 0 && DnsSocketB
< 0)
644 fds
[nfds
++] = DnsSocketA
;
647 fds
[nfds
++] = DnsSocketB
;
649 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
654 incoming_dns_interval
+= Config
.comm_incoming
.dns_average
- nevents
;
656 if (incoming_dns_interval
< Config
.comm_incoming
.dns_min_poll
)
657 incoming_dns_interval
= Config
.comm_incoming
.dns_min_poll
;
659 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
660 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
662 if (nevents
> INCOMING_DNS_MAX
)
663 nevents
= INCOMING_DNS_MAX
;
665 statCounter
.comm_dns_incoming
.count(nevents
);
669 Comm::SelectLoopInit(void)
673 FD_ZERO(&global_readfds
);
674 FD_ZERO(&global_writefds
);
675 nreadfds
= nwritefds
= 0;
677 Mgr::RegisterAction("comm_select_incoming",
678 "comm_incoming() stats",
679 commIncomingStats
, 0, 1);
683 * examine_select - debug routine.
685 * I spend the day chasing this core dump that occurs when both the client
686 * and the server side of a cache fetch simultaneoulsy abort the
687 * connection. While I haven't really studied the code to figure out how
688 * it happens, the snippet below may prevent the cache from exitting:
690 * Call this from where the select loop fails.
693 examine_select(fd_set
* readfds
, fd_set
* writefds
)
700 AsyncCall::Pointer ch
= NULL
;
704 debugs(5, 0, "examine_select: Examining open file descriptors...");
706 for (fd
= 0; fd
< Squid_MaxFD
; fd
++) {
709 tv
.tv_sec
= tv
.tv_usec
= 0;
711 if (FD_ISSET(fd
, readfds
))
713 else if (FD_ISSET(fd
, writefds
))
714 FD_SET(fd
, &write_x
);
718 statCounter
.syscalls
.selects
++;
721 if (!fstat(fd
, &sb
)) {
722 debugs(5, 5, "FD " << fd
<< " is valid.");
727 debugs(5, 0, "FD " << fd
<< ": " << xstrerror());
728 debugs(5, 0, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
729 debugs(5, 0, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
] << " called '" << F
->desc
<< "'");
730 debugs(5, 0, "tmout:" << F
->timeoutHandler
<< " read:" << F
->read_handler
<< " write:" << F
->write_handler
);
732 for (ch
= F
->closeHandler
; ch
!= NULL
; ch
= ch
->Next())
733 debugs(5, 0, " close handler: " << ch
);
735 if (F
->closeHandler
!= NULL
) {
736 commCallCloseHandlers(fd
);
737 } else if (F
->timeoutHandler
!= NULL
) {
738 debugs(5, 0, "examine_select: Calling Timeout Handler");
739 ScheduleCallHere(F
->timeoutHandler
);
742 F
->closeHandler
= NULL
;
743 F
->timeoutHandler
= NULL
;
744 F
->read_handler
= NULL
;
745 F
->write_handler
= NULL
;
747 FD_CLR(fd
, writefds
);
755 commIncomingStats(StoreEntry
* sentry
)
757 StatCounters
*f
= &statCounter
;
758 storeAppendPrintf(sentry
, "Current incoming_icp_interval: %d\n",
759 incoming_icp_interval
>> INCOMING_FACTOR
);
760 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
761 incoming_dns_interval
>> INCOMING_FACTOR
);
762 storeAppendPrintf(sentry
, "Current incoming_http_interval: %d\n",
763 incoming_http_interval
>> INCOMING_FACTOR
);
764 storeAppendPrintf(sentry
, "\n");
765 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
766 storeAppendPrintf(sentry
, "ICP Messages handled per comm_select_icp_incoming() call:\n");
767 statHistDump(&f
->comm_icp_incoming
, sentry
, statHistIntDumper
);
768 storeAppendPrintf(sentry
, "DNS Messages handled per comm_select_dns_incoming() call:\n");
769 statHistDump(&f
->comm_dns_incoming
, sentry
, statHistIntDumper
);
770 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_select_http_incoming() call:\n");
771 statHistDump(&f
->comm_http_incoming
, sentry
, statHistIntDumper
);
775 commUpdateReadBits(int fd
, PF
* handler
)
777 if (handler
&& !FD_ISSET(fd
, &global_readfds
)) {
778 FD_SET(fd
, &global_readfds
);
780 } else if (!handler
&& FD_ISSET(fd
, &global_readfds
)) {
781 FD_CLR(fd
, &global_readfds
);
787 commUpdateWriteBits(int fd
, PF
* handler
)
789 if (handler
&& !FD_ISSET(fd
, &global_writefds
)) {
790 FD_SET(fd
, &global_writefds
);
792 } else if (!handler
&& FD_ISSET(fd
, &global_writefds
)) {
793 FD_CLR(fd
, &global_writefds
);
798 /* Called by async-io or diskd to speed up the polling */
800 Comm::QuickPollRequired(void)
805 #endif /* USE_SELECT */