Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / lib / poll-loop.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 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 "backtrace.h"
26 #include "coverage.h"
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
29 #include "list.h"
30 #include "timeval.h"
31 #include "vlog.h"
32
33 VLOG_DEFINE_THIS_MODULE(poll_loop);
34
35 COVERAGE_DEFINE(poll_fd_wait);
36 COVERAGE_DEFINE(poll_zero_timeout);
37
38 /* An event that will wake the following call to poll_block(). */
39 struct poll_waiter {
40     /* Set when the waiter is created. */
41     struct list node;           /* Element in global waiters list. */
42     int fd;                     /* File descriptor. */
43     short int events;           /* Events to wait for (POLLIN, POLLOUT). */
44     struct backtrace *backtrace; /* Optionally, event that created waiter. */
45
46     /* Set only when poll_block() is called. */
47     struct pollfd *pollfd;      /* Pointer to element of the pollfds array. */
48 };
49
50 /* All active poll waiters. */
51 static struct list waiters = LIST_INITIALIZER(&waiters);
52
53 /* Number of elements in the waiters list. */
54 static size_t n_waiters;
55
56 /* Max time to wait in next call to poll_block(), in milliseconds, or -1 to
57  * wait forever. */
58 static int timeout = -1;
59
60 /* Backtrace of 'timeout''s registration, if debugging is enabled. */
61 static struct backtrace timeout_backtrace;
62
63 static struct poll_waiter *new_waiter(int fd, short int events);
64
65 /* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
66  * or POLLOUT or POLLIN | POLLOUT).  The following call to poll_block() will
67  * wake up when 'fd' becomes ready for one or more of the requested events.
68  *
69  * The event registration is one-shot: only the following call to poll_block()
70  * is affected.  The event will need to be re-registered after poll_block() is
71  * called if it is to persist. */
72 struct poll_waiter *
73 poll_fd_wait(int fd, short int events)
74 {
75     COVERAGE_INC(poll_fd_wait);
76     return new_waiter(fd, events);
77 }
78
79 /* The caller must ensure that 'msec' is not negative. */
80 static void
81 poll_timer_wait__(int msec)
82 {
83     if (timeout < 0 || msec < timeout) {
84         timeout = msec;
85         if (VLOG_IS_DBG_ENABLED()) {
86             backtrace_capture(&timeout_backtrace);
87         }
88     }
89 }
90
91 /* Causes the following call to poll_block() to block for no more than 'msec'
92  * milliseconds.  If 'msec' is nonpositive, the following call to poll_block()
93  * will not block at all.
94  *
95  * The timer registration is one-shot: only the following call to poll_block()
96  * is affected.  The timer will need to be re-registered after poll_block() is
97  * called if it is to persist. */
98 void
99 poll_timer_wait(long long int msec)
100 {
101     poll_timer_wait__(msec < 0 ? 0
102                       : msec > INT_MAX ? INT_MAX
103                       : msec);
104 }
105
106 /* Causes the following call to poll_block() to wake up when the current time,
107  * as returned by time_msec(), reaches 'msec' or later.  If 'msec' is earlier
108  * than the current time, the following call to poll_block() will not block at
109  * all.
110  *
111  * The timer registration is one-shot: only the following call to poll_block()
112  * is affected.  The timer will need to be re-registered after poll_block() is
113  * called if it is to persist. */
114 void
115 poll_timer_wait_until(long long int msec)
116 {
117     long long int now = time_msec();
118     poll_timer_wait__(msec <= now ? 0
119                       : msec < now + INT_MAX ? msec - now
120                       : INT_MAX);
121 }
122
123 /* Causes the following call to poll_block() to wake up immediately, without
124  * blocking. */
125 void
126 poll_immediate_wake(void)
127 {
128     poll_timer_wait(0);
129 }
130
131 static void PRINTF_FORMAT(2, 3)
132 log_wakeup(const struct backtrace *backtrace, const char *format, ...)
133 {
134     struct ds ds;
135     va_list args;
136
137     ds_init(&ds);
138     va_start(args, format);
139     ds_put_format_valist(&ds, format, args);
140     va_end(args);
141
142     if (backtrace) {
143         int i;
144
145         ds_put_char(&ds, ':');
146         for (i = 0; i < backtrace->n_frames; i++) {
147             ds_put_format(&ds, " 0x%"PRIxPTR, backtrace->frames[i]);
148         }
149     }
150     VLOG_DBG("%s", ds_cstr(&ds));
151     ds_destroy(&ds);
152 }
153
154 /* Blocks until one or more of the events registered with poll_fd_wait()
155  * occurs, or until the minimum duration registered with poll_timer_wait()
156  * elapses, or not at all if poll_immediate_wake() has been called. */
157 void
158 poll_block(void)
159 {
160     static struct pollfd *pollfds;
161     static size_t max_pollfds;
162
163     struct poll_waiter *pw, *next;
164     int n_pollfds;
165     int retval;
166
167     /* Register fatal signal events before actually doing any real work for
168      * poll_block. */
169     fatal_signal_wait();
170
171     if (max_pollfds < n_waiters) {
172         max_pollfds = n_waiters;
173         pollfds = xrealloc(pollfds, max_pollfds * sizeof *pollfds);
174     }
175
176     n_pollfds = 0;
177     LIST_FOR_EACH (pw, node, &waiters) {
178         pw->pollfd = &pollfds[n_pollfds];
179         pollfds[n_pollfds].fd = pw->fd;
180         pollfds[n_pollfds].events = pw->events;
181         pollfds[n_pollfds].revents = 0;
182         n_pollfds++;
183     }
184
185     if (!timeout) {
186         COVERAGE_INC(poll_zero_timeout);
187     }
188     retval = time_poll(pollfds, n_pollfds, timeout);
189     if (retval < 0) {
190         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
191         VLOG_ERR_RL(&rl, "poll: %s", strerror(-retval));
192     } else if (!retval && VLOG_IS_DBG_ENABLED()) {
193         log_wakeup(&timeout_backtrace, "%d-ms timeout", timeout);
194     }
195
196     LIST_FOR_EACH_SAFE (pw, next, node, &waiters) {
197         if (pw->pollfd->revents && VLOG_IS_DBG_ENABLED()) {
198             log_wakeup(pw->backtrace, "%s%s%s%s%s on fd %d",
199                        pw->pollfd->revents & POLLIN ? "[POLLIN]" : "",
200                        pw->pollfd->revents & POLLOUT ? "[POLLOUT]" : "",
201                        pw->pollfd->revents & POLLERR ? "[POLLERR]" : "",
202                        pw->pollfd->revents & POLLHUP ? "[POLLHUP]" : "",
203                        pw->pollfd->revents & POLLNVAL ? "[POLLNVAL]" : "",
204                        pw->fd);
205         }
206         poll_cancel(pw);
207     }
208
209     timeout = -1;
210     timeout_backtrace.n_frames = 0;
211
212     /* Handle any pending signals before doing anything else. */
213     fatal_signal_run();
214 }
215
216 /* Cancels the file descriptor event registered with poll_fd_wait() using 'pw',
217  * the struct poll_waiter returned by that function.
218  *
219  * An event registered with poll_fd_wait() may be canceled from its time of
220  * registration until the next call to poll_block().  At that point, the event
221  * is automatically canceled by the system and its poll_waiter is freed. */
222 void
223 poll_cancel(struct poll_waiter *pw)
224 {
225     if (pw) {
226         list_remove(&pw->node);
227         free(pw->backtrace);
228         free(pw);
229         n_waiters--;
230     }
231 }
232 \f
233 /* Creates and returns a new poll_waiter for 'fd' and 'events'. */
234 static struct poll_waiter *
235 new_waiter(int fd, short int events)
236 {
237     struct poll_waiter *waiter = xzalloc(sizeof *waiter);
238     assert(fd >= 0);
239     waiter->fd = fd;
240     waiter->events = events;
241     if (VLOG_IS_DBG_ENABLED()) {
242         waiter->backtrace = xmalloc(sizeof *waiter->backtrace);
243         backtrace_capture(waiter->backtrace);
244     }
245     list_push_back(&waiters, &waiter->node);
246     n_waiters++;
247     return waiter;
248 }