/*
- * Copyright (c) 2013, 2014 Nicira, Inc.
+ * Copyright (c) 2013, 2014, 2015 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "ovs-thread.h"
#include <errno.h>
#include <poll.h>
+#ifndef _WIN32
+#include <signal.h>
+#endif
#include <stdlib.h>
#include <unistd.h>
#include "compiler.h"
+#include "fatal-signal.h"
#include "hash.h"
+#include "list.h"
+#include "netdev-dpdk.h"
+#include "ovs-rcu.h"
#include "poll-loop.h"
+#include "seq.h"
#include "socket-util.h"
#include "util.h"
* cut-and-paste. Since "sparse" is just a checker, not a compiler, it
* doesn't matter that we don't define them. */
#else
-#include "vlog.h"
+#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(ovs_thread);
OVS_NO_THREAD_SAFETY_ANALYSIS \
{ \
struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
- int error = pthread_##TYPE##_##FUN(&l->lock); \
+ int error; \
+ \
+ /* Verify that 'l' was initialized. */ \
+ if (OVS_UNLIKELY(!l->where)) { \
+ ovs_abort(0, "%s: %s() passed uninitialized ovs_"#TYPE, \
+ where, __func__); \
+ } \
+ \
+ error = pthread_##TYPE##_##FUN(&l->lock); \
if (OVS_UNLIKELY(error)) { \
- ovs_abort(error, "pthread_%s_%s failed", #TYPE, #FUN); \
+ ovs_abort(error, "%s: pthread_%s_%s failed", where, #TYPE, #FUN); \
} \
l->where = where; \
- }
+ }
LOCK_FUNCTION(mutex, lock);
LOCK_FUNCTION(rwlock, rdlock);
LOCK_FUNCTION(rwlock, wrlock);
OVS_NO_THREAD_SAFETY_ANALYSIS \
{ \
struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
- int error = pthread_##TYPE##_##FUN(&l->lock); \
+ int error; \
+ \
+ /* Verify that 'l' was initialized. */ \
+ if (OVS_UNLIKELY(!l->where)) { \
+ ovs_abort(0, "%s: %s() passed uninitialized ovs_"#TYPE, \
+ where, __func__); \
+ } \
+ \
+ error = pthread_##TYPE##_##FUN(&l->lock); \
if (OVS_UNLIKELY(error) && error != EBUSY) { \
- ovs_abort(error, "pthread_%s_%s failed", #TYPE, #FUN); \
+ ovs_abort(error, "%s: pthread_%s_%s failed", where, #TYPE, #FUN); \
} \
if (!error) { \
l->where = where; \
TRY_LOCK_FUNCTION(rwlock, tryrdlock);
TRY_LOCK_FUNCTION(rwlock, trywrlock);
-#define UNLOCK_FUNCTION(TYPE, FUN) \
+#define UNLOCK_FUNCTION(TYPE, FUN, WHERE) \
void \
ovs_##TYPE##_##FUN(const struct ovs_##TYPE *l_) \
OVS_NO_THREAD_SAFETY_ANALYSIS \
{ \
struct ovs_##TYPE *l = CONST_CAST(struct ovs_##TYPE *, l_); \
int error; \
- l->where = NULL; \
+ \
+ /* Verify that 'l' was initialized. */ \
+ ovs_assert(l->where); \
+ \
+ l->where = WHERE; \
error = pthread_##TYPE##_##FUN(&l->lock); \
if (OVS_UNLIKELY(error)) { \
- ovs_abort(error, "pthread_%s_%sfailed", #TYPE, #FUN); \
+ ovs_abort(error, "pthread_%s_%s failed", #TYPE, #FUN); \
} \
}
-UNLOCK_FUNCTION(mutex, unlock);
-UNLOCK_FUNCTION(mutex, destroy);
-UNLOCK_FUNCTION(rwlock, unlock);
-UNLOCK_FUNCTION(rwlock, destroy);
+UNLOCK_FUNCTION(mutex, unlock, "<unlocked>");
+UNLOCK_FUNCTION(mutex, destroy, NULL);
+UNLOCK_FUNCTION(rwlock, unlock, "<unlocked>");
+UNLOCK_FUNCTION(rwlock, destroy, NULL);
#define XPTHREAD_FUNC1(FUNCTION, PARAM1) \
void \
XPTHREAD_FUNC1(pthread_cond_signal, pthread_cond_t *);
XPTHREAD_FUNC1(pthread_cond_broadcast, pthread_cond_t *);
-XPTHREAD_FUNC3(pthread_barrier_init, pthread_barrier_t *,
- pthread_barrierattr_t *, unsigned int);
-XPTHREAD_FUNC1(pthread_barrier_destroy, pthread_barrier_t *);
-
XPTHREAD_FUNC2(pthread_join, pthread_t, void **);
typedef void destructor_func(void *);
XPTHREAD_FUNC1(pthread_key_delete, pthread_key_t);
XPTHREAD_FUNC2(pthread_setspecific, pthread_key_t, const void *);
+#ifndef _WIN32
+XPTHREAD_FUNC3(pthread_sigmask, int, const sigset_t *, sigset_t *);
+#endif
+
static void
ovs_mutex_init__(const struct ovs_mutex *l_, int type)
{
pthread_mutexattr_t attr;
int error;
- l->where = NULL;
+ l->where = "<unlocked>";
xpthread_mutexattr_init(&attr);
xpthread_mutexattr_settype(&attr, type);
error = pthread_mutex_init(&l->lock, &attr);
pthread_rwlockattr_t attr;
int error;
- l->where = NULL;
+ l->where = "<unlocked>";
xpthread_rwlockattr_init(&attr);
#ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
ovs_mutex_cond_wait(pthread_cond_t *cond, const struct ovs_mutex *mutex_)
{
struct ovs_mutex *mutex = CONST_CAST(struct ovs_mutex *, mutex_);
- int error = pthread_cond_wait(cond, &mutex->lock);
+ int error;
+
+ ovsrcu_quiesce_start();
+ error = pthread_cond_wait(cond, &mutex->lock);
+ ovsrcu_quiesce_end();
+
if (OVS_UNLIKELY(error)) {
ovs_abort(error, "pthread_cond_wait failed");
}
}
-int
-xpthread_barrier_wait(pthread_barrier_t *barrier)
+/* Initializes the 'barrier'. 'size' is the number of threads
+ * expected to hit the barrier. */
+void
+ovs_barrier_init(struct ovs_barrier *barrier, uint32_t size)
{
- int error;
+ barrier->size = size;
+ atomic_count_init(&barrier->count, 0);
+ barrier->seq = seq_create();
+}
- error = pthread_barrier_wait(barrier);
- if (error && OVS_UNLIKELY(error != PTHREAD_BARRIER_SERIAL_THREAD)) {
- ovs_abort(error, "pthread_barrier_wait failed");
- }
+/* Destroys the 'barrier'. */
+void
+ovs_barrier_destroy(struct ovs_barrier *barrier)
+{
+ seq_destroy(barrier->seq);
+}
- return error;
+/* Makes the calling thread block on the 'barrier' until all
+ * 'barrier->size' threads hit the barrier.
+ * ovs_barrier provides the necessary acquire-release semantics to make
+ * the effects of prior memory accesses of all the participating threads
+ * visible on return and to prevent the following memory accesses to be
+ * reordered before the ovs_barrier_block(). */
+void
+ovs_barrier_block(struct ovs_barrier *barrier)
+{
+ uint64_t seq = seq_read(barrier->seq);
+ uint32_t orig;
+
+ orig = atomic_count_inc(&barrier->count);
+ if (orig + 1 == barrier->size) {
+ atomic_count_set(&barrier->count, 0);
+ /* seq_change() serves as a release barrier against the other threads,
+ * so the zeroed count is visible to them as they continue. */
+ seq_change(barrier->seq);
+ } else {
+ /* To prevent thread from waking up by other event,
+ * keeps waiting for the change of 'barrier->seq'. */
+ while (seq == seq_read(barrier->seq)) {
+ seq_wait(barrier->seq, seq);
+ poll_block();
+ }
+ }
}
\f
DEFINE_EXTERN_PER_THREAD_DATA(ovsthread_id, 0);
struct ovsthread_aux {
void *(*start)(void *);
void *arg;
+ char name[16];
};
static void *
ovsthread_wrapper(void *aux_)
{
- static atomic_uint next_id = ATOMIC_VAR_INIT(1);
+ static atomic_count next_id = ATOMIC_COUNT_INIT(1);
struct ovsthread_aux *auxp = aux_;
struct ovsthread_aux aux;
unsigned int id;
- atomic_add(&next_id, 1, &id);
+ id = atomic_count_inc(&next_id);
*ovsthread_id_get() = id;
aux = *auxp;
free(auxp);
+ /* The order of the following calls is important, because
+ * ovsrcu_quiesce_end() saves a copy of the thread name. */
+ char *subprogram_name = xasprintf("%s%u", aux.name, id);
+ set_subprogram_name(subprogram_name);
+ free(subprogram_name);
+ ovsrcu_quiesce_end();
+
return aux.start(aux.arg);
}
-void
-xpthread_create(pthread_t *threadp, pthread_attr_t *attr,
- void *(*start)(void *), void *arg)
+static void
+set_min_stack_size(pthread_attr_t *attr, size_t min_stacksize)
+{
+ size_t stacksize;
+ int error;
+
+ error = pthread_attr_getstacksize(attr, &stacksize);
+ if (error) {
+ ovs_abort(error, "pthread_attr_getstacksize failed");
+ }
+
+ if (stacksize < min_stacksize) {
+ error = pthread_attr_setstacksize(attr, min_stacksize);
+ if (error) {
+ ovs_abort(error, "pthread_attr_setstacksize failed");
+ }
+ }
+}
+
+/* Starts a thread that calls 'start(arg)'. Sets the thread's name to 'name'
+ * (suffixed by its ovsthread_id()). Returns the new thread's pthread_t. */
+pthread_t
+ovs_thread_create(const char *name, void *(*start)(void *), void *arg)
{
struct ovsthread_aux *aux;
pthread_t thread;
forbid_forking("multiple threads exist");
multithreaded = true;
+ ovsrcu_quiesce_end();
aux = xmalloc(sizeof *aux);
aux->start = start;
aux->arg = arg;
-
- error = pthread_create(threadp ? threadp : &thread, attr,
- ovsthread_wrapper, aux);
+ ovs_strlcpy(aux->name, name, sizeof aux->name);
+
+ /* Some small systems use a default stack size as small as 80 kB, but OVS
+ * requires approximately 384 kB according to the following analysis:
+ * http://openvswitch.org/pipermail/dev/2016-January/065049.html
+ *
+ * We use 512 kB to give us some margin of error. */
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ set_min_stack_size(&attr, 512 * 1024);
+
+ error = pthread_create(&thread, NULL, ovsthread_wrapper, aux);
if (error) {
ovs_abort(error, "pthread_create failed");
}
+ pthread_attr_destroy(&attr);
+ return thread;
}
\f
bool
ovsthread_once_start__(struct ovsthread_once *once)
{
ovs_mutex_lock(&once->mutex);
- if (!ovsthread_once_is_done__(once)) {
- return false;
+ /* Mutex synchronizes memory, so we get the current value of 'done'. */
+ if (!once->done) {
+ return true;
}
ovs_mutex_unlock(&once->mutex);
- return true;
+ return false;
}
void
ovsthread_once_done(struct ovsthread_once *once)
{
- atomic_store(&once->done, true);
+ /* We need release semantics here, so that the following store may not
+ * be moved ahead of any of the preceding initialization operations.
+ * A release atomic_thread_fence provides that prior memory accesses
+ * will not be reordered to take place after the following store. */
+ atomic_thread_fence(memory_order_release);
+ once->done = true;
ovs_mutex_unlock(&once->mutex);
}
\f
+bool
+single_threaded(void)
+{
+ return !multithreaded;
+}
+
/* Asserts that the process has not yet created any threads (beyond the initial
* thread).
*
return !must_not_fork;
}
\f
-/* ovsthread_counter.
- *
- * We implement the counter as an array of N_COUNTERS individual counters, each
- * with its own lock. Each thread uses one of the counters chosen based on a
- * hash of the thread's ID, the idea being that, statistically, different
- * threads will tend to use different counters and therefore avoid
- * interfering with each other.
- *
- * Undoubtedly, better implementations are possible. */
-
-/* Basic counter structure. */
-struct ovsthread_counter__ {
- struct ovs_mutex mutex;
- unsigned long long int value;
-};
-
-/* Pad the basic counter structure to 64 bytes to avoid cache line
- * interference. */
-struct ovsthread_counter {
- struct ovsthread_counter__ c;
- char pad[ROUND_UP(sizeof(struct ovsthread_counter__), 64)
- - sizeof(struct ovsthread_counter__)];
-};
-
-#define N_COUNTERS 16
+/* ovsthread_stats. */
-struct ovsthread_counter *
-ovsthread_counter_create(void)
+void
+ovsthread_stats_init(struct ovsthread_stats *stats)
{
- struct ovsthread_counter *c;
int i;
- c = xmalloc(N_COUNTERS * sizeof *c);
- for (i = 0; i < N_COUNTERS; i++) {
- ovs_mutex_init(&c[i].c.mutex);
- c[i].c.value = 0;
+ ovs_mutex_init(&stats->mutex);
+ for (i = 0; i < ARRAY_SIZE(stats->buckets); i++) {
+ stats->buckets[i] = NULL;
}
- return c;
}
void
-ovsthread_counter_destroy(struct ovsthread_counter *c)
+ovsthread_stats_destroy(struct ovsthread_stats *stats)
{
- if (c) {
- int i;
-
- for (i = 0; i < N_COUNTERS; i++) {
- ovs_mutex_destroy(&c[i].c.mutex);
- }
- free(c);
- }
+ ovs_mutex_destroy(&stats->mutex);
}
-void
-ovsthread_counter_inc(struct ovsthread_counter *c, unsigned long long int n)
+void *
+ovsthread_stats_bucket_get(struct ovsthread_stats *stats,
+ void *(*new_bucket)(void))
{
- c = &c[hash_int(ovsthread_id_self(), 0) % N_COUNTERS];
-
- ovs_mutex_lock(&c->c.mutex);
- c->c.value += n;
- ovs_mutex_unlock(&c->c.mutex);
+ unsigned int idx = ovsthread_id_self() & (ARRAY_SIZE(stats->buckets) - 1);
+ void *bucket = stats->buckets[idx];
+ if (!bucket) {
+ ovs_mutex_lock(&stats->mutex);
+ bucket = stats->buckets[idx];
+ if (!bucket) {
+ bucket = stats->buckets[idx] = new_bucket();
+ }
+ ovs_mutex_unlock(&stats->mutex);
+ }
+ return bucket;
}
-unsigned long long int
-ovsthread_counter_read(const struct ovsthread_counter *c)
+size_t
+ovs_thread_stats_next_bucket(const struct ovsthread_stats *stats, size_t i)
{
- unsigned long long int sum;
- int i;
-
- sum = 0;
- for (i = 0; i < N_COUNTERS; i++) {
- ovs_mutex_lock(&c[i].c.mutex);
- sum += c[i].c.value;
- ovs_mutex_unlock(&c[i].c.mutex);
+ for (; i < ARRAY_SIZE(stats->buckets); i++) {
+ if (stats->buckets[i]) {
+ break;
+ }
}
- return sum;
+ return i;
}
+
\f
/* Parses /proc/cpuinfo for the total number of physical cores on this system
* across all CPU packages, not counting hyper-threads.
break;
}
- if (cpu & (1 << id)) {
+ if (cpu & (1ULL << id)) {
/* We've already counted this package's cores. */
continue;
}
- cpu |= 1 << id;
+ cpu |= 1ULL << id;
/* Find the number of cores for this package. */
while (fgets(line, sizeof line, stream)) {
return n_cores > 0 ? n_cores : 0;
}
+
+/* Returns 'true' if current thread is PMD thread. */
+bool
+thread_is_pmd(void)
+{
+ const char *name = get_subprogram_name();
+ return !strncmp(name, "pmd", 3);
+}
+
\f
/* ovsthread_key. */
/* A piece of thread-specific data. */
struct ovsthread_key {
- struct list list_node; /* In 'inuse_keys' or 'free_keys'. */
+ struct ovs_list list_node; /* In 'inuse_keys' or 'free_keys'. */
void (*destructor)(void *); /* Called at thread exit. */
/* Indexes into the per-thread array in struct ovsthread_key_slots.
/* Per-thread data structure. */
struct ovsthread_key_slots {
- struct list list_node; /* In 'slots_list'. */
+ struct ovs_list list_node; /* In 'slots_list'. */
void **p1[L1_SIZE];
};
*
* Together, 'inuse_keys' and 'free_keys' hold an ovsthread_key for every index
* from 0 to n_keys - 1, inclusive. */
-static struct list inuse_keys OVS_GUARDED_BY(key_mutex)
- = LIST_INITIALIZER(&inuse_keys);
-static struct list free_keys OVS_GUARDED_BY(key_mutex)
- = LIST_INITIALIZER(&free_keys);
+static struct ovs_list inuse_keys OVS_GUARDED_BY(key_mutex)
+ = OVS_LIST_INITIALIZER(&inuse_keys);
+static struct ovs_list free_keys OVS_GUARDED_BY(key_mutex)
+ = OVS_LIST_INITIALIZER(&free_keys);
static unsigned int n_keys OVS_GUARDED_BY(key_mutex);
/* All existing struct ovsthread_key_slots. */
-static struct list slots_list OVS_GUARDED_BY(key_mutex)
- = LIST_INITIALIZER(&slots_list);
+static struct ovs_list slots_list OVS_GUARDED_BY(key_mutex)
+ = OVS_LIST_INITIALIZER(&slots_list);
static void *
clear_slot(struct ovsthread_key_slots *slots, unsigned int index)
n = n_keys;
ovs_mutex_unlock(&key_mutex);
- for (i = 0; i < n / L2_SIZE; i++) {
+ for (i = 0; i < DIV_ROUND_UP(n, L2_SIZE); i++) {
free(slots->p1[i]);
}
free(slots);
}
+/* Cancels the callback to ovsthread_key_destruct__().
+ *
+ * Cancelling the call to the destructor during the main thread exit
+ * is needed while using pthreads-win32 library in Windows. It has been
+ * observed that in pthreads-win32, a call to the destructor during
+ * main thread exit causes undefined behavior. */
+static void
+ovsthread_cancel_ovsthread_key_destruct__(void *aux OVS_UNUSED)
+{
+ pthread_setspecific(tsd_key, NULL);
+}
+
/* Initializes '*keyp' as a thread-specific data key. The data items are
* initially null in all threads.
*
if (ovsthread_once_start(&once)) {
xpthread_key_create(&tsd_key, ovsthread_key_destruct__);
+ fatal_signal_add_hook(ovsthread_cancel_ovsthread_key_destruct__,
+ NULL, NULL, true);
ovsthread_once_done(&once);
}