2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "poll-loop.h"
25 #include "backtrace.h"
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
33 VLOG_DEFINE_THIS_MODULE(poll_loop
);
35 COVERAGE_DEFINE(poll_fd_wait
);
36 COVERAGE_DEFINE(poll_zero_timeout
);
38 /* An event that will wake the following call to poll_block(). */
40 /* Set when the waiter is created. */
41 struct list node
; /* Element in global waiters list. */
42 int fd
; /* File descriptor. */
43 short int events
; /* Events to wait for (POLLIN, POLLOUT). */
44 struct backtrace
*backtrace
; /* Optionally, event that created waiter. */
46 /* Set only when poll_block() is called. */
47 struct pollfd
*pollfd
; /* Pointer to element of the pollfds array. */
50 /* All active poll waiters. */
51 static struct list waiters
= LIST_INITIALIZER(&waiters
);
53 /* Number of elements in the waiters list. */
54 static size_t n_waiters
;
56 /* Max time to wait in next call to poll_block(), in milliseconds, or -1 to
58 static int timeout
= -1;
60 /* Backtrace of 'timeout''s registration, if debugging is enabled. */
61 static struct backtrace timeout_backtrace
;
63 static struct poll_waiter
*new_waiter(int fd
, short int events
);
65 /* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
66 * or POLLOUT or POLLIN | POLLOUT). The following call to poll_block() will
67 * wake up when 'fd' becomes ready for one or more of the requested events.
69 * The event registration is one-shot: only the following call to poll_block()
70 * is affected. The event will need to be re-registered after poll_block() is
71 * called if it is to persist. */
73 poll_fd_wait(int fd
, short int events
)
75 COVERAGE_INC(poll_fd_wait
);
76 return new_waiter(fd
, events
);
79 /* The caller must ensure that 'msec' is not negative. */
81 poll_timer_wait__(int msec
)
83 if (timeout
< 0 || msec
< timeout
) {
85 if (VLOG_IS_DBG_ENABLED()) {
86 backtrace_capture(&timeout_backtrace
);
91 /* Causes the following call to poll_block() to block for no more than 'msec'
92 * milliseconds. If 'msec' is nonpositive, the following call to poll_block()
93 * will not block at all.
95 * The timer registration is one-shot: only the following call to poll_block()
96 * is affected. The timer will need to be re-registered after poll_block() is
97 * called if it is to persist. */
99 poll_timer_wait(long long int msec
)
101 poll_timer_wait__(msec
< 0 ? 0
102 : msec
> INT_MAX
? INT_MAX
106 /* Causes the following call to poll_block() to wake up when the current time,
107 * as returned by time_msec(), reaches 'msec' or later. If 'msec' is earlier
108 * than the current time, the following call to poll_block() will not block at
111 * The timer registration is one-shot: only the following call to poll_block()
112 * is affected. The timer will need to be re-registered after poll_block() is
113 * called if it is to persist. */
115 poll_timer_wait_until(long long int msec
)
117 long long int now
= time_msec();
118 poll_timer_wait__(msec
<= now
? 0
119 : msec
< now
+ INT_MAX
? msec
- now
123 /* Causes the following call to poll_block() to wake up immediately, without
126 poll_immediate_wake(void)
131 static void PRINTF_FORMAT(2, 3)
132 log_wakeup(const struct backtrace
*backtrace
, const char *format
, ...)
138 va_start(args
, format
);
139 ds_put_format_valist(&ds
, format
, args
);
145 ds_put_char(&ds
, ':');
146 for (i
= 0; i
< backtrace
->n_frames
; i
++) {
147 ds_put_format(&ds
, " 0x%"PRIxPTR
, backtrace
->frames
[i
]);
150 VLOG_DBG("%s", ds_cstr(&ds
));
154 /* Blocks until one or more of the events registered with poll_fd_wait()
155 * occurs, or until the minimum duration registered with poll_timer_wait()
156 * elapses, or not at all if poll_immediate_wake() has been called. */
160 static struct pollfd
*pollfds
;
161 static size_t max_pollfds
;
163 struct poll_waiter
*pw
, *next
;
167 /* Register fatal signal events before actually doing any real work for
171 if (max_pollfds
< n_waiters
) {
172 max_pollfds
= n_waiters
;
173 pollfds
= xrealloc(pollfds
, max_pollfds
* sizeof *pollfds
);
177 LIST_FOR_EACH (pw
, node
, &waiters
) {
178 pw
->pollfd
= &pollfds
[n_pollfds
];
179 pollfds
[n_pollfds
].fd
= pw
->fd
;
180 pollfds
[n_pollfds
].events
= pw
->events
;
181 pollfds
[n_pollfds
].revents
= 0;
186 COVERAGE_INC(poll_zero_timeout
);
188 retval
= time_poll(pollfds
, n_pollfds
, timeout
);
190 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
191 VLOG_ERR_RL(&rl
, "poll: %s", strerror(-retval
));
192 } else if (!retval
&& VLOG_IS_DBG_ENABLED()) {
193 log_wakeup(&timeout_backtrace
, "%d-ms timeout", timeout
);
196 LIST_FOR_EACH_SAFE (pw
, next
, node
, &waiters
) {
197 if (pw
->pollfd
->revents
&& VLOG_IS_DBG_ENABLED()) {
198 log_wakeup(pw
->backtrace
, "%s%s%s%s%s on fd %d",
199 pw
->pollfd
->revents
& POLLIN
? "[POLLIN]" : "",
200 pw
->pollfd
->revents
& POLLOUT
? "[POLLOUT]" : "",
201 pw
->pollfd
->revents
& POLLERR
? "[POLLERR]" : "",
202 pw
->pollfd
->revents
& POLLHUP
? "[POLLHUP]" : "",
203 pw
->pollfd
->revents
& POLLNVAL
? "[POLLNVAL]" : "",
210 timeout_backtrace
.n_frames
= 0;
212 /* Handle any pending signals before doing anything else. */
216 /* Cancels the file descriptor event registered with poll_fd_wait() using 'pw',
217 * the struct poll_waiter returned by that function.
219 * An event registered with poll_fd_wait() may be canceled from its time of
220 * registration until the next call to poll_block(). At that point, the event
221 * is automatically canceled by the system and its poll_waiter is freed. */
223 poll_cancel(struct poll_waiter
*pw
)
226 list_remove(&pw
->node
);
233 /* Creates and returns a new poll_waiter for 'fd' and 'events'. */
234 static struct poll_waiter
*
235 new_waiter(int fd
, short int events
)
237 struct poll_waiter
*waiter
= xzalloc(sizeof *waiter
);
240 waiter
->events
= events
;
241 if (VLOG_IS_DBG_ENABLED()) {
242 waiter
->backtrace
= xmalloc(sizeof *waiter
->backtrace
);
243 backtrace_capture(waiter
->backtrace
);
245 list_push_back(&waiters
, &waiter
->node
);