2 * Copyright (c) 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ovs-thread.h"
25 #include "poll-loop.h"
26 #include "socket-util.h"
30 /* Omit the definitions in this file because they are somewhat difficult to
31 * write without prompting "sparse" complaints, without ugliness or
32 * cut-and-paste. Since "sparse" is just a checker, not a compiler, it
33 * doesn't matter that we don't define them. */
37 VLOG_DEFINE_THIS_MODULE(ovs_thread);
39 /* If there is a reason that we cannot fork anymore (unless the fork will be
40 * immediately followed by an exec), then this points to a string that
42 static const char *must_not_fork;
44 /* True if we created any threads beyond the main initial thread. */
45 static bool multithreaded;
47 #define LOCK_FUNCTION(TYPE, FUN) \
49 ovs_##TYPE##_##FUN##_at(const struct ovs_##TYPE *l_, \
51 OVS_NO_THREAD_SAFETY_ANALYSIS \
53 struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
54 int error = pthread_##TYPE##_##FUN(&l->lock); \
55 if (OVS_UNLIKELY(error)) { \
56 ovs_abort(error, "pthread_%s_%s failed", #TYPE, #FUN); \
60 LOCK_FUNCTION(mutex, lock);
61 LOCK_FUNCTION(rwlock, rdlock);
62 LOCK_FUNCTION(rwlock, wrlock);
64 #define TRY_LOCK_FUNCTION(TYPE, FUN) \
66 ovs_##TYPE##_##FUN##_at(const struct ovs_##TYPE *l_, \
68 OVS_NO_THREAD_SAFETY_ANALYSIS \
70 struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
71 int error = pthread_##TYPE##_##FUN(&l->lock); \
72 if (OVS_UNLIKELY(error) && error != EBUSY) { \
73 ovs_abort(error, "pthread_%s_%s failed", #TYPE, #FUN); \
80 TRY_LOCK_FUNCTION(mutex, trylock);
81 TRY_LOCK_FUNCTION(rwlock, tryrdlock);
82 TRY_LOCK_FUNCTION(rwlock, trywrlock);
84 #define UNLOCK_FUNCTION(TYPE, FUN) \
86 ovs_##TYPE##_##FUN(const struct ovs_##TYPE *l_) \
87 OVS_NO_THREAD_SAFETY_ANALYSIS \
89 struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
92 error = pthread_##TYPE##_##FUN(&l->lock); \
93 if (OVS_UNLIKELY(error)) { \
94 ovs_abort(error, "pthread_%s_%sfailed", #TYPE, #FUN); \
97 UNLOCK_FUNCTION(mutex, unlock);
98 UNLOCK_FUNCTION(mutex, destroy);
99 UNLOCK_FUNCTION(rwlock, unlock);
100 UNLOCK_FUNCTION(rwlock, destroy);
102 #define XPTHREAD_FUNC1(FUNCTION, PARAM1) \
104 x##FUNCTION(PARAM1 arg1) \
106 int error = FUNCTION(arg1); \
107 if (OVS_UNLIKELY(error)) { \
108 ovs_abort(error, "%s failed", #FUNCTION); \
111 #define XPTHREAD_FUNC2(FUNCTION, PARAM1, PARAM2) \
113 x##FUNCTION(PARAM1 arg1, PARAM2 arg2) \
115 int error = FUNCTION(arg1, arg2); \
116 if (OVS_UNLIKELY(error)) { \
117 ovs_abort(error, "%s failed", #FUNCTION); \
121 XPTHREAD_FUNC1(pthread_mutex_lock, pthread_mutex_t *);
122 XPTHREAD_FUNC1(pthread_mutex_unlock, pthread_mutex_t *);
123 XPTHREAD_FUNC1(pthread_mutexattr_init, pthread_mutexattr_t *);
124 XPTHREAD_FUNC1(pthread_mutexattr_destroy, pthread_mutexattr_t *);
125 XPTHREAD_FUNC2(pthread_mutexattr_settype, pthread_mutexattr_t *, int);
126 XPTHREAD_FUNC2(pthread_mutexattr_gettype, pthread_mutexattr_t *, int *);
128 XPTHREAD_FUNC2(pthread_cond_init, pthread_cond_t *, pthread_condattr_t *);
129 XPTHREAD_FUNC1(pthread_cond_destroy, pthread_cond_t *);
130 XPTHREAD_FUNC1(pthread_cond_signal, pthread_cond_t *);
131 XPTHREAD_FUNC1(pthread_cond_broadcast, pthread_cond_t *);
133 XPTHREAD_FUNC2(pthread_join, pthread_t, void **);
135 typedef void destructor_func(void *);
136 XPTHREAD_FUNC2(pthread_key_create, pthread_key_t *, destructor_func *);
137 XPTHREAD_FUNC1(pthread_key_delete, pthread_key_t);
138 XPTHREAD_FUNC2(pthread_setspecific, pthread_key_t, const void *);
141 ovs_mutex_init__(const struct ovs_mutex *l_, int type)
143 struct ovs_mutex *l = CONST_CAST(struct ovs_mutex *, l_);
144 pthread_mutexattr_t attr;
148 xpthread_mutexattr_init(&attr);
149 xpthread_mutexattr_settype(&attr, type);
150 error = pthread_mutex_init(&l->lock, &attr);
151 if (OVS_UNLIKELY(error)) {
152 ovs_abort(error, "pthread_mutex_init failed");
154 xpthread_mutexattr_destroy(&attr);
157 /* Initializes 'mutex' as a normal (non-recursive) mutex. */
159 ovs_mutex_init(const struct ovs_mutex *mutex)
161 ovs_mutex_init__(mutex, PTHREAD_MUTEX_ERRORCHECK);
164 /* Initializes 'mutex' as a recursive mutex. */
166 ovs_mutex_init_recursive(const struct ovs_mutex *mutex)
168 ovs_mutex_init__(mutex, PTHREAD_MUTEX_RECURSIVE);
171 /* Initializes 'mutex' as a recursive mutex. */
173 ovs_mutex_init_adaptive(const struct ovs_mutex *mutex)
175 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
176 ovs_mutex_init__(mutex, PTHREAD_MUTEX_ADAPTIVE_NP);
178 ovs_mutex_init(mutex);
183 ovs_rwlock_init(const struct ovs_rwlock *l_)
185 struct ovs_rwlock *l = CONST_CAST(struct ovs_rwlock *, l_);
189 error = pthread_rwlock_init(&l->lock, NULL);
190 if (OVS_UNLIKELY(error)) {
191 ovs_abort(error, "pthread_rwlock_init failed");
196 ovs_mutex_cond_wait(pthread_cond_t *cond, const struct ovs_mutex *mutex_)
198 struct ovs_mutex *mutex = CONST_CAST(struct ovs_mutex *, mutex_);
199 int error = pthread_cond_wait(cond, &mutex->lock);
200 if (OVS_UNLIKELY(error)) {
201 ovs_abort(error, "pthread_cond_wait failed");
205 DEFINE_EXTERN_PER_THREAD_DATA(ovsthread_id, 0);
207 struct ovsthread_aux {
208 void *(*start)(void *);
213 ovsthread_wrapper(void *aux_)
215 static atomic_uint next_id = ATOMIC_VAR_INIT(1);
217 struct ovsthread_aux *auxp = aux_;
218 struct ovsthread_aux aux;
221 atomic_add(&next_id, 1, &id);
222 *ovsthread_id_get() = id;
227 return aux.start(aux.arg);
231 xpthread_create(pthread_t *threadp, pthread_attr_t *attr,
232 void *(*start)(void *), void *arg)
234 struct ovsthread_aux *aux;
238 forbid_forking("multiple threads exist");
239 multithreaded = true;
241 aux = xmalloc(sizeof *aux);
245 error = pthread_create(threadp ? threadp : &thread, attr,
246 ovsthread_wrapper, aux);
248 ovs_abort(error, "pthread_create failed");
253 ovsthread_once_start__(struct ovsthread_once *once)
255 ovs_mutex_lock(&once->mutex);
256 if (!ovsthread_once_is_done__(once)) {
259 ovs_mutex_unlock(&once->mutex);
264 ovsthread_once_done(struct ovsthread_once *once)
266 atomic_store(&once->done, true);
267 ovs_mutex_unlock(&once->mutex);
270 /* Asserts that the process has not yet created any threads (beyond the initial
273 * ('where' is used in logging. Commonly one would use
274 * assert_single_threaded() to automatically provide the caller's source file
275 * and line number for 'where'.) */
277 assert_single_threaded_at(const char *where)
280 VLOG_FATAL("%s: attempted operation not allowed when multithreaded",
285 /* Forks the current process (checking that this is allowed). Aborts with
286 * VLOG_FATAL if fork() returns an error, and otherwise returns the value
287 * returned by fork().
289 * ('where' is used in logging. Commonly one would use xfork() to
290 * automatically provide the caller's source file and line number for
293 xfork_at(const char *where)
298 VLOG_FATAL("%s: attempted to fork but forking not allowed (%s)",
299 where, must_not_fork);
304 VLOG_FATAL("%s: fork failed (%s)", where, ovs_strerror(errno));
309 /* Notes that the process must not call fork() from now on, for the specified
310 * 'reason'. (The process may still fork() if it execs itself immediately
313 forbid_forking(const char *reason)
315 ovs_assert(reason != NULL);
316 must_not_fork = reason;
319 /* Returns true if the process is allowed to fork, false otherwise. */
323 return !must_not_fork;
326 /* ovsthread_counter.
328 * We implement the counter as an array of N_COUNTERS individual counters, each
329 * with its own lock. Each thread uses one of the counters chosen based on a
330 * hash of the thread's ID, the idea being that, statistically, different
331 * threads will tend to use different counters and therefore avoid
332 * interfering with each other.
334 * Undoubtedly, better implementations are possible. */
336 /* Basic counter structure. */
337 struct ovsthread_counter__ {
338 struct ovs_mutex mutex;
339 unsigned long long int value;
342 /* Pad the basic counter structure to 64 bytes to avoid cache line
344 struct ovsthread_counter {
345 struct ovsthread_counter__ c;
346 char pad[ROUND_UP(sizeof(struct ovsthread_counter__), 64)
347 - sizeof(struct ovsthread_counter__)];
350 #define N_COUNTERS 16
352 struct ovsthread_counter *
353 ovsthread_counter_create(void)
355 struct ovsthread_counter *c;
358 c = xmalloc(N_COUNTERS * sizeof *c);
359 for (i = 0; i < N_COUNTERS; i++) {
360 ovs_mutex_init(&c[i].c.mutex);
367 ovsthread_counter_destroy(struct ovsthread_counter *c)
372 for (i = 0; i < N_COUNTERS; i++) {
373 ovs_mutex_destroy(&c[i].c.mutex);
380 ovsthread_counter_inc(struct ovsthread_counter *c, unsigned long long int n)
382 c = &c[hash_int(ovsthread_id_self(), 0) % N_COUNTERS];
384 ovs_mutex_lock(&c->c.mutex);
386 ovs_mutex_unlock(&c->c.mutex);
389 unsigned long long int
390 ovsthread_counter_read(const struct ovsthread_counter *c)
392 unsigned long long int sum;
396 for (i = 0; i < N_COUNTERS; i++) {
397 ovs_mutex_lock(&c[i].c.mutex);
399 ovs_mutex_unlock(&c[i].c.mutex);
404 /* Parses /proc/cpuinfo for the total number of physical cores on this system
405 * across all CPU packages, not counting hyper-threads.
407 * Sets *n_cores to the total number of cores on this system, or 0 if the
408 * number cannot be determined. */
410 parse_cpuinfo(long int *n_cores)
412 static const char file_name[] = "/proc/cpuinfo";
414 uint64_t cpu = 0; /* Support up to 64 CPU packages on a single system. */
418 stream = fopen(file_name, "r");
420 VLOG_DBG("%s: open failed (%s)", file_name, ovs_strerror(errno));
424 while (fgets(line, sizeof line, stream)) {
427 /* Find the next CPU package. */
428 if (ovs_scan(line, "physical id%*[^:]: %u", &id)) {
430 VLOG_WARN("Counted over 64 CPU packages on this system. "
431 "Parsing %s for core count may be inaccurate.",
437 if (cpu & (1 << id)) {
438 /* We've already counted this package's cores. */
443 /* Find the number of cores for this package. */
444 while (fgets(line, sizeof line, stream)) {
447 if (ovs_scan(line, "cpu cores%*[^:]: %u", &count)) {
459 /* Returns the total number of cores on this system, or 0 if the number cannot
462 * Tries not to count hyper-threads, but may be inaccurate - particularly on
463 * platforms that do not provide /proc/cpuinfo, but also if /proc/cpuinfo is
464 * formatted different to the layout that parse_cpuinfo() expects. */
466 count_cpu_cores(void)
468 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
469 static long int n_cores;
471 if (ovsthread_once_start(&once)) {
472 parse_cpuinfo(&n_cores);
474 n_cores = sysconf(_SC_NPROCESSORS_ONLN);
476 ovsthread_once_done(&once);
479 return n_cores > 0 ? n_cores : 0;
486 #define MAX_KEYS (L1_SIZE * L2_SIZE)
488 /* A piece of thread-specific data. */
489 struct ovsthread_key {
490 struct list list_node; /* In 'inuse_keys' or 'free_keys'. */
491 void (*destructor)(void *); /* Called at thread exit. */
493 /* Indexes into the per-thread array in struct ovsthread_key_slots.
494 * This key's data is stored in p1[index / L2_SIZE][index % L2_SIZE]. */
498 /* Per-thread data structure. */
499 struct ovsthread_key_slots {
500 struct list list_node; /* In 'slots_list'. */
504 /* Contains "struct ovsthread_key_slots *". */
505 static pthread_key_t tsd_key;
507 /* Guards data structures below. */
508 static struct ovs_mutex key_mutex = OVS_MUTEX_INITIALIZER;
510 /* 'inuse_keys' holds "struct ovsthread_key"s that have been created and not
513 * 'free_keys' holds "struct ovsthread_key"s that have been deleted and are
514 * ready for reuse. (We keep them around only to be able to easily locate
517 * Together, 'inuse_keys' and 'free_keys' hold an ovsthread_key for every index
518 * from 0 to n_keys - 1, inclusive. */
519 static struct list inuse_keys OVS_GUARDED_BY(key_mutex)
520 = LIST_INITIALIZER(&inuse_keys);
521 static struct list free_keys OVS_GUARDED_BY(key_mutex)
522 = LIST_INITIALIZER(&free_keys);
523 static unsigned int n_keys OVS_GUARDED_BY(key_mutex);
525 /* All existing struct ovsthread_key_slots. */
526 static struct list slots_list OVS_GUARDED_BY(key_mutex)
527 = LIST_INITIALIZER(&slots_list);
530 clear_slot(struct ovsthread_key_slots *slots, unsigned int index)
532 void **p2 = slots->p1[index / L2_SIZE];
534 void **valuep = &p2[index % L2_SIZE];
535 void *value = *valuep;
544 ovsthread_key_destruct__(void *slots_)
546 struct ovsthread_key_slots *slots = slots_;
547 struct ovsthread_key *key;
551 ovs_mutex_lock(&key_mutex);
552 list_remove(&slots->list_node);
553 LIST_FOR_EACH (key, list_node, &inuse_keys) {
554 void *value = clear_slot(slots, key->index);
555 if (value && key->destructor) {
556 key->destructor(value);
560 ovs_mutex_unlock(&key_mutex);
562 for (i = 0; i < n / L2_SIZE; i++) {
568 /* Initializes '*keyp' as a thread-specific data key. The data items are
569 * initially null in all threads.
571 * If a thread exits with non-null data, then 'destructor', if nonnull, will be
572 * called passing the final data value as its argument. 'destructor' must not
573 * call any thread-specific data functions in this API.
575 * This function is similar to xpthread_key_create(). */
577 ovsthread_key_create(ovsthread_key_t *keyp, void (*destructor)(void *))
579 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
580 struct ovsthread_key *key;
582 if (ovsthread_once_start(&once)) {
583 xpthread_key_create(&tsd_key, ovsthread_key_destruct__);
584 ovsthread_once_done(&once);
587 ovs_mutex_lock(&key_mutex);
588 if (list_is_empty(&free_keys)) {
589 key = xmalloc(sizeof *key);
590 key->index = n_keys++;
591 if (key->index >= MAX_KEYS) {
595 key = CONTAINER_OF(list_pop_back(&free_keys),
596 struct ovsthread_key, list_node);
598 list_push_back(&inuse_keys, &key->list_node);
599 key->destructor = destructor;
600 ovs_mutex_unlock(&key_mutex);
605 /* Frees 'key'. The destructor supplied to ovsthread_key_create(), if any, is
608 * This function is similar to xpthread_key_delete(). */
610 ovsthread_key_delete(ovsthread_key_t key)
612 struct ovsthread_key_slots *slots;
614 ovs_mutex_lock(&key_mutex);
616 /* Move 'key' from 'inuse_keys' to 'free_keys'. */
617 list_remove(&key->list_node);
618 list_push_back(&free_keys, &key->list_node);
620 /* Clear this slot in all threads. */
621 LIST_FOR_EACH (slots, list_node, &slots_list) {
622 clear_slot(slots, key->index);
625 ovs_mutex_unlock(&key_mutex);
629 ovsthread_key_lookup__(const struct ovsthread_key *key)
631 struct ovsthread_key_slots *slots;
634 slots = pthread_getspecific(tsd_key);
636 slots = xzalloc(sizeof *slots);
638 ovs_mutex_lock(&key_mutex);
639 pthread_setspecific(tsd_key, slots);
640 list_push_back(&slots_list, &slots->list_node);
641 ovs_mutex_unlock(&key_mutex);
644 p2 = slots->p1[key->index / L2_SIZE];
646 p2 = xzalloc(L2_SIZE * sizeof *p2);
647 slots->p1[key->index / L2_SIZE] = p2;
650 return &p2[key->index % L2_SIZE];
653 /* Sets the value of thread-specific data item 'key', in the current thread, to
656 * This function is similar to pthread_setspecific(). */
658 ovsthread_setspecific(ovsthread_key_t key, const void *value)
660 *ovsthread_key_lookup__(key) = CONST_CAST(void *, value);
663 /* Returns the value of thread-specific data item 'key' in the current thread.
665 * This function is similar to pthread_getspecific(). */
667 ovsthread_getspecific(ovsthread_key_t key)
669 return *ovsthread_key_lookup__(key);