From: Mathieu Desnoyers Date: Wed, 21 Sep 2011 13:48:05 +0000 (-0400) Subject: rculfhash: fix node approx counting X-Git-Tag: v0.7.0~43^2~132 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=7ed7682ffb933c7376830bb73b7f5701e566a559;p=userspace-rcu.git rculfhash: fix node approx counting Signed-off-by: Mathieu Desnoyers --- diff --git a/rculfhash.c b/rculfhash.c index 93c2d78..7e959fb 100644 --- a/rculfhash.c +++ b/rculfhash.c @@ -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; diff --git a/tests/test_urcu_hash.c b/tests/test_urcu_hash.c index 1e1cf47..d2dd488 100644 --- a/tests/test_urcu_hash.c +++ b/tests/test_urcu_hash.c @@ -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,