2 * Copyright (c) 2008, 2009, 2010, 2011 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"
26 #include "dynamic-string.h"
27 #include "fatal-signal.h"
29 #include "socket-util.h"
34 #undef poll_timer_wait
35 #undef poll_timer_wait_until
36 #undef poll_immediate_wake
38 VLOG_DEFINE_THIS_MODULE(poll_loop);
40 COVERAGE_DEFINE(poll_fd_wait);
41 COVERAGE_DEFINE(poll_zero_timeout);
43 /* An event that will wake the following call to poll_block(). */
45 /* Set when the waiter is created. */
46 struct list node; /* Element in global waiters list. */
47 int fd; /* File descriptor. */
48 short int events; /* Events to wait for (POLLIN, POLLOUT). */
49 const char *where; /* Where the waiter was created. */
51 /* Set only when poll_block() is called. */
52 struct pollfd *pollfd; /* Pointer to element of the pollfds array. */
55 /* All active poll waiters. */
56 static struct list waiters = LIST_INITIALIZER(&waiters);
58 /* Number of elements in the waiters list. */
59 static size_t n_waiters;
61 /* Max time to wait in next call to poll_block(), in milliseconds, or -1 to
63 static int timeout = -1;
65 /* Location where waiter created. */
66 static const char *timeout_where;
68 static struct poll_waiter *new_waiter(int fd, short int events,
71 /* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
72 * or POLLOUT or POLLIN | POLLOUT). The following call to poll_block() will
73 * wake up when 'fd' becomes ready for one or more of the requested events.
75 * The event registration is one-shot: only the following call to poll_block()
76 * is affected. The event will need to be re-registered after poll_block() is
77 * called if it is to persist.
79 * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
80 * for more information. */
82 poll_fd_wait(int fd, short int events, const char *where)
84 COVERAGE_INC(poll_fd_wait);
85 return new_waiter(fd, events, where);
88 /* The caller must ensure that 'msec' is not negative. */
90 poll_timer_wait__(int msec, const char *where)
92 if (timeout < 0 || msec < timeout) {
94 timeout_where = where;
98 /* Causes the following call to poll_block() to block for no more than 'msec'
99 * milliseconds. If 'msec' is nonpositive, the following call to poll_block()
100 * will not block at all.
102 * The timer registration is one-shot: only the following call to poll_block()
103 * is affected. The timer will need to be re-registered after poll_block() is
104 * called if it is to persist.
106 * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
107 * for more information. */
109 poll_timer_wait(long long int msec, const char *where)
111 poll_timer_wait__((msec < 0 ? 0
112 : msec > INT_MAX ? INT_MAX
117 /* Causes the following call to poll_block() to wake up when the current time,
118 * as returned by time_msec(), reaches 'msec' or later. If 'msec' is earlier
119 * than the current time, the following call to poll_block() will not block at
122 * The timer registration is one-shot: only the following call to poll_block()
123 * is affected. The timer will need to be re-registered after poll_block() is
124 * called if it is to persist.
126 * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
127 * for more information. */
129 poll_timer_wait_until(long long int msec, const char *where)
131 long long int now = time_msec();
132 poll_timer_wait__((msec <= now ? 0
133 : msec < now + INT_MAX ? msec - now
138 /* Causes the following call to poll_block() to wake up immediately, without
141 * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
142 * for more information. */
144 poll_immediate_wake(const char *where)
146 poll_timer_wait(0, where);
149 /* Logs, if appropriate, that the poll loop was awakened by an event
150 * registered at 'where' (typically a source file and line number). The other
151 * arguments have two possible interpretations:
153 * - If 'pollfd' is nonnull then it should be the "struct pollfd" that caused
154 * the wakeup. 'timeout' is ignored.
156 * - If 'pollfd' is NULL then 'timeout' is the number of milliseconds after
157 * which the poll loop woke up.
160 log_wakeup(const char *where, const struct pollfd *pollfd, int timeout)
162 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(120, 120);
163 enum vlog_level level;
167 cpu_usage = get_cpu_usage();
168 if (VLOG_IS_DBG_ENABLED()) {
170 } else if (cpu_usage > 50 && !VLOG_DROP_WARN(&rl)) {
177 ds_put_cstr(&s, "wakeup due to ");
179 char *description = describe_fd(pollfd->fd);
180 if (pollfd->revents & POLLIN) {
181 ds_put_cstr(&s, "[POLLIN]");
183 if (pollfd->revents & POLLOUT) {
184 ds_put_cstr(&s, "[POLLOUT]");
186 if (pollfd->revents & POLLERR) {
187 ds_put_cstr(&s, "[POLLERR]");
189 if (pollfd->revents & POLLHUP) {
190 ds_put_cstr(&s, "[POLLHUP]");
192 if (pollfd->revents & POLLNVAL) {
193 ds_put_cstr(&s, "[POLLNVAL]");
195 ds_put_format(&s, " on fd %d (%s)", pollfd->fd, description);
198 ds_put_format(&s, "%d-ms timeout", timeout);
201 ds_put_format(&s, " at %s", where);
203 if (cpu_usage >= 0) {
204 ds_put_format(&s, " (%d%% CPU usage)", cpu_usage);
206 VLOG(level, "%s", ds_cstr(&s));
210 /* Blocks until one or more of the events registered with poll_fd_wait()
211 * occurs, or until the minimum duration registered with poll_timer_wait()
212 * elapses, or not at all if poll_immediate_wake() has been called. */
216 static struct pollfd *pollfds;
217 static size_t max_pollfds;
219 struct poll_waiter *pw, *next;
223 /* Register fatal signal events before actually doing any real work for
227 if (max_pollfds < n_waiters) {
228 max_pollfds = n_waiters;
229 pollfds = xrealloc(pollfds, max_pollfds * sizeof *pollfds);
233 LIST_FOR_EACH (pw, node, &waiters) {
234 pw->pollfd = &pollfds[n_pollfds];
235 pollfds[n_pollfds].fd = pw->fd;
236 pollfds[n_pollfds].events = pw->events;
237 pollfds[n_pollfds].revents = 0;
242 COVERAGE_INC(poll_zero_timeout);
244 retval = time_poll(pollfds, n_pollfds, timeout);
246 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
247 VLOG_ERR_RL(&rl, "poll: %s", strerror(-retval));
248 } else if (!retval) {
249 log_wakeup(timeout_where, NULL, timeout);
252 LIST_FOR_EACH_SAFE (pw, next, node, &waiters) {
253 if (pw->pollfd->revents) {
254 log_wakeup(pw->where, pw->pollfd, 0);
260 timeout_where = NULL;
262 /* Handle any pending signals before doing anything else. */
266 /* Cancels the file descriptor event registered with poll_fd_wait() using 'pw',
267 * the struct poll_waiter returned by that function.
269 * An event registered with poll_fd_wait() may be canceled from its time of
270 * registration until the next call to poll_block(). At that point, the event
271 * is automatically canceled by the system and its poll_waiter is freed. */
273 poll_cancel(struct poll_waiter *pw)
276 list_remove(&pw->node);
282 /* Creates and returns a new poll_waiter for 'fd' and 'events'. */
283 static struct poll_waiter *
284 new_waiter(int fd, short int events, const char *where)
286 struct poll_waiter *waiter = xzalloc(sizeof *waiter);
289 waiter->events = events;
290 waiter->where = where;
291 list_push_back(&waiters, &waiter->node);