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.
35 * This is a very simple driver for Solaris /dev/poll.
37 * The updates are batched, one trip through the comm loop.
38 * (like libevent.) We keep a pointer into the structs so we
39 * can zero out an entry in the poll list if its active.
41 * Ported by Peter Payne from Squid 2.7.STABLE9 comm_devpoll.c
42 * on August 11, 2010 at 3pm (GMT+0100 Europe/London).
44 * Last modified 2010-10-08
48 * There are several poll types in Squid, ALL of which are compiled and linked
49 * in. Thus conditional compile-time flags are used to prevent the different
50 * modules from creating several versions of the same function simultaneously.
58 #include "comm/Loops.h"
60 #include "mgr/Registration.h"
61 #include "SquidTime.h"
64 #if HAVE_SYS_DEVPOLL_H
65 /* Solaris /dev/poll support, see "man -s 7D poll" */
66 #include <sys/devpoll.h>
69 #define DEBUG_DEVPOLL 0
71 /* OPEN_MAX is defined in <limits.h>, presumably included by sys/devpoll.h */
72 #define DEVPOLL_UPDATESIZE OPEN_MAX
73 #define DEVPOLL_QUERYSIZE OPEN_MAX
76 typedef short pollfd_events_t
; /* type of pollfd.events from sys/poll.h */
79 /** \brief Current state */
80 struct _devpoll_state
{
81 pollfd_events_t state
; /**< current known state of file handle */
84 /** \brief Update list
86 * This structure contains an array of settings to send to the /dev/poll
87 * interface. Rather than send changes to /dev/poll one at a time they
88 * are pushed onto this array (updating cur to indicate how many of the
89 * pfds structure elements have been set) until it is full before it
90 * is written out the API.
93 struct pollfd
*pfds
; /**< ptr to array of struct pollfd config elements */
94 int cur
; /**< index of last written element of array, or -1 if none */
95 int size
; /**< maximum number of elements in array */
99 /* STATIC VARIABLES */
100 static int devpoll_fd
; /**< handle to /dev/poll device */
101 static int max_poll_time
= 1000; /**< maximum milliseconds to spend in poll */
103 static struct _devpoll_state
*devpoll_state
; /**< array of socket states */
104 static struct dvpoll do_poll
; /**< data struct for storing poll results */
105 static int dpoll_nfds
; /**< maximum number of poll results */
108 static void commDevPollRegisterWithCacheManager(void);
111 /* PRIVATE FUNCTIONS */
112 /** \brief Write batched file descriptor event changes to poll device
114 * Writes out the static array of file descriptor event changes to the
115 * poll device. This is done only when necessary (i.e. just before
116 * the poll device is queried during the select call, and whenever
117 * the number of changes to store in the array exceeds the size of the
121 comm_flush_updates(void)
124 if (devpoll_update
.cur
== -1)
125 return; /* array of changes to make is empty */
129 DEBUG_DEVPOLL
? 0 : 8,
130 HERE
<< (devpoll_update
.cur
+ 1) << " fds queued"
134 devpoll_fd
, /* open handle to /dev/poll */
135 devpoll_update
.pfds
, /* pointer to array of struct pollfd */
136 (devpoll_update
.cur
+ 1) * sizeof(struct pollfd
) /* bytes to process */
139 assert(static_cast<size_t>(i
) == (sizeof(struct pollfd
) * (devpoll_update
.cur
+ 1)));
140 devpoll_update
.cur
= -1; /* reset size of array, no elements remain */
143 /** \brief Register change in desired polling state for file descriptor
145 * Prevents unnecessary calls to the /dev/poll API by queueing changes
146 * in the devpoll_update array. If the array fills up the comm_flush_updates
147 * function is called.
149 * @param fd file descriptor to register change with
150 * @param events events to register (usually POLLIN, POLLOUT, or POLLREMOVE)
153 comm_update_fd(int fd
, int events
)
157 DEBUG_DEVPOLL
? 0 : 8,
158 HERE
<< "FD " << fd
<< ", events=" << events
161 /* Is the array already full and in need of flushing? */
162 if (devpoll_update
.cur
!= -1 && (devpoll_update
.cur
== devpoll_update
.size
))
163 comm_flush_updates();
165 /* Push new event onto array */
166 devpoll_update
.cur
++;
167 devpoll_update
.pfds
[devpoll_update
.cur
].fd
= fd
;
168 devpoll_update
.pfds
[devpoll_update
.cur
].events
= events
;
169 devpoll_update
.pfds
[devpoll_update
.cur
].revents
= 0;
173 static void commIncomingStats(StoreEntry
*sentry
)
175 StatCounters
*f
= &statCounter
;
176 storeAppendPrintf(sentry
, "Total number of devpoll loops: %ld\n", statCounter
.select_loops
);
177 storeAppendPrintf(sentry
, "Histogram of returned filedescriptors\n");
178 statHistDump(&f
->select_fds_hist
, sentry
, statHistIntDumper
);
183 commDevPollRegisterWithCacheManager(void)
186 "comm_devpoll_incoming",
187 "comm_incoming() stats",
195 /* PUBLIC FUNCTIONS */
197 /** \brief Initialise /dev/poll support
199 * Allocates memory, opens /dev/poll device handle.
202 Comm::SelectLoopInit(void)
204 /* allocate memory first before attempting to open poll device */
205 /* This tracks the FD devpoll offset+state */
206 devpoll_state
= (struct _devpoll_state
*)xcalloc(
207 SQUID_MAXFD
, sizeof(struct _devpoll_state
)
210 /* And this is the stuff we use to read events */
211 do_poll
.dp_fds
= (struct pollfd
*)xcalloc(
212 DEVPOLL_QUERYSIZE
, sizeof(struct pollfd
)
214 dpoll_nfds
= DEVPOLL_QUERYSIZE
;
216 devpoll_update
.pfds
= (struct pollfd
*)xcalloc(
217 DEVPOLL_UPDATESIZE
, sizeof(struct pollfd
)
219 devpoll_update
.cur
= -1;
220 devpoll_update
.size
= DEVPOLL_UPDATESIZE
;
222 /* attempt to open /dev/poll device */
223 devpoll_fd
= open("/dev/poll", O_RDWR
);
225 fatalf("comm_select_init: can't open /dev/poll: %s\n", xstrerror());
227 fd_open(devpoll_fd
, FD_UNKNOWN
, "devpoll ctl");
229 commDevPollRegisterWithCacheManager();
232 /** \brief Set polling state of file descriptor and callback functions
234 * Sets requested polling state for given file handle along with
235 * desired callback function in the event the request event triggers.
237 * Note that setting a polling state with a NULL callback function will
238 * clear the polling for that event on that file descriptor.
240 * @param fd file descriptor to change
241 * @param type may be COMM_SELECT_READ (input) or COMM_SELECT_WRITE (output)
242 * @param handler callback function, or NULL to stop type of polling
243 * @param client_data pointer to be provided to call back function
244 * @param timeout if non-zero then timeout relative to now
247 Comm::SetSelect(int fd
, unsigned int type
, PF
* handler
, void *client_data
, time_t timeout
)
252 DEBUG_DEVPOLL
? 0 : 8,
253 HERE
<< "FD " << fd
<< ",type=" << type
254 << ",handler=" << handler
<< ",client_data=" << client_data
255 << ",timeout=" << timeout
<< ")"
258 /* POLLIN/POLLOUT are defined in <sys/poll.h> */
259 fde
*F
= &fd_table
[fd
];
260 if (!F
->flags
.open
) {
261 /* remove from poll set */
262 comm_update_fd( fd
, POLLREMOVE
);
263 devpoll_state
[fd
].state
= 0;
267 pollfd_events_t state_old
= devpoll_state
[fd
].state
;
268 pollfd_events_t state_new
= 0; /* new state (derive from old state) */
270 if ( type
& COMM_SELECT_READ
) {
271 if ( handler
!= NULL
) {
272 /* we want to POLLIN */
275 ; /* we want to clear POLLIN because handler is NULL */
278 F
->read_handler
= handler
;
279 F
->read_data
= client_data
;
280 } else if ( state_old
& POLLIN
) {
281 /* we're not changing reading state so take from existing */
285 if ( type
& COMM_SELECT_WRITE
) {
286 if ( handler
!= NULL
) {
287 /* we want to POLLOUT */
288 state_new
|= POLLOUT
;
290 ; /* we want to clear POLLOUT because handler is NULL */
293 F
->write_handler
= handler
;
294 F
->write_data
= client_data
;
295 } else if ( state_old
& POLLOUT
) {
296 /* we're not changing writing state so take from existing */
297 state_new
|= POLLOUT
;
300 if ( pollfd_events_t bits_changed
= (state_old
^ state_new
) ) {
301 /* something has changed, update /dev/poll of what to listen for */
303 /* did any bits clear? (in which case a poll remove is necessary) */
304 if ( bits_changed
& state_old
) {
305 comm_update_fd( fd
, POLLREMOVE
);
306 /* existing state cleared, so update with all required events */
308 comm_update_fd( fd
, state_new
);
310 /* only update with new required event */
311 if ( pollfd_events_t newly_set_only
= (bits_changed
& state_new
) )
312 comm_update_fd( fd
, newly_set_only
);
315 devpoll_state
[fd
].state
= state_new
;
319 F
->timeout
= squid_curtime
+ timeout
;
323 /** \brief Clear polling of file handle (both read and write)
325 * @param fd file descriptor to clear polling on
328 Comm::ResetSelect(int fd
)
330 SetSelect(fd
, COMM_SELECT_WRITE
, NULL
, NULL
, 0);
331 SetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
335 /** \brief Do poll and trigger callback functions as appropriate
337 * Check all connections for new connections and input data that is to be
338 * processed. Also check for connections with data queued and whether we can
341 * Called to do the new-style IO, courtesy of of squid (like most of this
342 * new IO code). This routine handles the stuff we've hidden in
343 * comm_setselect and fd_table[] and calls callbacks for IO ready
346 * @param msec milliseconds to poll for (limited by max_poll_time)
349 Comm::DoSelect(int msec
)
355 PROF_start(comm_check_incoming
);
357 if (msec
> max_poll_time
)
358 msec
= max_poll_time
;
361 do_poll
.dp_timeout
= msec
;
362 do_poll
.dp_nfds
= dpoll_nfds
;
364 comm_flush_updates(); /* ensure latest changes are sent to /dev/poll */
366 num
= ioctl(devpoll_fd
, DP_POLL
, &do_poll
);
367 ++statCounter
.select_loops
;
370 break; /* no error, skip out of loop */
372 if (ignoreErrno(errno
))
373 break; /* error is one we may ignore, skip out of loop */
375 /* error during poll */
377 PROF_stop(comm_check_incoming
);
381 PROF_stop(comm_check_incoming
);
384 statHistCount(&statCounter
.select_fds_hist
, num
);
387 return COMM_TIMEOUT
; /* no error */
389 PROF_start(comm_handle_ready_fd
);
391 for (i
= 0; i
< num
; i
++) {
392 int fd
= (int)do_poll
.dp_fds
[i
].fd
;
396 DEBUG_DEVPOLL
? 0 : 8,
397 HERE
<< "got FD " << fd
398 << ",events=" << std::hex
<< do_poll
.dp_fds
[i
].revents
399 << ",monitoring=" << devpoll_state
[fd
].state
400 << ",F->read_handler=" << F
->read_handler
401 << ",F->write_handler=" << F
->write_handler
405 if (do_poll
.dp_fds
[i
].revents
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
408 DEBUG_DEVPOLL
? 0 : 8,
409 HERE
<< "devpoll event error: fd " << fd
414 /* check if file descriptor has data to read */
415 if (do_poll
.dp_fds
[i
].revents
& POLLIN
|| F
->flags
.read_pending
) {
416 if ( (hdl
= F
->read_handler
) != NULL
) {
419 DEBUG_DEVPOLL
? 0 : 8,
420 HERE
<< "Calling read handler on FD " << fd
422 PROF_start(comm_read_handler
);
423 F
->flags
.read_pending
= 0;
424 F
->read_handler
= NULL
;
425 hdl(fd
, F
->read_data
);
426 PROF_stop(comm_read_handler
);
427 statCounter
.select_fds
++;
431 DEBUG_DEVPOLL
? 0 : 8,
432 HERE
<< "no read handler for FD " << fd
434 // remove interest since no handler exist for this event.
435 SetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
439 /* check if file descriptor is ready to write */
440 if (do_poll
.dp_fds
[i
].revents
& POLLOUT
) {
441 if ((hdl
= F
->write_handler
) != NULL
) {
444 DEBUG_DEVPOLL
? 0 : 8,
445 HERE
<< "Calling write handler on FD " << fd
447 PROF_start(comm_write_handler
);
448 F
->write_handler
= NULL
;
449 hdl(fd
, F
->write_data
);
450 PROF_stop(comm_write_handler
);
451 statCounter
.select_fds
++;
455 DEBUG_DEVPOLL
? 0 : 8,
456 HERE
<< "no write handler for FD " << fd
458 // remove interest since no handler exist for this event.
459 SetSelect(fd
, COMM_SELECT_WRITE
, NULL
, NULL
, 0);
464 PROF_stop(comm_handle_ready_fd
);
469 Comm::QuickPollRequired(void)
474 #endif /* USE_DEVPOLL */