rculfhash: fix node approx counting
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 21 Sep 2011 13:48:05 +0000 (09:48 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 21 Sep 2011 13:48:05 +0000 (09:48 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
rculfhash.c
tests/test_urcu_hash.c

index 93c2d7850fad9da9c333b47f738af08758ed62ed..7e959fb4cd8228b161cbb7b4516ff36c4224270b 100644 (file)
@@ -1455,7 +1455,7 @@ void cds_lfht_count_nodes(struct cds_lfht *ht,
        struct _cds_lfht_node *lookup;
        unsigned long nr_dummy = 0;
 
-       *approx_before = uatomic_read(&ht->count);
+       *approx_before = 0;
        if (nr_cpus_mask >= 0) {
                int i;
 
@@ -1485,7 +1485,7 @@ void cds_lfht_count_nodes(struct cds_lfht *ht,
                node = clear_flag(next);
        } while (!is_end(node));
        dbg_printf("number of dummy nodes: %lu\n", nr_dummy);
-       *approx_after = uatomic_read(&ht->count);
+       *approx_after = 0;
        if (nr_cpus_mask >= 0) {
                int i;
 
index 1e1cf47423942dbac3279c12e777dfc3270a7d52..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)
@@ -197,21 +199,8 @@ void sigusr1_handler(int signo)
 static
 void sigusr2_handler(int signo)
 {
-       unsigned long count, removed, approx_before, approx_after;
-
-       /* Accounting */
-       printf("Counting nodes... ");
-       fflush(stdout);
-       cds_lfht_count_nodes(test_ht, &approx_before, &count, &removed,
-                       &approx_after);
-       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);
+       char msg[1] = { 0x42 };
+       write(count_pipe[1], msg, 1);   /* wakeup thread */
 }
 
 /*
@@ -400,6 +389,45 @@ 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;
@@ -638,6 +666,7 @@ 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;
@@ -785,6 +814,19 @@ int main(int argc, char **argv)
                perror("sigaction");
                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);
@@ -871,6 +913,23 @@ 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, &approx_before, &count, &removed,
This page took 0.028239 seconds and 4 git commands to generate.