]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm/ModSelectWin32.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / comm / ModSelectWin32.cc
CommitLineData
663c0a38 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
663c0a38 3 *
bbc27441
AJ
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.
663c0a38 7 */
bbc27441
AJ
8
9/* DEBUG: section 05 Socket Functions */
10
f7f3304a 11#include "squid.h"
d841c88d
AJ
12
13#if USE_SELECT_WIN32
65d448bc 14#include "anyp/PortCfg.h"
f8171fc7 15#include "comm/Connection.h"
d841c88d
AJ
16#include "comm/Loops.h"
17#include "fde.h"
f8171fc7 18#include "ICP.h"
8822ebee 19#include "mgr/Registration.h"
663c0a38 20#include "SquidTime.h"
e1656dc4 21#include "StatCounters.h"
00a7574e 22#include "StatHist.h"
663c0a38 23#include "Store.h"
663c0a38 24
1a30fdf5 25#include <cerrno>
21d845b1 26
f53969cc 27static int MAX_POLL_TIME = 1000; /* see also Comm::QuickPollRequired() */
663c0a38 28
29#ifndef howmany
30#define howmany(x, y) (((x)+((y)-1))/(y))
31#endif
32#ifndef NBBY
33#define NBBY 8
34#endif
35#define FD_MASK_BYTES sizeof(fd_mask)
36#define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
37
38/* STATIC */
39static int examine_select(fd_set *, fd_set *);
65d448bc
AJ
40static int fdIsTcpListener(int fd);
41static int fdIsUdpListener(int fd);
663c0a38 42static int fdIsDns(int fd);
43static OBJH commIncomingStats;
44static int comm_check_incoming_select_handlers(int nfds, int *fds);
45static void comm_select_dns_incoming(void);
46static void commUpdateReadBits(int fd, PF * handler);
47static void commUpdateWriteBits(int fd, PF * handler);
48
663c0a38 49static struct timeval zero_tv;
50static fd_set global_readfds;
51static fd_set global_writefds;
52static int nreadfds;
53static int nwritefds;
54
55/*
56 * Automatic tuning for incoming requests:
57 *
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.
63 *
65d448bc 64 * The variables 'incoming_udp_interval' and 'incoming_tcp_interval'
663c0a38 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.
69 *
65d448bc 70 * The variable 'udp_io_events' and 'tcp_io_events' counts how many normal
663c0a38 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
73 * sockets.
74 *
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.
81 *
82 * incoming_interval = incoming_interval + target_average - number_of_events_processed
83 *
65d448bc 84 * There are separate incoming_interval counters for DNS, UDP and TCP events
26ac0430 85 *
663c0a38 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.:
89 *
90 * % ./client mgr:comm_incoming
91 *
92 * Caveats:
93 *
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.
97 *
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.
101 */
102
103#define MAX_INCOMING_INTEGER 256
104#define INCOMING_FACTOR 5
105#define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
65d448bc 106static int udp_io_events = 0;
663c0a38 107static int dns_io_events = 0;
65d448bc
AJ
108static int tcp_io_events = 0;
109static int incoming_udp_interval = 16 << INCOMING_FACTOR;
663c0a38 110static int incoming_dns_interval = 16 << INCOMING_FACTOR;
65d448bc
AJ
111static 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))
663c0a38 115
116void
d841c88d 117Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout)
663c0a38 118{
119 fde *F = &fd_table[fd];
120 assert(fd >= 0);
121 assert(F->flags.open);
48e7baac
AJ
122 debugs(5, 5, HERE << "FD " << fd << ", type=" << type <<
123 ", handler=" << handler << ", client_data=" << client_data <<
124 ", timeout=" << timeout);
663c0a38 125
126 if (type & COMM_SELECT_READ) {
127 F->read_handler = handler;
128 F->read_data = client_data;
129 commUpdateReadBits(fd, handler);
130 }
131
132 if (type & COMM_SELECT_WRITE) {
133 F->write_handler = handler;
134 F->write_data = client_data;
135 commUpdateWriteBits(fd, handler);
136 }
137
138 if (timeout)
139 F->timeout = squid_curtime + timeout;
140}
141
3a5a4930 142void
d841c88d 143Comm::ResetSelect(int fd)
3a5a4930 144{
145}
146
663c0a38 147static int
65d448bc 148fdIsUdpListener(int fd)
663c0a38 149{
f8171fc7 150 if (icpIncomingConn != NULL && fd == icpIncomingConn->fd)
663c0a38 151 return 1;
152
f8171fc7 153 if (icpOutgoingConn != NULL && fd == icpOutgoingConn->fd)
663c0a38 154 return 1;
155
156 return 0;
157}
158
159static int
160fdIsDns(int fd)
161{
4d6c8504
AJ
162 if (fd == DnsSocketA)
163 return 1;
164
165 if (fd == DnsSocketB)
663c0a38 166 return 1;
167
168 return 0;
169}
170
171static int
65d448bc 172fdIsTcpListener(int fd)
663c0a38 173{
d00790b2 174 for (AnyP::PortCfgPointer s = HttpPortList; s != NULL; s = s->next) {
65d448bc 175 if (s->listenConn != NULL && s->listenConn->fd == fd)
663c0a38 176 return 1;
177 }
178
179 return 0;
180}
181
663c0a38 182static int
183comm_check_incoming_select_handlers(int nfds, int *fds)
184{
185 int i;
186 int fd;
187 int maxfd = 0;
188 PF *hdl = NULL;
189 fd_set read_mask;
190 fd_set write_mask;
663c0a38 191 FD_ZERO(&read_mask);
192 FD_ZERO(&write_mask);
193 incoming_sockets_accepted = 0;
194
a2f5277a 195 for (i = 0; i < nfds; ++i) {
663c0a38 196 fd = fds[i];
197
198 if (fd_table[fd].read_handler) {
199 FD_SET(fd, &read_mask);
200
201 if (fd > maxfd)
202 maxfd = fd;
203 }
204
205 if (fd_table[fd].write_handler) {
206 FD_SET(fd, &write_mask);
207
208 if (fd > maxfd)
209 maxfd = fd;
210 }
211 }
212
213 if (maxfd++ == 0)
214 return -1;
215
216 getCurrentTime();
217
a2f5277a 218 ++ statCounter.syscalls.selects;
663c0a38 219
f6d258c8 220 if (select(maxfd, &read_mask, &write_mask, NULL, &zero_tv) < 1)
663c0a38 221 return incoming_sockets_accepted;
222
a2f5277a 223 for (i = 0; i < nfds; ++i) {
663c0a38 224 fd = fds[i];
225
08fb215c 226 if (FD_ISSET(fd, &read_mask)) {
663c0a38 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);
231 } else {
e0236918 232 debugs(5, DBG_IMPORTANT, "comm_select_incoming: FD " << fd << " NULL read handler");
663c0a38 233 }
234 }
235
08fb215c 236 if (FD_ISSET(fd, &write_mask)) {
663c0a38 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);
241 } else {
e0236918 242 debugs(5, DBG_IMPORTANT, "comm_select_incoming: FD " << fd << " NULL write handler");
663c0a38 243 }
244 }
245 }
246
247 return incoming_sockets_accepted;
248}
249
250static void
65d448bc 251comm_select_udp_incoming(void)
663c0a38 252{
253 int nfds = 0;
254 int fds[2];
255 int nevents;
65d448bc 256 udp_io_events = 0;
663c0a38 257
f207fe64
FC
258 if (Comm::IsConnOpen(icpIncomingConn)) {
259 fds[nfds] = icpIncomingConn->fd;
260 ++nfds;
261 }
663c0a38 262
f207fe64
FC
263 if (Comm::IsConnOpen(icpOutgoingConn) && icpIncomingConn != icpOutgoingConn) {
264 fds[nfds] = icpOutgoingConn->fd;
265 ++nfds;
266 }
663c0a38 267
268 if (nfds == 0)
269 return;
270
271 nevents = comm_check_incoming_select_handlers(nfds, fds);
272
f6d258c8 273 incoming_udp_interval += Config.comm_incoming.udp.average - nevents;
663c0a38 274
65d448bc
AJ
275 if (incoming_udp_interval < 0)
276 incoming_udp_interval = 0;
663c0a38 277
65d448bc
AJ
278 if (incoming_udp_interval > MAX_INCOMING_INTERVAL)
279 incoming_udp_interval = MAX_INCOMING_INTERVAL;
663c0a38 280
65d448bc
AJ
281 if (nevents > INCOMING_UDP_MAX)
282 nevents = INCOMING_UDP_MAX;
663c0a38 283
65d448bc 284 statCounter.comm_udp_incoming.count(nevents);
663c0a38 285}
286
287static void
65d448bc 288comm_select_tcp_incoming(void)
663c0a38 289{
290 int nfds = 0;
65d448bc 291 int fds[MAXTCPLISTENPORTS];
663c0a38 292 int nevents;
65d448bc 293 tcp_io_events = 0;
663c0a38 294
65d448bc 295 // XXX: only poll sockets that won't be deferred. But how do we identify them?
663c0a38 296
d00790b2 297 for (AnyP::PortCfgPointer s = HttpPortList; s != NULL; s = s->next) {
f207fe64
FC
298 if (Comm::IsConnOpen(s->listenConn)) {
299 fds[nfds] = s->listenConn->fd;
300 ++nfds;
301 }
663c0a38 302 }
303
304 nevents = comm_check_incoming_select_handlers(nfds, fds);
f6d258c8 305 incoming_tcp_interval += Config.comm_incoming.tcp.average - nevents;
663c0a38 306
65d448bc
AJ
307 if (incoming_tcp_interval < 0)
308 incoming_tcp_interval = 0;
663c0a38 309
65d448bc
AJ
310 if (incoming_tcp_interval > MAX_INCOMING_INTERVAL)
311 incoming_tcp_interval = MAX_INCOMING_INTERVAL;
663c0a38 312
65d448bc
AJ
313 if (nevents > INCOMING_TCP_MAX)
314 nevents = INCOMING_TCP_MAX;
663c0a38 315
65d448bc 316 statCounter.comm_tcp_incoming.count(nevents);
663c0a38 317}
318
319#define DEBUG_FDBITS 0
320/* Select on all sockets; call handlers for those that are ready. */
c8407295 321Comm::Flag
d841c88d 322Comm::DoSelect(int msec)
663c0a38 323{
324 fd_set readfds;
325 fd_set pendingfds;
326 fd_set writefds;
663c0a38 327
328 PF *hdl = NULL;
329 int fd;
330 int maxfd;
331 int num;
332 int pending;
f6d258c8 333 int calldns = 0, calludp = 0, calltcp = 0;
663c0a38 334 int j;
335#if DEBUG_FDBITS
336
337 int i;
338#endif
663c0a38 339 struct timeval poll_time;
340 double timeout = current_dtime + (msec / 1000.0);
341 fde *F;
342
343 int no_bits;
344 fd_set errfds;
345 FD_ZERO(&errfds);
346
347 do {
348 double start;
349 getCurrentTime();
350 start = current_dtime;
663c0a38 351
65d448bc
AJ
352 if (commCheckUdpIncoming)
353 comm_select_udp_incoming();
663c0a38 354
65d448bc 355 if (commCheckDnsIncoming)
663c0a38 356 comm_select_dns_incoming();
357
65d448bc
AJ
358 if (commCheckTcpIncoming)
359 comm_select_tcp_incoming();
663c0a38 360
f6d258c8 361 calldns = calludp = calltcp = 0;
663c0a38 362
363 maxfd = Biggest_FD + 1;
364
41d00cd3 365 memcpy(&readfds, &global_readfds, sizeof(global_readfds));
663c0a38 366
41d00cd3 367 memcpy(&writefds, &global_writefds, sizeof(global_writefds));
663c0a38 368
41d00cd3 369 memcpy(&errfds, &global_writefds, sizeof(global_writefds));
663c0a38 370
371 /* remove stalled FDs, and deal with pending descriptors */
372 pending = 0;
373
374 FD_ZERO(&pendingfds);
375
a2f5277a 376 for (j = 0; j < (int) readfds.fd_count; ++j) {
663c0a38 377 register int readfds_handle = readfds.fd_array[j];
378 no_bits = 1;
379
f412b2d6 380 for ( fd = Biggest_FD; fd; --fd ) {
663c0a38 381 if ( fd_table[fd].win32.handle == readfds_handle ) {
382 if (fd_table[fd].flags.open) {
383 no_bits = 0;
384 break;
385 }
386 }
387 }
388
389 if (no_bits)
390 continue;
391
08fb215c 392 if (FD_ISSET(fd, &readfds) && fd_table[fd].flags.read_pending) {
663c0a38 393 FD_SET(fd, &pendingfds);
a2f5277a 394 ++pending;
663c0a38 395 }
396 }
397
398#if DEBUG_FDBITS
a2f5277a 399 for (i = 0; i < maxfd; ++i) {
663c0a38 400 /* Check each open socket for a handler. */
401
402 if (fd_table[i].read_handler) {
08fb215c 403 assert(FD_ISSET(i, readfds));
663c0a38 404 }
405
406 if (fd_table[i].write_handler) {
08fb215c 407 assert(FD_ISSET(i, writefds));
663c0a38 408 }
409 }
410
411#endif
412 if (nreadfds + nwritefds == 0) {
413 assert(shutting_down);
23ff0bee 414 return Comm::SHUTDOWN;
663c0a38 415 }
416
417 if (msec > MAX_POLL_TIME)
418 msec = MAX_POLL_TIME;
419
663c0a38 420 if (pending)
421 msec = 0;
422
423 for (;;) {
424 poll_time.tv_sec = msec / 1000;
425 poll_time.tv_usec = (msec % 1000) * 1000;
f6d258c8 426 ++ statCounter.syscalls.selects;
663c0a38 427 num = select(maxfd, &readfds, &writefds, &errfds, &poll_time);
f6d258c8 428 ++ statCounter.select_loops;
663c0a38 429
430 if (num >= 0 || pending > 0)
431 break;
432
433 if (ignoreErrno(errno))
434 break;
435
fa84c01d 436 debugs(5, DBG_CRITICAL, "comm_select: select failure: " << xstrerror());
663c0a38 437
438 examine_select(&readfds, &writefds);
439
4ee57cbe 440 return Comm::COMM_ERROR;
663c0a38 441
442 /* NOTREACHED */
443 }
444
445 if (num < 0 && !pending)
446 continue;
447
448 getCurrentTime();
449
1b826af5 450 debugs(5, num ? 5 : 8, "comm_select: " << num << "+" << pending << " FDs ready");
663c0a38 451
f30f7998 452 statCounter.select_fds_hist.count(num);
663c0a38 453
663c0a38 454 if (num == 0 && pending == 0)
455 continue;
456
457 /* Scan return fd masks for ready descriptors */
663c0a38 458 assert(readfds.fd_count <= (unsigned int) Biggest_FD);
663c0a38 459 assert(pendingfds.fd_count <= (unsigned int) Biggest_FD);
460
a2f5277a 461 for (j = 0; j < (int) readfds.fd_count; ++j) {
663c0a38 462 register int readfds_handle = readfds.fd_array[j];
463 register int pendingfds_handle = pendingfds.fd_array[j];
464 register int osfhandle;
465 no_bits = 1;
466
f412b2d6 467 for ( fd = Biggest_FD; fd; --fd ) {
663c0a38 468 osfhandle = fd_table[fd].win32.handle;
469
470 if (( osfhandle == readfds_handle ) ||
471 ( osfhandle == pendingfds_handle )) {
472 if (fd_table[fd].flags.open) {
473 no_bits = 0;
474 break;
475 }
476 }
477 }
478
479 if (no_bits)
480 continue;
481
482#if DEBUG_FDBITS
483
bf8fe701 484 debugs(5, 9, "FD " << fd << " bit set for reading");
663c0a38 485
08fb215c 486 assert(FD_ISSET(fd, readfds));
663c0a38 487
488#endif
489
65d448bc
AJ
490 if (fdIsUdpListener(fd)) {
491 calludp = 1;
663c0a38 492 continue;
493 }
494
495 if (fdIsDns(fd)) {
496 calldns = 1;
497 continue;
498 }
499
65d448bc
AJ
500 if (fdIsTcpListener(fd)) {
501 calltcp = 1;
663c0a38 502 continue;
503 }
504
505 F = &fd_table[fd];
bf8fe701 506 debugs(5, 6, "comm_select: FD " << fd << " ready for reading");
663c0a38 507
65d448bc 508 if ((hdl = F->read_handler)) {
663c0a38 509 F->read_handler = NULL;
1b826af5 510 F->flags.read_pending = 0;
663c0a38 511 commUpdateReadBits(fd, NULL);
512 hdl(fd, F->read_data);
a2f5277a 513 ++ statCounter.select_fds;
663c0a38 514
65d448bc
AJ
515 if (commCheckUdpIncoming)
516 comm_select_udp_incoming();
663c0a38 517
65d448bc 518 if (commCheckDnsIncoming)
663c0a38 519 comm_select_dns_incoming();
520
65d448bc
AJ
521 if (commCheckTcpIncoming)
522 comm_select_tcp_incoming();
663c0a38 523 }
524 }
525
526 assert(errfds.fd_count <= (unsigned int) Biggest_FD);
527
a2f5277a 528 for (j = 0; j < (int) errfds.fd_count; ++j) {
663c0a38 529 register int errfds_handle = errfds.fd_array[j];
530
f412b2d6 531 for ( fd = Biggest_FD; fd; --fd ) {
663c0a38 532 if ( fd_table[fd].win32.handle == errfds_handle )
533 break;
534 }
535
536 if (fd_table[fd].flags.open) {
537 F = &fd_table[fd];
538
539 if ((hdl = F->write_handler)) {
540 F->write_handler = NULL;
541 commUpdateWriteBits(fd, NULL);
542 hdl(fd, F->write_data);
a2f5277a 543 ++ statCounter.select_fds;
663c0a38 544 }
545 }
546 }
547
548 assert(writefds.fd_count <= (unsigned int) Biggest_FD);
549
a2f5277a 550 for (j = 0; j < (int) writefds.fd_count; ++j) {
663c0a38 551 register int writefds_handle = writefds.fd_array[j];
552 no_bits = 1;
553
f412b2d6 554 for ( fd = Biggest_FD; fd; --fd ) {
663c0a38 555 if ( fd_table[fd].win32.handle == writefds_handle ) {
556 if (fd_table[fd].flags.open) {
557 no_bits = 0;
558 break;
559 }
560 }
561 }
562
563 if (no_bits)
564 continue;
565
566#if DEBUG_FDBITS
567
bf8fe701 568 debugs(5, 9, "FD " << fd << " bit set for writing");
663c0a38 569
08fb215c 570 assert(FD_ISSET(fd, writefds));
663c0a38 571
572#endif
573
65d448bc
AJ
574 if (fdIsUdpListener(fd)) {
575 calludp = 1;
663c0a38 576 continue;
577 }
578
579 if (fdIsDns(fd)) {
580 calldns = 1;
581 continue;
582 }
583
65d448bc
AJ
584 if (fdIsTcpListener(fd)) {
585 calltcp = 1;
663c0a38 586 continue;
587 }
588
589 F = &fd_table[fd];
48e7baac 590 debugs(5, 6, "comm_select: FD " << fd << " ready for writing");
663c0a38 591
592 if ((hdl = F->write_handler)) {
593 F->write_handler = NULL;
594 commUpdateWriteBits(fd, NULL);
595 hdl(fd, F->write_data);
a2f5277a 596 ++ statCounter.select_fds;
663c0a38 597
65d448bc
AJ
598 if (commCheckUdpIncoming)
599 comm_select_udp_incoming();
663c0a38 600
65d448bc 601 if (commCheckDnsIncoming)
663c0a38 602 comm_select_dns_incoming();
603
65d448bc
AJ
604 if (commCheckTcpIncoming)
605 comm_select_tcp_incoming();
663c0a38 606 }
607 }
608
65d448bc
AJ
609 if (calludp)
610 comm_select_udp_incoming();
663c0a38 611
612 if (calldns)
613 comm_select_dns_incoming();
614
65d448bc
AJ
615 if (calltcp)
616 comm_select_tcp_incoming();
663c0a38 617
663c0a38 618 getCurrentTime();
619
620 statCounter.select_time += (current_dtime - start);
621
c8407295 622 return Comm::OK;
3d0ac046 623 } while (timeout > current_dtime);
4a7a3d56 624 debugs(5, 8, "comm_select: time out: " << squid_curtime);
663c0a38 625
c8407295 626 return Comm::TIMEOUT;
663c0a38 627}
628
629static void
630comm_select_dns_incoming(void)
631{
632 int nfds = 0;
4d6c8504 633 int fds[3];
663c0a38 634 int nevents;
635 dns_io_events = 0;
636
055421ee 637 if (DnsSocketA < 0 && DnsSocketB < 0)
663c0a38 638 return;
639
f207fe64
FC
640 if (DnsSocketA >= 0) {
641 fds[nfds] = DnsSocketA;
642 ++nfds;
643 }
4d6c8504 644
f207fe64
FC
645 if (DnsSocketB >= 0) {
646 fds[nfds] = DnsSocketB;
647 ++nfds;
648 }
663c0a38 649
650 nevents = comm_check_incoming_select_handlers(nfds, fds);
651
652 if (nevents < 0)
653 return;
654
65d448bc 655 incoming_dns_interval += Config.comm_incoming.dns.average - nevents;
663c0a38 656
65d448bc
AJ
657 if (incoming_dns_interval < Config.comm_incoming.dns.min_poll)
658 incoming_dns_interval = Config.comm_incoming.dns.min_poll;
663c0a38 659
660 if (incoming_dns_interval > MAX_INCOMING_INTERVAL)
661 incoming_dns_interval = MAX_INCOMING_INTERVAL;
662
663 if (nevents > INCOMING_DNS_MAX)
664 nevents = INCOMING_DNS_MAX;
665
f30f7998 666 statCounter.comm_dns_incoming.count(nevents);
663c0a38 667}
668
669void
d841c88d 670Comm::SelectLoopInit(void)
663c0a38 671{
672 zero_tv.tv_sec = 0;
673 zero_tv.tv_usec = 0;
674 FD_ZERO(&global_readfds);
675 FD_ZERO(&global_writefds);
676 nreadfds = nwritefds = 0;
da9b2c49 677
d841c88d
AJ
678 Mgr::RegisterAction("comm_select_incoming",
679 "comm_incoming() stats",
680 commIncomingStats, 0, 1);
663c0a38 681}
682
663c0a38 683/*
684 * examine_select - debug routine.
685 *
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:
26ac0430 690 *
663c0a38 691 * Call this from where the select loop fails.
692 */
693static int
694examine_select(fd_set * readfds, fd_set * writefds)
695{
696 int fd = 0;
697 fd_set read_x;
698 fd_set write_x;
699
700 struct timeval tv;
6d527e0a 701 AsyncCall::Pointer ch = NULL;
663c0a38 702 fde *F = NULL;
703
704 struct stat sb;
fa84c01d 705 debugs(5, DBG_CRITICAL, "examine_select: Examining open file descriptors...");
663c0a38 706
a2f5277a 707 for (fd = 0; fd < Squid_MaxFD; ++fd) {
663c0a38 708 FD_ZERO(&read_x);
709 FD_ZERO(&write_x);
710 tv.tv_sec = tv.tv_usec = 0;
711
08fb215c 712 if (FD_ISSET(fd, readfds))
663c0a38 713 FD_SET(fd, &read_x);
08fb215c 714 else if (FD_ISSET(fd, writefds))
663c0a38 715 FD_SET(fd, &write_x);
716 else
717 continue;
718
a2f5277a 719 ++ statCounter.syscalls.selects;
663c0a38 720 errno = 0;
721
722 if (!fstat(fd, &sb)) {
bf8fe701 723 debugs(5, 5, "FD " << fd << " is valid.");
663c0a38 724 continue;
725 }
726
727 F = &fd_table[fd];
fa84c01d
FC
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);
663c0a38 732
1b826af5 733 for (ch = F->closeHandler; ch != NULL; ch = ch->Next())
fa84c01d 734 debugs(5, DBG_CRITICAL, " close handler: " << ch);
663c0a38 735
6d527e0a 736 if (F->closeHandler != NULL) {
663c0a38 737 commCallCloseHandlers(fd);
6d527e0a 738 } else if (F->timeoutHandler != NULL) {
fa84c01d 739 debugs(5, DBG_CRITICAL, "examine_select: Calling Timeout Handler");
26ac0430 740 ScheduleCallHere(F->timeoutHandler);
663c0a38 741 }
742
743 F->closeHandler = NULL;
6d527e0a 744 F->timeoutHandler = NULL;
663c0a38 745 F->read_handler = NULL;
746 F->write_handler = NULL;
747 FD_CLR(fd, readfds);
748 FD_CLR(fd, writefds);
749 }
750
751 return 0;
752}
753
663c0a38 754static void
755commIncomingStats(StoreEntry * sentry)
756{
65d448bc
AJ
757 storeAppendPrintf(sentry, "Current incoming_udp_interval: %d\n",
758 incoming_udp_interval >> INCOMING_FACTOR);
663c0a38 759 storeAppendPrintf(sentry, "Current incoming_dns_interval: %d\n",
760 incoming_dns_interval >> INCOMING_FACTOR);
65d448bc
AJ
761 storeAppendPrintf(sentry, "Current incoming_tcp_interval: %d\n",
762 incoming_tcp_interval >> INCOMING_FACTOR);
663c0a38 763 storeAppendPrintf(sentry, "\n");
764 storeAppendPrintf(sentry, "Histogram of events per incoming socket type\n");
65d448bc
AJ
765 storeAppendPrintf(sentry, "ICP Messages handled per comm_select_udp_incoming() call:\n");
766 statCounter.comm_udp_incoming.dump(sentry, statHistIntDumper);
663c0a38 767 storeAppendPrintf(sentry, "DNS Messages handled per comm_select_dns_incoming() call:\n");
82146cc8 768 statCounter.comm_dns_incoming.dump(sentry, statHistIntDumper);
65d448bc
AJ
769 storeAppendPrintf(sentry, "HTTP Messages handled per comm_select_tcp_incoming() call:\n");
770 statCounter.comm_tcp_incoming.dump(sentry, statHistIntDumper);
663c0a38 771}
772
773void
774commUpdateReadBits(int fd, PF * handler)
775{
08fb215c 776 if (handler && !FD_ISSET(fd, &global_readfds)) {
663c0a38 777 FD_SET(fd, &global_readfds);
a2f5277a 778 ++nreadfds;
08fb215c 779 } else if (!handler && FD_ISSET(fd, &global_readfds)) {
663c0a38 780 FD_CLR(fd, &global_readfds);
a2f5277a 781 --nreadfds;
663c0a38 782 }
783}
784
785void
786commUpdateWriteBits(int fd, PF * handler)
787{
08fb215c 788 if (handler && !FD_ISSET(fd, &global_writefds)) {
663c0a38 789 FD_SET(fd, &global_writefds);
a2f5277a 790 ++nwritefds;
08fb215c 791 } else if (!handler && FD_ISSET(fd, &global_writefds)) {
663c0a38 792 FD_CLR(fd, &global_writefds);
a2f5277a 793 --nwritefds;
663c0a38 794 }
795}
796
797/* Called by async-io or diskd to speed up the polling */
798void
d841c88d 799Comm::QuickPollRequired(void)
663c0a38 800{
801 MAX_POLL_TIME = 10;
802}
803
804#endif /* USE_SELECT_WIN32 */
f53969cc 805