X-Git-Url: https://git.lttng.org/?p=urcu.git;a=blobdiff_plain;f=tests%2Ftest_urcu.c;h=eed3dfbdcdc295c880e626eff8f5802a30356dda;hp=1ea9d07945d1741ab2dd58db57c454950b113cd4;hb=95bc7fb90c36ee10dc171a23e8783eff30072b7b;hpb=31b598e0bb2fde285afa63986613e632e98b104d diff --git a/tests/test_urcu.c b/tests/test_urcu.c index 1ea9d07..eed3dfb 100644 --- a/tests/test_urcu.c +++ b/tests/test_urcu.c @@ -3,7 +3,7 @@ * * Userspace RCU library - test program * - * Copyright February 2009 - Mathieu Desnoyers + * Copyright February 2009 - Mathieu Desnoyers * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,11 +31,15 @@ #include #include #include -#include #include #include #include +#include + +#ifdef __linux__ +#include +#endif /* hardcoded number of CPUs */ #define NR_CPUS 16384 @@ -80,10 +84,10 @@ static unsigned long rduration; /* write-side C.S. duration, in loops */ static unsigned long wduration; -static inline void loop_sleep(unsigned long l) +static inline void loop_sleep(unsigned long loops) { - while(l-- != 0) - cpu_relax(); + while (loops-- != 0) + caa_cpu_relax(); } static int verbose_mode; @@ -108,9 +112,10 @@ typedef unsigned long cpu_set_t; static void set_affinity(void) { +#if HAVE_SCHED_SETAFFINITY cpu_set_t mask; - int cpu; - int ret; + int cpu, ret; +#endif /* HAVE_SCHED_SETAFFINITY */ if (!use_affinity) return; @@ -151,8 +156,8 @@ static int test_duration_read(void) return !test_stop; } -static unsigned long long __thread nr_writes; -static unsigned long long __thread nr_reads; +static DEFINE_URCU_TLS(unsigned long long, nr_writes); +static DEFINE_URCU_TLS(unsigned long long, nr_reads); static unsigned int nr_readers; static unsigned int nr_writers; @@ -183,7 +188,8 @@ void rcu_copy_mutex_unlock(void) /* * malloc/free are reusing memory areas too quickly, which does not let us * test races appropriately. Use a large circular array for allocations. - * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail. + * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across + * both alloc and free, which insures we never run over our tail. */ #define ARRAY_SIZE (1048576 * nr_writers) #define ARRAY_POISON 0xDEADBEEF @@ -195,7 +201,6 @@ static struct test_array *test_array_alloc(void) struct test_array *ret; int index; - rcu_copy_mutex_lock(); index = array_index % ARRAY_SIZE; assert(test_array[index].a == ARRAY_POISON || test_array[index].a == 0); @@ -203,7 +208,6 @@ static struct test_array *test_array_alloc(void) array_index++; if (array_index == ARRAY_SIZE) array_index = 0; - rcu_copy_mutex_unlock(); return ret; } @@ -211,9 +215,7 @@ static void test_array_free(struct test_array *ptr) { if (!ptr) return; - rcu_copy_mutex_lock(); ptr->a = ARRAY_POISON; - rcu_copy_mutex_unlock(); } void *thr_reader(void *_count) @@ -231,7 +233,7 @@ void *thr_reader(void *_count) while (!test_go) { } - smp_mb(); + cmm_smp_mb(); for (;;) { rcu_read_lock(); @@ -239,17 +241,21 @@ void *thr_reader(void *_count) debug_yield_read(); if (local_ptr) assert(local_ptr->a == 8); - if (unlikely(rduration)) + if (caa_unlikely(rduration)) loop_sleep(rduration); rcu_read_unlock(); - nr_reads++; - if (unlikely(!test_duration_read())) + URCU_TLS(nr_reads)++; + if (caa_unlikely(!test_duration_read())) break; } rcu_unregister_thread(); - *count = nr_reads; + /* test extra thread registration */ + rcu_register_thread(); + rcu_unregister_thread(); + + *count = URCU_TLS(nr_reads); printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", "reader", pthread_self(), (unsigned long)gettid()); return ((void*)1); @@ -269,28 +275,30 @@ void *thr_writer(void *_count) while (!test_go) { } - smp_mb(); + cmm_smp_mb(); for (;;) { + rcu_copy_mutex_lock(); new = test_array_alloc(); new->a = 8; old = rcu_xchg_pointer(&test_rcu_pointer, new); - if (unlikely(wduration)) + if (caa_unlikely(wduration)) loop_sleep(wduration); synchronize_rcu(); if (old) old->a = 0; test_array_free(old); - nr_writes++; - if (unlikely(!test_duration_write())) + rcu_copy_mutex_unlock(); + URCU_TLS(nr_writes)++; + if (caa_unlikely(!test_duration_write())) break; - if (unlikely(wdelay)) + if (caa_unlikely(wdelay)) loop_sleep(wdelay); } printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", "writer", pthread_self(), (unsigned long)gettid()); - *count = nr_writes; + *count = URCU_TLS(nr_writes); return ((void*)2); } @@ -396,7 +404,7 @@ int main(int argc, char **argv) printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", "main", pthread_self(), (unsigned long)gettid()); - test_array = malloc(sizeof(*test_array) * ARRAY_SIZE); + test_array = calloc(1, sizeof(*test_array) * ARRAY_SIZE); tid_reader = malloc(sizeof(*tid_reader) * nr_readers); tid_writer = malloc(sizeof(*tid_writer) * nr_writers); count_reader = malloc(sizeof(*count_reader) * nr_readers); @@ -417,7 +425,7 @@ int main(int argc, char **argv) exit(1); } - smp_mb(); + cmm_smp_mb(); test_go = 1; @@ -440,10 +448,10 @@ int main(int argc, char **argv) printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, tot_writes); - printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " + printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu " "nr_writers %3u " "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n", - argv[0], duration, nr_readers, rduration, + argv[0], duration, nr_readers, rduration, wduration, nr_writers, wdelay, tot_reads, tot_writes, tot_reads + tot_writes); test_array_free(test_rcu_pointer);