9855306834cf47744d52ec2d8cc87abb59b1d4eb
[sliver-openvswitch.git] / lib / poll-loop.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
3  *
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:
7  *
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.
15  */
16
17 #include <config.h>
18 #include "poll-loop.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <poll.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "coverage.h"
25 #include "dynamic-string.h"
26 #include "fatal-signal.h"
27 #include "list.h"
28 #include "socket-util.h"
29 #include "timeval.h"
30 #include "vlog.h"
31
32 #undef poll_fd_wait
33 #undef poll_timer_wait
34 #undef poll_timer_wait_until
35 #undef poll_immediate_wake
36
37 VLOG_DEFINE_THIS_MODULE(poll_loop);
38
39 COVERAGE_DEFINE(poll_fd_wait);
40 COVERAGE_DEFINE(poll_zero_timeout);
41
42 /* An event that will wake the following call to poll_block(). */
43 struct poll_waiter {
44     /* Set when the waiter is created. */
45     struct list node;           /* Element in global waiters list. */
46     int fd;                     /* File descriptor. */
47     short int events;           /* Events to wait for (POLLIN, POLLOUT). */
48     const char *where;          /* Where the waiter was created. */
49
50     /* Set only when poll_block() is called. */
51     struct pollfd *pollfd;      /* Pointer to element of the pollfds array. */
52 };
53
54 /* All active poll waiters. */
55 static struct list waiters = LIST_INITIALIZER(&waiters);
56
57 /* Time at which to wake up the next call to poll_block(), in milliseconds as
58  * returned by time_msec(), LLONG_MIN to wake up immediately, or LLONG_MAX to
59  * wait forever. */
60 static long long int timeout_when = LLONG_MAX;
61
62 /* Location where waiter created. */
63 static const char *timeout_where;
64
65 static struct poll_waiter *new_waiter(int fd, short int events,
66                                       const char *where);
67
68 /* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
69  * or POLLOUT or POLLIN | POLLOUT).  The following call to poll_block() will
70  * wake up when 'fd' becomes ready for one or more of the requested events.
71  *
72  * The event registration is one-shot: only the following call to poll_block()
73  * is affected.  The event will need to be re-registered after poll_block() is
74  * called if it is to persist.
75  *
76  * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
77  * for more information. */
78 struct poll_waiter *
79 poll_fd_wait(int fd, short int events, const char *where)
80 {
81     COVERAGE_INC(poll_fd_wait);
82     return new_waiter(fd, events, where);
83 }
84
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.
88  *
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.
92  *
93  * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
94  * for more information. */
95 void
96 poll_timer_wait(long long int msec, const char *where)
97 {
98     long long int now = time_msec();
99     long long int when;
100
101     if (msec <= 0) {
102         /* Wake up immediately. */
103         when = LLONG_MIN;
104     } else if ((unsigned long long int) now + msec <= LLONG_MAX) {
105         /* Normal case. */
106         when = now + msec;
107     } else {
108         /* now + msec would overflow. */
109         when = LLONG_MAX;
110     }
111
112     poll_timer_wait_until(when, where);
113 }
114
115 /* Causes the following call to poll_block() to wake up when the current time,
116  * as returned by time_msec(), reaches 'when' or later.  If 'when' is earlier
117  * than the current time, the following call to poll_block() will not block at
118  * all.
119  *
120  * The timer registration is one-shot: only the following call to poll_block()
121  * is affected.  The timer will need to be re-registered after poll_block() is
122  * called if it is to persist.
123  *
124  * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
125  * for more information. */
126 void
127 poll_timer_wait_until(long long int when, const char *where)
128 {
129     if (when < timeout_when) {
130         timeout_when = when;
131         timeout_where = where;
132     }
133 }
134
135 /* Causes the following call to poll_block() to wake up immediately, without
136  * blocking.
137  *
138  * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
139  * for more information. */
140 void
141 poll_immediate_wake(const char *where)
142 {
143     poll_timer_wait(0, where);
144 }
145
146 /* Logs, if appropriate, that the poll loop was awakened by an event
147  * registered at 'where' (typically a source file and line number).  The other
148  * arguments have two possible interpretations:
149  *
150  *   - If 'pollfd' is nonnull then it should be the "struct pollfd" that caused
151  *     the wakeup.  'timeout' is ignored.
152  *
153  *   - If 'pollfd' is NULL then 'timeout' is the number of milliseconds after
154  *     which the poll loop woke up.
155  */
156 static void
157 log_wakeup(const char *where, const struct pollfd *pollfd, int timeout)
158 {
159     static struct vlog_rate_limit trace_rl = VLOG_RATE_LIMIT_INIT(1, 1);
160     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 10);
161     enum vlog_level level;
162     int cpu_usage;
163     struct ds s;
164
165     cpu_usage = get_cpu_usage();
166     if (VLOG_IS_DBG_ENABLED()) {
167         level = VLL_DBG;
168     } else if (cpu_usage > 50 && !VLOG_DROP_INFO(&rl)) {
169         level = VLL_INFO;
170     } else {
171         return;
172     }
173
174     ds_init(&s);
175     ds_put_cstr(&s, "wakeup due to ");
176     if (pollfd) {
177         char *description = describe_fd(pollfd->fd);
178         if (pollfd->revents & POLLIN) {
179             ds_put_cstr(&s, "[POLLIN]");
180         }
181         if (pollfd->revents & POLLOUT) {
182             ds_put_cstr(&s, "[POLLOUT]");
183         }
184         if (pollfd->revents & POLLERR) {
185             ds_put_cstr(&s, "[POLLERR]");
186         }
187         if (pollfd->revents & POLLHUP) {
188             ds_put_cstr(&s, "[POLLHUP]");
189         }
190         if (pollfd->revents & POLLNVAL) {
191             ds_put_cstr(&s, "[POLLNVAL]");
192         }
193         ds_put_format(&s, " on fd %d (%s)", pollfd->fd, description);
194         free(description);
195     } else {
196         ds_put_format(&s, "%d-ms timeout", timeout);
197     }
198     if (where) {
199         ds_put_format(&s, " at %s", where);
200     }
201     if (cpu_usage >= 0) {
202         ds_put_format(&s, " (%d%% CPU usage)", cpu_usage);
203
204         if (!vlog_should_drop(THIS_MODULE, level, &trace_rl)) {
205             ds_put_char(&s, '\n');
206             format_backtraces(&s, 2);
207         }
208     }
209     VLOG(level, "%s", ds_cstr(&s));
210     ds_destroy(&s);
211 }
212
213 /* Blocks until one or more of the events registered with poll_fd_wait()
214  * occurs, or until the minimum duration registered with poll_timer_wait()
215  * elapses, or not at all if poll_immediate_wake() has been called. */
216 void
217 poll_block(void)
218 {
219     static struct pollfd *pollfds;
220     static size_t max_pollfds;
221
222     struct poll_waiter *pw, *next;
223     int n_waiters, n_pollfds;
224     int elapsed;
225     int retval;
226
227     /* Register fatal signal events before actually doing any real work for
228      * poll_block. */
229     fatal_signal_wait();
230
231     n_waiters = list_size(&waiters);
232     if (max_pollfds < n_waiters) {
233         max_pollfds = n_waiters;
234         pollfds = xrealloc(pollfds, max_pollfds * sizeof *pollfds);
235     }
236
237     n_pollfds = 0;
238     LIST_FOR_EACH (pw, node, &waiters) {
239         pw->pollfd = &pollfds[n_pollfds];
240         pollfds[n_pollfds].fd = pw->fd;
241         pollfds[n_pollfds].events = pw->events;
242         pollfds[n_pollfds].revents = 0;
243         n_pollfds++;
244     }
245
246     if (timeout_when == LLONG_MIN) {
247         COVERAGE_INC(poll_zero_timeout);
248     }
249     retval = time_poll(pollfds, n_pollfds, timeout_when, &elapsed);
250     if (retval < 0) {
251         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
252         VLOG_ERR_RL(&rl, "poll: %s", strerror(-retval));
253     } else if (!retval) {
254         log_wakeup(timeout_where, NULL, elapsed);
255     }
256
257     LIST_FOR_EACH_SAFE (pw, next, node, &waiters) {
258         if (pw->pollfd->revents) {
259             log_wakeup(pw->where, pw->pollfd, 0);
260         }
261         poll_cancel(pw);
262     }
263
264     timeout_when = LLONG_MAX;
265     timeout_where = NULL;
266
267     /* Handle any pending signals before doing anything else. */
268     fatal_signal_run();
269 }
270
271 /* Cancels the file descriptor event registered with poll_fd_wait() using 'pw',
272  * the struct poll_waiter returned by that function.
273  *
274  * An event registered with poll_fd_wait() may be canceled from its time of
275  * registration until the next call to poll_block().  At that point, the event
276  * is automatically canceled by the system and its poll_waiter is freed. */
277 void
278 poll_cancel(struct poll_waiter *pw)
279 {
280     if (pw) {
281         list_remove(&pw->node);
282         free(pw);
283     }
284 }
285 \f
286 /* Creates and returns a new poll_waiter for 'fd' and 'events'. */
287 static struct poll_waiter *
288 new_waiter(int fd, short int events, const char *where)
289 {
290     struct poll_waiter *waiter = xzalloc(sizeof *waiter);
291     ovs_assert(fd >= 0);
292     waiter->fd = fd;
293     waiter->events = events;
294     waiter->where = where;
295     list_push_back(&waiters, &waiter->node);
296     return waiter;
297 }