Merge branch 'master' into urcu/rcuja-range-merge
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 4 May 2015 17:35:35 +0000 (13:35 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 4 May 2015 17:35:35 +0000 (13:35 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
1  2 
Makefile.am
configure.ac
rculfhash.c
tests/benchmark/runall.sh
tests/regression/Makefile.am
tests/regression/test_urcu_ja.c
tests/regression/test_urcu_ja.h
tests/regression/test_urcu_ja_range.c
tests/regression/test_urcu_ja_range.h
urcu/hlist.h
urcu/rculfhash.h

diff --cc Makefile.am
index 2ac790f3c8979b2b96b459a08b08f6bd0bc31b1f,752510d717783c9d0868c6701767613b617ba357..df6eec168e6071a5ce0d3e41d59bd5adcdf80db7
@@@ -4,10 -4,13 +4,13 @@@ AM_CPPFLAGS = -I$(top_builddir)/urc
  
  #Add the -version-info directly here since we are only building
  # library that use the version-info
- AM_LDFLAGS=-lpthread -version-info $(URCU_LIBRARY_VERSION)
+ AM_LDFLAGS=-version-info $(URCU_LIBRARY_VERSION)
+ if !LIBC_INCLUDES_PTHREAD
+ AM_LDFLAGS+=-lpthread
+ endif
  AM_CFLAGS=-Wall
  
 -SUBDIRS = . doc tests
 +SUBDIRS = . doc tests rcuja
  
  include_HEADERS = urcu.h urcu-bp.h urcu-call-rcu.h urcu-defer.h \
                urcu-pointer.h urcu-qsbr.h urcu-flavor.h
@@@ -17,9 -20,10 +20,11 @@@ nobase_dist_include_HEADERS = urcu/comp
                urcu/wfqueue.h urcu/rculfstack.h urcu/rculfqueue.h \
                urcu/ref.h urcu/cds.h urcu/urcu_ref.h urcu/urcu-futex.h \
                urcu/uatomic_arch.h urcu/rculfhash.h urcu/wfcqueue.h \
 -              urcu/lfstack.h urcu/syscall-compat.h \
 +              urcu/rcuja.h urcu/rcuja-range.h urcu/lfstack.h \
++              urcu/syscall-compat.h \
                $(top_srcdir)/urcu/map/*.h \
                $(top_srcdir)/urcu/static/*.h \
+               urcu/rand-compat.h \
                urcu/tls-compat.h
  nobase_nodist_include_HEADERS = urcu/arch.h urcu/uatomic.h urcu/config.h
  
diff --cc configure.ac
index 017cdf1b9c623c025442cdc7efe68420dddfaf74,ed59d16eb026c24470524b348bedf40b0ca7d6a9..c3912bf2990d9c7c899b1fe973de261a67cec5b6
@@@ -293,7 -323,10 +323,11 @@@ AC_CONFIG_FILES(
        doc/Makefile
        doc/examples/Makefile
        tests/Makefile
 +      rcuja/Makefile
+       tests/common/Makefile
+       tests/unit/Makefile
+       tests/benchmark/Makefile
+       tests/regression/Makefile
        liburcu.pc
        liburcu-bp.pc
        liburcu-cds.pc
diff --cc rculfhash.c
Simple merge
index 0000000000000000000000000000000000000000,ced1765694ebfa1dbc9500f58b159fc33fc98a9b..92e57bc0409ef1e825128f81908d369785279aef
mode 000000,100755..100755
--- /dev/null
@@@ -1,0 -1,104 +1,107 @@@
+ #!/bin/sh
+ #run all tests
+ #set to number of active CPUS
+ NUM_CPUS=8
+ #extra options, e.g. for setting affinity on even CPUs :
+ #EXTRA_OPTS=$(for a in $(seq 0 2 127); do echo -n "-a ${a} "; done)
+ #ppc64 striding, use with NUM_CPUS=8
+ #stride 1
+ #EXTRA_OPTS=$(for a in $(seq 0 2 15); do echo -n "-a ${a} "; done)
+ #stride 2
+ #EXTRA_OPTS=$(for a in $(seq 0 4 31); do echo -n "-a ${a} "; done)
+ #stride 4
+ #EXTRA_OPTS=$(for a in $(seq 0 8 63); do echo -n "-a ${a} "; done)
+ #stride 8
+ #EXTRA_OPTS=$(for a in $(seq 0 16 127); do echo -n "-a ${a} "; done)
+ #Vary update fraction
+ #x: vary update fraction from 0 to 0.0001
+   #fix number of readers and reader C.S. length, vary delay between updates
+ #y: ops/s
+ rm -f runall.log
+ rm -fr runall.detail.log
+ echo Executing batch RCU test
+ DURATION=10
+ BATCH_ARRAY="1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536
+            131072 262144"
+ NR_WRITERS=$((${NUM_CPUS} / 2))
+ rm -f batch-rcu.log
+ NR_READERS=$((${NUM_CPUS} - ${NR_WRITERS}))
+ for BATCH_SIZE in ${BATCH_ARRAY}; do
+       echo "./runtests-batch.sh ${NR_READERS} ${NR_WRITERS} ${DURATION} -d 0 -b ${BATCH_SIZE} ${EXTRA_OPTS} | tee -a batch-rcu.log" >> runall.log
+       (./runtests-batch.sh ${NR_READERS} ${NR_WRITERS} ${DURATION} -d 0 -b ${BATCH_SIZE} ${EXTRA_OPTS} | tee -a batch-rcu.log) || exit 1
+ done
+ #setting gc each 32768. ** UPDATE FOR YOUR ARCHITECTURE BASED ON TEST ABOVE **
+ EXTRA_OPTS="${EXTRA_OPTS} -b 32768"
+ echo Executing update fraction test
+ DURATION=10
+ WDELAY_ARRAY="0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768
+               65536 131072 262144 524288 1048576 2097152 4194304 8388608
+               16777216 33554432 67108864 134217728"
+ NR_WRITERS=$((${NUM_CPUS} / 2))
+ rm -f update-fraction.log
+ NR_READERS=$((${NUM_CPUS} - ${NR_WRITERS}))
+ for WDELAY in ${WDELAY_ARRAY}; do
+       echo "./runtests.sh ${NR_READERS} ${NR_WRITERS} ${DURATION} -d ${WDELAY} ${EXTRA_OPTS} | tee -a update-fraction.log" >> runall.log
+       (./runtests.sh ${NR_READERS} ${NR_WRITERS} ${DURATION} -d ${WDELAY} ${EXTRA_OPTS} | tee -a update-fraction.log) || exit 1
+ done
+ #Test scalability :
+ # x: vary number of readers from 0 to num cpus
+ # y: ops/s
+ # 0 writer.
+ echo Executing scalability test
+ NR_WRITERS=0
+ DURATION=10
+ rm -f scalability.log
+ for NR_READERS in $(seq 1 ${NUM_CPUS}); do
+       echo "./runtests.sh ${NR_READERS} ${NR_WRITERS} ${DURATION} ${EXTRA_OPTS}| tee -a scalability.log" >> runall.log
+       (./runtests.sh ${NR_READERS} ${NR_WRITERS} ${DURATION} ${EXTRA_OPTS}| tee -a scalability.log) || exit 1
+ done
+ # x: Vary reader C.S. length from 0 to 100 us
+ # y: ops/s
+ # 8 readers
+ # 0 writers
+ echo Executing reader C.S. length test
+ NR_READERS=${NUM_CPUS}
+ NR_WRITERS=0
+ DURATION=10
+ #in loops.
+ READERCSLEN_ARRAY="0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152"
+ rm -f readercslen.log
+ for READERCSLEN in ${READERCSLEN_ARRAY}; do
+       echo "./runtests.sh ${NR_READERS} ${NR_WRITERS} ${DURATION} ${EXTRA_OPTS} -c ${READERCSLEN} | tee -a readercslen.log" >> runall.log
+       (./runtests.sh ${NR_READERS} ${NR_WRITERS} ${DURATION} ${EXTRA_OPTS} -c ${READERCSLEN} | tee -a readercslen.log) || exit 1
+ done
+ echo Executing Hash table test
+ ./runhash.sh || exit 1
++
++echo Executing Judy array test
++./runja.sh || exit 1
index 0000000000000000000000000000000000000000,8dfe542ff22e63f8d7825d6e801b714b37e14329..1b99abd07351f0e3cea145368be1f0801910ea77
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,59 +1,71 @@@
 -      rcutorture_urcu_qsbr
+ if !LIBC_INCLUDES_PTHREAD
+ AM_LDFLAGS=-lpthread
+ endif
+ AM_CFLAGS=-I$(top_srcdir) -I$(top_builddir) -I$(top_srcdir)/tests/common -g
+ noinst_PROGRAMS = test_urcu_fork \
+       rcutorture_urcu \
+       rcutorture_urcu_signal \
+       rcutorture_urcu_mb \
+       rcutorture_urcu_bp \
++      rcutorture_urcu_qsbr \
++      test_urcu_ja \
++      test_urcu_ja_range
+ noinst_HEADERS = rcutorture.h
+ URCU_COMMON_LIB=$(top_builddir)/liburcu-common.la
+ URCU_LIB=$(top_builddir)/liburcu.la
+ URCU_QSBR_LIB=$(top_builddir)/liburcu-qsbr.la
+ URCU_MB_LIB=$(top_builddir)/liburcu-mb.la
+ URCU_SIGNAL_LIB=$(top_builddir)/liburcu-signal.la
+ URCU_BP_LIB=$(top_builddir)/liburcu-bp.la
+ URCU_CDS_LIB=$(top_builddir)/liburcu-cds.la
+ test_urcu_fork_SOURCES = test_urcu_fork.c
+ test_urcu_fork_LDADD = $(URCU_LIB)
+ rcutorture_urcu_SOURCES = urcutorture.c
+ rcutorture_urcu_CFLAGS = -DRCU_MEMBARRIER $(AM_CFLAGS)
+ rcutorture_urcu_LDADD = $(URCU_LIB)
+ rcutorture_urcu_mb_SOURCES = urcutorture.c
+ rcutorture_urcu_mb_CFLAGS = -DRCU_MB $(AM_CFLAGS)
+ rcutorture_urcu_mb_LDADD = $(URCU_MB_LIB)
+ rcutorture_urcu_qsbr_SOURCES = urcutorture.c
+ rcutorture_urcu_qsbr_CFLAGS = -DTORTURE_QSBR -DRCU_QSBR $(AM_CFLAGS)
+ rcutorture_urcu_qsbr_LDADD = $(URCU_QSBR_LIB)
+ rcutorture_urcu_signal_SOURCES = urcutorture.c
+ rcutorture_urcu_signal_CFLAGS = -DRCU_SIGNAL $(AM_CFLAGS)
+ rcutorture_urcu_signal_LDADD = $(URCU_SIGNAL_LIB)
+ rcutorture_urcu_bp_SOURCES = urcutorture.c
+ rcutorture_urcu_bp_CFLAGS = -DRCU_BP $(AM_CFLAGS)
+ rcutorture_urcu_bp_LDADD = $(URCU_BP_LIB)
++test_urcu_ja_SOURCES = test_urcu_ja.c test_urcu_ja.h \
++              $(COMPAT)
++test_urcu_ja_CFLAGS = -DRCU_QSBR $(AM_CFLAGS)
++test_urcu_ja_LDADD = $(URCU_QSBR_LIB) $(URCU_CDS_LIB)
++
++test_urcu_ja_range_SOURCES = test_urcu_ja_range.c test_urcu_ja_range.h \
++              $(COMPAT)
++test_urcu_ja_range_CFLAGS = -DRCU_QSBR $(AM_CFLAGS)
++test_urcu_ja_range_LDADD = $(URCU_QSBR_LIB) $(URCU_CDS_LIB)
++
+ urcutorture.c: ../common/api.h
+ .PHONY: regtest
+ # For now, run the benchmarks too as regression tests.
+ # TODO: split benchmarks from regression tests
+ regtest:
+       ./test_urcu_fork
+       ./rcutorture_urcu
+       ./rcutorture_urcu_signal
+       ./rcutorture_urcu_mb
+       ./rcutorture_urcu_bp
+       ./rcutorture_urcu_qsbr
+       cd ../benchmark && ./runall.sh && cd ..
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..05725275cffaa0781e8bf8636c206e833b7db6bd
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,1339 @@@
++/*
++ * test_urcu_ja.c
++ *
++ * Userspace RCU library - test program
++ *
++ * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
++ *
++ * 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
++ * the Free Software Foundation; either version 2 of the License, or
++ * (at your option) any later version.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++ * GNU General Public License for more details.
++ *
++ * You should have received a copy of the GNU General Public License along
++ * with this program; if not, write to the Free Software Foundation, Inc.,
++ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
++ */
++
++#define _GNU_SOURCE
++#include "test_urcu_ja.h"
++#include <inttypes.h>
++#include <stdint.h>
++
++DEFINE_URCU_TLS(unsigned int, rand_lookup);
++DEFINE_URCU_TLS(unsigned long, nr_add);
++DEFINE_URCU_TLS(unsigned long, nr_addexist);
++DEFINE_URCU_TLS(unsigned long, nr_del);
++DEFINE_URCU_TLS(unsigned long, nr_delnoent);
++DEFINE_URCU_TLS(unsigned long, lookup_fail);
++DEFINE_URCU_TLS(unsigned long, lookup_ok);
++
++struct cds_ja *test_ja;
++
++volatile int test_go, test_stop;
++
++unsigned long wdelay;
++
++unsigned long duration;
++
++/* read-side C.S. duration, in loops */
++unsigned long rduration;
++
++unsigned long init_populate;
++int add_only;
++
++unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
++unsigned long init_pool_size = DEFAULT_RAND_POOL,
++      lookup_pool_size = DEFAULT_RAND_POOL,
++      write_pool_size = DEFAULT_RAND_POOL;
++int validate_lookup;
++int sanity_test;
++unsigned int key_bits = 32;
++
++int count_pipe[2];
++
++int verbose_mode;
++
++unsigned int cpu_affinities[NR_CPUS];
++unsigned int next_aff = 0;
++int use_affinity = 0;
++
++pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
++
++DEFINE_URCU_TLS(unsigned long long, nr_writes);
++DEFINE_URCU_TLS(unsigned long long, nr_reads);
++
++unsigned int nr_readers;
++unsigned int nr_writers;
++
++static unsigned int add_ratio = 50;
++static uint64_t key_mul = 1ULL;
++
++static int add_unique, add_replace;
++
++static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
++
++static int leak_detection;
++static unsigned long test_nodes_allocated, test_nodes_freed;
++
++void set_affinity(void)
++{
++      cpu_set_t mask;
++      int cpu;
++      int ret;
++
++      if (!use_affinity)
++              return;
++
++#if HAVE_SCHED_SETAFFINITY
++      ret = pthread_mutex_lock(&affinity_mutex);
++      if (ret) {
++              perror("Error in pthread mutex lock");
++              exit(-1);
++      }
++      cpu = cpu_affinities[next_aff++];
++      ret = pthread_mutex_unlock(&affinity_mutex);
++      if (ret) {
++              perror("Error in pthread mutex unlock");
++              exit(-1);
++      }
++      CPU_ZERO(&mask);
++      CPU_SET(cpu, &mask);
++#if SCHED_SETAFFINITY_ARGS == 2
++      sched_setaffinity(0, &mask);
++#else
++        sched_setaffinity(0, sizeof(mask), &mask);
++#endif
++#endif /* HAVE_SCHED_SETAFFINITY */
++}
++
++void rcu_copy_mutex_lock(void)
++{
++      int ret;
++      ret = pthread_mutex_lock(&rcu_copy_mutex);
++      if (ret) {
++              perror("Error in pthread mutex lock");
++              exit(-1);
++      }
++}
++
++void rcu_copy_mutex_unlock(void)
++{
++      int ret;
++
++      ret = pthread_mutex_unlock(&rcu_copy_mutex);
++      if (ret) {
++              perror("Error in pthread mutex unlock");
++              exit(-1);
++      }
++}
++
++static
++struct ja_test_node *node_alloc(void)
++{
++      struct ja_test_node *node;
++
++      node = calloc(sizeof(*node), 1);
++      if (leak_detection && node)
++              uatomic_inc(&test_nodes_allocated);
++      return node;
++}
++
++static
++void free_test_node(struct ja_test_node *node)
++{
++      poison_free(node);
++      if (leak_detection)
++              uatomic_inc(&test_nodes_freed);
++}
++
++static
++void free_test_node_cb(struct rcu_head *head)
++{
++      struct ja_test_node *node =
++              caa_container_of(head, struct ja_test_node, head);
++      free_test_node(node);
++}
++
++static
++void rcu_free_test_node(struct ja_test_node *test_node)
++{
++      call_rcu(&test_node->head, free_test_node_cb);
++}
++
++static
++void free_node(struct cds_ja_node *node)
++{
++      struct ja_test_node *test_node = to_test_node(node);
++
++      free_test_node(test_node);
++}
++
++#if 0
++static
++void test_delete_all_nodes(struct cds_lfht *ht)
++{
++      struct cds_lfht_iter iter;
++      struct lfht_test_node *node;
++      unsigned long count = 0;
++
++      cds_lfht_for_each_entry(ht, &iter, node, node) {
++              int ret;
++
++              ret = cds_lfht_del(test_ht, cds_lfht_iter_get_node(&iter));
++              assert(!ret);
++              call_rcu(&node->head, free_node_cb);
++              count++;
++      }
++      printf("deleted %lu nodes.\n", count);
++}
++#endif
++
++void show_usage(int argc, char **argv)
++{
++      printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]);
++#ifdef DEBUG_YIELD
++      printf("        [-r] [-w] (yield reader and/or writer)\n");
++#endif
++      printf("        [-d delay] (writer period (us))\n");
++      printf("        [-c duration] (reader C.S. duration (in loops))\n");
++      printf("        [-v] (verbose output)\n");
++      printf("        [-a cpu#] [-a cpu#]... (affinity)\n");
++      printf("        [-u] Add unique keys.\n");
++      printf("        [-s] Replace existing keys.\n");
++printf("        [not -u nor -s] Add entries (supports redundant keys).\n");
++      printf("        [-r ratio] Add ratio (in %% of add+removal).\n");
++      printf("        [-k] Populate init nodes.\n");
++      printf("        [-R offset] Lookup pool offset.\n");
++      printf("        [-S offset] Write pool offset.\n");
++      printf("        [-T offset] Init pool offset.\n");
++      printf("        [-M size] Lookup pool size.\n");
++      printf("        [-N size] Write pool size.\n");
++      printf("        [-O size] Init pool size.\n");
++      printf("        [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
++      printf("        [-t] Do sanity test.\n");
++      printf("        [-B] Key bits for multithread test (default: 32).\n");
++      printf("        [-m factor] Key multiplication factor.\n");
++      printf("        [-l] Memory leak detection.\n");
++      printf("\n\n");
++}
++
++static
++int test_free_all_nodes(struct cds_ja *ja)
++{
++      uint64_t key;
++      struct cds_ja_node *ja_node;
++      int ret = 0;
++
++      rcu_read_lock();
++      cds_ja_for_each_key_rcu(test_ja, key, ja_node) {
++              struct cds_ja_node *tmp_node;
++
++              cds_ja_for_each_duplicate_safe_rcu(ja_node, tmp_node) {
++                      ret = cds_ja_del(test_ja, key, ja_node);
++                      if (ret) {
++                              fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key);
++                              goto end;
++                      }
++                      /* Alone using Judy array, OK to free now */
++                      free_node(ja_node);
++              }
++      }
++end:
++      rcu_read_unlock();
++      return ret;
++}
++
++static
++int test_8bit_key(void)
++{
++      int ret, i;
++      uint64_t key;
++      uint64_t ka[] = { 5, 17, 100, 222 };
++      uint64_t ka_test_offset = 5;
++      struct cds_ja_node *ja_node;
++
++      /* Test with 8-bit key */
++      test_ja = cds_ja_new(8);
++      if (!test_ja) {
++              printf("Error allocating judy array.\n");
++              return -1;
++      }
++
++      /* Add keys */
++      printf("Test #1: add keys (8-bit).\n");
++      for (key = 0; key < 200; key++) {
++              struct ja_test_node *node = node_alloc();
++
++              ja_test_node_init(node, key);
++              rcu_read_lock();
++              ret = cds_ja_add(test_ja, key, &node->node);
++              rcu_read_unlock();
++              if (ret) {
++                      fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
++                              ret, key);
++                      assert(0);
++              }
++      }
++      printf("OK\n");
++
++      printf("Test #2: successful key lookup (8-bit).\n");
++      for (key = 0; key < 200; key++) {
++              rcu_read_lock();
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++      printf("OK\n");
++      printf("Test #3: unsuccessful key lookup (8-bit).\n");
++      for (key = 200; key < 240; key++) {
++              rcu_read_lock();
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (ja_node) {
++                      fprintf(stderr,
++                              "Error unexpected lookup node %" PRIu64 "\n",
++                              key);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++      printf("OK\n");
++      printf("Test #4: remove keys (8-bit).\n");
++      for (key = 0; key < 200; key++) {
++              struct ja_test_node *node;
++
++              rcu_read_lock();
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
++                      assert(0);
++              }
++              node = caa_container_of(ja_node, struct ja_test_node, node);
++              ret = cds_ja_del(test_ja, key, &node->node);
++              if (ret) {
++                      fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key);
++                      assert(0);
++              }
++              rcu_free_test_node(node);
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (ja_node) {
++                      fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, ja_node);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++      printf("OK\n");
++
++      printf("Test #5: lookup below/above equal (8-bit).\n");
++
++      for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) {
++              struct ja_test_node *node = node_alloc();
++
++              key = ka[i];
++              ja_test_node_init(node, key);
++              rcu_read_lock();
++              ret = cds_ja_add(test_ja, key, &node->node);
++              rcu_read_unlock();
++              if (ret) {
++                      fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
++                              ret, key);
++                      assert(0);
++              }
++      }
++
++      for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) {
++              struct ja_test_node *node;
++              uint64_t result_key;
++
++              key = ka[i] + ka_test_offset;
++              rcu_read_lock();
++              ja_node = cds_ja_lookup_below_equal(test_ja, key, &result_key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup below equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n",
++                              ka[i], key);
++                      assert(0);
++              }
++              node = caa_container_of(ja_node, struct ja_test_node, node);
++              if (node->key != ka[i] || result_key != ka[i]) {
++                      fprintf(stderr, "Error lookup below equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n",
++                              ka[i], key, node->key, result_key);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++
++      for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) {
++              struct ja_test_node *node;
++              uint64_t result_key;
++
++              key = ka[i] - ka_test_offset;
++              rcu_read_lock();
++              ja_node = cds_ja_lookup_above_equal(test_ja, key, &result_key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup above equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n",
++                              ka[i], key);
++                      assert(0);
++              }
++              node = caa_container_of(ja_node, struct ja_test_node, node);
++              if (node->key != ka[i] || result_key != ka[i]) {
++                      fprintf(stderr, "Error lookup above equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n",
++                              ka[i], key, node->key, result_key);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++
++      for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) {
++              struct ja_test_node *node;
++              uint64_t result_key;
++
++              key = ka[i];    /* without offset */
++              rcu_read_lock();
++              ja_node = cds_ja_lookup_below_equal(test_ja, key, &result_key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup below equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n",
++                              ka[i], key);
++                      assert(0);
++              }
++              node = caa_container_of(ja_node, struct ja_test_node, node);
++              if (node->key != ka[i] || result_key != ka[i]) {
++                      fprintf(stderr, "Error lookup below equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n",
++                              ka[i], key, node->key, result_key);
++                      assert(0);
++              }
++
++              ja_node = cds_ja_lookup_above_equal(test_ja, key, &result_key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup above equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n",
++                              ka[i], key);
++                      assert(0);
++              }
++              node = caa_container_of(ja_node, struct ja_test_node, node);
++              if (node->key != ka[i] || result_key != ka[i]) {
++                      fprintf(stderr, "Error lookup above equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n",
++                              ka[i], key, node->key, result_key);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++
++      printf("OK\n");
++
++      ret = test_free_all_nodes(test_ja);
++      if (ret) {
++              fprintf(stderr, "Error freeing all nodes\n");
++              return -1;
++      }
++
++      ret = cds_ja_destroy(test_ja);
++      if (ret) {
++              fprintf(stderr, "Error destroying judy array\n");
++              return -1;
++      }
++      return 0;
++}
++
++static
++int test_16bit_key(void)
++{
++      int ret, i;
++      uint64_t key;
++      uint64_t ka[] = { 105, 206, 4000, 4111, 59990, 65435 };
++      uint64_t ka_test_offset = 100;
++      struct cds_ja_node *ja_node;
++
++      /* Test with 16-bit key */
++      test_ja = cds_ja_new(16);
++      if (!test_ja) {
++              printf("Error allocating judy array.\n");
++              return -1;
++      }
++
++      /* Add keys */
++      printf("Test #1: add keys (16-bit).\n");
++      for (key = 0; key < 10000; key++) {
++      //for (key = 0; key < 65536; key+=256) {
++              struct ja_test_node *node = node_alloc();
++
++              ja_test_node_init(node, key);
++              rcu_read_lock();
++              ret = cds_ja_add(test_ja, key, &node->node);
++              rcu_read_unlock();
++              if (ret) {
++                      fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
++                              ret, key);
++                      assert(0);
++              }
++      }
++      printf("OK\n");
++
++      printf("Test #2: successful key lookup (16-bit).\n");
++      for (key = 0; key < 10000; key++) {
++      //for (key = 0; key < 65536; key+=256) {
++              struct cds_ja_node *ja_node;
++
++              rcu_read_lock();
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++      printf("OK\n");
++      printf("Test #3: unsuccessful key lookup (16-bit).\n");
++      for (key = 11000; key <= 11002; key++) {
++              struct cds_ja_node *ja_node;
++
++              rcu_read_lock();
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (ja_node) {
++                      fprintf(stderr,
++                              "Error unexpected lookup node %" PRIu64 "\n",
++                              key);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++      printf("OK\n");
++      printf("Test #4: remove keys (16-bit).\n");
++      for (key = 0; key < 10000; key++) {
++      //for (key = 0; key < 65536; key+=256) {
++              struct ja_test_node *node;
++
++              rcu_read_lock();
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
++                      assert(0);
++              }
++              node = caa_container_of(ja_node, struct ja_test_node, node);
++              ret = cds_ja_del(test_ja, key, &node->node);
++              if (ret) {
++                      fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key);
++                      assert(0);
++              }
++              rcu_free_test_node(node);
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (ja_node) {
++                      fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, ja_node);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++      printf("OK\n");
++
++      printf("Test #5: lookup below/above equal (16-bit).\n");
++
++      for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) {
++              struct ja_test_node *node = node_alloc();
++
++              key = ka[i];
++              ja_test_node_init(node, key);
++              rcu_read_lock();
++              ret = cds_ja_add(test_ja, key, &node->node);
++              rcu_read_unlock();
++              if (ret) {
++                      fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
++                              ret, key);
++                      assert(0);
++              }
++      }
++
++      for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) {
++              struct ja_test_node *node;
++              uint64_t result_key;
++
++              key = ka[i] + ka_test_offset;
++              rcu_read_lock();
++              ja_node = cds_ja_lookup_below_equal(test_ja, key, &result_key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup below equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n",
++                              ka[i], key);
++                      assert(0);
++              }
++              node = caa_container_of(ja_node, struct ja_test_node, node);
++              if (node->key != ka[i] || result_key != ka[i]) {
++                      fprintf(stderr, "Error lookup below equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n",
++                              ka[i], key, node->key, result_key);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++
++      for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) {
++              struct ja_test_node *node;
++              uint64_t result_key;
++
++              key = ka[i] - ka_test_offset;
++              rcu_read_lock();
++              ja_node = cds_ja_lookup_above_equal(test_ja, key, &result_key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup above equal. Cannot find expected key %" PRIu64" above or equal to %" PRIu64 ".\n",
++                              ka[i], key);
++                      assert(0);
++              }
++              node = caa_container_of(ja_node, struct ja_test_node, node);
++              if (node->key != ka[i] || result_key != ka[i]) {
++                      fprintf(stderr, "Error lookup above equal. Expecting key %" PRIu64 " above or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n",
++                              ka[i], key, node->key, result_key);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++
++      for (i = 0; i < CAA_ARRAY_SIZE(ka); i++) {
++              struct ja_test_node *node;
++              uint64_t result_key;
++
++              key = ka[i];    /* without offset */
++              rcu_read_lock();
++              ja_node = cds_ja_lookup_below_equal(test_ja, key, &result_key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup below equal. Cannot find expected key %" PRIu64" below or equal to %" PRIu64 ".\n",
++                              ka[i], key);
++                      assert(0);
++              }
++              node = caa_container_of(ja_node, struct ja_test_node, node);
++              if (node->key != ka[i] || result_key != ka[i]) {
++                      fprintf(stderr, "Error lookup below equal. Expecting key %" PRIu64 " below or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n",
++                              ka[i], key, node->key, result_key);
++                      assert(0);
++              }
++
++              ja_node = cds_ja_lookup_above_equal(test_ja, key, &result_key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup above equal. Cannot find expected key %" PRIu64" above or equal to %" PRIu64 ".\n",
++                              ka[i], key);
++                      assert(0);
++              }
++              node = caa_container_of(ja_node, struct ja_test_node, node);
++              if (node->key != ka[i] || result_key != ka[i]) {
++                      fprintf(stderr, "Error lookup above equal. Expecting key %" PRIu64 " above or equal to %" PRIu64 ", but found %" PRIu64 "/%" PRIu64" instead.\n",
++                              ka[i], key, node->key, result_key);
++                      assert(0);
++              }
++              rcu_read_unlock();
++      }
++
++      printf("OK\n");
++
++      ret = test_free_all_nodes(test_ja);
++      if (ret) {
++              fprintf(stderr, "Error freeing all nodes\n");
++              return -1;
++      }
++
++      ret = cds_ja_destroy(test_ja);
++      if (ret) {
++              fprintf(stderr, "Error destroying judy array\n");
++              return -1;
++      }
++      return 0;
++}
++
++/*
++ * nr_dup is number of nodes per key.
++ */
++static
++int test_sparse_key(unsigned int bits, int nr_dup)
++{
++      uint64_t key, max_key;
++      int zerocount, i, ret;
++      struct cds_ja_node *ja_node;
++
++      if (bits == 64)
++              max_key = UINT64_MAX;
++      else
++              max_key = (1ULL << bits) - 1;
++
++      printf("Sparse key test begins for %u-bit keys\n", bits);
++      /* Test with 16-bit key */
++      test_ja = cds_ja_new(bits);
++      if (!test_ja) {
++              printf("Error allocating judy array.\n");
++              return -1;
++      }
++
++      /* Add keys */
++      printf("Test #1: add keys (%u-bit).\n", bits);
++      for (i = 0; i < nr_dup; i++) {
++              zerocount = 0;
++              for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
++                      struct ja_test_node *node = node_alloc();
++
++                      ja_test_node_init(node, key);
++                      rcu_read_lock();
++                      ret = cds_ja_add(test_ja, key, &node->node);
++                      rcu_read_unlock();
++                      if (ret) {
++                              fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
++                                      ret, key);
++                              assert(0);
++                      }
++                      if (key == 0)
++                              zerocount++;
++              }
++      }
++      printf("OK\n");
++
++      printf("Test #2: successful key lookup (%u-bit).\n", bits);
++      zerocount = 0;
++      for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
++              struct ja_test_node *node;
++              int count = 0;
++
++              rcu_read_lock();
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (!ja_node) {
++                      fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
++                      assert(0);
++              }
++              cds_ja_for_each_duplicate_rcu(ja_node) {
++                      count++;
++              }
++              if (count != nr_dup) {
++                      fprintf(stderr, "Unexpected number of match for key %" PRIu64 ", expected %d, got %d.\n", key, nr_dup, count);
++              }
++              rcu_read_unlock();
++              if (key == 0)
++                      zerocount++;
++      }
++      printf("OK\n");
++      if (bits > 8) {
++              printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits);
++              zerocount = 0;
++              for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
++                      rcu_read_lock();
++                      ja_node = cds_ja_lookup(test_ja, key + 42);
++                      if (ja_node) {
++                              fprintf(stderr,
++                                      "Error unexpected lookup node %" PRIu64 "\n",
++                                      key + 42);
++                              assert(0);
++                      }
++                      rcu_read_unlock();
++                      if (key == 0)
++                              zerocount++;
++              }
++              printf("OK\n");
++      }
++      printf("Test #4: remove keys (%u-bit).\n", bits);
++      zerocount = 0;
++      for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
++              int count = 0;
++
++              rcu_read_lock();
++              ja_node = cds_ja_lookup(test_ja, key);
++
++              cds_ja_for_each_duplicate_rcu(ja_node) {
++                      struct cds_ja_node *test_ja_node;
++                      struct ja_test_node *node;
++
++                      count++;
++                      node = caa_container_of(ja_node,
++                              struct ja_test_node, node);
++                      ret = cds_ja_del(test_ja, key, &node->node);
++                      if (ret) {
++                              fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key);
++                              assert(0);
++                      }
++                      rcu_free_test_node(node);
++                      test_ja_node = cds_ja_lookup(test_ja, key);
++                      if (count < nr_dup && !test_ja_node) {
++                              fprintf(stderr, "Error: no node found after deletion of some nodes of a key\n");
++                              assert(0);
++                      }
++              }
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (ja_node) {
++                      fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, ja_node);
++                      assert(0);
++              }
++              rcu_read_unlock();
++              if (key == 0)
++                      zerocount++;
++      }
++      printf("OK\n");
++
++      ret = test_free_all_nodes(test_ja);
++      if (ret) {
++              fprintf(stderr, "Error freeing all nodes\n");
++              return -1;
++      }
++
++      ret = cds_ja_destroy(test_ja);
++      if (ret) {
++              fprintf(stderr, "Error destroying judy array\n");
++              return -1;
++      }
++      printf("Test ends\n");
++
++      return 0;
++}
++
++static
++int do_sanity_test(void)
++{
++      int i, j, ret;
++
++      printf("Sanity test start.\n");
++
++      for (i = 0; i < 3; i++) {
++              ret = test_8bit_key();
++              if (ret) {
++                      return ret;
++              }
++              rcu_quiescent_state();
++      }
++      ret = test_16bit_key();
++      if (ret) {
++              return ret;
++      }
++      rcu_quiescent_state();
++
++      /* key bits */
++      for (i = 8; i <= 64; i *= 2) {
++              /* nr of nodes per key */
++              for (j = 1; j < 4; j++) {
++                      ret = test_sparse_key(i, j);
++                      if (ret) {
++                              return ret;
++                      }
++                      rcu_quiescent_state();
++              }
++      }
++      printf("Sanity test end.\n");
++
++      return 0;
++}
++
++enum urcu_ja_addremove {
++      AR_RANDOM = 0,
++      AR_ADD = 1,
++      AR_REMOVE = -1,
++};    /* 1: add, -1 remove, 0: random */
++
++static enum urcu_ja_addremove addremove; /* 1: add, -1 remove, 0: random */
++
++static
++void test_ja_rw_sigusr1_handler(int signo)
++{
++      switch (addremove) {
++      case AR_ADD:
++              printf("Add/Remove: random.\n");
++              addremove = AR_RANDOM;
++              break;
++      case AR_RANDOM:
++              printf("Add/Remove: remove only.\n");
++              addremove = AR_REMOVE;
++              break;
++      case AR_REMOVE:
++              printf("Add/Remove: add only.\n");
++              addremove = AR_ADD;
++              break;
++      }
++}
++
++static
++void *test_ja_rw_thr_reader(void *_count)
++{
++      unsigned long long *count = _count;
++      struct cds_ja_node *ja_node;
++      uint64_t key;
++
++      printf_verbose("thread_begin %s, tid %lu\n",
++                      "reader", urcu_get_thread_id());
++
++      URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL);
++
++      set_affinity();
++
++      rcu_register_thread();
++
++      while (!test_go)
++      {
++      }
++      cmm_smp_mb();
++
++      for (;;) {
++              rcu_read_lock();
++
++              /* note: only looking up ulong keys */
++              key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % lookup_pool_size) + lookup_pool_offset;
++              key *= key_mul;
++              ja_node = cds_ja_lookup(test_ja, key);
++              if (!ja_node) {
++                      if (validate_lookup) {
++                              printf("[ERROR] Lookup cannot find initial node.\n");
++                              exit(-1);
++                      }
++                      URCU_TLS(lookup_fail)++;
++              } else {
++                      URCU_TLS(lookup_ok)++;
++              }
++              rcu_debug_yield_read();
++              if (caa_unlikely(rduration))
++                      loop_sleep(rduration);
++              rcu_read_unlock();
++              URCU_TLS(nr_reads)++;
++              if (caa_unlikely(!test_duration_read()))
++                      break;
++              if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
++                      rcu_quiescent_state();
++      }
++
++      rcu_unregister_thread();
++
++      *count = URCU_TLS(nr_reads);
++      printf_verbose("thread_end %s, tid %lu\n",
++                      "reader", urcu_get_thread_id());
++      printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
++                      pthread_self(), URCU_TLS(lookup_fail),
++                      URCU_TLS(lookup_ok));
++      return ((void*)1);
++}
++
++static
++int is_add(void)
++{
++      return ((unsigned int) rand_r(&URCU_TLS(rand_lookup)) % 100) < add_ratio;
++}
++
++static
++void *test_ja_rw_thr_writer(void *_count)
++{
++      struct wr_count *count = _count;
++      uint64_t key;
++      int ret;
++
++      printf_verbose("thread_begin %s, tid %lu\n",
++                      "writer", urcu_get_thread_id());
++
++      URCU_TLS(rand_lookup) = urcu_get_thread_id() ^ time(NULL);
++
++      set_affinity();
++
++      rcu_register_thread();
++
++      while (!test_go)
++      {
++      }
++      cmm_smp_mb();
++
++      for (;;) {
++              if ((addremove == AR_ADD)
++                              || (addremove == AR_RANDOM && is_add())) {
++                      struct ja_test_node *node = node_alloc();
++                      struct cds_ja_node *ret_node;
++
++                      /* note: only inserting ulong keys */
++                      key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset;
++                      key *= key_mul;
++                      ja_test_node_init(node, key);
++                      rcu_read_lock();
++                      if (add_unique) {
++                              ret_node = cds_ja_add_unique(test_ja, key, &node->node);
++                              if (ret_node != &node->node) {
++                                      free_test_node(node);
++                                      URCU_TLS(nr_addexist)++;
++                              } else {
++                                      URCU_TLS(nr_add)++;
++                              }
++                      } else if (add_replace) {
++                              assert(0);      /* not implemented yet. */
++                      } else {
++                              ret = cds_ja_add(test_ja, key, &node->node);
++                              if (ret) {
++                                      fprintf(stderr, "Error in cds_ja_add: %d\n", ret);
++                                      free_test_node(node);
++                              } else {
++                                      URCU_TLS(nr_add)++;
++                              }
++                      }
++                      rcu_read_unlock();
++              } else {
++                      struct cds_ja_node *ja_node;
++                      struct ja_test_node *node;
++
++                      /* May delete */
++                      /* note: only deleting ulong keys */
++                      key = ((unsigned long) rand_r(&URCU_TLS(rand_lookup)) % write_pool_size) + write_pool_offset;
++                      key *= key_mul;
++
++                      rcu_read_lock();
++
++                      ja_node = cds_ja_lookup(test_ja, key);
++                      /* Remove first entry */
++                      if (ja_node) {
++                              node = caa_container_of(ja_node,
++                                      struct ja_test_node, node);
++                              ret = cds_ja_del(test_ja, key, &node->node);
++                              if (!ret) {
++                                      rcu_free_test_node(node);
++                                      URCU_TLS(nr_del)++;
++                              } else {
++                                      URCU_TLS(nr_delnoent)++;
++                              }
++                      } else {
++                              URCU_TLS(nr_delnoent)++;
++                      }
++                      rcu_read_unlock();
++              }
++
++              URCU_TLS(nr_writes)++;
++              if (caa_unlikely(!test_duration_write()))
++                      break;
++              if (caa_unlikely(wdelay))
++                      loop_sleep(wdelay);
++              if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0))
++                      rcu_quiescent_state();
++      }
++
++      rcu_unregister_thread();
++
++      printf_verbose("thread_end %s, tid %lu\n",
++                      "writer", urcu_get_thread_id());
++      printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
++                      "nr_delnoent %lu\n", pthread_self(), URCU_TLS(nr_add),
++                      URCU_TLS(nr_addexist), URCU_TLS(nr_del),
++                      URCU_TLS(nr_delnoent));
++      count->update_ops = URCU_TLS(nr_writes);
++      count->add = URCU_TLS(nr_add);
++      count->add_exist = URCU_TLS(nr_addexist);
++      count->remove = URCU_TLS(nr_del);
++      return ((void*)2);
++}
++
++static
++int do_mt_populate_ja(void)
++{
++      uint64_t iter;
++      int ret;
++
++      if (!init_populate)
++              return 0;
++
++      printf("Starting rw test\n");
++
++      for (iter = init_pool_offset; iter < init_pool_offset + init_pool_size; iter++) {
++              struct ja_test_node *node = node_alloc();
++              uint64_t key;
++
++              /* note: only inserting ulong keys */
++              key = (unsigned long) iter;
++              key *= key_mul;
++              ja_test_node_init(node, key);
++              rcu_read_lock();
++              ret = cds_ja_add(test_ja, key, &node->node);
++              URCU_TLS(nr_add)++;
++              URCU_TLS(nr_writes)++;
++              rcu_read_unlock();
++              /* Hash table resize only occurs in call_rcu thread */
++              if (!(iter % 100))
++                      rcu_quiescent_state();
++              if (ret) {
++                      fprintf(stderr, "Error (%d) adding node %" PRIu64 "\n",
++                              ret, key);
++                      assert(0);
++              }
++      }
++      return 0;
++}
++
++static
++int do_mt_test(void)
++{
++      pthread_t *tid_reader, *tid_writer;
++      void *tret;
++      int ret, i, err;
++      unsigned long long *count_reader;
++      struct wr_count *count_writer;
++      unsigned long long tot_reads = 0, tot_writes = 0,
++              tot_add = 0, tot_add_exist = 0, tot_remove = 0;
++      unsigned int remain;
++
++      tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
++      tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
++      count_reader = malloc(sizeof(*count_reader) * nr_readers);
++      count_writer = malloc(sizeof(*count_writer) * nr_writers);
++
++      printf("Allocating Judy Array for %u-bit keys\n", key_bits);
++      test_ja = cds_ja_new(key_bits);
++      if (!test_ja) {
++              printf("Error allocating judy array.\n");
++              ret = -1;
++              goto end;
++      }
++
++      do_mt_populate_ja();
++
++      next_aff = 0;
++
++      for (i = 0; i < nr_readers; i++) {
++              err = pthread_create(&tid_reader[i],
++                                   NULL, test_ja_rw_thr_reader,
++                                   &count_reader[i]);
++              if (err != 0)
++                      exit(1);
++      }
++      for (i = 0; i < nr_writers; i++) {
++              err = pthread_create(&tid_writer[i],
++                                   NULL, test_ja_rw_thr_writer,
++                                   &count_writer[i]);
++              if (err != 0)
++                      exit(1);
++      }
++
++      cmm_smp_mb();
++
++      test_go = 1;
++
++      rcu_thread_offline_qsbr();
++
++      remain = duration;
++      do {
++              remain = sleep(remain);
++      } while (remain > 0);
++
++      test_stop = 1;
++
++      for (i = 0; i < nr_readers; i++) {
++              err = pthread_join(tid_reader[i], &tret);
++              if (err != 0)
++                      exit(1);
++              tot_reads += count_reader[i];
++      }
++      for (i = 0; i < nr_writers; i++) {
++              err = pthread_join(tid_writer[i], &tret);
++              if (err != 0)
++                      exit(1);
++              tot_writes += count_writer[i].update_ops;
++              tot_add += count_writer[i].add;
++              tot_add_exist += count_writer[i].add_exist;
++              tot_remove += count_writer[i].remove;
++      }
++      rcu_thread_online_qsbr();
++
++      ret = test_free_all_nodes(test_ja);
++      if (ret) {
++              fprintf(stderr, "Error freeing all nodes\n");
++              return -1;
++      }
++
++      ret = cds_ja_destroy(test_ja);
++      if (ret) {
++              fprintf(stderr, "Error destroying judy array\n");
++              goto end;
++      }
++
++      free(tid_reader);
++      free(tid_writer);
++      free(count_reader);
++      free(count_writer);
++      ret = 0;
++end:
++      return ret;
++}
++
++static
++int check_memory_leaks(void)
++{
++      unsigned long na, nf;
++
++      na = uatomic_read(&test_nodes_allocated);
++      nf = uatomic_read(&test_nodes_freed);
++      if (na != nf) {
++              fprintf(stderr, "Memory leak of %ld test nodes detected. Allocated: %lu, freed: %lu\n",
++                      na - nf, na, nf);
++              return -1;
++      }
++      return 0;
++}
++
++int main(int argc, char **argv)
++{
++      int i, j, a, ret, err;
++      uint64_t key;
++      struct sigaction act;
++
++      if (argc < 4) {
++              show_usage(argc, argv);
++              return -1;
++      }
++
++      err = sscanf(argv[1], "%u", &nr_readers);
++      if (err != 1) {
++              show_usage(argc, argv);
++              return -1;
++      }
++
++      err = sscanf(argv[2], "%u", &nr_writers);
++      if (err != 1) {
++              show_usage(argc, argv);
++              return -1;
++      }
++      
++      err = sscanf(argv[3], "%lu", &duration);
++      if (err != 1) {
++              show_usage(argc, argv);
++              return -1;
++      }
++
++      for (i = 4; i < argc; i++) {
++              if (argv[i][0] != '-')
++                      continue;
++              switch (argv[i][1]) {
++#ifdef DEBUG_YIELD
++              case 'r':
++                      yield_active |= YIELD_READ;
++                      break;
++              case 'w':
++                      yield_active |= YIELD_WRITE;
++                      break;
++#endif
++              case 'a':
++                      if (argc < i + 2) {
++                              show_usage(argc, argv);
++                              return -1;
++                      }
++                      a = atoi(argv[++i]);
++                      cpu_affinities[next_aff++] = a;
++                      use_affinity = 1;
++                      printf_verbose("Adding CPU %d affinity\n", a);
++                      break;
++              case 'c':
++                      if (argc < i + 2) {
++                              show_usage(argc, argv);
++                              return -1;
++                      }
++                      rduration = atol(argv[++i]);
++                      break;
++              case 'd':
++                      if (argc < i + 2) {
++                              show_usage(argc, argv);
++                              return -1;
++                      }
++                      wdelay = atol(argv[++i]);
++                      break;
++              case 'v':
++                      verbose_mode = 1;
++                      break;
++              case 'r':
++                      add_ratio = atoi(argv[++i]);
++                      break;
++              case 'k':
++                      init_populate = 1;
++                      break;
++              case 'R':
++                      lookup_pool_offset = atol(argv[++i]);
++                      break;
++              case 'S':
++                      write_pool_offset = atol(argv[++i]);
++                      break;
++              case 'T':
++                      init_pool_offset = atol(argv[++i]);
++                      break;
++              case 'M':
++                      lookup_pool_size = atol(argv[++i]);
++                      break;
++              case 'N':
++                      write_pool_size = atol(argv[++i]);
++                      break;
++              case 'O':
++                      init_pool_size = atol(argv[++i]);
++                      break;
++              case 'V':
++                      validate_lookup = 1;
++                      break;
++              case 't':
++                      sanity_test = 1;
++                      break;
++              case 'B':
++                      key_bits = atol(argv[++i]);
++                      break;
++              case 'm':
++                      key_mul = atoll(argv[++i]);
++                      break;
++              case 'u':
++                      add_unique = 1;
++                      break;
++              case 's':
++                      add_replace = 1;
++                      break;
++              case 'l':
++                      leak_detection = 1;
++                      break;
++              }
++      }
++
++      printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
++              duration, nr_readers, nr_writers);
++      printf_verbose("Writer delay : %lu loops.\n", wdelay);
++      printf_verbose("Reader duration : %lu loops.\n", rduration);
++      printf_verbose("Add ratio: %u%%.\n", add_ratio);
++      printf_verbose("Mode:%s%s.\n",
++              " add/remove",
++              add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
++      printf_verbose("Key multiplication factor: %" PRIu64 ".\n", key_mul);
++      printf_verbose("Init pool size offset %lu size %lu.\n",
++              init_pool_offset, init_pool_size);
++      printf_verbose("Lookup pool size offset %lu size %lu.\n",
++              lookup_pool_offset, lookup_pool_size);
++      printf_verbose("Update pool size offset %lu size %lu.\n",
++              write_pool_offset, write_pool_size);
++      if (validate_lookup)
++              printf_verbose("Validating lookups.\n");
++      if (leak_detection)
++              printf_verbose("Memory leak dection activated.\n");
++      printf_verbose("thread %-6s, tid %lu\n",
++                      "main", urcu_get_thread_id());
++
++      memset(&act, 0, sizeof(act));
++      ret = sigemptyset(&act.sa_mask);
++      if (ret == -1) {
++              perror("sigemptyset");
++              return -1;
++      }
++      act.sa_handler = test_ja_rw_sigusr1_handler;
++      act.sa_flags = SA_RESTART;
++      ret = sigaction(SIGUSR1, &act, NULL);
++      if (ret == -1) {
++              perror("sigaction");
++              return -1;
++      }
++
++      err = create_all_cpu_call_rcu_data(0);
++      if (err) {
++              printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
++      }
++
++      rcu_register_thread();
++
++      if (sanity_test) {
++              ret = do_sanity_test();
++      } else {
++              ret = do_mt_test();
++      }
++
++      /* Wait for in-flight call_rcu free to complete for leak detection */
++      rcu_barrier();
++
++      ret |= check_memory_leaks();
++
++      rcu_unregister_thread();
++      free_all_cpu_call_rcu_data();
++
++      if (ret) {
++              printf("Test ended with error: %d\n", ret);
++      }
++      return ret;
++}
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..1dab42910e06c345152e9d966913bec23b7c4d49
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,176 @@@
++#ifndef _TEST_URCU_JA_H
++#define _TEST_URCU_JA_H
++
++/*
++ * test_urcu_ja.h
++ *
++ * Userspace RCU library - test program
++ *
++ * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
++ *
++ * 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
++ * the Free Software Foundation; either version 2 of the License, or
++ * (at your option) any later version.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++ * GNU General Public License for more details.
++ *
++ * You should have received a copy of the GNU General Public License along
++ * with this program; if not, write to the Free Software Foundation, Inc.,
++ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
++ */
++
++#include "../config.h"
++#include <stdio.h>
++#include <pthread.h>
++#include <stdlib.h>
++#include <string.h>
++#include <sys/types.h>
++#include <sys/wait.h>
++#include <unistd.h>
++#include <stdio.h>
++#include <assert.h>
++#include <sched.h>
++#include <errno.h>
++#include <signal.h>
++
++#include <urcu/tls-compat.h>
++#include "thread-id.h"
++
++#define DEFAULT_RAND_POOL     1000000
++
++/* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
++#define CACHE_LINE_SIZE 4096
++
++/* hardcoded number of CPUs */
++#define NR_CPUS 16384
++
++#ifdef POISON_FREE
++#define poison_free(ptr)                              \
++      do {                                            \
++              memset(ptr, 0x42, sizeof(*(ptr)));      \
++              free(ptr);                              \
++      } while (0)
++#else
++#define poison_free(ptr)      free(ptr)
++#endif
++
++#ifndef DYNAMIC_LINK_TEST
++#define _LGPL_SOURCE
++#else
++#define debug_yield_read()
++#endif
++#include <urcu-qsbr.h>
++#include <urcu/rcuja.h>
++#include <urcu-call-rcu.h>
++
++struct wr_count {
++      unsigned long update_ops;
++      unsigned long add;
++      unsigned long add_exist;
++      unsigned long remove;
++};
++
++extern DECLARE_URCU_TLS(unsigned int, rand_lookup);
++extern DECLARE_URCU_TLS(unsigned long, nr_add);
++extern DECLARE_URCU_TLS(unsigned long, nr_addexist);
++extern DECLARE_URCU_TLS(unsigned long, nr_del);
++extern DECLARE_URCU_TLS(unsigned long, nr_delnoent);
++extern DECLARE_URCU_TLS(unsigned long, lookup_fail);
++extern DECLARE_URCU_TLS(unsigned long, lookup_ok);
++
++extern struct cds_ja *test_ja;
++
++struct ja_test_node {
++      struct cds_ja_node node;
++      uint64_t key;           /* for testing */
++      struct rcu_head head;   /* delayed reclaim */
++};
++
++static inline struct ja_test_node *
++to_test_node(struct cds_ja_node *node)
++{
++      return caa_container_of(node, struct ja_test_node, node);
++}
++
++static inline
++void ja_test_node_init(struct ja_test_node *node, uint64_t key)
++{
++      cds_ja_node_init(&node->node);
++      node->key = key;
++}
++
++extern volatile int test_go, test_stop;
++
++extern unsigned long wdelay;
++
++extern unsigned long duration;
++
++/* read-side C.S. duration, in loops */
++extern unsigned long rduration;
++
++extern unsigned long init_populate;
++extern int add_only;
++
++extern unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
++extern unsigned long init_pool_size,
++      lookup_pool_size,
++      write_pool_size;
++extern int validate_lookup;
++
++extern int count_pipe[2];
++
++static inline void loop_sleep(unsigned long l)
++{
++      while(l-- != 0)
++              caa_cpu_relax();
++}
++
++extern int verbose_mode;
++
++#define printf_verbose(fmt, args...)          \
++      do {                                    \
++              if (verbose_mode)               \
++                      printf(fmt, ## args);   \
++      } while (0)
++
++extern unsigned int cpu_affinities[NR_CPUS];
++extern unsigned int next_aff;
++extern int use_affinity;
++
++extern pthread_mutex_t affinity_mutex;
++
++#ifndef HAVE_CPU_SET_T
++typedef unsigned long cpu_set_t;
++# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
++# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
++#endif
++
++void set_affinity(void);
++
++/*
++ * returns 0 if test should end.
++ */
++static inline int test_duration_write(void)
++{
++      return !test_stop;
++}
++
++static inline int test_duration_read(void)
++{
++      return !test_stop;
++}
++
++extern DECLARE_URCU_TLS(unsigned long long, nr_writes);
++extern DECLARE_URCU_TLS(unsigned long long, nr_reads);
++
++extern unsigned int nr_readers;
++extern unsigned int nr_writers;
++
++void rcu_copy_mutex_lock(void);
++void rcu_copy_mutex_unlock(void);
++
++#endif /* _TEST_URCU_JA_H */
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..93ac706c40e7d1b6544d04588f6404471e02a734
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,680 @@@
++/*
++ * test_urcu_ja_range.c
++ *
++ * Userspace RCU library - test program
++ *
++ * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
++ *
++ * 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
++ * the Free Software Foundation; either version 2 of the License, or
++ * (at your option) any later version.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++ * GNU General Public License for more details.
++ *
++ * You should have received a copy of the GNU General Public License along
++ * with this program; if not, write to the Free Software Foundation, Inc.,
++ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
++ */
++
++#define _GNU_SOURCE
++#include "test_urcu_ja_range.h"
++#include <inttypes.h>
++#include <stdint.h>
++
++DEFINE_URCU_TLS(unsigned int, rand_lookup);
++DEFINE_URCU_TLS(unsigned long, nr_add);
++DEFINE_URCU_TLS(unsigned long, nr_addexist);
++DEFINE_URCU_TLS(unsigned long, nr_del);
++DEFINE_URCU_TLS(unsigned long, nr_delnoent);
++DEFINE_URCU_TLS(unsigned long, lookup_fail);
++DEFINE_URCU_TLS(unsigned long, lookup_ok);
++
++struct cds_ja *test_ja;
++
++volatile int test_go, test_stop;
++
++unsigned long wdelay;
++
++unsigned long duration;
++
++/* read-side C.S. duration, in loops */
++unsigned long rduration;
++
++unsigned long init_populate;
++int add_only;
++
++unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
++unsigned long init_pool_size = DEFAULT_RAND_POOL,
++      lookup_pool_size = DEFAULT_RAND_POOL,
++      write_pool_size = DEFAULT_RAND_POOL;
++int validate_lookup;
++int sanity_test;
++unsigned int key_bits = 32;
++
++int count_pipe[2];
++
++int verbose_mode;
++
++unsigned int cpu_affinities[NR_CPUS];
++unsigned int next_aff = 0;
++int use_affinity = 0;
++
++pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
++
++DEFINE_URCU_TLS(unsigned long long, nr_writes);
++DEFINE_URCU_TLS(unsigned long long, nr_reads);
++
++unsigned int nr_readers;
++unsigned int nr_writers;
++
++static unsigned int add_ratio = 50, range_max_len = 0;
++static uint64_t key_mul = 1ULL;
++
++static int add_unique, add_replace;
++
++static pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
++
++static int leak_detection;
++static unsigned long test_nodes_allocated, test_nodes_freed;
++
++void set_affinity(void)
++{
++      cpu_set_t mask;
++      int cpu;
++      int ret;
++
++      if (!use_affinity)
++              return;
++
++#if HAVE_SCHED_SETAFFINITY
++      ret = pthread_mutex_lock(&affinity_mutex);
++      if (ret) {
++              perror("Error in pthread mutex lock");
++              exit(-1);
++      }
++      cpu = cpu_affinities[next_aff++];
++      ret = pthread_mutex_unlock(&affinity_mutex);
++      if (ret) {
++              perror("Error in pthread mutex unlock");
++              exit(-1);
++      }
++      CPU_ZERO(&mask);
++      CPU_SET(cpu, &mask);
++#if SCHED_SETAFFINITY_ARGS == 2
++      sched_setaffinity(0, &mask);
++#else
++        sched_setaffinity(0, sizeof(mask), &mask);
++#endif
++#endif /* HAVE_SCHED_SETAFFINITY */
++}
++
++void rcu_copy_mutex_lock(void)
++{
++      int ret;
++      ret = pthread_mutex_lock(&rcu_copy_mutex);
++      if (ret) {
++              perror("Error in pthread mutex lock");
++              exit(-1);
++      }
++}
++
++void rcu_copy_mutex_unlock(void)
++{
++      int ret;
++
++      ret = pthread_mutex_unlock(&rcu_copy_mutex);
++      if (ret) {
++              perror("Error in pthread mutex unlock");
++              exit(-1);
++      }
++}
++
++void show_usage(int argc, char **argv)
++{
++      printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]);
++#ifdef DEBUG_YIELD
++      printf("        [-r] [-w] (yield reader and/or writer)\n");
++#endif
++      printf("        [-d delay] (writer period (us))\n");
++      printf("        [-c duration] (reader C.S. duration (in loops))\n");
++      printf("        [-v] (verbose output)\n");
++      printf("        [-a cpu#] [-a cpu#]... (affinity)\n");
++      printf("        [-u] Add unique keys.\n");
++      printf("        [-s] Replace existing keys.\n");
++printf("        [not -u nor -s] Add entries (supports redundant keys).\n");
++      printf("        [-r ratio] Add ratio (in %% of add+removal).\n");
++      printf("        [-k] Populate init nodes.\n");
++      printf("        [-R offset] Lookup pool offset.\n");
++      printf("        [-S offset] Write pool offset.\n");
++      printf("        [-T offset] Init pool offset.\n");
++      printf("        [-M size] Lookup pool size.\n");
++      printf("        [-N size] Write pool size.\n");
++      printf("        [-O size] Init pool size.\n");
++      printf("        [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
++      printf("        [-t] Do sanity test.\n");
++      printf("        [-B] Key bits for multithread test (default: 32).\n");
++      printf("        [-m factor] Key multiplication factor.\n");
++      printf("        [-l] Memory leak detection.\n");
++      printf("        [-L len] Range max len.\n");
++      printf("\n\n");
++}
++
++enum urcu_ja_addremove {
++      AR_RANDOM = 0,
++      AR_ADD = 1,
++      AR_REMOVE = -1,
++};    /* 1: add, -1 remove, 0: random */
++
++static enum urcu_ja_addremove addremove; /* 1: add, -1 remove, 0: random */
++
++static
++void test_ja_rw_sigusr1_handler(int signo)
++{
++      switch (addremove) {
++      case AR_ADD:
++              printf("Add/Remove: random.\n");
++              addremove = AR_RANDOM;
++              break;
++      case AR_RANDOM:
++              printf("Add/Remove: remove only.\n");
++              addremove = AR_REMOVE;
++              break;
++      case AR_REMOVE:
++              printf("Add/Remove: add only.\n");
++              addremove = AR_ADD;
++              break;
++      }
++}
++
++static
++void *test_ja_rw_thr_reader(void *_count)
++{
++      unsigned long long *count = _count;
++      struct cds_ja_range *range;
++      uint64_t key;
++
++      printf_verbose("thread_begin %s, tid %lu\n",
++                      "reader", urcu_get_thread_id());
++
++      URCU_TLS(rand_lookup) = (unsigned int) urcu_get_thread_id() ^ time(NULL);
++
++      set_affinity();
++
++      rcu_register_thread();
++
++      while (!test_go)
++      {
++      }
++      cmm_smp_mb();
++
++      for (;;) {
++              rcu_read_lock();
++
++              key = (uint64_t) rand_r(&URCU_TLS(rand_lookup));
++              key += (uint64_t) rand_r(&URCU_TLS(rand_lookup)) << 32ULL;
++              key = (key % lookup_pool_size) + lookup_pool_offset;
++              key *= key_mul;
++
++              range = cds_ja_range_lookup(test_ja, key);
++              if (!range) {
++                      if (validate_lookup) {
++                              printf("[ERROR] Lookup cannot find initial node.\n");
++                              exit(-1);
++                      }
++                      URCU_TLS(lookup_fail)++;
++              } else {
++                      range = cds_ja_range_lock(range);
++                      if (!range) {
++                              if (validate_lookup) {
++                                      printf("[ERROR] Lookup cannot find initial node.\n");
++                                      exit(-1);
++                              }
++                      } else {
++                              URCU_TLS(lookup_ok)++;
++                              cds_ja_range_unlock(range);
++                      }
++              }
++              rcu_debug_yield_read();
++              if (caa_unlikely(rduration))
++                      loop_sleep(rduration);
++              rcu_read_unlock();
++              URCU_TLS(nr_reads)++;
++              if (caa_unlikely(!test_duration_read()))
++                      break;
++              if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
++                      rcu_quiescent_state();
++      }
++
++      rcu_unregister_thread();
++
++      *count = URCU_TLS(nr_reads);
++      printf_verbose("thread_end %s, tid %lu\n",
++                      "reader", urcu_get_thread_id());
++      printf_verbose("read tid : %lu, lookupfail %lu, lookupok %lu\n",
++                      urcu_get_thread_id(), URCU_TLS(lookup_fail),
++                      URCU_TLS(lookup_ok));
++      return ((void*)1);
++}
++
++static
++int is_add(void)
++{
++      return ((unsigned int) rand_r(&URCU_TLS(rand_lookup)) % 100) < add_ratio;
++}
++
++static
++void *test_ja_rw_thr_writer(void *_count)
++{
++      struct wr_count *count = _count;
++      int ret;
++
++      printf_verbose("thread_begin %s, tid %lu\n",
++                      "writer", urcu_get_thread_id());
++
++      URCU_TLS(rand_lookup) = (unsigned int) urcu_get_thread_id() ^ time(NULL);
++
++      set_affinity();
++
++      rcu_register_thread();
++
++      while (!test_go)
++      {
++      }
++      cmm_smp_mb();
++
++      for (;;) {
++              if ((addremove == AR_ADD)
++                              || (addremove == AR_RANDOM && is_add())) {
++                      struct cds_ja_range *range;
++                      uint64_t start, end, tmp;
++
++                      start = (uint64_t) rand_r(&URCU_TLS(rand_lookup));
++                      start += (uint64_t) rand_r(&URCU_TLS(rand_lookup)) << 32ULL;
++                      start = (start % write_pool_size) + write_pool_offset;
++
++                      end = (uint64_t) rand_r(&URCU_TLS(rand_lookup));
++                      end += (uint64_t) rand_r(&URCU_TLS(rand_lookup)) << 32ULL;
++                      end = (end % write_pool_size) + write_pool_offset;
++
++                      start *= key_mul;
++                      end *= key_mul;
++                      if (start > end) {
++                              tmp = start;
++                              start = end;
++                              end = tmp;
++                      }
++                      if (end - start > range_max_len) {
++                              end = start + range_max_len;
++                      }
++                      rcu_read_lock();
++                      ret = cds_ja_range_add(test_ja, start, end, NULL);
++                      if (ret) {
++                              if (ret == -EEXIST) {
++                                      URCU_TLS(nr_addexist)++;
++                              } else {
++                                      assert(0);
++                              }
++                      } else {
++                              URCU_TLS(nr_add)++;
++                      }
++                      rcu_read_unlock();
++              } else {
++                      struct cds_ja_range *range;
++                      uint64_t key;
++
++                      /* May delete */
++                      key = (uint64_t) rand_r(&URCU_TLS(rand_lookup));
++                      key += (uint64_t) rand_r(&URCU_TLS(rand_lookup)) << 32ULL;
++                      key = (key % write_pool_size) + write_pool_offset;
++                      key *= key_mul;
++
++                      rcu_read_lock();
++
++                      range = cds_ja_range_lookup(test_ja, key);
++                      if (range) {
++                              ret = cds_ja_range_del(test_ja, range);
++                              if (!ret) {
++                                      URCU_TLS(nr_del)++;
++                              } else {
++                                      URCU_TLS(nr_delnoent)++;
++                              }
++                      } else {
++                              URCU_TLS(nr_delnoent)++;
++                      }
++                      rcu_read_unlock();
++              }
++
++              URCU_TLS(nr_writes)++;
++              if (caa_unlikely(!test_duration_write()))
++                      break;
++              if (caa_unlikely(wdelay))
++                      loop_sleep(wdelay);
++              if (caa_unlikely((URCU_TLS(nr_writes) & ((1 << 10) - 1)) == 0))
++                      rcu_quiescent_state();
++      }
++
++      rcu_unregister_thread();
++
++      printf_verbose("thread_end %s, tid %lu\n",
++                      "writer", urcu_get_thread_id());
++      printf_verbose("info tid %lu: nr_add %lu, nr_addexist %lu, nr_del %lu, "
++                      "nr_delnoent %lu\n", urcu_get_thread_id(),
++                      URCU_TLS(nr_add),
++                      URCU_TLS(nr_addexist), URCU_TLS(nr_del),
++                      URCU_TLS(nr_delnoent));
++      count->update_ops = URCU_TLS(nr_writes);
++      count->add = URCU_TLS(nr_add);
++      count->add_exist = URCU_TLS(nr_addexist);
++      count->remove = URCU_TLS(nr_del);
++      return ((void*)2);
++}
++
++static
++int do_mt_populate_ja(void)
++{
++      uint64_t iter;
++      int ret;
++
++      if (!init_populate)
++              return 0;
++
++      printf("Starting rw test\n");
++
++      for (iter = init_pool_offset; iter < init_pool_offset + init_pool_size; iter++) {
++              struct cds_ja_range *range;
++              uint64_t key;
++
++              /* note: only inserting ulong keys */
++              key = (unsigned long) iter;
++              key *= key_mul;
++              rcu_read_lock();
++              ret = cds_ja_range_add(test_ja, key, key, NULL);
++              URCU_TLS(nr_add)++;
++              URCU_TLS(nr_writes)++;
++              rcu_read_unlock();
++              /* Hash table resize only occurs in call_rcu thread */
++              if (!(iter % 100))
++                      rcu_quiescent_state();
++              if (ret) {
++                      fprintf(stderr, "Error (%d) adding range %" PRIu64 "\n",
++                              ret, key);
++                      assert(0);
++              }
++      }
++      return 0;
++}
++
++static
++int do_mt_test(void)
++{
++      pthread_t *tid_reader, *tid_writer;
++      void *tret;
++      int ret, i, err;
++      unsigned long long *count_reader;
++      struct wr_count *count_writer;
++      unsigned long long tot_reads = 0, tot_writes = 0,
++              tot_add = 0, tot_add_exist = 0, tot_remove = 0;
++      unsigned int remain;
++
++      tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
++      tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
++      count_reader = malloc(sizeof(*count_reader) * nr_readers);
++      count_writer = malloc(sizeof(*count_writer) * nr_writers);
++
++      printf("Allocating %u-bit Judy Array for ranges\n",
++              key_bits);
++      test_ja = cds_ja_range_new(key_bits);
++      if (!test_ja) {
++              printf("Error allocating judy array.\n");
++              ret = -1;
++              goto end;
++      }
++
++      do_mt_populate_ja();
++
++      next_aff = 0;
++
++      for (i = 0; i < nr_readers; i++) {
++              err = pthread_create(&tid_reader[i],
++                                   NULL, test_ja_rw_thr_reader,
++                                   &count_reader[i]);
++              if (err != 0)
++                      exit(1);
++      }
++      for (i = 0; i < nr_writers; i++) {
++              err = pthread_create(&tid_writer[i],
++                                   NULL, test_ja_rw_thr_writer,
++                                   &count_writer[i]);
++              if (err != 0)
++                      exit(1);
++      }
++
++      cmm_smp_mb();
++
++      test_go = 1;
++
++      rcu_thread_offline_qsbr();
++
++      remain = duration;
++      do {
++              remain = sleep(remain);
++      } while (remain > 0);
++
++      test_stop = 1;
++
++      for (i = 0; i < nr_readers; i++) {
++              err = pthread_join(tid_reader[i], &tret);
++              if (err != 0)
++                      exit(1);
++              tot_reads += count_reader[i];
++      }
++      for (i = 0; i < nr_writers; i++) {
++              err = pthread_join(tid_writer[i], &tret);
++              if (err != 0)
++                      exit(1);
++              tot_writes += count_writer[i].update_ops;
++              tot_add += count_writer[i].add;
++              tot_add_exist += count_writer[i].add_exist;
++              tot_remove += count_writer[i].remove;
++      }
++      rcu_thread_online_qsbr();
++
++      ret = cds_ja_range_validate(test_ja);
++      assert(!ret);
++
++      ret = cds_ja_range_destroy(test_ja, NULL);
++      if (ret) {
++              fprintf(stderr, "Error destroying judy array\n");
++              goto end;
++      }
++
++      free(tid_reader);
++      free(tid_writer);
++      free(count_reader);
++      free(count_writer);
++      ret = 0;
++end:
++      return ret;
++}
++
++int main(int argc, char **argv)
++{
++      int i, j, a, ret, err;
++      uint64_t key;
++      struct sigaction act;
++
++      if (argc < 4) {
++              show_usage(argc, argv);
++              return -1;
++      }
++
++      err = sscanf(argv[1], "%u", &nr_readers);
++      if (err != 1) {
++              show_usage(argc, argv);
++              return -1;
++      }
++
++      err = sscanf(argv[2], "%u", &nr_writers);
++      if (err != 1) {
++              show_usage(argc, argv);
++              return -1;
++      }
++
++      err = sscanf(argv[3], "%lu", &duration);
++      if (err != 1) {
++              show_usage(argc, argv);
++              return -1;
++      }
++
++      for (i = 4; i < argc; i++) {
++              if (argv[i][0] != '-')
++                      continue;
++              switch (argv[i][1]) {
++#ifdef DEBUG_YIELD
++              case 'r':
++                      yield_active |= YIELD_READ;
++                      break;
++              case 'w':
++                      yield_active |= YIELD_WRITE;
++                      break;
++#endif
++              case 'a':
++                      if (argc < i + 2) {
++                              show_usage(argc, argv);
++                              return -1;
++                      }
++                      a = atoi(argv[++i]);
++                      cpu_affinities[next_aff++] = a;
++                      use_affinity = 1;
++                      printf_verbose("Adding CPU %d affinity\n", a);
++                      break;
++              case 'c':
++                      if (argc < i + 2) {
++                              show_usage(argc, argv);
++                              return -1;
++                      }
++                      rduration = atol(argv[++i]);
++                      break;
++              case 'd':
++                      if (argc < i + 2) {
++                              show_usage(argc, argv);
++                              return -1;
++                      }
++                      wdelay = atol(argv[++i]);
++                      break;
++              case 'v':
++                      verbose_mode = 1;
++                      break;
++              case 'r':
++                      add_ratio = atoi(argv[++i]);
++                      break;
++              case 'k':
++                      init_populate = 1;
++                      break;
++              case 'R':
++                      lookup_pool_offset = atol(argv[++i]);
++                      break;
++              case 'S':
++                      write_pool_offset = atol(argv[++i]);
++                      break;
++              case 'T':
++                      init_pool_offset = atol(argv[++i]);
++                      break;
++              case 'M':
++                      lookup_pool_size = atol(argv[++i]);
++                      break;
++              case 'N':
++                      write_pool_size = atol(argv[++i]);
++                      break;
++              case 'O':
++                      init_pool_size = atol(argv[++i]);
++                      break;
++              case 'V':
++                      validate_lookup = 1;
++                      break;
++              case 't':
++                      sanity_test = 1;
++                      break;
++              case 'B':
++                      key_bits = atol(argv[++i]);
++                      break;
++              case 'm':
++                      key_mul = atoll(argv[++i]);
++                      break;
++              case 'u':
++                      add_unique = 1;
++                      break;
++              case 's':
++                      add_replace = 1;
++                      break;
++              case 'l':
++                      leak_detection = 1;
++                      break;
++              case 'L':
++                      range_max_len = atol(argv[++i]);
++                      break;
++              }
++      }
++
++      printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
++              duration, nr_readers, nr_writers);
++      printf_verbose("Writer delay : %lu loops.\n", wdelay);
++      printf_verbose("Reader duration : %lu loops.\n", rduration);
++      printf_verbose("Add ratio: %u%%.\n", add_ratio);
++      printf_verbose("Mode:%s%s.\n",
++              " add/remove",
++              add_unique ? " uniquify" : ( add_replace ? " replace" : " insert"));
++      printf_verbose("Key multiplication factor: %" PRIu64 ".\n", key_mul);
++      printf_verbose("Init pool size offset %lu size %lu.\n",
++              init_pool_offset, init_pool_size);
++      printf_verbose("Lookup pool size offset %lu size %lu.\n",
++              lookup_pool_offset, lookup_pool_size);
++      printf_verbose("Update pool size offset %lu size %lu.\n",
++              write_pool_offset, write_pool_size);
++      printf_verbose("Range max len: %lu.\n",
++              range_max_len);
++      if (validate_lookup)
++              printf_verbose("Validating lookups.\n");
++      if (leak_detection)
++              printf_verbose("Memory leak dection activated.\n");
++      printf_verbose("thread %-6s, tid %lu\n",
++                      "main", urcu_get_thread_id());
++
++      memset(&act, 0, sizeof(act));
++      ret = sigemptyset(&act.sa_mask);
++      if (ret == -1) {
++              perror("sigemptyset");
++              return -1;
++      }
++      act.sa_handler = test_ja_rw_sigusr1_handler;
++      act.sa_flags = SA_RESTART;
++      ret = sigaction(SIGUSR1, &act, NULL);
++      if (ret == -1) {
++              perror("sigaction");
++              return -1;
++      }
++
++      err = create_all_cpu_call_rcu_data(0);
++      if (err) {
++              printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
++      }
++
++      rcu_register_thread();
++
++      ret = do_mt_test();
++
++      /* Wait for in-flight call_rcu free to complete for leak detection */
++      rcu_barrier();
++
++      rcu_unregister_thread();
++      free_all_cpu_call_rcu_data();
++
++      if (ret) {
++              printf("Test ended with error: %d\n", ret);
++      }
++      return ret;
++}
index 0000000000000000000000000000000000000000,0000000000000000000000000000000000000000..1f8c2a2f046d48a5aea3ece45cec14412e53f2f0
new file mode 100644 (file)
--- /dev/null
--- /dev/null
@@@ -1,0 -1,0 +1,157 @@@
++#ifndef _TEST_URCU_JA_RANGE_H
++#define _TEST_URCU_JA_RANGE_H
++
++/*
++ * test_urcu_ja.h
++ *
++ * Userspace RCU library - test program
++ *
++ * Copyright 2009-2012 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
++ *
++ * 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
++ * the Free Software Foundation; either version 2 of the License, or
++ * (at your option) any later version.
++ *
++ * This program is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
++ * GNU General Public License for more details.
++ *
++ * You should have received a copy of the GNU General Public License along
++ * with this program; if not, write to the Free Software Foundation, Inc.,
++ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
++ */
++
++#include "../config.h"
++#include <stdio.h>
++#include <pthread.h>
++#include <stdlib.h>
++#include <string.h>
++#include <sys/types.h>
++#include <sys/wait.h>
++#include <unistd.h>
++#include <stdio.h>
++#include <assert.h>
++#include <sched.h>
++#include <errno.h>
++#include <signal.h>
++
++#include <urcu/tls-compat.h>
++#include "thread-id.h"
++
++#define DEFAULT_RAND_POOL     1000000
++
++/* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
++#define CACHE_LINE_SIZE 4096
++
++/* hardcoded number of CPUs */
++#define NR_CPUS 16384
++
++#ifdef POISON_FREE
++#define poison_free(ptr)                              \
++      do {                                            \
++              memset(ptr, 0x42, sizeof(*(ptr)));      \
++              free(ptr);                              \
++      } while (0)
++#else
++#define poison_free(ptr)      free(ptr)
++#endif
++
++#ifndef DYNAMIC_LINK_TEST
++#define _LGPL_SOURCE
++#else
++#define debug_yield_read()
++#endif
++#include <urcu-qsbr.h>
++#include <urcu/rcuja-range.h>
++#include <urcu-call-rcu.h>
++
++struct wr_count {
++      unsigned long update_ops;
++      unsigned long add;
++      unsigned long add_exist;
++      unsigned long remove;
++};
++
++extern DECLARE_URCU_TLS(unsigned int, rand_lookup);
++extern DECLARE_URCU_TLS(unsigned long, nr_add);
++extern DECLARE_URCU_TLS(unsigned long, nr_addexist);
++extern DECLARE_URCU_TLS(unsigned long, nr_del);
++extern DECLARE_URCU_TLS(unsigned long, nr_delnoent);
++extern DECLARE_URCU_TLS(unsigned long, lookup_fail);
++extern DECLARE_URCU_TLS(unsigned long, lookup_ok);
++
++extern struct cds_ja *test_ja;
++
++extern volatile int test_go, test_stop;
++
++extern unsigned long wdelay;
++
++extern unsigned long duration;
++
++/* read-side C.S. duration, in loops */
++extern unsigned long rduration;
++
++extern unsigned long init_populate;
++extern int add_only;
++
++extern unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset;
++extern unsigned long init_pool_size,
++      lookup_pool_size,
++      write_pool_size;
++extern int validate_lookup;
++
++extern int count_pipe[2];
++
++static inline void loop_sleep(unsigned long l)
++{
++      while(l-- != 0)
++              caa_cpu_relax();
++}
++
++extern int verbose_mode;
++
++#define printf_verbose(fmt, args...)          \
++      do {                                    \
++              if (verbose_mode)               \
++                      printf(fmt, ## args);   \
++      } while (0)
++
++extern unsigned int cpu_affinities[NR_CPUS];
++extern unsigned int next_aff;
++extern int use_affinity;
++
++extern pthread_mutex_t affinity_mutex;
++
++#ifndef HAVE_CPU_SET_T
++typedef unsigned long cpu_set_t;
++# define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
++# define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
++#endif
++
++void set_affinity(void);
++
++/*
++ * returns 0 if test should end.
++ */
++static inline int test_duration_write(void)
++{
++      return !test_stop;
++}
++
++static inline int test_duration_read(void)
++{
++      return !test_stop;
++}
++
++extern DECLARE_URCU_TLS(unsigned long long, nr_writes);
++extern DECLARE_URCU_TLS(unsigned long long, nr_reads);
++
++extern unsigned int nr_readers;
++extern unsigned int nr_writers;
++
++void rcu_copy_mutex_lock(void);
++void rcu_copy_mutex_unlock(void);
++
++#endif /* _TEST_URCU_JA_RANGE_H */
diff --cc urcu/hlist.h
Simple merge
Simple merge
This page took 0.053859 seconds and 4 git commands to generate.