2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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"
25 #include "dynamic-string.h"
26 #include "fatal-signal.h"
28 #include "ovs-thread.h"
30 #include "socket-util.h"
34 VLOG_DEFINE_THIS_MODULE(poll_loop);
36 COVERAGE_DEFINE(poll_fd_wait);
37 COVERAGE_DEFINE(poll_zero_timeout);
40 /* All active poll waiters. */
41 struct pollfd *pollfds; /* Events to pass to poll(). */
42 const char **where; /* Where each pollfd was created. */
43 size_t n_waiters; /* Number of elems in 'where' and 'pollfds'. */
44 size_t allocated_waiters; /* Allocated elems in 'where' and 'pollfds'. */
46 /* Time at which to wake up the next call to poll_block(), LLONG_MIN to
47 * wake up immediately, or LLONG_MAX to wait forever. */
48 long long int timeout_when; /* In msecs as returned by time_msec(). */
49 const char *timeout_where; /* Where 'timeout_when' was set. */
52 static struct poll_loop *poll_loop(void);
54 /* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
55 * or POLLOUT or POLLIN | POLLOUT). The following call to poll_block() will
56 * wake up when 'fd' becomes ready for one or more of the requested events.
58 * The event registration is one-shot: only the following call to poll_block()
59 * is affected. The event will need to be re-registered after poll_block() is
60 * called if it is to persist.
62 * ('where' is used in debug logging. Commonly one would use poll_fd_wait() to
63 * automatically provide the caller's source file and line number for
66 poll_fd_wait_at(int fd, short int events, const char *where)
68 struct poll_loop *loop = poll_loop();
70 COVERAGE_INC(poll_fd_wait);
71 if (loop->n_waiters >= loop->allocated_waiters) {
72 loop->where = x2nrealloc(loop->where, &loop->allocated_waiters,
74 loop->pollfds = xrealloc(loop->pollfds,
75 (loop->allocated_waiters
76 * sizeof *loop->pollfds));
79 loop->where[loop->n_waiters] = where;
80 loop->pollfds[loop->n_waiters].fd = fd;
81 loop->pollfds[loop->n_waiters].events = events;
85 /* Causes the following call to poll_block() to block for no more than 'msec'
86 * milliseconds. If 'msec' is nonpositive, the following call to poll_block()
87 * will not block at all.
89 * The timer registration is one-shot: only the following call to poll_block()
90 * is affected. The timer will need to be re-registered after poll_block() is
91 * called if it is to persist.
93 * ('where' is used in debug logging. Commonly one would use poll_timer_wait()
94 * to automatically provide the caller's source file and line number for
97 poll_timer_wait_at(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_at(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 * ('where' is used in debug logging. Commonly one would use
126 * poll_timer_wait_until() to automatically provide the caller's source file
127 * and line number for 'where'.) */
129 poll_timer_wait_until_at(long long int when, const char *where)
131 struct poll_loop *loop = poll_loop();
132 if (when < loop->timeout_when) {
133 loop->timeout_when = when;
134 loop->timeout_where = where;
138 /* Causes the following call to poll_block() to wake up immediately, without
141 * ('where' is used in debug logging. Commonly one would use
142 * poll_immediate_wake() to automatically provide the caller's source file and
143 * line number for 'where'.) */
145 poll_immediate_wake_at(const char *where)
147 poll_timer_wait_at(0, where);
150 /* Logs, if appropriate, that the poll loop was awakened by an event
151 * registered at 'where' (typically a source file and line number). The other
152 * arguments have two possible interpretations:
154 * - If 'pollfd' is nonnull then it should be the "struct pollfd" that caused
155 * the wakeup. 'timeout' is ignored.
157 * - If 'pollfd' is NULL then 'timeout' is the number of milliseconds after
158 * which the poll loop woke up.
161 log_wakeup(const char *where, const struct pollfd *pollfd, int timeout)
163 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
164 enum vlog_level level;
168 cpu_usage = get_cpu_usage();
169 if (VLOG_IS_DBG_ENABLED()) {
171 } else if (cpu_usage > 50 && !VLOG_DROP_INFO(&rl)) {
178 ds_put_cstr(&s, "wakeup due to ");
180 char *description = describe_fd(pollfd->fd);
181 if (pollfd->revents & POLLIN) {
182 ds_put_cstr(&s, "[POLLIN]");
184 if (pollfd->revents & POLLOUT) {
185 ds_put_cstr(&s, "[POLLOUT]");
187 if (pollfd->revents & POLLERR) {
188 ds_put_cstr(&s, "[POLLERR]");
190 if (pollfd->revents & POLLHUP) {
191 ds_put_cstr(&s, "[POLLHUP]");
193 if (pollfd->revents & POLLNVAL) {
194 ds_put_cstr(&s, "[POLLNVAL]");
196 ds_put_format(&s, " on fd %d (%s)", pollfd->fd, description);
199 ds_put_format(&s, "%d-ms timeout", timeout);
202 ds_put_format(&s, " at %s", where);
204 if (cpu_usage >= 0) {
205 ds_put_format(&s, " (%d%% CPU usage)", cpu_usage);
207 VLOG(level, "%s", ds_cstr(&s));
211 /* Blocks until one or more of the events registered with poll_fd_wait()
212 * occurs, or until the minimum duration registered with poll_timer_wait()
213 * elapses, or not at all if poll_immediate_wake() has been called. */
217 struct poll_loop *loop = poll_loop();
221 /* Register fatal signal events before actually doing any real work for
225 if (loop->timeout_when == LLONG_MIN) {
226 COVERAGE_INC(poll_zero_timeout);
229 retval = time_poll(loop->pollfds, loop->n_waiters,
230 loop->timeout_when, &elapsed);
232 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
233 VLOG_ERR_RL(&rl, "poll: %s", ovs_strerror(-retval));
234 } else if (!retval) {
235 log_wakeup(loop->timeout_where, NULL, elapsed);
236 } else if (get_cpu_usage() > 50 || VLOG_IS_DBG_ENABLED()) {
239 for (i = 0; i < loop->n_waiters; i++) {
240 if (loop->pollfds[i].revents) {
241 log_wakeup(loop->where[i], &loop->pollfds[i], 0);
246 loop->timeout_when = LLONG_MAX;
247 loop->timeout_where = NULL;
250 /* Handle any pending signals before doing anything else. */
257 free_poll_loop(void *loop_)
259 struct poll_loop *loop = loop_;
266 static struct poll_loop *
269 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
270 static pthread_key_t key;
271 struct poll_loop *loop;
273 if (ovsthread_once_start(&once)) {
274 xpthread_key_create(&key, free_poll_loop);
275 ovsthread_once_done(&once);
278 loop = pthread_getspecific(key);
280 loop = xzalloc(sizeof *loop);
281 xpthread_setspecific(key, loop);