]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm/ModSelectWin32.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / comm / ModSelectWin32.cc
CommitLineData
663c0a38 1/*
5b74111a 2 * Copyright (C) 1996-2018 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);
b69e9ffa 428 int xerrno = errno;
f6d258c8 429 ++ statCounter.select_loops;
663c0a38 430
431 if (num >= 0 || pending > 0)
432 break;
433
b69e9ffa 434 if (ignoreErrno(xerrno))
663c0a38 435 break;
436
b69e9ffa 437 debugs(5, DBG_CRITICAL, MYNAME << "WARNING: select failure: " << xstrerr(xerrno));
663c0a38 438
439 examine_select(&readfds, &writefds);
440
4ee57cbe 441 return Comm::COMM_ERROR;
663c0a38 442
443 /* NOTREACHED */
444 }
445
446 if (num < 0 && !pending)
447 continue;
448
449 getCurrentTime();
450
1b826af5 451 debugs(5, num ? 5 : 8, "comm_select: " << num << "+" << pending << " FDs ready");
663c0a38 452
f30f7998 453 statCounter.select_fds_hist.count(num);
663c0a38 454
663c0a38 455 if (num == 0 && pending == 0)
456 continue;
457
458 /* Scan return fd masks for ready descriptors */
663c0a38 459 assert(readfds.fd_count <= (unsigned int) Biggest_FD);
663c0a38 460 assert(pendingfds.fd_count <= (unsigned int) Biggest_FD);
461
a2f5277a 462 for (j = 0; j < (int) readfds.fd_count; ++j) {
663c0a38 463 register int readfds_handle = readfds.fd_array[j];
464 register int pendingfds_handle = pendingfds.fd_array[j];
465 register int osfhandle;
466 no_bits = 1;
467
f412b2d6 468 for ( fd = Biggest_FD; fd; --fd ) {
663c0a38 469 osfhandle = fd_table[fd].win32.handle;
470
471 if (( osfhandle == readfds_handle ) ||
472 ( osfhandle == pendingfds_handle )) {
473 if (fd_table[fd].flags.open) {
474 no_bits = 0;
475 break;
476 }
477 }
478 }
479
480 if (no_bits)
481 continue;
482
483#if DEBUG_FDBITS
484
bf8fe701 485 debugs(5, 9, "FD " << fd << " bit set for reading");
663c0a38 486
08fb215c 487 assert(FD_ISSET(fd, readfds));
663c0a38 488
489#endif
490
65d448bc
AJ
491 if (fdIsUdpListener(fd)) {
492 calludp = 1;
663c0a38 493 continue;
494 }
495
496 if (fdIsDns(fd)) {
497 calldns = 1;
498 continue;
499 }
500
65d448bc
AJ
501 if (fdIsTcpListener(fd)) {
502 calltcp = 1;
663c0a38 503 continue;
504 }
505
506 F = &fd_table[fd];
bf8fe701 507 debugs(5, 6, "comm_select: FD " << fd << " ready for reading");
663c0a38 508
65d448bc 509 if ((hdl = F->read_handler)) {
663c0a38 510 F->read_handler = NULL;
1b826af5 511 F->flags.read_pending = 0;
663c0a38 512 commUpdateReadBits(fd, NULL);
513 hdl(fd, F->read_data);
a2f5277a 514 ++ statCounter.select_fds;
663c0a38 515
65d448bc
AJ
516 if (commCheckUdpIncoming)
517 comm_select_udp_incoming();
663c0a38 518
65d448bc 519 if (commCheckDnsIncoming)
663c0a38 520 comm_select_dns_incoming();
521
65d448bc
AJ
522 if (commCheckTcpIncoming)
523 comm_select_tcp_incoming();
663c0a38 524 }
525 }
526
527 assert(errfds.fd_count <= (unsigned int) Biggest_FD);
528
a2f5277a 529 for (j = 0; j < (int) errfds.fd_count; ++j) {
663c0a38 530 register int errfds_handle = errfds.fd_array[j];
531
f412b2d6 532 for ( fd = Biggest_FD; fd; --fd ) {
663c0a38 533 if ( fd_table[fd].win32.handle == errfds_handle )
534 break;
535 }
536
537 if (fd_table[fd].flags.open) {
538 F = &fd_table[fd];
539
540 if ((hdl = F->write_handler)) {
541 F->write_handler = NULL;
542 commUpdateWriteBits(fd, NULL);
543 hdl(fd, F->write_data);
a2f5277a 544 ++ statCounter.select_fds;
663c0a38 545 }
546 }
547 }
548
549 assert(writefds.fd_count <= (unsigned int) Biggest_FD);
550
a2f5277a 551 for (j = 0; j < (int) writefds.fd_count; ++j) {
663c0a38 552 register int writefds_handle = writefds.fd_array[j];
553 no_bits = 1;
554
f412b2d6 555 for ( fd = Biggest_FD; fd; --fd ) {
663c0a38 556 if ( fd_table[fd].win32.handle == writefds_handle ) {
557 if (fd_table[fd].flags.open) {
558 no_bits = 0;
559 break;
560 }
561 }
562 }
563
564 if (no_bits)
565 continue;
566
567#if DEBUG_FDBITS
568
bf8fe701 569 debugs(5, 9, "FD " << fd << " bit set for writing");
663c0a38 570
08fb215c 571 assert(FD_ISSET(fd, writefds));
663c0a38 572
573#endif
574
65d448bc
AJ
575 if (fdIsUdpListener(fd)) {
576 calludp = 1;
663c0a38 577 continue;
578 }
579
580 if (fdIsDns(fd)) {
581 calldns = 1;
582 continue;
583 }
584
65d448bc
AJ
585 if (fdIsTcpListener(fd)) {
586 calltcp = 1;
663c0a38 587 continue;
588 }
589
590 F = &fd_table[fd];
48e7baac 591 debugs(5, 6, "comm_select: FD " << fd << " ready for writing");
663c0a38 592
593 if ((hdl = F->write_handler)) {
594 F->write_handler = NULL;
595 commUpdateWriteBits(fd, NULL);
596 hdl(fd, F->write_data);
a2f5277a 597 ++ statCounter.select_fds;
663c0a38 598
65d448bc
AJ
599 if (commCheckUdpIncoming)
600 comm_select_udp_incoming();
663c0a38 601
65d448bc 602 if (commCheckDnsIncoming)
663c0a38 603 comm_select_dns_incoming();
604
65d448bc
AJ
605 if (commCheckTcpIncoming)
606 comm_select_tcp_incoming();
663c0a38 607 }
608 }
609
65d448bc
AJ
610 if (calludp)
611 comm_select_udp_incoming();
663c0a38 612
613 if (calldns)
614 comm_select_dns_incoming();
615
65d448bc
AJ
616 if (calltcp)
617 comm_select_tcp_incoming();
663c0a38 618
663c0a38 619 getCurrentTime();
620
621 statCounter.select_time += (current_dtime - start);
622
c8407295 623 return Comm::OK;
3d0ac046 624 } while (timeout > current_dtime);
4a7a3d56 625 debugs(5, 8, "comm_select: time out: " << squid_curtime);
663c0a38 626
c8407295 627 return Comm::TIMEOUT;
663c0a38 628}
629
630static void
631comm_select_dns_incoming(void)
632{
633 int nfds = 0;
4d6c8504 634 int fds[3];
663c0a38 635 int nevents;
636 dns_io_events = 0;
637
055421ee 638 if (DnsSocketA < 0 && DnsSocketB < 0)
663c0a38 639 return;
640
f207fe64
FC
641 if (DnsSocketA >= 0) {
642 fds[nfds] = DnsSocketA;
643 ++nfds;
644 }
4d6c8504 645
f207fe64
FC
646 if (DnsSocketB >= 0) {
647 fds[nfds] = DnsSocketB;
648 ++nfds;
649 }
663c0a38 650
651 nevents = comm_check_incoming_select_handlers(nfds, fds);
652
653 if (nevents < 0)
654 return;
655
65d448bc 656 incoming_dns_interval += Config.comm_incoming.dns.average - nevents;
663c0a38 657
65d448bc
AJ
658 if (incoming_dns_interval < Config.comm_incoming.dns.min_poll)
659 incoming_dns_interval = Config.comm_incoming.dns.min_poll;
663c0a38 660
661 if (incoming_dns_interval > MAX_INCOMING_INTERVAL)
662 incoming_dns_interval = MAX_INCOMING_INTERVAL;
663
664 if (nevents > INCOMING_DNS_MAX)
665 nevents = INCOMING_DNS_MAX;
666
f30f7998 667 statCounter.comm_dns_incoming.count(nevents);
663c0a38 668}
669
670void
d841c88d 671Comm::SelectLoopInit(void)
663c0a38 672{
673 zero_tv.tv_sec = 0;
674 zero_tv.tv_usec = 0;
675 FD_ZERO(&global_readfds);
676 FD_ZERO(&global_writefds);
677 nreadfds = nwritefds = 0;
da9b2c49 678
d841c88d
AJ
679 Mgr::RegisterAction("comm_select_incoming",
680 "comm_incoming() stats",
681 commIncomingStats, 0, 1);
663c0a38 682}
683
663c0a38 684/*
685 * examine_select - debug routine.
686 *
687 * I spend the day chasing this core dump that occurs when both the client
688 * and the server side of a cache fetch simultaneoulsy abort the
689 * connection. While I haven't really studied the code to figure out how
690 * it happens, the snippet below may prevent the cache from exitting:
26ac0430 691 *
663c0a38 692 * Call this from where the select loop fails.
693 */
694static int
695examine_select(fd_set * readfds, fd_set * writefds)
696{
697 int fd = 0;
698 fd_set read_x;
699 fd_set write_x;
700
701 struct timeval tv;
6d527e0a 702 AsyncCall::Pointer ch = NULL;
663c0a38 703 fde *F = NULL;
704
705 struct stat sb;
fa84c01d 706 debugs(5, DBG_CRITICAL, "examine_select: Examining open file descriptors...");
663c0a38 707
a2f5277a 708 for (fd = 0; fd < Squid_MaxFD; ++fd) {
663c0a38 709 FD_ZERO(&read_x);
710 FD_ZERO(&write_x);
711 tv.tv_sec = tv.tv_usec = 0;
712
08fb215c 713 if (FD_ISSET(fd, readfds))
663c0a38 714 FD_SET(fd, &read_x);
08fb215c 715 else if (FD_ISSET(fd, writefds))
663c0a38 716 FD_SET(fd, &write_x);
717 else
718 continue;
719
a2f5277a 720 ++ statCounter.syscalls.selects;
663c0a38 721 errno = 0;
722
723 if (!fstat(fd, &sb)) {
bf8fe701 724 debugs(5, 5, "FD " << fd << " is valid.");
663c0a38 725 continue;
726 }
b69e9ffa 727 int xerrno = errno;
663c0a38 728
729 F = &fd_table[fd];
b69e9ffa 730 debugs(5, DBG_CRITICAL, "fstat(FD " << fd << "): " << xstrerr(xerrno));
fa84c01d
FC
731 debugs(5, DBG_CRITICAL, "WARNING: FD " << fd << " has handlers, but it's invalid.");
732 debugs(5, DBG_CRITICAL, "FD " << fd << " is a " << fdTypeStr[F->type] << " called '" << F->desc << "'");
733 debugs(5, DBG_CRITICAL, "tmout:" << F->timeoutHandler << " read:" << F->read_handler << " write:" << F->write_handler);
663c0a38 734
1b826af5 735 for (ch = F->closeHandler; ch != NULL; ch = ch->Next())
fa84c01d 736 debugs(5, DBG_CRITICAL, " close handler: " << ch);
663c0a38 737
6d527e0a 738 if (F->closeHandler != NULL) {
663c0a38 739 commCallCloseHandlers(fd);
6d527e0a 740 } else if (F->timeoutHandler != NULL) {
fa84c01d 741 debugs(5, DBG_CRITICAL, "examine_select: Calling Timeout Handler");
26ac0430 742 ScheduleCallHere(F->timeoutHandler);
663c0a38 743 }
744
745 F->closeHandler = NULL;
6d527e0a 746 F->timeoutHandler = NULL;
663c0a38 747 F->read_handler = NULL;
748 F->write_handler = NULL;
749 FD_CLR(fd, readfds);
750 FD_CLR(fd, writefds);
751 }
752
753 return 0;
754}
755
663c0a38 756static void
757commIncomingStats(StoreEntry * sentry)
758{
65d448bc
AJ
759 storeAppendPrintf(sentry, "Current incoming_udp_interval: %d\n",
760 incoming_udp_interval >> INCOMING_FACTOR);
663c0a38 761 storeAppendPrintf(sentry, "Current incoming_dns_interval: %d\n",
762 incoming_dns_interval >> INCOMING_FACTOR);
65d448bc
AJ
763 storeAppendPrintf(sentry, "Current incoming_tcp_interval: %d\n",
764 incoming_tcp_interval >> INCOMING_FACTOR);
663c0a38 765 storeAppendPrintf(sentry, "\n");
766 storeAppendPrintf(sentry, "Histogram of events per incoming socket type\n");
65d448bc
AJ
767 storeAppendPrintf(sentry, "ICP Messages handled per comm_select_udp_incoming() call:\n");
768 statCounter.comm_udp_incoming.dump(sentry, statHistIntDumper);
663c0a38 769 storeAppendPrintf(sentry, "DNS Messages handled per comm_select_dns_incoming() call:\n");
82146cc8 770 statCounter.comm_dns_incoming.dump(sentry, statHistIntDumper);
65d448bc
AJ
771 storeAppendPrintf(sentry, "HTTP Messages handled per comm_select_tcp_incoming() call:\n");
772 statCounter.comm_tcp_incoming.dump(sentry, statHistIntDumper);
663c0a38 773}
774
775void
776commUpdateReadBits(int fd, PF * handler)
777{
08fb215c 778 if (handler && !FD_ISSET(fd, &global_readfds)) {
663c0a38 779 FD_SET(fd, &global_readfds);
a2f5277a 780 ++nreadfds;
08fb215c 781 } else if (!handler && FD_ISSET(fd, &global_readfds)) {
663c0a38 782 FD_CLR(fd, &global_readfds);
a2f5277a 783 --nreadfds;
663c0a38 784 }
785}
786
787void
788commUpdateWriteBits(int fd, PF * handler)
789{
08fb215c 790 if (handler && !FD_ISSET(fd, &global_writefds)) {
663c0a38 791 FD_SET(fd, &global_writefds);
a2f5277a 792 ++nwritefds;
08fb215c 793 } else if (!handler && FD_ISSET(fd, &global_writefds)) {
663c0a38 794 FD_CLR(fd, &global_writefds);
a2f5277a 795 --nwritefds;
663c0a38 796 }
797}
798
799/* Called by async-io or diskd to speed up the polling */
800void
d841c88d 801Comm::QuickPollRequired(void)
663c0a38 802{
803 MAX_POLL_TIME = 10;
804}
805
806#endif /* USE_SELECT_WIN32 */
f53969cc 807