3 * $Id: comm_select_win32.cc,v 1.5 2008/01/07 16:22:06 hno Exp $
5 * DEBUG: section 5 Socket Functions
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #include "comm_select.h"
37 #include "CacheManager.h"
38 #include "SquidTime.h"
40 #ifdef USE_SELECT_WIN32
44 static int MAX_POLL_TIME
= 1000; /* see also comm_quick_poll_required() */
47 #define howmany(x, y) (((x)+((y)-1))/(y))
52 #define FD_MASK_BYTES sizeof(fd_mask)
53 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
56 static int examine_select(fd_set
*, fd_set
*);
57 static int fdIsHttp(int fd
);
58 static int fdIsIcp(int fd
);
59 static int fdIsDns(int fd
);
60 static OBJH commIncomingStats
;
61 static int comm_check_incoming_select_handlers(int nfds
, int *fds
);
62 static void comm_select_dns_incoming(void);
63 static void commUpdateReadBits(int fd
, PF
* handler
);
64 static void commUpdateWriteBits(int fd
, PF
* handler
);
67 static struct timeval zero_tv
;
68 static fd_set global_readfds
;
69 static fd_set global_writefds
;
74 * Automatic tuning for incoming requests:
76 * INCOMING sockets are the ICP and HTTP ports. We need to check these
77 * fairly regularly, but how often? When the load increases, we
78 * want to check the incoming sockets more often. If we have a lot
79 * of incoming ICP, then we need to check these sockets more than
80 * if we just have HTTP.
82 * The variables 'incoming_icp_interval' and 'incoming_http_interval'
83 * determine how many normal I/O events to process before checking
84 * incoming sockets again. Note we store the incoming_interval
85 * multipled by a factor of (2^INCOMING_FACTOR) to have some
86 * pseudo-floating point precision.
88 * The variable 'icp_io_events' and 'http_io_events' counts how many normal
89 * I/O events have been processed since the last check on the incoming
90 * sockets. When io_events > incoming_interval, its time to check incoming
93 * Every time we check incoming sockets, we count how many new messages
94 * or connections were processed. This is used to adjust the
95 * incoming_interval for the next iteration. The new incoming_interval
96 * is calculated as the current incoming_interval plus what we would
97 * like to see as an average number of events minus the number of
98 * events just processed.
100 * incoming_interval = incoming_interval + target_average - number_of_events_processed
102 * There are separate incoming_interval counters for both HTTP and ICP events
104 * You can see the current values of the incoming_interval's, as well as
105 * a histogram of 'incoming_events' by asking the cache manager
106 * for 'comm_incoming', e.g.:
108 * % ./client mgr:comm_incoming
112 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
113 * incoming_interval for both types of sockets. At the
114 * largest value the cache will effectively be idling.
116 * - The higher the INCOMING_FACTOR, the slower the algorithm will
117 * respond to load spikes/increases/decreases in demand. A value
118 * between 3 and 8 is recommended.
121 #define MAX_INCOMING_INTEGER 256
122 #define INCOMING_FACTOR 5
123 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
124 static int icp_io_events
= 0;
125 static int dns_io_events
= 0;
126 static int http_io_events
= 0;
127 static int incoming_icp_interval
= 16 << INCOMING_FACTOR
;
128 static int incoming_dns_interval
= 16 << INCOMING_FACTOR
;
129 static int incoming_http_interval
= 16 << INCOMING_FACTOR
;
130 #define commCheckICPIncoming (++icp_io_events > (incoming_icp_interval>> INCOMING_FACTOR))
131 #define commCheckDNSIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
132 #define commCheckHTTPIncoming (++http_io_events > (incoming_http_interval>> INCOMING_FACTOR))
135 commSetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
,
138 fde
*F
= &fd_table
[fd
];
140 assert(F
->flags
.open
);
141 debugs(5, 5, "commSetSelect: FD " << fd
<< " type " << type
);
143 if (type
& COMM_SELECT_READ
) {
144 F
->read_handler
= handler
;
145 F
->read_data
= client_data
;
146 commUpdateReadBits(fd
, handler
);
149 if (type
& COMM_SELECT_WRITE
) {
150 F
->write_handler
= handler
;
151 F
->write_data
= client_data
;
152 commUpdateWriteBits(fd
, handler
);
156 F
->timeout
= squid_curtime
+ timeout
;
160 commResetSelect(int fd
)
168 if (fd
== theInIcpConnection
)
171 if (fd
== theOutIcpConnection
)
191 for (j
= 0; j
< NHttpSockets
; j
++) {
192 if (fd
== HttpSockets
[j
])
200 static int slowfdcnt
= 0;
201 static int slowfdarr
[SQUID_MAXFD
];
204 commAddSlowFd(int fd
)
206 assert(slowfdcnt
< SQUID_MAXFD
);
207 slowfdarr
[slowfdcnt
++] = fd
;
218 whichfd
= squid_random() % slowfdcnt
;
220 retfd
= slowfdarr
[whichfd
];
222 slowfdarr
[whichfd
] = slowfdarr
[--slowfdcnt
];
230 comm_check_incoming_select_handlers(int nfds
, int *fds
)
241 FD_ZERO(&write_mask
);
242 incoming_sockets_accepted
= 0;
244 for (i
= 0; i
< nfds
; i
++) {
247 if (fd_table
[fd
].read_handler
) {
248 FD_SET(fd
, &read_mask
);
254 if (fd_table
[fd
].write_handler
) {
255 FD_SET(fd
, &write_mask
);
267 statCounter
.syscalls
.selects
++;
269 if (select(maxfd
, &read_mask
, &write_mask
, &errfds
, &zero_tv
) < 1)
271 return incoming_sockets_accepted
;
273 for (i
= 0; i
< nfds
; i
++) {
276 if (FD_ISSET(fd
, &read_mask
)) {
277 if ((hdl
= fd_table
[fd
].read_handler
) != NULL
) {
278 fd_table
[fd
].read_handler
= NULL
;
279 commUpdateReadBits(fd
, NULL
);
280 hdl(fd
, fd_table
[fd
].read_data
);
282 debugs(5, 1, "comm_select_incoming: FD " << fd
<< " NULL read handler");
286 if (FD_ISSET(fd
, &write_mask
)) {
287 if ((hdl
= fd_table
[fd
].write_handler
) != NULL
) {
288 fd_table
[fd
].write_handler
= NULL
;
289 commUpdateWriteBits(fd
, NULL
);
290 hdl(fd
, fd_table
[fd
].write_data
);
292 debugs(5, 1, "comm_select_incoming: FD " << fd
<< " NULL write handler");
297 return incoming_sockets_accepted
;
301 comm_select_icp_incoming(void)
308 if (theInIcpConnection
>= 0)
309 fds
[nfds
++] = theInIcpConnection
;
311 if (theInIcpConnection
!= theOutIcpConnection
)
312 if (theOutIcpConnection
>= 0)
313 fds
[nfds
++] = theOutIcpConnection
;
318 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
320 incoming_icp_interval
+= Config
.comm_incoming
.icp_average
- nevents
;
322 if (incoming_icp_interval
< 0)
323 incoming_icp_interval
= 0;
325 if (incoming_icp_interval
> MAX_INCOMING_INTERVAL
)
326 incoming_icp_interval
= MAX_INCOMING_INTERVAL
;
328 if (nevents
> INCOMING_ICP_MAX
)
329 nevents
= INCOMING_ICP_MAX
;
331 statHistCount(&statCounter
.comm_icp_incoming
, nevents
);
335 comm_select_http_incoming(void)
338 int fds
[MAXHTTPPORTS
];
343 for (j
= 0; j
< NHttpSockets
; j
++) {
344 if (HttpSockets
[j
] < 0)
347 fds
[nfds
++] = HttpSockets
[j
];
350 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
351 incoming_http_interval
+= Config
.comm_incoming
.http_average
- nevents
;
353 if (incoming_http_interval
< 0)
354 incoming_http_interval
= 0;
356 if (incoming_http_interval
> MAX_INCOMING_INTERVAL
)
357 incoming_http_interval
= MAX_INCOMING_INTERVAL
;
359 if (nevents
> INCOMING_HTTP_MAX
)
360 nevents
= INCOMING_HTTP_MAX
;
362 statHistCount(&statCounter
.comm_http_incoming
, nevents
);
365 #define DEBUG_FDBITS 0
366 /* Select on all sockets; call handlers for those that are ready. */
368 comm_select(int msec
)
383 int callicp
= 0, callhttp
= 0;
390 struct timeval poll_time
;
391 double timeout
= current_dtime
+ (msec
/ 1000.0);
401 start
= current_dtime
;
407 if (commCheckICPIncoming
)
408 comm_select_icp_incoming();
410 if (commCheckDNSIncoming
)
411 comm_select_dns_incoming();
413 if (commCheckHTTPIncoming
)
414 comm_select_http_incoming();
416 callicp
= calldns
= callhttp
= 0;
418 maxfd
= Biggest_FD
+ 1;
420 xmemcpy(&readfds
, &global_readfds
, sizeof(global_readfds
));
422 xmemcpy(&writefds
, &global_writefds
, sizeof(global_writefds
));
424 xmemcpy(&errfds
, &global_writefds
, sizeof(global_writefds
));
426 /* remove stalled FDs, and deal with pending descriptors */
429 FD_ZERO(&pendingfds
);
431 for (j
= 0; j
< (int) readfds
.fd_count
; j
++) {
432 register int readfds_handle
= readfds
.fd_array
[j
];
435 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
436 if ( fd_table
[fd
].win32
.handle
== readfds_handle
) {
437 if (fd_table
[fd
].flags
.open
) {
447 if (FD_ISSET(fd
, &readfds
) && fd_table
[fd
].flags
.read_pending
) {
448 FD_SET(fd
, &pendingfds
);
454 for (i
= 0; i
< maxfd
; i
++) {
455 /* Check each open socket for a handler. */
457 if (fd_table
[i
].read_handler
) {
458 assert(FD_ISSET(i
, &readfds
));
461 if (fd_table
[i
].write_handler
) {
462 assert(FD_ISSET(i
, &writefds
));
467 if (nreadfds
+ nwritefds
== 0) {
468 assert(shutting_down
);
469 return COMM_SHUTDOWN
;
472 if (msec
> MAX_POLL_TIME
)
473 msec
= MAX_POLL_TIME
;
475 if (comm_iocallbackpending())
482 poll_time
.tv_sec
= msec
/ 1000;
483 poll_time
.tv_usec
= (msec
% 1000) * 1000;
484 statCounter
.syscalls
.selects
++;
485 num
= select(maxfd
, &readfds
, &writefds
, &errfds
, &poll_time
);
486 statCounter
.select_loops
++;
488 if (num
>= 0 || pending
> 0)
491 if (ignoreErrno(errno
))
494 debugs(5, 0, "comm_select: select failure: " << xstrerror());
496 examine_select(&readfds
, &writefds
);
503 if (num
< 0 && !pending
)
508 debugs(5, num
? 5 : 8, "comm_select: " << num
<< "+" << pending
<< " FDs ready\n");
510 statHistCount(&statCounter
.select_fds_hist
, num
);
512 if (num
== 0 && pending
== 0)
515 /* Scan return fd masks for ready descriptors */
517 assert(readfds
.fd_count
<= (unsigned int) Biggest_FD
);
519 assert(pendingfds
.fd_count
<= (unsigned int) Biggest_FD
);
521 for (j
= 0; j
< (int) readfds
.fd_count
; j
++) {
522 register int readfds_handle
= readfds
.fd_array
[j
];
523 register int pendingfds_handle
= pendingfds
.fd_array
[j
];
524 register int osfhandle
;
527 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
528 osfhandle
= fd_table
[fd
].win32
.handle
;
530 if (( osfhandle
== readfds_handle
) ||
531 ( osfhandle
== pendingfds_handle
)) {
532 if (fd_table
[fd
].flags
.open
) {
544 debugs(5, 9, "FD " << fd
<< " bit set for reading");
546 assert(FD_ISSET(fd
, &readfds
));
566 debugs(5, 6, "comm_select: FD " << fd
<< " ready for reading");
568 if (NULL
== (hdl
= F
->read_handler
))
573 else if (FD_ISSET(fd
, &slowfds
))
579 F
->read_handler
= NULL
;
580 commUpdateReadBits(fd
, NULL
);
581 hdl(fd
, F
->read_data
);
582 statCounter
.select_fds
++;
584 if (commCheckICPIncoming
)
585 comm_select_icp_incoming();
587 if (commCheckDNSIncoming
)
588 comm_select_dns_incoming();
590 if (commCheckHTTPIncoming
)
591 comm_select_http_incoming();
595 assert(errfds
.fd_count
<= (unsigned int) Biggest_FD
);
597 for (j
= 0; j
< (int) errfds
.fd_count
; j
++) {
598 register int errfds_handle
= errfds
.fd_array
[j
];
600 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
601 if ( fd_table
[fd
].win32
.handle
== errfds_handle
)
605 if (fd_table
[fd
].flags
.open
) {
608 if ((hdl
= F
->write_handler
)) {
609 F
->write_handler
= NULL
;
610 commUpdateWriteBits(fd
, NULL
);
611 hdl(fd
, F
->write_data
);
612 statCounter
.select_fds
++;
617 assert(writefds
.fd_count
<= (unsigned int) Biggest_FD
);
619 for (j
= 0; j
< (int) writefds
.fd_count
; j
++) {
620 register int writefds_handle
= writefds
.fd_array
[j
];
623 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
624 if ( fd_table
[fd
].win32
.handle
== writefds_handle
) {
625 if (fd_table
[fd
].flags
.open
) {
637 debugs(5, 9, "FD " << fd
<< " bit set for writing");
639 assert(FD_ISSET(fd
, &writefds
));
659 debugs(5, 5, "comm_select: FD " << fd
<< " ready for writing");
661 if ((hdl
= F
->write_handler
)) {
662 F
->write_handler
= NULL
;
663 commUpdateWriteBits(fd
, NULL
);
664 hdl(fd
, F
->write_data
);
665 statCounter
.select_fds
++;
667 if (commCheckICPIncoming
)
668 comm_select_icp_incoming();
670 if (commCheckDNSIncoming
)
671 comm_select_dns_incoming();
673 if (commCheckHTTPIncoming
)
674 comm_select_http_incoming();
681 comm_select_icp_incoming();
684 comm_select_dns_incoming();
687 comm_select_http_incoming();
691 while ((fd
= commGetSlowFd()) != -1) {
693 debugs(5, 6, "comm_select: slow FD " << fd
<< " selected for reading");
695 if ((hdl
= F
->read_handler
)) {
696 F
->read_handler
= NULL
;
697 commUpdateReadBits(fd
, NULL
);
698 hdl(fd
, F
->read_data
);
699 statCounter
.select_fds
++;
701 if (commCheckICPIncoming
)
702 comm_select_icp_incoming();
704 if (commCheckDNSIncoming
)
705 comm_select_dns_incoming();
707 if (commCheckHTTPIncoming
)
708 comm_select_http_incoming();
715 statCounter
.select_time
+= (current_dtime
- start
);
718 } while (timeout
> current_dtime
)
721 debugs(5, 8, "comm_select: time out: " << squid_curtime
);
727 comm_select_dns_incoming(void)
737 fds
[nfds
++] = DnsSocket
;
739 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
744 incoming_dns_interval
+= Config
.comm_incoming
.dns_average
- nevents
;
746 if (incoming_dns_interval
< Config
.comm_incoming
.dns_min_poll
)
747 incoming_dns_interval
= Config
.comm_incoming
.dns_min_poll
;
749 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
750 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
752 if (nevents
> INCOMING_DNS_MAX
)
753 nevents
= INCOMING_DNS_MAX
;
755 statHistCount(&statCounter
.comm_dns_incoming
, nevents
);
759 comm_select_init(void)
763 FD_ZERO(&global_readfds
);
764 FD_ZERO(&global_writefds
);
765 nreadfds
= nwritefds
= 0;
769 commSelectRegisterWithCacheManager(CacheManager
& manager
)
771 manager
.registerAction("comm_select_incoming",
772 "comm_incoming() stats",
773 commIncomingStats
, 0, 1);
777 * examine_select - debug routine.
779 * I spend the day chasing this core dump that occurs when both the client
780 * and the server side of a cache fetch simultaneoulsy abort the
781 * connection. While I haven't really studied the code to figure out how
782 * it happens, the snippet below may prevent the cache from exitting:
784 * Call this from where the select loop fails.
787 examine_select(fd_set
* readfds
, fd_set
* writefds
)
794 close_handler
*ch
= NULL
;
798 debugs(5, 0, "examine_select: Examining open file descriptors...");
800 for (fd
= 0; fd
< Squid_MaxFD
; fd
++) {
803 tv
.tv_sec
= tv
.tv_usec
= 0;
805 if (FD_ISSET(fd
, readfds
))
807 else if (FD_ISSET(fd
, writefds
))
808 FD_SET(fd
, &write_x
);
812 statCounter
.syscalls
.selects
++;
816 if (!fstat(fd
, &sb
)) {
817 debugs(5, 5, "FD " << fd
<< " is valid.");
822 debugs(5, 0, "FD " << fd
<< ": " << xstrerror());
823 debugs(5, 0, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
824 debugs(5, 0, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
] << " called '" << F
->desc
<< "'");
825 debugs(5, 0, "tmout:" << F
->timeout_handler
<< " read:" << F
->read_handler
<< " write:" << F
->write_handler
);
827 for (ch
= F
->closeHandler
; ch
; ch
= ch
->next
)
828 debugs(5, 0, " close handler: " << ch
->handler
);
830 if (F
->closeHandler
) {
831 commCallCloseHandlers(fd
);
832 } else if (F
->timeout_handler
) {
833 debugs(5, 0, "examine_select: Calling Timeout Handler");
834 F
->timeout_handler(fd
, F
->timeout_data
);
837 F
->closeHandler
= NULL
;
838 F
->timeout_handler
= NULL
;
839 F
->read_handler
= NULL
;
840 F
->write_handler
= NULL
;
842 FD_CLR(fd
, writefds
);
850 commIncomingStats(StoreEntry
* sentry
)
852 StatCounters
*f
= &statCounter
;
853 storeAppendPrintf(sentry
, "Current incoming_icp_interval: %d\n",
854 incoming_icp_interval
>> INCOMING_FACTOR
);
855 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
856 incoming_dns_interval
>> INCOMING_FACTOR
);
857 storeAppendPrintf(sentry
, "Current incoming_http_interval: %d\n",
858 incoming_http_interval
>> INCOMING_FACTOR
);
859 storeAppendPrintf(sentry
, "\n");
860 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
861 storeAppendPrintf(sentry
, "ICP Messages handled per comm_select_icp_incoming() call:\n");
862 statHistDump(&f
->comm_icp_incoming
, sentry
, statHistIntDumper
);
863 storeAppendPrintf(sentry
, "DNS Messages handled per comm_select_dns_incoming() call:\n");
864 statHistDump(&f
->comm_dns_incoming
, sentry
, statHistIntDumper
);
865 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_select_http_incoming() call:\n");
866 statHistDump(&f
->comm_http_incoming
, sentry
, statHistIntDumper
);
870 commUpdateReadBits(int fd
, PF
* handler
)
872 if (handler
&& !FD_ISSET(fd
, &global_readfds
)) {
873 FD_SET(fd
, &global_readfds
);
875 } else if (!handler
&& FD_ISSET(fd
, &global_readfds
)) {
876 FD_CLR(fd
, &global_readfds
);
882 commUpdateWriteBits(int fd
, PF
* handler
)
884 if (handler
&& !FD_ISSET(fd
, &global_writefds
)) {
885 FD_SET(fd
, &global_writefds
);
887 } else if (!handler
&& FD_ISSET(fd
, &global_writefds
)) {
888 FD_CLR(fd
, &global_writefds
);
893 /* Called by async-io or diskd to speed up the polling */
895 comm_quick_poll_required(void)
900 #endif /* USE_SELECT_WIN32 */