2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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"
32 #include "openvswitch/vlog.h"
36 VLOG_DEFINE_THIS_MODULE(poll_loop);
38 COVERAGE_DEFINE(poll_create_node);
39 COVERAGE_DEFINE(poll_zero_timeout);
42 struct hmap_node hmap_node;
43 struct pollfd pollfd; /* Events to pass to time_poll(). */
44 HANDLE wevent; /* Events for WaitForMultipleObjects(). */
45 const char *where; /* Where poll_node was created. */
49 /* All active poll waiters. */
50 struct hmap poll_nodes;
52 /* Time at which to wake up the next call to poll_block(), LLONG_MIN to
53 * wake up immediately, or LLONG_MAX to wait forever. */
54 long long int timeout_when; /* In msecs as returned by time_msec(). */
55 const char *timeout_where; /* Where 'timeout_when' was set. */
58 static struct poll_loop *poll_loop(void);
60 /* Look up the node with same fd or wevent. */
61 static struct poll_node *
62 find_poll_node(struct poll_loop *loop, int fd, HANDLE wevent)
64 struct poll_node *node;
66 /* Both 'fd' and 'wevent' cannot be set. */
67 ovs_assert(!fd != !wevent);
69 HMAP_FOR_EACH_WITH_HASH (node, hmap_node,
70 hash_2words(fd, (uint32_t)wevent),
72 if ((fd && node->pollfd.fd == fd)
73 || (wevent && node->wevent == wevent)) {
80 /* On Unix based systems:
82 * Registers 'fd' as waiting for the specified 'events' (which should be
83 * POLLIN or POLLOUT or POLLIN | POLLOUT). The following call to
84 * poll_block() will wake up when 'fd' becomes ready for one or more of the
85 * requested events. The 'fd's are given to poll() function later.
89 * If 'fd' is specified, create a new 'wevent'. Association of 'fd' and
90 * 'wevent' for 'events' happens in poll_block(). If 'wevent' is specified,
91 * it is assumed that it is unrelated to any sockets and poll_block()
92 * will wake up on any event on that 'wevent'. It is an error to pass
93 * both 'wevent' and 'fd'.
95 * The event registration is one-shot: only the following call to
96 * poll_block() is affected. The event will need to be re-registered after
97 * poll_block() is called if it is to persist.
99 * ('where' is used in debug logging. Commonly one would use poll_fd_wait() to
100 * automatically provide the caller's source file and line number for
103 poll_create_node(int fd, HANDLE wevent, short int events, const char *where)
105 struct poll_loop *loop = poll_loop();
106 struct poll_node *node;
108 COVERAGE_INC(poll_create_node);
110 /* Both 'fd' and 'wevent' cannot be set. */
111 ovs_assert(!fd != !wevent);
113 /* Check for duplicate. If found, "or" the events. */
114 node = find_poll_node(loop, fd, wevent);
116 node->pollfd.events |= events;
118 node = xzalloc(sizeof *node);
119 hmap_insert(&loop->poll_nodes, &node->hmap_node,
120 hash_2words(fd, (uint32_t)wevent));
121 node->pollfd.fd = fd;
122 node->pollfd.events = events;
125 wevent = CreateEvent(NULL, FALSE, FALSE, NULL);
128 node->wevent = wevent;
133 /* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
134 * or POLLOUT or POLLIN | POLLOUT). The following call to poll_block() will
135 * wake up when 'fd' becomes ready for one or more of the requested events.
137 * On Windows, 'fd' must be a socket.
139 * The event registration is one-shot: only the following call to poll_block()
140 * is affected. The event will need to be re-registered after poll_block() is
141 * called if it is to persist.
143 * ('where' is used in debug logging. Commonly one would use poll_fd_wait() to
144 * automatically provide the caller's source file and line number for
147 poll_fd_wait_at(int fd, short int events, const char *where)
149 poll_create_node(fd, 0, events, where);
153 /* Registers for the next call to poll_block() to wake up when 'wevent' is
156 * The event registration is one-shot: only the following call to poll_block()
157 * is affected. The event will need to be re-registered after poll_block() is
158 * called if it is to persist.
160 * ('where' is used in debug logging. Commonly one would use
161 * poll_wevent_wait() to automatically provide the caller's source file and
162 * line number for 'where'.) */
164 poll_wevent_wait_at(HANDLE wevent, const char *where)
166 poll_create_node(0, wevent, 0, where);
170 /* Causes the following call to poll_block() to block for no more than 'msec'
171 * milliseconds. If 'msec' is nonpositive, the following call to poll_block()
172 * will not block at all.
174 * The timer registration is one-shot: only the following call to poll_block()
175 * is affected. The timer will need to be re-registered after poll_block() is
176 * called if it is to persist.
178 * ('where' is used in debug logging. Commonly one would use poll_timer_wait()
179 * to automatically provide the caller's source file and line number for
182 poll_timer_wait_at(long long int msec, const char *where)
184 long long int now = time_msec();
188 /* Wake up immediately. */
190 } else if ((unsigned long long int) now + msec <= LLONG_MAX) {
194 /* now + msec would overflow. */
198 poll_timer_wait_until_at(when, where);
201 /* Causes the following call to poll_block() to wake up when the current time,
202 * as returned by time_msec(), reaches 'when' or later. If 'when' is earlier
203 * than the current time, the following call to poll_block() will not block at
206 * The timer registration is one-shot: only the following call to poll_block()
207 * is affected. The timer will need to be re-registered after poll_block() is
208 * called if it is to persist.
210 * ('where' is used in debug logging. Commonly one would use
211 * poll_timer_wait_until() to automatically provide the caller's source file
212 * and line number for 'where'.) */
214 poll_timer_wait_until_at(long long int when, const char *where)
216 struct poll_loop *loop = poll_loop();
217 if (when < loop->timeout_when) {
218 loop->timeout_when = when;
219 loop->timeout_where = where;
223 /* Causes the following call to poll_block() to wake up immediately, without
226 * ('where' is used in debug logging. Commonly one would use
227 * poll_immediate_wake() to automatically provide the caller's source file and
228 * line number for 'where'.) */
230 poll_immediate_wake_at(const char *where)
232 poll_timer_wait_at(0, where);
235 /* Logs, if appropriate, that the poll loop was awakened by an event
236 * registered at 'where' (typically a source file and line number). The other
237 * arguments have two possible interpretations:
239 * - If 'pollfd' is nonnull then it should be the "struct pollfd" that caused
240 * the wakeup. 'timeout' is ignored.
242 * - If 'pollfd' is NULL then 'timeout' is the number of milliseconds after
243 * which the poll loop woke up.
246 log_wakeup(const char *where, const struct pollfd *pollfd, int timeout)
248 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
249 enum vlog_level level;
253 cpu_usage = get_cpu_usage();
254 if (VLOG_IS_DBG_ENABLED()) {
256 } else if (cpu_usage > 50 && !VLOG_DROP_INFO(&rl)) {
263 ds_put_cstr(&s, "wakeup due to ");
265 char *description = describe_fd(pollfd->fd);
266 if (pollfd->revents & POLLIN) {
267 ds_put_cstr(&s, "[POLLIN]");
269 if (pollfd->revents & POLLOUT) {
270 ds_put_cstr(&s, "[POLLOUT]");
272 if (pollfd->revents & POLLERR) {
273 ds_put_cstr(&s, "[POLLERR]");
275 if (pollfd->revents & POLLHUP) {
276 ds_put_cstr(&s, "[POLLHUP]");
278 if (pollfd->revents & POLLNVAL) {
279 ds_put_cstr(&s, "[POLLNVAL]");
281 ds_put_format(&s, " on fd %d (%s)", pollfd->fd, description);
284 ds_put_format(&s, "%d-ms timeout", timeout);
287 ds_put_format(&s, " at %s", where);
289 if (cpu_usage >= 0) {
290 ds_put_format(&s, " (%d%% CPU usage)", cpu_usage);
292 VLOG(level, "%s", ds_cstr(&s));
297 free_poll_nodes(struct poll_loop *loop)
299 struct poll_node *node, *next;
301 HMAP_FOR_EACH_SAFE (node, next, hmap_node, &loop->poll_nodes) {
302 hmap_remove(&loop->poll_nodes, &node->hmap_node);
304 if (node->wevent && node->pollfd.fd) {
305 WSAEventSelect(node->pollfd.fd, NULL, 0);
306 CloseHandle(node->wevent);
313 /* Blocks until one or more of the events registered with poll_fd_wait()
314 * occurs, or until the minimum duration registered with poll_timer_wait()
315 * elapses, or not at all if poll_immediate_wake() has been called. */
319 struct poll_loop *loop = poll_loop();
320 struct poll_node *node;
321 struct pollfd *pollfds;
322 HANDLE *wevents = NULL;
327 /* Register fatal signal events before actually doing any real work for
331 if (loop->timeout_when == LLONG_MIN) {
332 COVERAGE_INC(poll_zero_timeout);
336 pollfds = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *pollfds);
339 wevents = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *wevents);
342 /* Populate with all the fds and events. */
344 HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) {
345 pollfds[i] = node->pollfd;
347 wevents[i] = node->wevent;
348 if (node->pollfd.fd && node->wevent) {
349 short int wsa_events = 0;
350 if (node->pollfd.events & POLLIN) {
351 wsa_events |= FD_READ | FD_ACCEPT | FD_CLOSE;
353 if (node->pollfd.events & POLLOUT) {
354 wsa_events |= FD_WRITE | FD_CONNECT | FD_CLOSE;
356 WSAEventSelect(node->pollfd.fd, node->wevent, wsa_events);
362 retval = time_poll(pollfds, hmap_count(&loop->poll_nodes), wevents,
363 loop->timeout_when, &elapsed);
365 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
366 VLOG_ERR_RL(&rl, "poll: %s", ovs_strerror(-retval));
367 } else if (!retval) {
368 log_wakeup(loop->timeout_where, NULL, elapsed);
369 } else if (get_cpu_usage() > 50 || VLOG_IS_DBG_ENABLED()) {
371 HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) {
372 if (pollfds[i].revents) {
373 log_wakeup(node->where, &pollfds[i], 0);
379 free_poll_nodes(loop);
380 loop->timeout_when = LLONG_MAX;
381 loop->timeout_where = NULL;
385 /* Handle any pending signals before doing anything else. */
392 free_poll_loop(void *loop_)
394 struct poll_loop *loop = loop_;
396 free_poll_nodes(loop);
397 hmap_destroy(&loop->poll_nodes);
401 static struct poll_loop *
404 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
405 static pthread_key_t key;
406 struct poll_loop *loop;
408 if (ovsthread_once_start(&once)) {
409 xpthread_key_create(&key, free_poll_loop);
410 ovsthread_once_done(&once);
413 loop = pthread_getspecific(key);
415 loop = xzalloc(sizeof *loop);
416 hmap_init(&loop->poll_nodes);
417 xpthread_setspecific(key, loop);