]> git.proxmox.com Git - mirror_ovs.git/blame - lib/poll-loop.c
ovs-thread: Replace ovsthread_counter by more general ovsthread_stats.
[mirror_ovs.git] / lib / poll-loop.c
CommitLineData
064af421 1/*
4ca828d7 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
064af421
BP
15 */
16
17#include <config.h>
18#include "poll-loop.h"
064af421 19#include <errno.h>
2886875a 20#include <inttypes.h>
064af421
BP
21#include <poll.h>
22#include <stdlib.h>
23#include <string.h>
064af421
BP
24#include "coverage.h"
25#include "dynamic-string.h"
d8b30702 26#include "fatal-signal.h"
064af421 27#include "list.h"
2c06a966 28#include "ovs-thread.h"
55b40355 29#include "seq.h"
f89ffb0e 30#include "socket-util.h"
064af421 31#include "timeval.h"
064af421 32#include "vlog.h"
4ca828d7
LS
33#include "hmap.h"
34#include "hash.h"
064af421 35
d98e6007 36VLOG_DEFINE_THIS_MODULE(poll_loop);
5136ce49 37
d76f09ea
BP
38COVERAGE_DEFINE(poll_fd_wait);
39COVERAGE_DEFINE(poll_zero_timeout);
40
4ca828d7
LS
41struct poll_node {
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. */
46};
47
2c06a966
BP
48struct poll_loop {
49 /* All active poll waiters. */
4ca828d7 50 struct hmap poll_nodes;
064af421 51
2c06a966
BP
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. */
56};
064af421 57
2c06a966 58static struct poll_loop *poll_loop(void);
064af421 59
4ca828d7
LS
60/* Look up the node with same fd and wevent. */
61static struct poll_node *
62find_poll_node(struct poll_loop *loop, int fd, uint32_t wevent)
63{
64 struct poll_node *node;
65
66 HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_2words(fd, wevent),
67 &loop->poll_nodes) {
68 if (node->pollfd.fd == fd && node->wevent == wevent) {
69 return node;
70 }
71 }
72 return NULL;
73}
74
75/* On Unix based systems:
064af421 76 *
4ca828d7
LS
77 * Registers 'fd' as waiting for the specified 'events' (which should be
78 * POLLIN or POLLOUT or POLLIN | POLLOUT). The following call to
79 * poll_block() will wake up when 'fd' becomes ready for one or more of the
80 * requested events. the 'fd's are given to poll() function later.
81 *
82 * On Windows system:
83 *
55489d31
GS
84 * If both 'wevent' handle and 'fd' is specified, associate the 'fd' with
85 * with that 'wevent' for 'events' (implemented in poll_block()).
86 * In case of no 'fd' specified, wake up on any event on that 'wevent'.
87 * These wevents are given to the WaitForMultipleObjects() to be polled.
88 * The event registration is one-shot: only the following call to
89 * poll_block() is affected. The event will need to be re-registered after
90 * poll_block() is called if it is to persist.
f89ffb0e 91 *
5453ae20
BP
92 * ('where' is used in debug logging. Commonly one would use poll_fd_wait() to
93 * automatically provide the caller's source file and line number for
94 * 'where'.) */
8f6c3ad7 95void
4ca828d7 96poll_fd_wait_at(int fd, HANDLE wevent, short int events, const char *where)
064af421 97{
2c06a966 98 struct poll_loop *loop = poll_loop();
4ca828d7 99 struct poll_node *node;
2c06a966 100
064af421 101 COVERAGE_INC(poll_fd_wait);
4ca828d7
LS
102
103#ifdef _WIN32
104 /* Null event cannot be polled. */
105 if (wevent == 0) {
106 VLOG_ERR("No event to wait fd %d", fd);
107 return;
2c06a966 108 }
4ca828d7 109#endif
2c06a966 110
4ca828d7
LS
111 /* Check for duplicate. If found, "or" the event. */
112 node = find_poll_node(loop, fd, wevent);
113 if (node) {
114 node->pollfd.events |= events;
115 } else {
116 node = xzalloc(sizeof *node);
117 hmap_insert(&loop->poll_nodes, &node->hmap_node,
118 hash_2words(fd, wevent));
119 node->pollfd.fd = fd;
120 node->pollfd.events = events;
121 node->wevent = wevent;
122 node->where = where;
123 }
064af421
BP
124}
125
126/* Causes the following call to poll_block() to block for no more than 'msec'
127 * milliseconds. If 'msec' is nonpositive, the following call to poll_block()
128 * will not block at all.
129 *
130 * The timer registration is one-shot: only the following call to poll_block()
131 * is affected. The timer will need to be re-registered after poll_block() is
f89ffb0e
BP
132 * called if it is to persist.
133 *
5453ae20
BP
134 * ('where' is used in debug logging. Commonly one would use poll_timer_wait()
135 * to automatically provide the caller's source file and line number for
136 * 'where'.) */
064af421 137void
5453ae20 138poll_timer_wait_at(long long int msec, const char *where)
064af421 139{
cee03df4
BP
140 long long int now = time_msec();
141 long long int when;
142
143 if (msec <= 0) {
144 /* Wake up immediately. */
145 when = LLONG_MIN;
146 } else if ((unsigned long long int) now + msec <= LLONG_MAX) {
147 /* Normal case. */
148 when = now + msec;
149 } else {
150 /* now + msec would overflow. */
151 when = LLONG_MAX;
152 }
153
5453ae20 154 poll_timer_wait_until_at(when, where);
064af421
BP
155}
156
7cf8b266 157/* Causes the following call to poll_block() to wake up when the current time,
cee03df4 158 * as returned by time_msec(), reaches 'when' or later. If 'when' is earlier
7cf8b266
BP
159 * than the current time, the following call to poll_block() will not block at
160 * all.
161 *
162 * The timer registration is one-shot: only the following call to poll_block()
163 * is affected. The timer will need to be re-registered after poll_block() is
f89ffb0e
BP
164 * called if it is to persist.
165 *
5453ae20
BP
166 * ('where' is used in debug logging. Commonly one would use
167 * poll_timer_wait_until() to automatically provide the caller's source file
168 * and line number for 'where'.) */
7cf8b266 169void
5453ae20 170poll_timer_wait_until_at(long long int when, const char *where)
7cf8b266 171{
2c06a966
BP
172 struct poll_loop *loop = poll_loop();
173 if (when < loop->timeout_when) {
174 loop->timeout_when = when;
175 loop->timeout_where = where;
cee03df4 176 }
7cf8b266
BP
177}
178
064af421 179/* Causes the following call to poll_block() to wake up immediately, without
f89ffb0e
BP
180 * blocking.
181 *
5453ae20
BP
182 * ('where' is used in debug logging. Commonly one would use
183 * poll_immediate_wake() to automatically provide the caller's source file and
184 * line number for 'where'.) */
064af421 185void
5453ae20 186poll_immediate_wake_at(const char *where)
064af421 187{
5453ae20 188 poll_timer_wait_at(0, where);
064af421
BP
189}
190
959ec62e
BP
191/* Logs, if appropriate, that the poll loop was awakened by an event
192 * registered at 'where' (typically a source file and line number). The other
193 * arguments have two possible interpretations:
194 *
195 * - If 'pollfd' is nonnull then it should be the "struct pollfd" that caused
d19cedb2 196 * the wakeup. 'timeout' is ignored.
959ec62e 197 *
d19cedb2
BP
198 * - If 'pollfd' is NULL then 'timeout' is the number of milliseconds after
199 * which the poll loop woke up.
959ec62e
BP
200 */
201static void
202log_wakeup(const char *where, const struct pollfd *pollfd, int timeout)
064af421 203{
cf1b8a92 204 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
959ec62e
BP
205 enum vlog_level level;
206 int cpu_usage;
207 struct ds s;
064af421 208
959ec62e
BP
209 cpu_usage = get_cpu_usage();
210 if (VLOG_IS_DBG_ENABLED()) {
211 level = VLL_DBG;
692bf61a
BP
212 } else if (cpu_usage > 50 && !VLOG_DROP_INFO(&rl)) {
213 level = VLL_INFO;
959ec62e
BP
214 } else {
215 return;
216 }
064af421 217
959ec62e
BP
218 ds_init(&s);
219 ds_put_cstr(&s, "wakeup due to ");
220 if (pollfd) {
221 char *description = describe_fd(pollfd->fd);
222 if (pollfd->revents & POLLIN) {
223 ds_put_cstr(&s, "[POLLIN]");
224 }
225 if (pollfd->revents & POLLOUT) {
226 ds_put_cstr(&s, "[POLLOUT]");
227 }
228 if (pollfd->revents & POLLERR) {
229 ds_put_cstr(&s, "[POLLERR]");
230 }
231 if (pollfd->revents & POLLHUP) {
232 ds_put_cstr(&s, "[POLLHUP]");
233 }
234 if (pollfd->revents & POLLNVAL) {
235 ds_put_cstr(&s, "[POLLNVAL]");
236 }
237 ds_put_format(&s, " on fd %d (%s)", pollfd->fd, description);
238 free(description);
239 } else {
240 ds_put_format(&s, "%d-ms timeout", timeout);
241 }
f89ffb0e 242 if (where) {
959ec62e 243 ds_put_format(&s, " at %s", where);
064af421 244 }
959ec62e
BP
245 if (cpu_usage >= 0) {
246 ds_put_format(&s, " (%d%% CPU usage)", cpu_usage);
247 }
248 VLOG(level, "%s", ds_cstr(&s));
249 ds_destroy(&s);
064af421
BP
250}
251
4ca828d7
LS
252static void
253free_poll_nodes(struct poll_loop *loop)
254{
255 struct poll_node *node, *next;
256
257 HMAP_FOR_EACH_SAFE (node, next, hmap_node, &loop->poll_nodes) {
258 hmap_remove(&loop->poll_nodes, &node->hmap_node);
259 free(node);
260 }
261}
262
064af421
BP
263/* Blocks until one or more of the events registered with poll_fd_wait()
264 * occurs, or until the minimum duration registered with poll_timer_wait()
d474bd01 265 * elapses, or not at all if poll_immediate_wake() has been called. */
064af421
BP
266void
267poll_block(void)
268{
2c06a966 269 struct poll_loop *loop = poll_loop();
4ca828d7
LS
270 struct poll_node *node;
271 struct pollfd *pollfds;
272 HANDLE *wevents = NULL;
cee03df4 273 int elapsed;
064af421 274 int retval;
4ca828d7 275 int i;
064af421 276
d8b30702
JG
277 /* Register fatal signal events before actually doing any real work for
278 * poll_block. */
279 fatal_signal_wait();
280
2c06a966 281 if (loop->timeout_when == LLONG_MIN) {
064af421
BP
282 COVERAGE_INC(poll_zero_timeout);
283 }
2c06a966 284
53d98a1e 285 timewarp_wait();
4ca828d7
LS
286 pollfds = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *pollfds);
287
288#ifdef _WIN32
289 wevents = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *wevents);
290#endif
291
292 /* Populate with all the fds and events. */
293 i = 0;
294 HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) {
295 pollfds[i] = node->pollfd;
296#ifdef _WIN32
297 wevents[i] = node->wevent;
55489d31
GS
298 if (node->pollfd.fd && node->wevent) {
299 short int wsa_events = 0;
300 if (node->pollfd.events & POLLIN) {
301 wsa_events |= FD_READ | FD_ACCEPT | FD_CLOSE;
302 }
303 if (node->pollfd.events & POLLOUT) {
304 wsa_events |= FD_WRITE | FD_CONNECT | FD_CLOSE;
305 }
306 WSAEventSelect(node->pollfd.fd, node->wevent, wsa_events);
307 }
4ca828d7
LS
308#endif
309 i++;
310 }
311
312 retval = time_poll(pollfds, hmap_count(&loop->poll_nodes), wevents,
2c06a966 313 loop->timeout_when, &elapsed);
064af421
BP
314 if (retval < 0) {
315 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
10a89ef0 316 VLOG_ERR_RL(&rl, "poll: %s", ovs_strerror(-retval));
959ec62e 317 } else if (!retval) {
2c06a966 318 log_wakeup(loop->timeout_where, NULL, elapsed);
8f6c3ad7 319 } else if (get_cpu_usage() > 50 || VLOG_IS_DBG_ENABLED()) {
4ca828d7
LS
320 i = 0;
321 HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) {
322 if (pollfds[i].revents) {
323 log_wakeup(node->where, &pollfds[i], 0);
8f6c3ad7 324 }
4ca828d7 325 i++;
064af421 326 }
064af421
BP
327 }
328
4ca828d7 329 free_poll_nodes(loop);
2c06a966
BP
330 loop->timeout_when = LLONG_MAX;
331 loop->timeout_where = NULL;
4ca828d7
LS
332 free(pollfds);
333 free(wevents);
d8b30702
JG
334
335 /* Handle any pending signals before doing anything else. */
336 fatal_signal_run();
55b40355
BP
337
338 seq_woke();
064af421 339}
064af421 340\f
8f6c3ad7 341static void
2c06a966 342free_poll_loop(void *loop_)
064af421 343{
2c06a966
BP
344 struct poll_loop *loop = loop_;
345
4ca828d7
LS
346 free_poll_nodes(loop);
347 hmap_destroy(&loop->poll_nodes);
2c06a966
BP
348 free(loop);
349}
350
351static struct poll_loop *
352poll_loop(void)
353{
354 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
355 static pthread_key_t key;
356 struct poll_loop *loop;
357
358 if (ovsthread_once_start(&once)) {
359 xpthread_key_create(&key, free_poll_loop);
360 ovsthread_once_done(&once);
8f6c3ad7
BP
361 }
362
2c06a966
BP
363 loop = pthread_getspecific(key);
364 if (!loop) {
365 loop = xzalloc(sizeof *loop);
4ca828d7 366 hmap_init(&loop->poll_nodes);
9c4c45ed 367 xpthread_setspecific(key, loop);
2c06a966
BP
368 }
369 return loop;
064af421 370}
2c06a966 371