4 * DEBUG: section 05 Socket Functions
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 #include "squid-old.h"
38 #include "comm/Connection.h"
39 #include "comm/Loops.h"
42 #include "mgr/Registration.h"
43 #include "SquidTime.h"
44 #include "StatCounters.h"
51 /* Needed for poll() on Linux at least */
54 #define POLLRDNORM POLLIN
57 #define POLLWRNORM POLLOUT
61 static int MAX_POLL_TIME
= 1000; /* see also Comm::QuickPollRequired() */
64 #define howmany(x, y) (((x)+((y)-1))/(y))
69 #define FD_MASK_BYTES sizeof(fd_mask)
70 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
73 static int fdIsHttp(int fd
);
74 static int fdIsIcp(int fd
);
75 static int fdIsDns(int fd
);
76 static OBJH commIncomingStats
;
77 static int comm_check_incoming_poll_handlers(int nfds
, int *fds
);
78 static void comm_poll_dns_incoming(void);
81 * Automatic tuning for incoming requests:
83 * INCOMING sockets are the ICP and HTTP ports. We need to check these
84 * fairly regularly, but how often? When the load increases, we
85 * want to check the incoming sockets more often. If we have a lot
86 * of incoming ICP, then we need to check these sockets more than
87 * if we just have HTTP.
89 * The variables 'incoming_icp_interval' and 'incoming_http_interval'
90 * determine how many normal I/O events to process before checking
91 * incoming sockets again. Note we store the incoming_interval
92 * multipled by a factor of (2^INCOMING_FACTOR) to have some
93 * pseudo-floating point precision.
95 * The variable 'icp_io_events' and 'http_io_events' counts how many normal
96 * I/O events have been processed since the last check on the incoming
97 * sockets. When io_events > incoming_interval, its time to check incoming
100 * Every time we check incoming sockets, we count how many new messages
101 * or connections were processed. This is used to adjust the
102 * incoming_interval for the next iteration. The new incoming_interval
103 * is calculated as the current incoming_interval plus what we would
104 * like to see as an average number of events minus the number of
105 * events just processed.
107 * incoming_interval = incoming_interval + target_average - number_of_events_processed
109 * There are separate incoming_interval counters for both HTTP and ICP events
111 * You can see the current values of the incoming_interval's, as well as
112 * a histogram of 'incoming_events' by asking the cache manager
113 * for 'comm_incoming', e.g.:
115 * % ./client mgr:comm_poll_incoming
119 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
120 * incoming_interval for both types of sockets. At the
121 * largest value the cache will effectively be idling.
123 * - The higher the INCOMING_FACTOR, the slower the algorithm will
124 * respond to load spikes/increases/decreases in demand. A value
125 * between 3 and 8 is recommended.
128 #define MAX_INCOMING_INTEGER 256
129 #define INCOMING_FACTOR 5
130 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
131 static int icp_io_events
= 0;
132 static int dns_io_events
= 0;
133 static int http_io_events
= 0;
134 static int incoming_icp_interval
= 16 << INCOMING_FACTOR
;
135 static int incoming_dns_interval
= 16 << INCOMING_FACTOR
;
136 static int incoming_http_interval
= 16 << INCOMING_FACTOR
;
137 #define commCheckICPIncoming (++icp_io_events > (incoming_icp_interval>> INCOMING_FACTOR))
138 #define commCheckDNSIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
139 #define commCheckHTTPIncoming (++http_io_events > (incoming_http_interval>> INCOMING_FACTOR))
143 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
145 fde
*F
= &fd_table
[fd
];
147 assert(F
->flags
.open
);
148 debugs(5, 5, HERE
<< "FD " << fd
<< ", type=" << type
<<
149 ", handler=" << handler
<< ", client_data=" << client_data
<<
150 ", timeout=" << timeout
);
152 if (type
& COMM_SELECT_READ
) {
153 F
->read_handler
= handler
;
154 F
->read_data
= client_data
;
157 if (type
& COMM_SELECT_WRITE
) {
158 F
->write_handler
= handler
;
159 F
->write_data
= client_data
;
163 F
->timeout
= squid_curtime
+ timeout
;
167 Comm::ResetSelect(int fd
)
174 if (icpIncomingConn
!= NULL
&& icpIncomingConn
->fd
== fd
)
177 if (icpOutgoingConn
!= NULL
&& icpOutgoingConn
->fd
== fd
)
186 if (fd
== DnsSocketA
)
189 if (fd
== DnsSocketB
)
200 for (j
= 0; j
< NHttpSockets
; j
++) {
201 if (fd
== HttpSockets
[j
])
209 comm_check_incoming_poll_handlers(int nfds
, int *fds
)
216 struct pollfd pfds
[3 + MAXHTTPPORTS
];
217 PROF_start(comm_check_incoming
);
218 incoming_sockets_accepted
= 0;
220 for (i
= npfds
= 0; i
< nfds
; i
++) {
225 if (fd_table
[fd
].read_handler
)
226 events
|= POLLRDNORM
;
228 if (fd_table
[fd
].write_handler
)
229 events
|= POLLWRNORM
;
233 pfds
[npfds
].events
= events
;
234 pfds
[npfds
].revents
= 0;
240 PROF_stop(comm_check_incoming
);
245 statCounter
.syscalls
.selects
++;
247 if (poll(pfds
, npfds
, 0) < 1) {
248 PROF_stop(comm_check_incoming
);
249 return incoming_sockets_accepted
;
252 for (i
= 0; i
< npfds
; i
++) {
255 if (((revents
= pfds
[i
].revents
) == 0) || ((fd
= pfds
[i
].fd
) == -1))
258 if (revents
& (POLLRDNORM
| POLLIN
| POLLHUP
| POLLERR
)) {
259 if ((hdl
= fd_table
[fd
].read_handler
)) {
260 fd_table
[fd
].read_handler
= NULL
;
261 hdl(fd
, fd_table
[fd
].read_data
);
262 } else if (pfds
[i
].events
& POLLRDNORM
)
263 debugs(5, 1, "comm_poll_incoming: FD " << fd
<< " NULL read handler");
266 if (revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
| POLLERR
)) {
267 if ((hdl
= fd_table
[fd
].write_handler
)) {
268 fd_table
[fd
].write_handler
= NULL
;
269 hdl(fd
, fd_table
[fd
].write_data
);
270 } else if (pfds
[i
].events
& POLLWRNORM
)
271 debugs(5, 1, "comm_poll_incoming: FD " << fd
<< " NULL write_handler");
275 PROF_stop(comm_check_incoming
);
276 return incoming_sockets_accepted
;
280 comm_poll_icp_incoming(void)
287 if (Comm::IsConnOpen(icpIncomingConn
))
288 fds
[nfds
++] = icpIncomingConn
->fd
;
290 if (icpIncomingConn
!= icpOutgoingConn
&& Comm::IsConnOpen(icpOutgoingConn
))
291 fds
[nfds
++] = icpOutgoingConn
->fd
;
296 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
298 incoming_icp_interval
+= Config
.comm_incoming
.icp_average
- nevents
;
300 if (incoming_icp_interval
< Config
.comm_incoming
.icp_min_poll
)
301 incoming_icp_interval
= Config
.comm_incoming
.icp_min_poll
;
303 if (incoming_icp_interval
> MAX_INCOMING_INTERVAL
)
304 incoming_icp_interval
= MAX_INCOMING_INTERVAL
;
306 if (nevents
> INCOMING_ICP_MAX
)
307 nevents
= INCOMING_ICP_MAX
;
309 statCounter
.comm_icp_incoming
.count(nevents
);
313 comm_poll_http_incoming(void)
316 int fds
[MAXHTTPPORTS
];
321 /* only poll sockets that won't be deferred */
323 for (j
= 0; j
< NHttpSockets
; j
++) {
324 if (HttpSockets
[j
] < 0)
327 fds
[nfds
++] = HttpSockets
[j
];
330 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
331 incoming_http_interval
= incoming_http_interval
332 + Config
.comm_incoming
.http_average
- nevents
;
334 if (incoming_http_interval
< Config
.comm_incoming
.http_min_poll
)
335 incoming_http_interval
= Config
.comm_incoming
.http_min_poll
;
337 if (incoming_http_interval
> MAX_INCOMING_INTERVAL
)
338 incoming_http_interval
= MAX_INCOMING_INTERVAL
;
340 if (nevents
> INCOMING_HTTP_MAX
)
341 nevents
= INCOMING_HTTP_MAX
;
343 statCounter
.comm_http_incoming
.count(nevents
);
346 /* poll all sockets; call handlers for those that are ready. */
348 Comm::DoSelect(int msec
)
350 struct pollfd pfds
[SQUID_MAXFD
];
356 unsigned long npending
;
358 int callicp
= 0, callhttp
= 0;
360 double timeout
= current_dtime
+ (msec
/ 1000.0);
365 start
= current_dtime
;
367 if (commCheckICPIncoming
)
368 comm_poll_icp_incoming();
370 if (commCheckDNSIncoming
)
371 comm_poll_dns_incoming();
373 if (commCheckHTTPIncoming
)
374 comm_poll_http_incoming();
376 PROF_start(comm_poll_prep_pfds
);
378 callicp
= calldns
= callhttp
= 0;
384 maxfd
= Biggest_FD
+ 1;
386 for (int i
= 0; i
< maxfd
; i
++) {
389 /* Check each open socket for a handler. */
391 if (fd_table
[i
].read_handler
)
392 events
|= POLLRDNORM
;
394 if (fd_table
[i
].write_handler
)
395 events
|= POLLWRNORM
;
399 pfds
[nfds
].events
= events
;
400 pfds
[nfds
].revents
= 0;
403 if ((events
& POLLRDNORM
) && fd_table
[i
].flags
.read_pending
)
408 PROF_stop(comm_poll_prep_pfds
);
413 if (msec
> MAX_POLL_TIME
)
414 msec
= MAX_POLL_TIME
;
418 * Note that this will only ever trigger when there are no log files
419 * and stdout/err/in are all closed too.
421 if (nfds
== 0 && npending
== 0) {
423 return COMM_SHUTDOWN
;
429 PROF_start(comm_poll_normal
);
430 ++statCounter
.syscalls
.selects
;
431 num
= poll(pfds
, nfds
, msec
);
432 ++statCounter
.select_loops
;
433 PROF_stop(comm_poll_normal
);
435 if (num
>= 0 || npending
> 0)
438 if (ignoreErrno(errno
))
441 debugs(5, 0, "comm_poll: poll failure: " << xstrerror());
443 assert(errno
!= EINVAL
);
452 debugs(5, num
? 5 : 8, "comm_poll: " << num
<< "+" << npending
<< " FDs ready");
453 statCounter
.select_fds_hist
.count(num
);
455 if (num
== 0 && npending
== 0)
458 /* scan each socket but the accept socket. Poll this
459 * more frequently to minimize losses due to the 5 connect
461 PROF_start(comm_handle_ready_fd
);
463 for (size_t loopIndex
= 0; loopIndex
< nfds
; loopIndex
++) {
465 int revents
= pfds
[loopIndex
].revents
;
466 fd
= pfds
[loopIndex
].fd
;
471 if (fd_table
[fd
].flags
.read_pending
)
494 if (revents
& (POLLRDNORM
| POLLIN
| POLLHUP
| POLLERR
)) {
495 debugs(5, 6, "comm_poll: FD " << fd
<< " ready for reading");
497 if (NULL
== (hdl
= F
->read_handler
))
500 PROF_start(comm_read_handler
);
501 F
->read_handler
= NULL
;
502 F
->flags
.read_pending
= 0;
503 hdl(fd
, F
->read_data
);
504 PROF_stop(comm_read_handler
);
505 statCounter
.select_fds
++;
507 if (commCheckICPIncoming
)
508 comm_poll_icp_incoming();
510 if (commCheckDNSIncoming
)
511 comm_poll_dns_incoming();
513 if (commCheckHTTPIncoming
)
514 comm_poll_http_incoming();
518 if (revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
| POLLERR
)) {
519 debugs(5, 6, "comm_poll: FD " << fd
<< " ready for writing");
521 if ((hdl
= F
->write_handler
)) {
522 PROF_start(comm_write_handler
);
523 F
->write_handler
= NULL
;
524 hdl(fd
, F
->write_data
);
525 PROF_stop(comm_write_handler
);
526 statCounter
.select_fds
++;
528 if (commCheckICPIncoming
)
529 comm_poll_icp_incoming();
531 if (commCheckDNSIncoming
)
532 comm_poll_dns_incoming();
534 if (commCheckHTTPIncoming
)
535 comm_poll_http_incoming();
539 if (revents
& POLLNVAL
) {
540 AsyncCall::Pointer ch
;
541 debugs(5, 0, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
542 debugs(5, 0, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
]);
543 debugs(5, 0, "--> " << F
->desc
);
544 debugs(5, 0, "tmout:" << F
->timeoutHandler
<< "read:" <<
545 F
->read_handler
<< " write:" << F
->write_handler
);
547 for (ch
= F
->closeHandler
; ch
!= NULL
; ch
= ch
->Next())
548 debugs(5, 0, " close handler: " << ch
);
550 if (F
->closeHandler
!= NULL
) {
551 commCallCloseHandlers(fd
);
552 } else if (F
->timeoutHandler
!= NULL
) {
553 debugs(5, 0, "comm_poll: Calling Timeout Handler");
554 ScheduleCallHere(F
->timeoutHandler
);
557 F
->closeHandler
= NULL
;
558 F
->timeoutHandler
= NULL
;
559 F
->read_handler
= NULL
;
560 F
->write_handler
= NULL
;
567 PROF_stop(comm_handle_ready_fd
);
570 comm_poll_icp_incoming();
573 comm_poll_dns_incoming();
576 comm_poll_http_incoming();
580 statCounter
.select_time
+= (current_dtime
- start
);
583 } while (timeout
> current_dtime
);
585 debugs(5, 8, "comm_poll: time out: " << squid_curtime
<< ".");
592 comm_poll_dns_incoming(void)
599 if (DnsSocketA
< 0 && DnsSocketB
< 0)
603 fds
[nfds
++] = DnsSocketA
;
606 fds
[nfds
++] = DnsSocketB
;
608 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
613 incoming_dns_interval
+= Config
.comm_incoming
.dns_average
- nevents
;
615 if (incoming_dns_interval
< Config
.comm_incoming
.dns_min_poll
)
616 incoming_dns_interval
= Config
.comm_incoming
.dns_min_poll
;
618 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
619 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
621 if (nevents
> INCOMING_DNS_MAX
)
622 nevents
= INCOMING_DNS_MAX
;
624 statCounter
.comm_dns_incoming
.count(nevents
);
629 commPollRegisterWithCacheManager(void)
631 Mgr::RegisterAction("comm_poll_incoming",
632 "comm_incoming() stats",
633 commIncomingStats
, 0, 1);
637 Comm::SelectLoopInit(void)
639 commPollRegisterWithCacheManager();
643 commIncomingStats(StoreEntry
* sentry
)
645 storeAppendPrintf(sentry
, "Current incoming_icp_interval: %d\n",
646 incoming_icp_interval
>> INCOMING_FACTOR
);
647 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
648 incoming_dns_interval
>> INCOMING_FACTOR
);
649 storeAppendPrintf(sentry
, "Current incoming_http_interval: %d\n",
650 incoming_http_interval
>> INCOMING_FACTOR
);
651 storeAppendPrintf(sentry
, "\n");
652 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
653 storeAppendPrintf(sentry
, "ICP Messages handled per comm_poll_icp_incoming() call:\n");
654 statCounter
.comm_icp_incoming
.dump(sentry
, statHistIntDumper
);
655 storeAppendPrintf(sentry
, "DNS Messages handled per comm_poll_dns_incoming() call:\n");
656 statCounter
.comm_dns_incoming
.dump(sentry
, statHistIntDumper
);
657 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_poll_http_incoming() call:\n");
658 statCounter
.comm_http_incoming
.dump(sentry
, statHistIntDumper
);
661 /* Called by async-io or diskd to speed up the polling */
663 Comm::QuickPollRequired(void)
668 #endif /* USE_POLL */