2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 05 Socket Functions */
15 #include "anyp/PortCfg.h"
16 #include "comm/Connection.h"
17 #include "comm/Loops.h"
21 #include "mgr/Registration.h"
22 #include "SquidConfig.h"
23 #include "StatCounters.h"
32 static int MAX_POLL_TIME
= 1000; /* see also Comm::QuickPollRequired() */
35 #define howmany(x, y) (((x)+((y)-1))/(y))
40 #define FD_MASK_BYTES sizeof(fd_mask)
41 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
44 static int examine_select(fd_set
*, fd_set
*);
45 static int fdIsTcpListener(int fd
);
46 static int fdIsUdpListener(int fd
);
47 static int fdIsDns(int fd
);
48 static OBJH commIncomingStats
;
49 static int comm_check_incoming_select_handlers(int nfds
, int *fds
);
50 static void comm_select_dns_incoming(void);
51 static void commUpdateReadBits(int fd
, PF
* handler
);
52 static void commUpdateWriteBits(int fd
, PF
* handler
);
54 static struct timeval zero_tv
;
55 static fd_set global_readfds
;
56 static fd_set global_writefds
;
61 * Automatic tuning for incoming requests:
63 * INCOMING sockets are the ICP and HTTP ports. We need to check these
64 * fairly regularly, but how often? When the load increases, we
65 * want to check the incoming sockets more often. If we have a lot
66 * of incoming ICP, then we need to check these sockets more than
67 * if we just have HTTP.
69 * The variables 'incoming_udp_interval' and 'incoming_tcp_interval'
70 * determine how many normal I/O events to process before checking
71 * incoming sockets again. Note we store the incoming_interval
72 * multiplied by a factor of (2^INCOMING_FACTOR) to have some
73 * pseudo-floating point precision.
75 * The variable 'udp_io_events' and 'tcp_io_events' counts how many normal
76 * I/O events have been processed since the last check on the incoming
77 * sockets. When io_events > incoming_interval, its time to check incoming
80 * Every time we check incoming sockets, we count how many new messages
81 * or connections were processed. This is used to adjust the
82 * incoming_interval for the next iteration. The new incoming_interval
83 * is calculated as the current incoming_interval plus what we would
84 * like to see as an average number of events minus the number of
85 * events just processed.
87 * incoming_interval = incoming_interval + target_average - number_of_events_processed
89 * There are separate incoming_interval counters for DNS, UDP and TCP events
91 * You can see the current values of the incoming_interval's, as well as
92 * a histogram of 'incoming_events' by asking the cache manager
93 * for 'comm_incoming', e.g.:
95 * % ./client mgr:comm_incoming
99 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
100 * incoming_interval for both types of sockets. At the
101 * largest value the cache will effectively be idling.
103 * - The higher the INCOMING_FACTOR, the slower the algorithm will
104 * respond to load spikes/increases/decreases in demand. A value
105 * between 3 and 8 is recommended.
108 #define MAX_INCOMING_INTEGER 256
109 #define INCOMING_FACTOR 5
110 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
111 static int udp_io_events
= 0;
112 static int dns_io_events
= 0;
113 static int tcp_io_events
= 0;
114 static int incoming_udp_interval
= 16 << INCOMING_FACTOR
;
115 static int incoming_dns_interval
= 16 << INCOMING_FACTOR
;
116 static int incoming_tcp_interval
= 16 << INCOMING_FACTOR
;
117 #define commCheckUdpIncoming (++udp_io_events > (incoming_udp_interval>> INCOMING_FACTOR))
118 #define commCheckDnsIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
119 #define commCheckTcpIncoming (++tcp_io_events > (incoming_tcp_interval>> INCOMING_FACTOR))
122 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
124 fde
*F
= &fd_table
[fd
];
126 assert(F
->flags
.open
|| (!handler
&& !client_data
&& !timeout
));
127 debugs(5, 5, "FD " << fd
<< ", type=" << type
<<
128 ", handler=" << handler
<< ", client_data=" << client_data
<<
129 ", timeout=" << timeout
);
131 if (type
& COMM_SELECT_READ
) {
132 F
->read_handler
= handler
;
133 F
->read_data
= client_data
;
134 commUpdateReadBits(fd
, handler
);
137 if (type
& COMM_SELECT_WRITE
) {
138 F
->write_handler
= handler
;
139 F
->write_data
= client_data
;
140 commUpdateWriteBits(fd
, handler
);
144 F
->timeout
= squid_curtime
+ timeout
;
148 fdIsUdpListener(int fd
)
150 if (icpIncomingConn
!= nullptr && fd
== icpIncomingConn
->fd
)
153 if (icpOutgoingConn
!= nullptr && fd
== icpOutgoingConn
->fd
)
162 if (fd
== DnsSocketA
)
165 if (fd
== DnsSocketB
)
172 fdIsTcpListener(int fd
)
174 for (AnyP::PortCfgPointer s
= HttpPortList
; s
!= nullptr; s
= s
->next
) {
175 if (s
->listenConn
!= nullptr && s
->listenConn
->fd
== fd
)
183 comm_check_incoming_select_handlers(int nfds
, int *fds
)
192 FD_ZERO(&write_mask
);
193 incoming_sockets_accepted
= 0;
195 for (i
= 0; i
< nfds
; ++i
) {
198 if (fd_table
[fd
].read_handler
) {
199 FD_SET(fd
, &read_mask
);
205 if (fd_table
[fd
].write_handler
) {
206 FD_SET(fd
, &write_mask
);
218 ++ statCounter
.syscalls
.selects
;
220 if (select(maxfd
, &read_mask
, &write_mask
, nullptr, &zero_tv
) < 1)
221 return incoming_sockets_accepted
;
223 for (i
= 0; i
< nfds
; ++i
) {
226 if (FD_ISSET(fd
, &read_mask
)) {
227 if ((hdl
= fd_table
[fd
].read_handler
) != nullptr) {
228 fd_table
[fd
].read_handler
= nullptr;
229 commUpdateReadBits(fd
, nullptr);
230 hdl(fd
, fd_table
[fd
].read_data
);
232 debugs(5, DBG_IMPORTANT
, "comm_select_incoming: FD " << fd
<< " NULL read handler");
236 if (FD_ISSET(fd
, &write_mask
)) {
237 if ((hdl
= fd_table
[fd
].write_handler
) != nullptr) {
238 fd_table
[fd
].write_handler
= nullptr;
239 commUpdateWriteBits(fd
, nullptr);
240 hdl(fd
, fd_table
[fd
].write_data
);
242 debugs(5, DBG_IMPORTANT
, "comm_select_incoming: FD " << fd
<< " NULL write handler");
247 return incoming_sockets_accepted
;
251 comm_select_udp_incoming(void)
258 if (Comm::IsConnOpen(icpIncomingConn
)) {
259 fds
[nfds
] = icpIncomingConn
->fd
;
263 if (Comm::IsConnOpen(icpOutgoingConn
) && icpIncomingConn
!= icpOutgoingConn
) {
264 fds
[nfds
] = icpOutgoingConn
->fd
;
271 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
273 incoming_udp_interval
+= Config
.comm_incoming
.udp
.average
- nevents
;
275 if (incoming_udp_interval
< 0)
276 incoming_udp_interval
= 0;
278 if (incoming_udp_interval
> MAX_INCOMING_INTERVAL
)
279 incoming_udp_interval
= MAX_INCOMING_INTERVAL
;
281 if (nevents
> INCOMING_UDP_MAX
)
282 nevents
= INCOMING_UDP_MAX
;
284 statCounter
.comm_udp_incoming
.count(nevents
);
288 comm_select_tcp_incoming(void)
291 int fds
[MAXTCPLISTENPORTS
];
295 // XXX: only poll sockets that won't be deferred. But how do we identify them?
297 for (AnyP::PortCfgPointer s
= HttpPortList
; s
!= nullptr; s
= s
->next
) {
298 if (Comm::IsConnOpen(s
->listenConn
)) {
299 fds
[nfds
] = s
->listenConn
->fd
;
304 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
305 incoming_tcp_interval
+= Config
.comm_incoming
.tcp
.average
- nevents
;
307 if (incoming_tcp_interval
< 0)
308 incoming_tcp_interval
= 0;
310 if (incoming_tcp_interval
> MAX_INCOMING_INTERVAL
)
311 incoming_tcp_interval
= MAX_INCOMING_INTERVAL
;
313 if (nevents
> INCOMING_TCP_MAX
)
314 nevents
= INCOMING_TCP_MAX
;
316 statCounter
.comm_tcp_incoming
.count(nevents
);
319 /* Select on all sockets; call handlers for those that are ready. */
321 Comm::DoSelect(int msec
)
332 int calldns
= 0, calludp
= 0, calltcp
= 0;
340 struct timeval poll_time
;
341 double timeout
= current_dtime
+ (msec
/ 1000.0);
347 start
= current_dtime
;
349 if (commCheckUdpIncoming
)
350 comm_select_udp_incoming();
352 if (commCheckDnsIncoming
)
353 comm_select_dns_incoming();
355 if (commCheckTcpIncoming
)
356 comm_select_tcp_incoming();
358 calldns
= calludp
= calltcp
= 0;
360 maxfd
= Biggest_FD
+ 1;
362 memcpy(&readfds
, &global_readfds
,
363 howmany(maxfd
, FD_MASK_BITS
) * FD_MASK_BYTES
);
365 memcpy(&writefds
, &global_writefds
,
366 howmany(maxfd
, FD_MASK_BITS
) * FD_MASK_BYTES
);
368 /* remove stalled FDs, and deal with pending descriptors */
371 FD_ZERO(&pendingfds
);
373 maxindex
= howmany(maxfd
, FD_MASK_BITS
);
375 fdsp
= (fd_mask
*) & readfds
;
377 for (j
= 0; j
< maxindex
; ++j
) {
378 if ((tmask
= fdsp
[j
]) == 0)
379 continue; /* no bits here */
381 for (k
= 0; k
< FD_MASK_BITS
; ++k
) {
382 if (!EBIT_TEST(tmask
, k
))
385 /* Found a set bit */
386 fd
= (j
* FD_MASK_BITS
) + k
;
388 if (FD_ISSET(fd
, &readfds
) && fd_table
[fd
].flags
.read_pending
) {
389 FD_SET(fd
, &pendingfds
);
395 if (nreadfds
+ nwritefds
== 0) {
396 assert(shutting_down
);
397 return Comm::SHUTDOWN
;
400 if (msec
> MAX_POLL_TIME
)
401 msec
= MAX_POLL_TIME
;
407 poll_time
.tv_sec
= msec
/ 1000;
408 poll_time
.tv_usec
= (msec
% 1000) * 1000;
409 ++ statCounter
.syscalls
.selects
;
410 num
= select(maxfd
, &readfds
, &writefds
, nullptr, &poll_time
);
412 ++ statCounter
.select_loops
;
414 if (num
>= 0 || pending
> 0)
417 if (ignoreErrno(xerrno
))
420 debugs(5, DBG_CRITICAL
, MYNAME
<< "select failure: " << xstrerr(xerrno
));
422 examine_select(&readfds
, &writefds
);
424 return Comm::COMM_ERROR
;
429 if (num
< 0 && !pending
)
434 debugs(5, num
? 5 : 8, "comm_select: " << num
<< "+" << pending
<< " FDs ready");
436 statCounter
.select_fds_hist
.count(num
);
438 if (num
== 0 && pending
== 0)
441 /* Scan return fd masks for ready descriptors */
442 fdsp
= (fd_mask
*) & readfds
;
444 pfdsp
= (fd_mask
*) & pendingfds
;
446 maxindex
= howmany(maxfd
, FD_MASK_BITS
);
448 for (j
= 0; j
< maxindex
; ++j
) {
449 if ((tmask
= (fdsp
[j
] | pfdsp
[j
])) == 0)
450 continue; /* no bits here */
452 for (k
= 0; k
< FD_MASK_BITS
; ++k
) {
454 break; /* no more bits left */
456 if (!EBIT_TEST(tmask
, k
))
459 /* Found a set bit */
460 fd
= (j
* FD_MASK_BITS
) + k
;
462 EBIT_CLR(tmask
, k
); /* this will be done */
464 if (fdIsUdpListener(fd
)) {
474 if (fdIsTcpListener(fd
)) {
480 debugs(5, 6, "comm_select: FD " << fd
<< " ready for reading");
482 if (nullptr == (hdl
= F
->read_handler
))
485 F
->read_handler
= nullptr;
486 commUpdateReadBits(fd
, nullptr);
487 hdl(fd
, F
->read_data
);
488 ++ statCounter
.select_fds
;
490 if (commCheckUdpIncoming
)
491 comm_select_udp_incoming();
493 if (commCheckDnsIncoming
)
494 comm_select_dns_incoming();
496 if (commCheckTcpIncoming
)
497 comm_select_tcp_incoming();
502 fdsp
= (fd_mask
*) & writefds
;
504 for (j
= 0; j
< maxindex
; ++j
) {
505 if ((tmask
= fdsp
[j
]) == 0)
506 continue; /* no bits here */
508 for (k
= 0; k
< FD_MASK_BITS
; ++k
) {
510 break; /* no more bits left */
512 if (!EBIT_TEST(tmask
, k
))
515 /* Found a set bit */
516 fd
= (j
* FD_MASK_BITS
) + k
;
518 EBIT_CLR(tmask
, k
); /* this will be done */
520 if (fdIsUdpListener(fd
)) {
530 if (fdIsTcpListener(fd
)) {
536 debugs(5, 6, "comm_select: FD " << fd
<< " ready for writing");
538 if ((hdl
= F
->write_handler
)) {
539 F
->write_handler
= nullptr;
540 commUpdateWriteBits(fd
, nullptr);
541 hdl(fd
, F
->write_data
);
542 ++ statCounter
.select_fds
;
544 if (commCheckUdpIncoming
)
545 comm_select_udp_incoming();
547 if (commCheckDnsIncoming
)
548 comm_select_dns_incoming();
550 if (commCheckTcpIncoming
)
551 comm_select_tcp_incoming();
557 comm_select_udp_incoming();
560 comm_select_dns_incoming();
563 comm_select_tcp_incoming();
567 statCounter
.select_time
+= (current_dtime
- start
);
570 } while (timeout
> current_dtime
);
571 debugs(5, 8, "comm_select: time out: " << squid_curtime
);
573 return Comm::TIMEOUT
;
577 comm_select_dns_incoming(void)
584 if (DnsSocketA
< 0 && DnsSocketB
< 0)
587 if (DnsSocketA
>= 0) {
588 fds
[nfds
] = DnsSocketA
;
592 if (DnsSocketB
>= 0) {
593 fds
[nfds
] = DnsSocketB
;
597 nevents
= comm_check_incoming_select_handlers(nfds
, fds
);
602 incoming_dns_interval
+= Config
.comm_incoming
.dns
.average
- nevents
;
604 if (incoming_dns_interval
< Config
.comm_incoming
.dns
.min_poll
)
605 incoming_dns_interval
= Config
.comm_incoming
.dns
.min_poll
;
607 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
608 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
610 if (nevents
> INCOMING_DNS_MAX
)
611 nevents
= INCOMING_DNS_MAX
;
613 statCounter
.comm_dns_incoming
.count(nevents
);
617 Comm::SelectLoopInit(void)
621 FD_ZERO(&global_readfds
);
622 FD_ZERO(&global_writefds
);
623 nreadfds
= nwritefds
= 0;
625 Mgr::RegisterAction("comm_select_incoming",
626 "comm_incoming() stats",
627 commIncomingStats
, 0, 1);
631 * examine_select - debug routine.
633 * I spend the day chasing this core dump that occurs when both the client
634 * and the server side of a cache fetch simultaneoulsy abort the
635 * connection. While I haven't really studied the code to figure out how
636 * it happens, the snippet below may prevent the cache from exitting:
638 * Call this from where the select loop fails.
641 examine_select(fd_set
* readfds
, fd_set
* writefds
)
648 AsyncCall::Pointer ch
= nullptr;
652 debugs(5, DBG_CRITICAL
, "examine_select: Examining open file descriptors...");
654 for (fd
= 0; fd
< Squid_MaxFD
; ++fd
) {
657 tv
.tv_sec
= tv
.tv_usec
= 0;
659 if (FD_ISSET(fd
, readfds
))
661 else if (FD_ISSET(fd
, writefds
))
662 FD_SET(fd
, &write_x
);
666 ++ statCounter
.syscalls
.selects
;
669 if (!fstat(fd
, &sb
)) {
670 debugs(5, 5, "FD " << fd
<< " is valid.");
676 debugs(5, DBG_CRITICAL
, "fstat(FD " << fd
<< "): " << xstrerr(xerrno
));
677 debugs(5, DBG_CRITICAL
, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
678 debugs(5, DBG_CRITICAL
, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
] << " called '" << F
->desc
<< "'");
679 debugs(5, DBG_CRITICAL
, "tmout:" << F
->timeoutHandler
<< " read:" << F
->read_handler
<< " write:" << F
->write_handler
);
681 for (ch
= F
->closeHandler
; ch
!= nullptr; ch
= ch
->Next())
682 debugs(5, DBG_CRITICAL
, " close handler: " << ch
);
684 if (F
->closeHandler
!= nullptr) {
685 commCallCloseHandlers(fd
);
686 } else if (F
->timeoutHandler
!= nullptr) {
687 debugs(5, DBG_CRITICAL
, "examine_select: Calling Timeout Handler");
688 ScheduleCallHere(F
->timeoutHandler
);
691 F
->closeHandler
= nullptr;
692 F
->timeoutHandler
= nullptr;
693 F
->read_handler
= nullptr;
694 F
->write_handler
= nullptr;
696 FD_CLR(fd
, writefds
);
703 commIncomingStats(StoreEntry
* sentry
)
705 storeAppendPrintf(sentry
, "Current incoming_udp_interval: %d\n",
706 incoming_udp_interval
>> INCOMING_FACTOR
);
707 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
708 incoming_dns_interval
>> INCOMING_FACTOR
);
709 storeAppendPrintf(sentry
, "Current incoming_tcp_interval: %d\n",
710 incoming_tcp_interval
>> INCOMING_FACTOR
);
711 storeAppendPrintf(sentry
, "\n");
712 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
713 storeAppendPrintf(sentry
, "ICP Messages handled per comm_select_udp_incoming() call:\n");
714 statCounter
.comm_udp_incoming
.dump(sentry
, statHistIntDumper
);
715 storeAppendPrintf(sentry
, "DNS Messages handled per comm_select_dns_incoming() call:\n");
716 statCounter
.comm_dns_incoming
.dump(sentry
, statHistIntDumper
);
717 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_select_tcp_incoming() call:\n");
718 statCounter
.comm_tcp_incoming
.dump(sentry
, statHistIntDumper
);
722 commUpdateReadBits(int fd
, PF
* handler
)
724 if (handler
&& !FD_ISSET(fd
, &global_readfds
)) {
725 FD_SET(fd
, &global_readfds
);
727 } else if (!handler
&& FD_ISSET(fd
, &global_readfds
)) {
728 FD_CLR(fd
, &global_readfds
);
734 commUpdateWriteBits(int fd
, PF
* handler
)
736 if (handler
&& !FD_ISSET(fd
, &global_writefds
)) {
737 FD_SET(fd
, &global_writefds
);
739 } else if (!handler
&& FD_ISSET(fd
, &global_writefds
)) {
740 FD_CLR(fd
, &global_writefds
);
745 /* Called by async-io or diskd to speed up the polling */
747 Comm::QuickPollRequired(void)
752 #endif /* USE_SELECT */