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