From: Mathieu Desnoyers Date: Wed, 23 Sep 2009 17:14:34 +0000 (-0400) Subject: urcu-defer: make call_rcu() energy efficient using futex() X-Git-Tag: v0.1~45 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=4ce9e4f28aa6de3f3443066c7a64d12156904d6b;p=urcu.git urcu-defer: make call_rcu() energy efficient using futex() Signed-off-by: Mathieu Desnoyers --- diff --git a/urcu-defer-static.h b/urcu-defer-static.h index a00ced0..dd24c61 100644 --- a/urcu-defer-static.h +++ b/urcu-defer-static.h @@ -125,6 +125,7 @@ struct defer_queue { extern struct defer_queue __thread defer_queue; extern void rcu_defer_barrier_thread(void); +extern void wake_up_defer(void); /* * not signal-safe. @@ -183,6 +184,10 @@ static inline void _rcu_defer_queue(void (*fct)(void *p), void *p) smp_wmb(); /* Publish new pointer before head */ /* Write q[] before head. */ STORE_SHARED(defer_queue.head, head); + /* + * Wake-up any waiting defer thread. + */ + wake_up_defer(); } #endif /* _URCU_DEFER_STATIC_H */ diff --git a/urcu-defer.c b/urcu-defer.c index 412eb30..5b904f8 100644 --- a/urcu-defer.c +++ b/urcu-defer.c @@ -28,11 +28,17 @@ #include #include #include +#include +#include +#include +#include #include "urcu-defer-static.h" /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ #include "urcu-defer.h" +#define futex(...) syscall(__NR_futex, __VA_ARGS__) + void __attribute__((destructor)) urcu_defer_exit(void); extern void synchronize_rcu(void); @@ -43,6 +49,8 @@ extern void synchronize_rcu(void); static pthread_mutex_t urcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER; +int defer_thread_futex; + /* * Written to only by each individual deferer. Read by both the deferer and * the reclamation tread. @@ -62,7 +70,28 @@ static struct deferer_registry *registry; static int num_deferers, alloc_deferers; static pthread_t tid_defer; -static int exit_defer; + +/* + * Wake-up any waiting defer thread. Called from many concurrent threads. + */ +void wake_up_defer(void) +{ + if (unlikely(atomic_read(&defer_thread_futex) == -1)) + atomic_set(&defer_thread_futex, 0); + futex(&defer_thread_futex, FUTEX_WAKE, + 0, NULL, NULL, 0); +} + +/* + * Defer thread waiting. Single thread. + */ +static void wait_defer(void) +{ + atomic_dec(&defer_thread_futex); + if (atomic_read(&defer_thread_futex) == -1) + futex(&defer_thread_futex, FUTEX_WAIT, -1, + NULL, NULL, 0); +} static void internal_urcu_lock(pthread_mutex_t *mutex) { @@ -81,6 +110,7 @@ static void internal_urcu_lock(pthread_mutex_t *mutex) perror("Error in pthread mutex lock"); exit(-1); } + pthread_testcancel(); poll(NULL,0,10); } #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ @@ -195,10 +225,22 @@ end: void *thr_defer(void *args) { for (;;) { - if (LOAD_SHARED(exit_defer)) - break; - poll(NULL,0,100); /* wait for 100ms */ + pthread_testcancel(); + printf("a\n"); + /* + * "Be green". Don't wake up the CPU if there is no RCU work + * to perform whatsoever. Aims at saving laptop battery life by + * leaving the processor in sleep state when idle. + */ + printf("b\n"); + wait_defer(); + printf("e\n"); + /* Sleeping after wait_defer to let many callbacks enqueue */ + //TEST poll(NULL,0,100); /* wait for 100ms */ + printf("f\n"); rcu_defer_barrier(); + printf("perform deferred call_rcu() from worker thread %lu.\n", + time(NULL)); } return NULL; @@ -277,7 +319,8 @@ static void stop_defer_thread(void) int ret; void *tret; - STORE_SHARED(exit_defer, 1); + pthread_cancel(tid_defer); + wake_up_defer(); ret = pthread_join(tid_defer, &tret); assert(!ret); }