3 * $Id: comm_select_win32.cc,v 1.8 2008/02/24 12:59:08 serassio 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
)
167 if (fd
== theInIcpConnection
)
170 if (fd
== theOutIcpConnection
)
190 for (j
= 0; j
< NHttpSockets
; j
++) {
191 if (fd
== HttpSockets
[j
])
199 comm_check_incoming_select_handlers(int nfds
, int *fds
)
210 FD_ZERO(&write_mask
);
211 incoming_sockets_accepted
= 0;
213 for (i
= 0; i
< nfds
; i
++) {
216 if (fd_table
[fd
].read_handler
) {
217 FD_SET(fd
, &read_mask
);
223 if (fd_table
[fd
].write_handler
) {
224 FD_SET(fd
, &write_mask
);
236 statCounter
.syscalls
.selects
++;
238 if (select(maxfd
, &read_mask
, &write_mask
, &errfds
, &zero_tv
) < 1)
240 return incoming_sockets_accepted
;
242 for (i
= 0; i
< nfds
; i
++) {
245 if (__WSAFDIsSet(fd_table
[fd
].win32
.handle
, &read_mask
)) {
246 if ((hdl
= fd_table
[fd
].read_handler
) != NULL
) {
247 fd_table
[fd
].read_handler
= NULL
;
248 commUpdateReadBits(fd
, NULL
);
249 hdl(fd
, fd_table
[fd
].read_data
);
251 debugs(5, 1, "comm_select_incoming: FD " << fd
<< " NULL read handler");
255 if (__WSAFDIsSet(fd_table
[fd
].win32
.handle
, &write_mask
)) {
256 if ((hdl
= fd_table
[fd
].write_handler
) != NULL
) {
257 fd_table
[fd
].write_handler
= NULL
;
258 commUpdateWriteBits(fd
, NULL
);
259 hdl(fd
, fd_table
[fd
].write_data
);
261 debugs(5, 1, "comm_select_incoming: FD " << fd
<< " NULL write handler");
266 return incoming_sockets_accepted
;
270 comm_select_icp_incoming(void)
277 if (theInIcpConnection
>= 0)
278 fds
[nfds
++] = theInIcpConnection
;
280 if (theInIcpConnection
!= theOutIcpConnection
)
281 if (theOutIcpConnection
>= 0)
282 fds
[nfds
++] = theOutIcpConnection
;
287 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
289 incoming_icp_interval
+= Config
.comm_incoming
.icp_average
- nevents
;
291 if (incoming_icp_interval
< 0)
292 incoming_icp_interval
= 0;
294 if (incoming_icp_interval
> MAX_INCOMING_INTERVAL
)
295 incoming_icp_interval
= MAX_INCOMING_INTERVAL
;
297 if (nevents
> INCOMING_ICP_MAX
)
298 nevents
= INCOMING_ICP_MAX
;
300 statHistCount(&statCounter
.comm_icp_incoming
, nevents
);
304 comm_select_http_incoming(void)
307 int fds
[MAXHTTPPORTS
];
312 for (j
= 0; j
< NHttpSockets
; j
++) {
313 if (HttpSockets
[j
] < 0)
316 fds
[nfds
++] = HttpSockets
[j
];
319 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
320 incoming_http_interval
+= Config
.comm_incoming
.http_average
- nevents
;
322 if (incoming_http_interval
< 0)
323 incoming_http_interval
= 0;
325 if (incoming_http_interval
> MAX_INCOMING_INTERVAL
)
326 incoming_http_interval
= MAX_INCOMING_INTERVAL
;
328 if (nevents
> INCOMING_HTTP_MAX
)
329 nevents
= INCOMING_HTTP_MAX
;
331 statHistCount(&statCounter
.comm_http_incoming
, nevents
);
334 #define DEBUG_FDBITS 0
335 /* Select on all sockets; call handlers for those that are ready. */
337 comm_select(int msec
)
348 int callicp
= 0, callhttp
= 0;
355 struct timeval poll_time
;
356 double timeout
= current_dtime
+ (msec
/ 1000.0);
366 start
= current_dtime
;
368 if (commCheckICPIncoming
)
369 comm_select_icp_incoming();
371 if (commCheckDNSIncoming
)
372 comm_select_dns_incoming();
374 if (commCheckHTTPIncoming
)
375 comm_select_http_incoming();
377 callicp
= calldns
= callhttp
= 0;
379 maxfd
= Biggest_FD
+ 1;
381 xmemcpy(&readfds
, &global_readfds
, sizeof(global_readfds
));
383 xmemcpy(&writefds
, &global_writefds
, sizeof(global_writefds
));
385 xmemcpy(&errfds
, &global_writefds
, sizeof(global_writefds
));
387 /* remove stalled FDs, and deal with pending descriptors */
390 FD_ZERO(&pendingfds
);
392 for (j
= 0; j
< (int) readfds
.fd_count
; j
++) {
393 register int readfds_handle
= readfds
.fd_array
[j
];
396 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
397 if ( fd_table
[fd
].win32
.handle
== readfds_handle
) {
398 if (fd_table
[fd
].flags
.open
) {
408 if (__WSAFDIsSet(fd_table
[fd
].win32
.handle
, &readfds
) && fd_table
[fd
].flags
.read_pending
) {
409 FD_SET(fd
, &pendingfds
);
415 for (i
= 0; i
< maxfd
; i
++) {
416 /* Check each open socket for a handler. */
418 if (fd_table
[i
].read_handler
) {
419 assert(__WSAFDIsSet(fd_table
[i
].win32
.handle
, readfds
));
422 if (fd_table
[i
].write_handler
) {
423 assert(__WSAFDIsSet(fd_table
[i
].win32
.handle
, writefds
));
428 if (nreadfds
+ nwritefds
== 0) {
429 assert(shutting_down
);
430 return COMM_SHUTDOWN
;
433 if (msec
> MAX_POLL_TIME
)
434 msec
= MAX_POLL_TIME
;
440 poll_time
.tv_sec
= msec
/ 1000;
441 poll_time
.tv_usec
= (msec
% 1000) * 1000;
442 statCounter
.syscalls
.selects
++;
443 num
= select(maxfd
, &readfds
, &writefds
, &errfds
, &poll_time
);
444 statCounter
.select_loops
++;
446 if (num
>= 0 || pending
> 0)
449 if (ignoreErrno(errno
))
452 debugs(5, 0, "comm_select: select failure: " << xstrerror());
454 examine_select(&readfds
, &writefds
);
461 if (num
< 0 && !pending
)
466 debugs(5, num
? 5 : 8, "comm_select: " << num
<< "+" << pending
<< " FDs ready");
468 statHistCount(&statCounter
.select_fds_hist
, num
);
470 if (num
== 0 && pending
== 0)
473 /* Scan return fd masks for ready descriptors */
474 assert(readfds
.fd_count
<= (unsigned int) Biggest_FD
);
475 assert(pendingfds
.fd_count
<= (unsigned int) Biggest_FD
);
477 for (j
= 0; j
< (int) readfds
.fd_count
; j
++) {
478 register int readfds_handle
= readfds
.fd_array
[j
];
479 register int pendingfds_handle
= pendingfds
.fd_array
[j
];
480 register int osfhandle
;
483 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
484 osfhandle
= fd_table
[fd
].win32
.handle
;
486 if (( osfhandle
== readfds_handle
) ||
487 ( osfhandle
== pendingfds_handle
)) {
488 if (fd_table
[fd
].flags
.open
) {
500 debugs(5, 9, "FD " << fd
<< " bit set for reading");
502 assert(__WSAFDIsSet(fd_table
[fd
].win32
.handle
, readfds
));
522 debugs(5, 6, "comm_select: FD " << fd
<< " ready for reading");
524 if (NULL
== (hdl
= F
->read_handler
))
527 F
->read_handler
= NULL
;
528 F
->flags
.read_pending
= 0;
529 commUpdateReadBits(fd
, NULL
);
530 hdl(fd
, F
->read_data
);
531 statCounter
.select_fds
++;
533 if (commCheckICPIncoming
)
534 comm_select_icp_incoming();
536 if (commCheckDNSIncoming
)
537 comm_select_dns_incoming();
539 if (commCheckHTTPIncoming
)
540 comm_select_http_incoming();
544 assert(errfds
.fd_count
<= (unsigned int) Biggest_FD
);
546 for (j
= 0; j
< (int) errfds
.fd_count
; j
++) {
547 register int errfds_handle
= errfds
.fd_array
[j
];
549 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
550 if ( fd_table
[fd
].win32
.handle
== errfds_handle
)
554 if (fd_table
[fd
].flags
.open
) {
557 if ((hdl
= F
->write_handler
)) {
558 F
->write_handler
= NULL
;
559 commUpdateWriteBits(fd
, NULL
);
560 hdl(fd
, F
->write_data
);
561 statCounter
.select_fds
++;
566 assert(writefds
.fd_count
<= (unsigned int) Biggest_FD
);
568 for (j
= 0; j
< (int) writefds
.fd_count
; j
++) {
569 register int writefds_handle
= writefds
.fd_array
[j
];
572 for ( fd
= Biggest_FD
; fd
; fd
-- ) {
573 if ( fd_table
[fd
].win32
.handle
== writefds_handle
) {
574 if (fd_table
[fd
].flags
.open
) {
586 debugs(5, 9, "FD " << fd
<< " bit set for writing");
588 assert(__WSAFDIsSet(fd_table
[fd
].win32
.handle
, writefds
));
608 debugs(5, 5, "comm_select: FD " << fd
<< " ready for writing");
610 if ((hdl
= F
->write_handler
)) {
611 F
->write_handler
= NULL
;
612 commUpdateWriteBits(fd
, NULL
);
613 hdl(fd
, F
->write_data
);
614 statCounter
.select_fds
++;
616 if (commCheckICPIncoming
)
617 comm_select_icp_incoming();
619 if (commCheckDNSIncoming
)
620 comm_select_dns_incoming();
622 if (commCheckHTTPIncoming
)
623 comm_select_http_incoming();
628 comm_select_icp_incoming();
631 comm_select_dns_incoming();
634 comm_select_http_incoming();
638 statCounter
.select_time
+= (current_dtime
- start
);
641 } while (timeout
> current_dtime
);
642 debugs(5, 8, "comm_select: time out: " << squid_curtime
);
648 comm_select_dns_incoming(void)
658 fds
[nfds
++] = DnsSocket
;
660 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
665 incoming_dns_interval
+= Config
.comm_incoming
.dns_average
- nevents
;
667 if (incoming_dns_interval
< Config
.comm_incoming
.dns_min_poll
)
668 incoming_dns_interval
= Config
.comm_incoming
.dns_min_poll
;
670 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
671 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
673 if (nevents
> INCOMING_DNS_MAX
)
674 nevents
= INCOMING_DNS_MAX
;
676 statHistCount(&statCounter
.comm_dns_incoming
, nevents
);
680 comm_select_init(void)
684 FD_ZERO(&global_readfds
);
685 FD_ZERO(&global_writefds
);
686 nreadfds
= nwritefds
= 0;
690 commSelectRegisterWithCacheManager(CacheManager
& manager
)
692 manager
.registerAction("comm_select_incoming",
693 "comm_incoming() stats",
694 commIncomingStats
, 0, 1);
698 * examine_select - debug routine.
700 * I spend the day chasing this core dump that occurs when both the client
701 * and the server side of a cache fetch simultaneoulsy abort the
702 * connection. While I haven't really studied the code to figure out how
703 * it happens, the snippet below may prevent the cache from exitting:
705 * Call this from where the select loop fails.
708 examine_select(fd_set
* readfds
, fd_set
* writefds
)
715 AsyncCall::Pointer ch
= NULL
;
719 debugs(5, 0, "examine_select: Examining open file descriptors...");
721 for (fd
= 0; fd
< Squid_MaxFD
; fd
++) {
724 tv
.tv_sec
= tv
.tv_usec
= 0;
726 if (__WSAFDIsSet(fd_table
[fd
].win32
.handle
, readfds
))
728 else if (__WSAFDIsSet(fd_table
[fd
].win32
.handle
, writefds
))
729 FD_SET(fd
, &write_x
);
733 statCounter
.syscalls
.selects
++;
736 if (!fstat(fd
, &sb
)) {
737 debugs(5, 5, "FD " << fd
<< " is valid.");
742 debugs(5, 0, "FD " << fd
<< ": " << xstrerror());
743 debugs(5, 0, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
744 debugs(5, 0, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
] << " called '" << F
->desc
<< "'");
745 debugs(5, 0, "tmout:" << F
->timeoutHandler
<< " read:" << F
->read_handler
<< " write:" << F
->write_handler
);
747 for (ch
= F
->closeHandler
; ch
!= NULL
; ch
= ch
->Next())
748 debugs(5, 0, " close handler: " << ch
);
750 if (F
->closeHandler
!= NULL
) {
751 commCallCloseHandlers(fd
);
752 } else if (F
->timeoutHandler
!= NULL
) {
753 debugs(5, 0, "examine_select: Calling Timeout Handler");
754 ScheduleCallHere(F
->timeoutHandler
);
757 F
->closeHandler
= NULL
;
758 F
->timeoutHandler
= NULL
;
759 F
->read_handler
= NULL
;
760 F
->write_handler
= NULL
;
762 FD_CLR(fd
, writefds
);
770 commIncomingStats(StoreEntry
* sentry
)
772 StatCounters
*f
= &statCounter
;
773 storeAppendPrintf(sentry
, "Current incoming_icp_interval: %d\n",
774 incoming_icp_interval
>> INCOMING_FACTOR
);
775 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
776 incoming_dns_interval
>> INCOMING_FACTOR
);
777 storeAppendPrintf(sentry
, "Current incoming_http_interval: %d\n",
778 incoming_http_interval
>> INCOMING_FACTOR
);
779 storeAppendPrintf(sentry
, "\n");
780 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
781 storeAppendPrintf(sentry
, "ICP Messages handled per comm_select_icp_incoming() call:\n");
782 statHistDump(&f
->comm_icp_incoming
, sentry
, statHistIntDumper
);
783 storeAppendPrintf(sentry
, "DNS Messages handled per comm_select_dns_incoming() call:\n");
784 statHistDump(&f
->comm_dns_incoming
, sentry
, statHistIntDumper
);
785 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_select_http_incoming() call:\n");
786 statHistDump(&f
->comm_http_incoming
, sentry
, statHistIntDumper
);
790 commUpdateReadBits(int fd
, PF
* handler
)
792 if (handler
&& !__WSAFDIsSet(fd_table
[fd
].win32
.handle
, &global_readfds
)) {
793 FD_SET(fd
, &global_readfds
);
795 } else if (!handler
&& __WSAFDIsSet(fd_table
[fd
].win32
.handle
, &global_readfds
)) {
796 FD_CLR(fd
, &global_readfds
);
802 commUpdateWriteBits(int fd
, PF
* handler
)
804 if (handler
&& !__WSAFDIsSet(fd_table
[fd
].win32
.handle
, &global_writefds
)) {
805 FD_SET(fd
, &global_writefds
);
807 } else if (!handler
&& __WSAFDIsSet(fd_table
[fd
].win32
.handle
, &global_writefds
)) {
808 FD_CLR(fd
, &global_writefds
);
813 /* Called by async-io or diskd to speed up the polling */
815 comm_quick_poll_required(void)
820 #endif /* USE_SELECT_WIN32 */