2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc.
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 /* Time at which to wake up the next call to poll_block(), in milliseconds as
59 * returned by time_msec(), LLONG_MIN to wake up immediately, or LLONG_MAX to
61 static long long int timeout_when = LLONG_MAX;
63 /* Location where waiter created. */
64 static const char *timeout_where;
66 static struct poll_waiter *new_waiter(int fd, short int events,
69 /* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
70 * or POLLOUT or POLLIN | POLLOUT). The following call to poll_block() will
71 * wake up when 'fd' becomes ready for one or more of the requested events.
73 * The event registration is one-shot: only the following call to poll_block()
74 * is affected. The event will need to be re-registered after poll_block() is
75 * called if it is to persist.
77 * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
78 * for more information. */
80 poll_fd_wait(int fd, short int events, const char *where)
82 COVERAGE_INC(poll_fd_wait);
83 return new_waiter(fd, events, where);
86 /* Causes the following call to poll_block() to block for no more than 'msec'
87 * milliseconds. If 'msec' is nonpositive, the following call to poll_block()
88 * will not block at all.
90 * The timer registration is one-shot: only the following call to poll_block()
91 * is affected. The timer will need to be re-registered after poll_block() is
92 * called if it is to persist.
94 * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
95 * for more information. */
97 poll_timer_wait(long long int msec, const char *where)
99 long long int now = time_msec();
103 /* Wake up immediately. */
105 } else if ((unsigned long long int) now + msec <= LLONG_MAX) {
109 /* now + msec would overflow. */
113 poll_timer_wait_until(when, where);
116 /* Causes the following call to poll_block() to wake up when the current time,
117 * as returned by time_msec(), reaches 'when' or later. If 'when' is earlier
118 * than the current time, the following call to poll_block() will not block at
121 * The timer registration is one-shot: only the following call to poll_block()
122 * is affected. The timer will need to be re-registered after poll_block() is
123 * called if it is to persist.
125 * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
126 * for more information. */
128 poll_timer_wait_until(long long int when, const char *where)
130 if (when < timeout_when) {
132 timeout_where = where;
136 /* Causes the following call to poll_block() to wake up immediately, without
139 * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
140 * for more information. */
142 poll_immediate_wake(const char *where)
144 poll_timer_wait(0, where);
147 /* Logs, if appropriate, that the poll loop was awakened by an event
148 * registered at 'where' (typically a source file and line number). The other
149 * arguments have two possible interpretations:
151 * - If 'pollfd' is nonnull then it should be the "struct pollfd" that caused
152 * the wakeup. 'timeout' is ignored.
154 * - If 'pollfd' is NULL then 'timeout' is the number of milliseconds after
155 * which the poll loop woke up.
158 log_wakeup(const char *where, const struct pollfd *pollfd, int timeout)
160 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(120, 120);
161 enum vlog_level level;
165 cpu_usage = get_cpu_usage();
166 if (VLOG_IS_DBG_ENABLED()) {
168 } else if (cpu_usage > 50 && !VLOG_DROP_WARN(&rl)) {
175 ds_put_cstr(&s, "wakeup due to ");
177 char *description = describe_fd(pollfd->fd);
178 if (pollfd->revents & POLLIN) {
179 ds_put_cstr(&s, "[POLLIN]");
181 if (pollfd->revents & POLLOUT) {
182 ds_put_cstr(&s, "[POLLOUT]");
184 if (pollfd->revents & POLLERR) {
185 ds_put_cstr(&s, "[POLLERR]");
187 if (pollfd->revents & POLLHUP) {
188 ds_put_cstr(&s, "[POLLHUP]");
190 if (pollfd->revents & POLLNVAL) {
191 ds_put_cstr(&s, "[POLLNVAL]");
193 ds_put_format(&s, " on fd %d (%s)", pollfd->fd, description);
196 ds_put_format(&s, "%d-ms timeout", timeout);
199 ds_put_format(&s, " at %s", where);
201 if (cpu_usage >= 0) {
202 ds_put_format(&s, " (%d%% CPU usage)", cpu_usage);
204 VLOG(level, "%s", ds_cstr(&s));
208 /* Blocks until one or more of the events registered with poll_fd_wait()
209 * occurs, or until the minimum duration registered with poll_timer_wait()
210 * elapses, or not at all if poll_immediate_wake() has been called. */
214 static struct pollfd *pollfds;
215 static size_t max_pollfds;
217 struct poll_waiter *pw, *next;
218 int n_waiters, n_pollfds;
222 /* Register fatal signal events before actually doing any real work for
226 n_waiters = list_size(&waiters);
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;
241 if (timeout_when == LLONG_MIN) {
242 COVERAGE_INC(poll_zero_timeout);
244 retval = time_poll(pollfds, n_pollfds, timeout_when, &elapsed);
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, elapsed);
252 LIST_FOR_EACH_SAFE (pw, next, node, &waiters) {
253 if (pw->pollfd->revents) {
254 log_wakeup(pw->where, pw->pollfd, 0);
259 timeout_when = LLONG_MAX;
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);
281 /* Creates and returns a new poll_waiter for 'fd' and 'events'. */
282 static struct poll_waiter *
283 new_waiter(int fd, short int events, const char *where)
285 struct poll_waiter *waiter = xzalloc(sizeof *waiter);
288 waiter->events = events;
289 waiter->where = where;
290 list_push_back(&waiters, &waiter->node);