Revert "poll-loop: Enable checking whether a FD caused a wakeup."
[sliver-openvswitch.git] / lib / poll-loop.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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 <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <poll.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "coverage.h"
26 #include "dynamic-string.h"
27 #include "fatal-signal.h"
28 #include "list.h"
29 #include "socket-util.h"
30 #include "timeval.h"
31 #include "vlog.h"
32
33 #undef poll_fd_wait
34 #undef poll_timer_wait
35 #undef poll_timer_wait_until
36 #undef poll_immediate_wake
37
38 VLOG_DEFINE_THIS_MODULE(poll_loop);
39
40 COVERAGE_DEFINE(poll_fd_wait);
41 COVERAGE_DEFINE(poll_zero_timeout);
42
43 /* An event that will wake the following call to poll_block(). */
44 struct poll_waiter {
45     /* Set when the waiter is created. */
46     struct list node;           /* Element in global waiters list. */
47     int fd;                     /* File descriptor. */
48     short int events;           /* Events to wait for (POLLIN, POLLOUT). */
49     const char *where;          /* Where the waiter was created. */
50
51     /* Set only when poll_block() is called. */
52     struct pollfd *pollfd;      /* Pointer to element of the pollfds array. */
53 };
54
55 /* All active poll waiters. */
56 static struct list waiters = LIST_INITIALIZER(&waiters);
57
58 /* Max time to wait in next call to poll_block(), in milliseconds, or -1 to
59  * wait forever. */
60 static int timeout = -1;
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 /* The caller must ensure that 'msec' is not negative. */
86 static void
87 poll_timer_wait__(int msec, const char *where)
88 {
89     if (timeout < 0 || msec < timeout) {
90         timeout = msec;
91         timeout_where = where;
92     }
93 }
94
95 /* Causes the following call to poll_block() to block for no more than 'msec'
96  * milliseconds.  If 'msec' is nonpositive, the following call to poll_block()
97  * will not block at all.
98  *
99  * The timer registration is one-shot: only the following call to poll_block()
100  * is affected.  The timer will need to be re-registered after poll_block() is
101  * called if it is to persist.
102  *
103  * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
104  * for more information. */
105 void
106 poll_timer_wait(long long int msec, const char *where)
107 {
108     poll_timer_wait__((msec < 0 ? 0
109                        : msec > INT_MAX ? INT_MAX
110                        : msec),
111                       where);
112 }
113
114 /* Causes the following call to poll_block() to wake up when the current time,
115  * as returned by time_msec(), reaches 'msec' or later.  If 'msec' is earlier
116  * than the current time, the following call to poll_block() will not block at
117  * all.
118  *
119  * The timer registration is one-shot: only the following call to poll_block()
120  * is affected.  The timer will need to be re-registered after poll_block() is
121  * called if it is to persist.
122  *
123  * Ordinarily the 'where' argument is supplied automatically; see poll-loop.h
124  * for more information. */
125 void
126 poll_timer_wait_until(long long int msec, const char *where)
127 {
128     long long int now = time_msec();
129     poll_timer_wait__((msec <= now ? 0
130                        : msec < now + INT_MAX ? msec - now
131                        : INT_MAX),
132                       where);
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 rl = VLOG_RATE_LIMIT_INIT(120, 120);
160     enum vlog_level level;
161     int cpu_usage;
162     struct ds s;
163
164     cpu_usage = get_cpu_usage();
165     if (VLOG_IS_DBG_ENABLED()) {
166         level = VLL_DBG;
167     } else if (cpu_usage > 50 && !VLOG_DROP_WARN(&rl)) {
168         level = VLL_WARN;
169     } else {
170         return;
171     }
172
173     ds_init(&s);
174     ds_put_cstr(&s, "wakeup due to ");
175     if (pollfd) {
176         char *description = describe_fd(pollfd->fd);
177         if (pollfd->revents & POLLIN) {
178             ds_put_cstr(&s, "[POLLIN]");
179         }
180         if (pollfd->revents & POLLOUT) {
181             ds_put_cstr(&s, "[POLLOUT]");
182         }
183         if (pollfd->revents & POLLERR) {
184             ds_put_cstr(&s, "[POLLERR]");
185         }
186         if (pollfd->revents & POLLHUP) {
187             ds_put_cstr(&s, "[POLLHUP]");
188         }
189         if (pollfd->revents & POLLNVAL) {
190             ds_put_cstr(&s, "[POLLNVAL]");
191         }
192         ds_put_format(&s, " on fd %d (%s)", pollfd->fd, description);
193         free(description);
194     } else {
195         ds_put_format(&s, "%d-ms timeout", timeout);
196     }
197     if (where) {
198         ds_put_format(&s, " at %s", where);
199     }
200     if (cpu_usage >= 0) {
201         ds_put_format(&s, " (%d%% CPU usage)", cpu_usage);
202     }
203     VLOG(level, "%s", ds_cstr(&s));
204     ds_destroy(&s);
205 }
206
207 /* Blocks until one or more of the events registered with poll_fd_wait()
208  * occurs, or until the minimum duration registered with poll_timer_wait()
209  * elapses, or not at all if poll_immediate_wake() has been called. */
210 void
211 poll_block(void)
212 {
213     static struct pollfd *pollfds;
214     static size_t max_pollfds;
215
216     struct poll_waiter *pw, *next;
217     int n_waiters, n_pollfds;
218     int retval;
219
220     /* Register fatal signal events before actually doing any real work for
221      * poll_block. */
222     fatal_signal_wait();
223
224     n_waiters = list_size(&waiters);
225     if (max_pollfds < n_waiters) {
226         max_pollfds = n_waiters;
227         pollfds = xrealloc(pollfds, max_pollfds * sizeof *pollfds);
228     }
229
230     n_pollfds = 0;
231     LIST_FOR_EACH (pw, node, &waiters) {
232         pw->pollfd = &pollfds[n_pollfds];
233         pollfds[n_pollfds].fd = pw->fd;
234         pollfds[n_pollfds].events = pw->events;
235         pollfds[n_pollfds].revents = 0;
236         n_pollfds++;
237     }
238
239     if (!timeout) {
240         COVERAGE_INC(poll_zero_timeout);
241     }
242     retval = time_poll(pollfds, n_pollfds, timeout);
243     if (retval < 0) {
244         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
245         VLOG_ERR_RL(&rl, "poll: %s", strerror(-retval));
246     } else if (!retval) {
247         log_wakeup(timeout_where, NULL, timeout);
248     }
249
250     LIST_FOR_EACH_SAFE (pw, next, node, &waiters) {
251         if (pw->pollfd->revents) {
252             log_wakeup(pw->where, pw->pollfd, 0);
253         }
254         poll_cancel(pw);
255     }
256
257     timeout = -1;
258     timeout_where = NULL;
259
260     /* Handle any pending signals before doing anything else. */
261     fatal_signal_run();
262 }
263
264 /* Cancels the file descriptor event registered with poll_fd_wait() using 'pw',
265  * the struct poll_waiter returned by that function.
266  *
267  * An event registered with poll_fd_wait() may be canceled from its time of
268  * registration until the next call to poll_block().  At that point, the event
269  * is automatically canceled by the system and its poll_waiter is freed. */
270 void
271 poll_cancel(struct poll_waiter *pw)
272 {
273     if (pw) {
274         list_remove(&pw->node);
275         free(pw);
276     }
277 }
278 \f
279 /* Creates and returns a new poll_waiter for 'fd' and 'events'. */
280 static struct poll_waiter *
281 new_waiter(int fd, short int events, const char *where)
282 {
283     struct poll_waiter *waiter = xzalloc(sizeof *waiter);
284     assert(fd >= 0);
285     waiter->fd = fd;
286     waiter->events = events;
287     waiter->where = where;
288     list_push_back(&waiters, &waiter->node);
289     return waiter;
290 }