rculfhash: fix node approx counting
[urcu.git] / tests / test_urcu_hash.c
index 89063b98626042262341fdcc27e5b6ee28c0277c..d2dd4889f70d45361a6a88c84efea516f13cece8 100644 (file)
@@ -124,6 +124,8 @@ static unsigned long init_pool_size = DEFAULT_RAND_POOL,
        write_pool_size = DEFAULT_RAND_POOL;
 static int validate_lookup;
 
+static int count_pipe[2];
+
 static inline void loop_sleep(unsigned long l)
 {
        while(l-- != 0)
@@ -194,6 +196,13 @@ void sigusr1_handler(int signo)
        }
 }
 
+static
+void sigusr2_handler(int signo)
+{
+       char msg[1] = { 0x42 };
+       write(count_pipe[1], msg, 1);   /* wakeup thread */
+}
+
 /*
  * returns 0 if test should end.
  */
@@ -380,10 +389,50 @@ unsigned long test_compare(void *key1, size_t key1_len,
                return 1;
 }
 
+void *thr_count(void *arg)
+{
+       printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
+                       "counter", pthread_self(), (unsigned long)gettid());
+
+       rcu_register_thread();
+
+       for (;;) {
+               unsigned long count, removed, approx_before, approx_after;
+               ssize_t len;
+               char buf[1];
+
+               rcu_thread_offline();
+               len = read(count_pipe[0], buf, 1);
+               rcu_thread_online();
+               if (unlikely(!test_duration_read()))
+                       break;
+               if (len != 1)
+                       continue;
+               /* Accounting */
+               printf("Counting nodes... ");
+               fflush(stdout);
+               rcu_read_lock();
+               cds_lfht_count_nodes(test_ht, &approx_before, &count, &removed,
+                               &approx_after);
+               rcu_read_unlock();
+               printf("done.\n");
+               printf("Approximation before node accounting: %lu nodes.\n",
+                       approx_before);
+               printf("Accounting of nodes in the hash table: "
+                       "%lu nodes + %lu logically removed.\n",
+                       count, removed);
+               printf("Approximation after node accounting: %lu nodes.\n",
+                       approx_after);
+       }
+       rcu_unregister_thread();
+       return NULL;
+}
+
 void *thr_reader(void *_count)
 {
        unsigned long long *count = _count;
        struct cds_lfht_node *node;
+       struct cds_lfht_iter iter;
 
        printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
                        "reader", pthread_self(), (unsigned long)gettid());
@@ -399,9 +448,10 @@ void *thr_reader(void *_count)
 
        for (;;) {
                rcu_read_lock();
-               node = cds_lfht_lookup(test_ht,
+               cds_lfht_lookup(test_ht,
                        (void *)(((unsigned long) rand_r(&rand_lookup) % lookup_pool_size) + lookup_pool_offset),
-                       sizeof(void *));
+                       sizeof(void *), &iter);
+               node = cds_lfht_iter_get_node(&iter);
                if (node == NULL) {
                        if (validate_lookup) {
                                printf("[ERROR] Lookup cannot find initial node.\n");
@@ -444,6 +494,7 @@ void free_node_cb(struct rcu_head *head)
 void *thr_writer(void *_count)
 {
        struct cds_lfht_node *node, *ret_node;
+       struct cds_lfht_iter iter;
        struct wr_count *count = _count;
        int ret;
 
@@ -490,9 +541,10 @@ void *thr_writer(void *_count)
                } else {
                        /* May delete */
                        rcu_read_lock();
-                       node = cds_lfht_lookup(test_ht,
+                       cds_lfht_lookup(test_ht,
                                (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset),
-                               sizeof(void *));
+                               sizeof(void *), &iter);
+                       node = cds_lfht_iter_get_node(&iter);
                        if (node)
                                ret = cds_lfht_del(test_ht, node);
                        else
@@ -614,12 +666,13 @@ int main(int argc, char **argv)
 {
        int err;
        pthread_t *tid_reader, *tid_writer;
+       pthread_t tid_count;
        void *tret;
        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 long count, removed;
+       unsigned long count, removed, approx_before, approx_after;
        int i, a, ret;
        struct sigaction act;
        unsigned int remain;
@@ -762,6 +815,26 @@ int main(int argc, char **argv)
                return -1;
        }
 
+       ret = pipe(count_pipe);
+       if (ret == -1) {
+               perror("pipe");
+               return -1;
+       }
+
+       /* spawn counter thread */
+       err = pthread_create(&tid_count, NULL, thr_count,
+                            NULL);
+       if (err != 0)
+               exit(1);
+
+       act.sa_handler = sigusr2_handler;
+       act.sa_flags = SA_RESTART;
+       ret = sigaction(SIGUSR2, &act, NULL);
+       if (ret == -1) {
+               perror("sigaction");
+               return -1;
+       }
+
        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);
@@ -840,13 +913,37 @@ int main(int argc, char **argv)
                tot_add_exist += count_writer[i].add_exist;
                tot_remove += count_writer[i].remove;
        }
+
+       /* teardown counter thread */
+       act.sa_handler = SIG_IGN;
+       act.sa_flags = SA_RESTART;
+       ret = sigaction(SIGUSR2, &act, NULL);
+       if (ret == -1) {
+               perror("sigaction");
+               return -1;
+       }
+       {
+               char msg[1] = { 0x42 };
+               write(count_pipe[1], msg, 1);   /* wakeup thread */
+       }
+       err = pthread_join(tid_count, &tret);
+       if (err != 0)
+               exit(1);
+
        printf("Counting nodes... ");
        fflush(stdout);
-       cds_lfht_count_nodes(test_ht, &count, &removed);
+       cds_lfht_count_nodes(test_ht, &approx_before, &count, &removed,
+               &approx_after);
        printf("done.\n");
-       if (count || removed)
+       if (count || removed) {
+               printf("Approximation before node accounting: %lu nodes.\n",
+                       approx_before);
                printf("WARNING: nodes left in the hash table upon destroy: "
-                       "%lu nodes + %lu logically removed.\n", count, removed);
+                       "%lu nodes + %lu logically removed.\n",
+                       count, removed);
+               printf("Approximation after node accounting: %lu nodes.\n",
+                       approx_after);
+       }
        ret = cds_lfht_destroy(test_ht, NULL);
 
        if (ret)
This page took 0.026463 seconds and 4 git commands to generate.