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