]> git.proxmox.com Git - mirror_ovs.git/blame - lib/poll-loop.c
rpm: improved RPM sources dir explanation
[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 }
6fe0610c
BP
109#else
110 wevent = 0;
4ca828d7 111#endif
2c06a966 112
4ca828d7
LS
113 /* Check for duplicate. If found, "or" the event. */
114 node = find_poll_node(loop, fd, wevent);
115 if (node) {
116 node->pollfd.events |= events;
117 } else {
118 node = xzalloc(sizeof *node);
119 hmap_insert(&loop->poll_nodes, &node->hmap_node,
120 hash_2words(fd, wevent));
121 node->pollfd.fd = fd;
122 node->pollfd.events = events;
123 node->wevent = wevent;
124 node->where = where;
125 }
064af421
BP
126}
127
128/* Causes the following call to poll_block() to block for no more than 'msec'
129 * milliseconds. If 'msec' is nonpositive, the following call to poll_block()
130 * will not block at all.
131 *
132 * The timer registration is one-shot: only the following call to poll_block()
133 * is affected. The timer will need to be re-registered after poll_block() is
f89ffb0e
BP
134 * called if it is to persist.
135 *
5453ae20
BP
136 * ('where' is used in debug logging. Commonly one would use poll_timer_wait()
137 * to automatically provide the caller's source file and line number for
138 * 'where'.) */
064af421 139void
5453ae20 140poll_timer_wait_at(long long int msec, const char *where)
064af421 141{
cee03df4
BP
142 long long int now = time_msec();
143 long long int when;
144
145 if (msec <= 0) {
146 /* Wake up immediately. */
147 when = LLONG_MIN;
148 } else if ((unsigned long long int) now + msec <= LLONG_MAX) {
149 /* Normal case. */
150 when = now + msec;
151 } else {
152 /* now + msec would overflow. */
153 when = LLONG_MAX;
154 }
155
5453ae20 156 poll_timer_wait_until_at(when, where);
064af421
BP
157}
158
7cf8b266 159/* Causes the following call to poll_block() to wake up when the current time,
cee03df4 160 * as returned by time_msec(), reaches 'when' or later. If 'when' is earlier
7cf8b266
BP
161 * than the current time, the following call to poll_block() will not block at
162 * all.
163 *
164 * The timer registration is one-shot: only the following call to poll_block()
165 * is affected. The timer will need to be re-registered after poll_block() is
f89ffb0e
BP
166 * called if it is to persist.
167 *
5453ae20
BP
168 * ('where' is used in debug logging. Commonly one would use
169 * poll_timer_wait_until() to automatically provide the caller's source file
170 * and line number for 'where'.) */
7cf8b266 171void
5453ae20 172poll_timer_wait_until_at(long long int when, const char *where)
7cf8b266 173{
2c06a966
BP
174 struct poll_loop *loop = poll_loop();
175 if (when < loop->timeout_when) {
176 loop->timeout_when = when;
177 loop->timeout_where = where;
cee03df4 178 }
7cf8b266
BP
179}
180
064af421 181/* Causes the following call to poll_block() to wake up immediately, without
f89ffb0e
BP
182 * blocking.
183 *
5453ae20
BP
184 * ('where' is used in debug logging. Commonly one would use
185 * poll_immediate_wake() to automatically provide the caller's source file and
186 * line number for 'where'.) */
064af421 187void
5453ae20 188poll_immediate_wake_at(const char *where)
064af421 189{
5453ae20 190 poll_timer_wait_at(0, where);
064af421
BP
191}
192
959ec62e
BP
193/* Logs, if appropriate, that the poll loop was awakened by an event
194 * registered at 'where' (typically a source file and line number). The other
195 * arguments have two possible interpretations:
196 *
197 * - If 'pollfd' is nonnull then it should be the "struct pollfd" that caused
d19cedb2 198 * the wakeup. 'timeout' is ignored.
959ec62e 199 *
d19cedb2
BP
200 * - If 'pollfd' is NULL then 'timeout' is the number of milliseconds after
201 * which the poll loop woke up.
959ec62e
BP
202 */
203static void
204log_wakeup(const char *where, const struct pollfd *pollfd, int timeout)
064af421 205{
cf1b8a92 206 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
959ec62e
BP
207 enum vlog_level level;
208 int cpu_usage;
209 struct ds s;
064af421 210
959ec62e
BP
211 cpu_usage = get_cpu_usage();
212 if (VLOG_IS_DBG_ENABLED()) {
213 level = VLL_DBG;
692bf61a
BP
214 } else if (cpu_usage > 50 && !VLOG_DROP_INFO(&rl)) {
215 level = VLL_INFO;
959ec62e
BP
216 } else {
217 return;
218 }
064af421 219
959ec62e
BP
220 ds_init(&s);
221 ds_put_cstr(&s, "wakeup due to ");
222 if (pollfd) {
223 char *description = describe_fd(pollfd->fd);
224 if (pollfd->revents & POLLIN) {
225 ds_put_cstr(&s, "[POLLIN]");
226 }
227 if (pollfd->revents & POLLOUT) {
228 ds_put_cstr(&s, "[POLLOUT]");
229 }
230 if (pollfd->revents & POLLERR) {
231 ds_put_cstr(&s, "[POLLERR]");
232 }
233 if (pollfd->revents & POLLHUP) {
234 ds_put_cstr(&s, "[POLLHUP]");
235 }
236 if (pollfd->revents & POLLNVAL) {
237 ds_put_cstr(&s, "[POLLNVAL]");
238 }
239 ds_put_format(&s, " on fd %d (%s)", pollfd->fd, description);
240 free(description);
241 } else {
242 ds_put_format(&s, "%d-ms timeout", timeout);
243 }
f89ffb0e 244 if (where) {
959ec62e 245 ds_put_format(&s, " at %s", where);
064af421 246 }
959ec62e
BP
247 if (cpu_usage >= 0) {
248 ds_put_format(&s, " (%d%% CPU usage)", cpu_usage);
249 }
250 VLOG(level, "%s", ds_cstr(&s));
251 ds_destroy(&s);
064af421
BP
252}
253
4ca828d7
LS
254static void
255free_poll_nodes(struct poll_loop *loop)
256{
257 struct poll_node *node, *next;
258
259 HMAP_FOR_EACH_SAFE (node, next, hmap_node, &loop->poll_nodes) {
260 hmap_remove(&loop->poll_nodes, &node->hmap_node);
261 free(node);
262 }
263}
264
064af421
BP
265/* Blocks until one or more of the events registered with poll_fd_wait()
266 * occurs, or until the minimum duration registered with poll_timer_wait()
d474bd01 267 * elapses, or not at all if poll_immediate_wake() has been called. */
064af421
BP
268void
269poll_block(void)
270{
2c06a966 271 struct poll_loop *loop = poll_loop();
4ca828d7
LS
272 struct poll_node *node;
273 struct pollfd *pollfds;
274 HANDLE *wevents = NULL;
cee03df4 275 int elapsed;
064af421 276 int retval;
4ca828d7 277 int i;
064af421 278
d8b30702
JG
279 /* Register fatal signal events before actually doing any real work for
280 * poll_block. */
281 fatal_signal_wait();
282
2c06a966 283 if (loop->timeout_when == LLONG_MIN) {
064af421
BP
284 COVERAGE_INC(poll_zero_timeout);
285 }
2c06a966 286
8661af79 287 timewarp_run();
4ca828d7
LS
288 pollfds = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *pollfds);
289
290#ifdef _WIN32
291 wevents = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *wevents);
292#endif
293
294 /* Populate with all the fds and events. */
295 i = 0;
296 HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) {
297 pollfds[i] = node->pollfd;
298#ifdef _WIN32
299 wevents[i] = node->wevent;
55489d31
GS
300 if (node->pollfd.fd && node->wevent) {
301 short int wsa_events = 0;
302 if (node->pollfd.events & POLLIN) {
303 wsa_events |= FD_READ | FD_ACCEPT | FD_CLOSE;
304 }
305 if (node->pollfd.events & POLLOUT) {
306 wsa_events |= FD_WRITE | FD_CONNECT | FD_CLOSE;
307 }
308 WSAEventSelect(node->pollfd.fd, node->wevent, wsa_events);
309 }
4ca828d7
LS
310#endif
311 i++;
312 }
313
314 retval = time_poll(pollfds, hmap_count(&loop->poll_nodes), wevents,
2c06a966 315 loop->timeout_when, &elapsed);
064af421
BP
316 if (retval < 0) {
317 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
10a89ef0 318 VLOG_ERR_RL(&rl, "poll: %s", ovs_strerror(-retval));
959ec62e 319 } else if (!retval) {
2c06a966 320 log_wakeup(loop->timeout_where, NULL, elapsed);
8f6c3ad7 321 } else if (get_cpu_usage() > 50 || VLOG_IS_DBG_ENABLED()) {
4ca828d7
LS
322 i = 0;
323 HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) {
324 if (pollfds[i].revents) {
325 log_wakeup(node->where, &pollfds[i], 0);
8f6c3ad7 326 }
4ca828d7 327 i++;
064af421 328 }
064af421
BP
329 }
330
4ca828d7 331 free_poll_nodes(loop);
2c06a966
BP
332 loop->timeout_when = LLONG_MAX;
333 loop->timeout_where = NULL;
4ca828d7
LS
334 free(pollfds);
335 free(wevents);
d8b30702
JG
336
337 /* Handle any pending signals before doing anything else. */
338 fatal_signal_run();
55b40355
BP
339
340 seq_woke();
064af421 341}
064af421 342\f
8f6c3ad7 343static void
2c06a966 344free_poll_loop(void *loop_)
064af421 345{
2c06a966
BP
346 struct poll_loop *loop = loop_;
347
4ca828d7
LS
348 free_poll_nodes(loop);
349 hmap_destroy(&loop->poll_nodes);
2c06a966
BP
350 free(loop);
351}
352
353static struct poll_loop *
354poll_loop(void)
355{
356 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
357 static pthread_key_t key;
358 struct poll_loop *loop;
359
360 if (ovsthread_once_start(&once)) {
361 xpthread_key_create(&key, free_poll_loop);
362 ovsthread_once_done(&once);
8f6c3ad7
BP
363 }
364
2c06a966
BP
365 loop = pthread_getspecific(key);
366 if (!loop) {
367 loop = xzalloc(sizeof *loop);
4ca828d7 368 hmap_init(&loop->poll_nodes);
9c4c45ed 369 xpthread_setspecific(key, loop);
2c06a966
BP
370 }
371 return loop;
064af421 372}
2c06a966 373