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.
38 #include "comm/Connection.h"
39 #include "comm/Loops.h"
42 #include "mgr/Registration.h"
43 #include "SquidTime.h"
50 /* Needed for poll() on Linux at least */
53 #define POLLRDNORM POLLIN
56 #define POLLWRNORM POLLOUT
60 static int MAX_POLL_TIME
= 1000; /* see also Comm::QuickPollRequired() */
63 #define howmany(x, y) (((x)+((y)-1))/(y))
68 #define FD_MASK_BYTES sizeof(fd_mask)
69 #define FD_MASK_BITS (FD_MASK_BYTES*NBBY)
72 static int fdIsHttp(int fd
);
73 static int fdIsIcp(int fd
);
74 static int fdIsDns(int fd
);
75 static OBJH commIncomingStats
;
76 static int comm_check_incoming_poll_handlers(int nfds
, int *fds
);
77 static void comm_poll_dns_incoming(void);
80 * Automatic tuning for incoming requests:
82 * INCOMING sockets are the ICP and HTTP ports. We need to check these
83 * fairly regularly, but how often? When the load increases, we
84 * want to check the incoming sockets more often. If we have a lot
85 * of incoming ICP, then we need to check these sockets more than
86 * if we just have HTTP.
88 * The variables 'incoming_icp_interval' and 'incoming_http_interval'
89 * determine how many normal I/O events to process before checking
90 * incoming sockets again. Note we store the incoming_interval
91 * multipled by a factor of (2^INCOMING_FACTOR) to have some
92 * pseudo-floating point precision.
94 * The variable 'icp_io_events' and 'http_io_events' counts how many normal
95 * I/O events have been processed since the last check on the incoming
96 * sockets. When io_events > incoming_interval, its time to check incoming
99 * Every time we check incoming sockets, we count how many new messages
100 * or connections were processed. This is used to adjust the
101 * incoming_interval for the next iteration. The new incoming_interval
102 * is calculated as the current incoming_interval plus what we would
103 * like to see as an average number of events minus the number of
104 * events just processed.
106 * incoming_interval = incoming_interval + target_average - number_of_events_processed
108 * There are separate incoming_interval counters for both HTTP and ICP events
110 * You can see the current values of the incoming_interval's, as well as
111 * a histogram of 'incoming_events' by asking the cache manager
112 * for 'comm_incoming', e.g.:
114 * % ./client mgr:comm_poll_incoming
118 * - We have MAX_INCOMING_INTEGER as a magic upper limit on
119 * incoming_interval for both types of sockets. At the
120 * largest value the cache will effectively be idling.
122 * - The higher the INCOMING_FACTOR, the slower the algorithm will
123 * respond to load spikes/increases/decreases in demand. A value
124 * between 3 and 8 is recommended.
127 #define MAX_INCOMING_INTEGER 256
128 #define INCOMING_FACTOR 5
129 #define MAX_INCOMING_INTERVAL (MAX_INCOMING_INTEGER << INCOMING_FACTOR)
130 static int icp_io_events
= 0;
131 static int dns_io_events
= 0;
132 static int http_io_events
= 0;
133 static int incoming_icp_interval
= 16 << INCOMING_FACTOR
;
134 static int incoming_dns_interval
= 16 << INCOMING_FACTOR
;
135 static int incoming_http_interval
= 16 << INCOMING_FACTOR
;
136 #define commCheckICPIncoming (++icp_io_events > (incoming_icp_interval>> INCOMING_FACTOR))
137 #define commCheckDNSIncoming (++dns_io_events > (incoming_dns_interval>> INCOMING_FACTOR))
138 #define commCheckHTTPIncoming (++http_io_events > (incoming_http_interval>> INCOMING_FACTOR))
142 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
144 fde
*F
= &fd_table
[fd
];
146 assert(F
->flags
.open
);
147 debugs(5, 5, "commSetSelect: FD " << fd
<< " type " << type
);
149 if (type
& COMM_SELECT_READ
) {
150 F
->read_handler
= handler
;
151 F
->read_data
= client_data
;
154 if (type
& COMM_SELECT_WRITE
) {
155 F
->write_handler
= handler
;
156 F
->write_data
= client_data
;
160 F
->timeout
= squid_curtime
+ timeout
;
164 Comm::ResetSelect(int fd
)
171 if (icpIncomingConn
!= NULL
&& icpIncomingConn
->fd
== fd
)
174 if (icpOutgoingConn
!= NULL
&& icpOutgoingConn
->fd
== fd
)
183 if (fd
== DnsSocketA
)
186 if (fd
== DnsSocketB
)
197 for (j
= 0; j
< NHttpSockets
; j
++) {
198 if (fd
== HttpSockets
[j
])
206 comm_check_incoming_poll_handlers(int nfds
, int *fds
)
213 struct pollfd pfds
[3 + MAXHTTPPORTS
];
214 PROF_start(comm_check_incoming
);
215 incoming_sockets_accepted
= 0;
217 for (i
= npfds
= 0; i
< nfds
; i
++) {
222 if (fd_table
[fd
].read_handler
)
223 events
|= POLLRDNORM
;
225 if (fd_table
[fd
].write_handler
)
226 events
|= POLLWRNORM
;
230 pfds
[npfds
].events
= events
;
231 pfds
[npfds
].revents
= 0;
237 PROF_stop(comm_check_incoming
);
242 statCounter
.syscalls
.selects
++;
244 if (poll(pfds
, npfds
, 0) < 1) {
245 PROF_stop(comm_check_incoming
);
246 return incoming_sockets_accepted
;
249 for (i
= 0; i
< npfds
; i
++) {
252 if (((revents
= pfds
[i
].revents
) == 0) || ((fd
= pfds
[i
].fd
) == -1))
255 if (revents
& (POLLRDNORM
| POLLIN
| POLLHUP
| POLLERR
)) {
256 if ((hdl
= fd_table
[fd
].read_handler
)) {
257 fd_table
[fd
].read_handler
= NULL
;
258 hdl(fd
, fd_table
[fd
].read_data
);
259 } else if (pfds
[i
].events
& POLLRDNORM
)
260 debugs(5, 1, "comm_poll_incoming: FD " << fd
<< " NULL read handler");
263 if (revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
| POLLERR
)) {
264 if ((hdl
= fd_table
[fd
].write_handler
)) {
265 fd_table
[fd
].write_handler
= NULL
;
266 hdl(fd
, fd_table
[fd
].write_data
);
267 } else if (pfds
[i
].events
& POLLWRNORM
)
268 debugs(5, 1, "comm_poll_incoming: FD " << fd
<< " NULL write_handler");
272 PROF_stop(comm_check_incoming
);
273 return incoming_sockets_accepted
;
277 comm_poll_icp_incoming(void)
284 if (Comm::IsConnOpen(icpIncomingConn
))
285 fds
[nfds
++] = icpIncomingConn
->fd
;
287 if (icpIncomingConn
!= icpOutgoingConn
&& Comm::IsConnOpen(icpOutgoingConn
))
288 fds
[nfds
++] = icpOutgoingConn
->fd
;
293 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
295 incoming_icp_interval
+= Config
.comm_incoming
.icp_average
- nevents
;
297 if (incoming_icp_interval
< Config
.comm_incoming
.icp_min_poll
)
298 incoming_icp_interval
= Config
.comm_incoming
.icp_min_poll
;
300 if (incoming_icp_interval
> MAX_INCOMING_INTERVAL
)
301 incoming_icp_interval
= MAX_INCOMING_INTERVAL
;
303 if (nevents
> INCOMING_ICP_MAX
)
304 nevents
= INCOMING_ICP_MAX
;
306 statHistCount(&statCounter
.comm_icp_incoming
, nevents
);
310 comm_poll_http_incoming(void)
313 int fds
[MAXHTTPPORTS
];
318 /* only poll sockets that won't be deferred */
320 for (j
= 0; j
< NHttpSockets
; j
++) {
321 if (HttpSockets
[j
] < 0)
324 fds
[nfds
++] = HttpSockets
[j
];
327 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
328 incoming_http_interval
= incoming_http_interval
329 + Config
.comm_incoming
.http_average
- nevents
;
331 if (incoming_http_interval
< Config
.comm_incoming
.http_min_poll
)
332 incoming_http_interval
= Config
.comm_incoming
.http_min_poll
;
334 if (incoming_http_interval
> MAX_INCOMING_INTERVAL
)
335 incoming_http_interval
= MAX_INCOMING_INTERVAL
;
337 if (nevents
> INCOMING_HTTP_MAX
)
338 nevents
= INCOMING_HTTP_MAX
;
340 statHistCount(&statCounter
.comm_http_incoming
, nevents
);
343 /* poll all sockets; call handlers for those that are ready. */
345 Comm::DoSelect(int msec
)
347 struct pollfd pfds
[SQUID_MAXFD
];
353 unsigned long npending
;
355 int callicp
= 0, callhttp
= 0;
357 double timeout
= current_dtime
+ (msec
/ 1000.0);
362 start
= current_dtime
;
364 if (commCheckICPIncoming
)
365 comm_poll_icp_incoming();
367 if (commCheckDNSIncoming
)
368 comm_poll_dns_incoming();
370 if (commCheckHTTPIncoming
)
371 comm_poll_http_incoming();
373 PROF_start(comm_poll_prep_pfds
);
375 callicp
= calldns
= callhttp
= 0;
381 maxfd
= Biggest_FD
+ 1;
383 for (int i
= 0; i
< maxfd
; i
++) {
386 /* Check each open socket for a handler. */
388 if (fd_table
[i
].read_handler
)
389 events
|= POLLRDNORM
;
391 if (fd_table
[i
].write_handler
)
392 events
|= POLLWRNORM
;
396 pfds
[nfds
].events
= events
;
397 pfds
[nfds
].revents
= 0;
400 if ((events
& POLLRDNORM
) && fd_table
[i
].flags
.read_pending
)
405 PROF_stop(comm_poll_prep_pfds
);
410 if (msec
> MAX_POLL_TIME
)
411 msec
= MAX_POLL_TIME
;
415 * Note that this will only ever trigger when there are no log files
416 * and stdout/err/in are all closed too.
418 if (nfds
== 0 && npending
== 0) {
420 return COMM_SHUTDOWN
;
426 PROF_start(comm_poll_normal
);
427 ++statCounter
.syscalls
.selects
;
428 num
= poll(pfds
, nfds
, msec
);
429 ++statCounter
.select_loops
;
430 PROF_stop(comm_poll_normal
);
432 if (num
>= 0 || npending
> 0)
435 if (ignoreErrno(errno
))
438 debugs(5, 0, "comm_poll: poll failure: " << xstrerror());
440 assert(errno
!= EINVAL
);
449 debugs(5, num
? 5 : 8, "comm_poll: " << num
<< "+" << npending
<< " FDs ready");
450 statHistCount(&statCounter
.select_fds_hist
, num
);
452 if (num
== 0 && npending
== 0)
455 /* scan each socket but the accept socket. Poll this
456 * more frequently to minimize losses due to the 5 connect
458 PROF_start(comm_handle_ready_fd
);
460 for (size_t loopIndex
= 0; loopIndex
< nfds
; loopIndex
++) {
462 int revents
= pfds
[loopIndex
].revents
;
463 fd
= pfds
[loopIndex
].fd
;
468 if (fd_table
[fd
].flags
.read_pending
)
491 if (revents
& (POLLRDNORM
| POLLIN
| POLLHUP
| POLLERR
)) {
492 debugs(5, 6, "comm_poll: FD " << fd
<< " ready for reading");
494 if (NULL
== (hdl
= F
->read_handler
))
497 PROF_start(comm_read_handler
);
498 F
->read_handler
= NULL
;
499 F
->flags
.read_pending
= 0;
500 hdl(fd
, F
->read_data
);
501 PROF_stop(comm_read_handler
);
502 statCounter
.select_fds
++;
504 if (commCheckICPIncoming
)
505 comm_poll_icp_incoming();
507 if (commCheckDNSIncoming
)
508 comm_poll_dns_incoming();
510 if (commCheckHTTPIncoming
)
511 comm_poll_http_incoming();
515 if (revents
& (POLLWRNORM
| POLLOUT
| POLLHUP
| POLLERR
)) {
516 debugs(5, 5, "comm_poll: FD " << fd
<< " ready for writing");
518 if ((hdl
= F
->write_handler
)) {
519 PROF_start(comm_write_handler
);
520 F
->write_handler
= NULL
;
521 hdl(fd
, F
->write_data
);
522 PROF_stop(comm_write_handler
);
523 statCounter
.select_fds
++;
525 if (commCheckICPIncoming
)
526 comm_poll_icp_incoming();
528 if (commCheckDNSIncoming
)
529 comm_poll_dns_incoming();
531 if (commCheckHTTPIncoming
)
532 comm_poll_http_incoming();
536 if (revents
& POLLNVAL
) {
537 AsyncCall::Pointer ch
;
538 debugs(5, 0, "WARNING: FD " << fd
<< " has handlers, but it's invalid.");
539 debugs(5, 0, "FD " << fd
<< " is a " << fdTypeStr
[F
->type
]);
540 debugs(5, 0, "--> " << F
->desc
);
541 debugs(5, 0, "tmout:" << F
->timeoutHandler
<< "read:" <<
542 F
->read_handler
<< " write:" << F
->write_handler
);
544 for (ch
= F
->closeHandler
; ch
!= NULL
; ch
= ch
->Next())
545 debugs(5, 0, " close handler: " << ch
);
547 if (F
->closeHandler
!= NULL
) {
548 commCallCloseHandlers(fd
);
549 } else if (F
->timeoutHandler
!= NULL
) {
550 debugs(5, 0, "comm_poll: Calling Timeout Handler");
551 ScheduleCallHere(F
->timeoutHandler
);
554 F
->closeHandler
= NULL
;
555 F
->timeoutHandler
= NULL
;
556 F
->read_handler
= NULL
;
557 F
->write_handler
= NULL
;
564 PROF_stop(comm_handle_ready_fd
);
567 comm_poll_icp_incoming();
570 comm_poll_dns_incoming();
573 comm_poll_http_incoming();
577 statCounter
.select_time
+= (current_dtime
- start
);
580 } while (timeout
> current_dtime
);
582 debugs(5, 8, "comm_poll: time out: " << squid_curtime
<< ".");
589 comm_poll_dns_incoming(void)
596 if (DnsSocketA
< 0 && DnsSocketB
< 0)
600 fds
[nfds
++] = DnsSocketA
;
603 fds
[nfds
++] = DnsSocketB
;
605 nevents
= comm_check_incoming_poll_handlers(nfds
, fds
);
610 incoming_dns_interval
+= Config
.comm_incoming
.dns_average
- nevents
;
612 if (incoming_dns_interval
< Config
.comm_incoming
.dns_min_poll
)
613 incoming_dns_interval
= Config
.comm_incoming
.dns_min_poll
;
615 if (incoming_dns_interval
> MAX_INCOMING_INTERVAL
)
616 incoming_dns_interval
= MAX_INCOMING_INTERVAL
;
618 if (nevents
> INCOMING_DNS_MAX
)
619 nevents
= INCOMING_DNS_MAX
;
621 statHistCount(&statCounter
.comm_dns_incoming
, nevents
);
626 commPollRegisterWithCacheManager(void)
628 Mgr::RegisterAction("comm_poll_incoming",
629 "comm_incoming() stats",
630 commIncomingStats
, 0, 1);
634 Comm::SelectLoopInit(void)
636 commPollRegisterWithCacheManager();
640 commIncomingStats(StoreEntry
* sentry
)
642 StatCounters
*f
= &statCounter
;
643 storeAppendPrintf(sentry
, "Current incoming_icp_interval: %d\n",
644 incoming_icp_interval
>> INCOMING_FACTOR
);
645 storeAppendPrintf(sentry
, "Current incoming_dns_interval: %d\n",
646 incoming_dns_interval
>> INCOMING_FACTOR
);
647 storeAppendPrintf(sentry
, "Current incoming_http_interval: %d\n",
648 incoming_http_interval
>> INCOMING_FACTOR
);
649 storeAppendPrintf(sentry
, "\n");
650 storeAppendPrintf(sentry
, "Histogram of events per incoming socket type\n");
651 storeAppendPrintf(sentry
, "ICP Messages handled per comm_poll_icp_incoming() call:\n");
652 statHistDump(&f
->comm_icp_incoming
, sentry
, statHistIntDumper
);
653 storeAppendPrintf(sentry
, "DNS Messages handled per comm_poll_dns_incoming() call:\n");
654 statHistDump(&f
->comm_dns_incoming
, sentry
, statHistIntDumper
);
655 storeAppendPrintf(sentry
, "HTTP Messages handled per comm_poll_http_incoming() call:\n");
656 statHistDump(&f
->comm_http_incoming
, sentry
, statHistIntDumper
);
659 /* Called by async-io or diskd to speed up the polling */
661 Comm::QuickPollRequired(void)
666 #endif /* USE_POLL */