ovs-rcu: Log a helpful warning when ovsrcu_synchronize() stalls.
[sliver-openvswitch.git] / lib / ovs-rcu.c
1 /*
2  * Copyright (c) 2014 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 "ovs-rcu.h"
19 #include "guarded-list.h"
20 #include "list.h"
21 #include "ovs-thread.h"
22 #include "poll-loop.h"
23 #include "seq.h"
24 #include "timeval.h"
25 #include "vlog.h"
26
27 VLOG_DEFINE_THIS_MODULE(ovs_rcu);
28
29 struct ovsrcu_cb {
30     void (*function)(void *aux);
31     void *aux;
32 };
33
34 struct ovsrcu_cbset {
35     struct list list_node;
36     struct ovsrcu_cb cbs[16];
37     int n_cbs;
38 };
39
40 struct ovsrcu_perthread {
41     struct list list_node;      /* In global list. */
42
43     struct ovs_mutex mutex;
44     uint64_t seqno;
45     struct ovsrcu_cbset *cbset;
46     char name[16];              /* This thread's name. */
47 };
48
49 static struct seq *global_seqno;
50
51 static pthread_key_t perthread_key;
52 static struct list ovsrcu_threads;
53 static struct ovs_mutex ovsrcu_threads_mutex;
54
55 static struct guarded_list flushed_cbsets;
56 static struct seq *flushed_cbsets_seq;
57
58 static void ovsrcu_init(void);
59 static void ovsrcu_flush_cbset(struct ovsrcu_perthread *);
60 static void ovsrcu_unregister__(struct ovsrcu_perthread *);
61 static bool ovsrcu_call_postponed(void);
62 static void *ovsrcu_postpone_thread(void *arg OVS_UNUSED);
63 static void ovsrcu_synchronize(void);
64
65 static struct ovsrcu_perthread *
66 ovsrcu_perthread_get(void)
67 {
68     struct ovsrcu_perthread *perthread;
69
70     ovsrcu_init();
71
72     perthread = pthread_getspecific(perthread_key);
73     if (!perthread) {
74         perthread = xmalloc(sizeof *perthread);
75         ovs_mutex_init(&perthread->mutex);
76         perthread->seqno = seq_read(global_seqno);
77         perthread->cbset = NULL;
78         ovs_strlcpy(perthread->name, get_subprogram_name(),
79                     sizeof perthread->name);
80
81         ovs_mutex_lock(&ovsrcu_threads_mutex);
82         list_push_back(&ovsrcu_threads, &perthread->list_node);
83         ovs_mutex_unlock(&ovsrcu_threads_mutex);
84
85         pthread_setspecific(perthread_key, perthread);
86     }
87     return perthread;
88 }
89
90 /* Indicates the end of a quiescent state.  See "Details" near the top of
91  * ovs-rcu.h.
92  *
93  * Quiescent states don't stack or nest, so this always ends a quiescent state
94  * even if ovsrcu_quiesce_start() was called multiple times in a row. */
95 void
96 ovsrcu_quiesce_end(void)
97 {
98     ovsrcu_perthread_get();
99 }
100
101 static void
102 ovsrcu_quiesced(void)
103 {
104     if (single_threaded()) {
105         ovsrcu_call_postponed();
106     } else {
107         static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
108         if (ovsthread_once_start(&once)) {
109             ovs_thread_create("urcu", ovsrcu_postpone_thread, NULL);
110             ovsthread_once_done(&once);
111         }
112     }
113 }
114
115 /* Indicates the beginning of a quiescent state.  See "Details" near the top of
116  * ovs-rcu.h. */
117 void
118 ovsrcu_quiesce_start(void)
119 {
120     struct ovsrcu_perthread *perthread;
121
122     ovsrcu_init();
123     perthread = pthread_getspecific(perthread_key);
124     if (perthread) {
125         pthread_setspecific(perthread_key, NULL);
126         ovsrcu_unregister__(perthread);
127     }
128
129     ovsrcu_quiesced();
130 }
131
132 /* Indicates a momentary quiescent state.  See "Details" near the top of
133  * ovs-rcu.h. */
134 void
135 ovsrcu_quiesce(void)
136 {
137     ovsrcu_init();
138     ovsrcu_perthread_get()->seqno = seq_read(global_seqno);
139     seq_change(global_seqno);
140
141     ovsrcu_quiesced();
142 }
143
144 bool
145 ovsrcu_is_quiescent(void)
146 {
147     ovsrcu_init();
148     return pthread_getspecific(perthread_key) == NULL;
149 }
150
151 static void
152 ovsrcu_synchronize(void)
153 {
154     unsigned int warning_threshold = 1000;
155     uint64_t target_seqno;
156     long long int start;
157
158     if (single_threaded()) {
159         return;
160     }
161
162     target_seqno = seq_read(global_seqno);
163     ovsrcu_quiesce_start();
164     start = time_msec();
165
166     for (;;) {
167         uint64_t cur_seqno = seq_read(global_seqno);
168         struct ovsrcu_perthread *perthread;
169         char stalled_thread[16];
170         unsigned int elapsed;
171         bool done = true;
172
173         ovs_mutex_lock(&ovsrcu_threads_mutex);
174         LIST_FOR_EACH (perthread, list_node, &ovsrcu_threads) {
175             if (perthread->seqno <= target_seqno) {
176                 ovs_strlcpy(stalled_thread, perthread->name,
177                             sizeof stalled_thread);
178                 done = false;
179                 break;
180             }
181         }
182         ovs_mutex_unlock(&ovsrcu_threads_mutex);
183
184         if (done) {
185             break;
186         }
187
188         elapsed = time_msec() - start;
189         if (elapsed >= warning_threshold) {
190             VLOG_WARN("blocked %u ms waiting for %s to quiesce",
191                       elapsed, stalled_thread);
192             warning_threshold *= 2;
193         }
194         poll_timer_wait_until(start + warning_threshold);
195
196         seq_wait(global_seqno, cur_seqno);
197         poll_block();
198     }
199     ovsrcu_quiesce_end();
200 }
201
202 /* Registers 'function' to be called, passing 'aux' as argument, after the
203  * next grace period.
204  *
205  * This function is more conveniently called through the ovsrcu_postpone()
206  * macro, which provides a type-safe way to allow 'function''s parameter to be
207  * any pointer type. */
208 void
209 ovsrcu_postpone__(void (*function)(void *aux), void *aux)
210 {
211     struct ovsrcu_perthread *perthread = ovsrcu_perthread_get();
212     struct ovsrcu_cbset *cbset;
213     struct ovsrcu_cb *cb;
214
215     cbset = perthread->cbset;
216     if (!cbset) {
217         cbset = perthread->cbset = xmalloc(sizeof *perthread->cbset);
218         cbset->n_cbs = 0;
219     }
220
221     cb = &cbset->cbs[cbset->n_cbs++];
222     cb->function = function;
223     cb->aux = aux;
224
225     if (cbset->n_cbs >= ARRAY_SIZE(cbset->cbs)) {
226         ovsrcu_flush_cbset(perthread);
227     }
228 }
229
230 static bool
231 ovsrcu_call_postponed(void)
232 {
233     struct ovsrcu_cbset *cbset, *next_cbset;
234     struct list cbsets;
235
236     guarded_list_pop_all(&flushed_cbsets, &cbsets);
237     if (list_is_empty(&cbsets)) {
238         return false;
239     }
240
241     ovsrcu_synchronize();
242
243     LIST_FOR_EACH_SAFE (cbset, next_cbset, list_node, &cbsets) {
244         struct ovsrcu_cb *cb;
245
246         for (cb = cbset->cbs; cb < &cbset->cbs[cbset->n_cbs]; cb++) {
247             cb->function(cb->aux);
248         }
249         list_remove(&cbset->list_node);
250         free(cbset);
251     }
252
253     return true;
254 }
255
256 static void *
257 ovsrcu_postpone_thread(void *arg OVS_UNUSED)
258 {
259     pthread_detach(pthread_self());
260
261     for (;;) {
262         uint64_t seqno = seq_read(flushed_cbsets_seq);
263         if (!ovsrcu_call_postponed()) {
264             seq_wait(flushed_cbsets_seq, seqno);
265             poll_block();
266         }
267     }
268
269     OVS_NOT_REACHED();
270 }
271
272 static void
273 ovsrcu_flush_cbset(struct ovsrcu_perthread *perthread)
274 {
275     struct ovsrcu_cbset *cbset = perthread->cbset;
276
277     if (cbset) {
278         guarded_list_push_back(&flushed_cbsets, &cbset->list_node, SIZE_MAX);
279         perthread->cbset = NULL;
280
281         seq_change(flushed_cbsets_seq);
282     }
283 }
284
285 static void
286 ovsrcu_unregister__(struct ovsrcu_perthread *perthread)
287 {
288     if (perthread->cbset) {
289         ovsrcu_flush_cbset(perthread);
290     }
291
292     ovs_mutex_lock(&ovsrcu_threads_mutex);
293     list_remove(&perthread->list_node);
294     ovs_mutex_unlock(&ovsrcu_threads_mutex);
295
296     ovs_mutex_destroy(&perthread->mutex);
297     free(perthread);
298
299     seq_change(global_seqno);
300 }
301
302 static void
303 ovsrcu_thread_exit_cb(void *perthread)
304 {
305     ovsrcu_unregister__(perthread);
306 }
307
308 static void
309 ovsrcu_init(void)
310 {
311     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
312     if (ovsthread_once_start(&once)) {
313         global_seqno = seq_create();
314         xpthread_key_create(&perthread_key, ovsrcu_thread_exit_cb);
315         list_init(&ovsrcu_threads);
316         ovs_mutex_init(&ovsrcu_threads_mutex);
317
318         guarded_list_init(&flushed_cbsets);
319         flushed_cbsets_seq = seq_create();
320
321         ovsthread_once_done(&once);
322     }
323 }