4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/types.h>
40 #define DEFAULT_HASH_SIZE 32
41 #define DEFAULT_RAND_POOL 1000000
43 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
44 #define CACHE_LINE_SIZE 4096
46 /* hardcoded number of CPUs */
53 #define poison_free(ptr) \
55 memset(ptr, 0x42, sizeof(*(ptr))); \
59 #define poison_free(ptr) free(ptr)
64 #if defined(_syscall0)
65 _syscall0(pid_t
, gettid
)
66 #elif defined(__NR_gettid)
67 static inline pid_t
gettid(void)
69 return syscall(__NR_gettid
);
72 #warning "use pid as tid"
73 static inline pid_t
gettid(void)
79 #ifndef DYNAMIC_LINK_TEST
82 #define debug_yield_read()
84 #include <urcu-qsbr.h>
85 #include <urcu/rculfhash.h>
86 #include <urcu-call-rcu.h>
89 unsigned long update_ops
;
91 unsigned long add_exist
;
95 static unsigned int __thread rand_lookup
;
96 static unsigned long __thread nr_add
;
97 static unsigned long __thread nr_addexist
;
98 static unsigned long __thread nr_del
;
99 static unsigned long __thread nr_delnoent
;
100 static unsigned long __thread lookup_fail
;
101 static unsigned long __thread lookup_ok
;
103 static struct cds_lfht
*test_ht
;
110 static volatile int test_go
, test_stop
;
112 static unsigned long wdelay
;
114 static unsigned long duration
;
116 /* read-side C.S. duration, in loops */
117 static unsigned long rduration
;
119 static unsigned long init_hash_size
= DEFAULT_HASH_SIZE
;
120 static unsigned long init_populate
;
121 static unsigned long rand_pool
= DEFAULT_RAND_POOL
;
122 static int opt_auto_resize
;
123 static int add_only
, add_unique
;
125 static inline void loop_sleep(unsigned long l
)
131 static int verbose_mode
;
133 #define printf_verbose(fmt, args...) \
136 printf(fmt, ## args); \
139 static unsigned int cpu_affinities
[NR_CPUS
];
140 static unsigned int next_aff
= 0;
141 static int use_affinity
= 0;
143 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
145 static void set_affinity(void)
154 ret
= pthread_mutex_lock(&affinity_mutex
);
156 perror("Error in pthread mutex lock");
159 cpu
= cpu_affinities
[next_aff
++];
160 ret
= pthread_mutex_unlock(&affinity_mutex
);
162 perror("Error in pthread mutex unlock");
167 sched_setaffinity(0, sizeof(mask
), &mask
);
174 } addremove
; /* 1: add, -1 remove, 0: random */
177 void sigusr1_handler(int signo
)
181 printf("Add/Remove: random.\n");
182 addremove
= AR_RANDOM
;
185 printf("Add/Remove: remove only.\n");
186 addremove
= AR_REMOVE
;
189 printf("Add/Remove: add only.\n");
196 * returns 0 if test should end.
198 static int test_duration_write(void)
203 static int test_duration_read(void)
208 static unsigned long long __thread nr_writes
;
209 static unsigned long long __thread nr_reads
;
211 static unsigned int nr_readers
;
212 static unsigned int nr_writers
;
214 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
216 void rcu_copy_mutex_lock(void)
219 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
221 perror("Error in pthread mutex lock");
226 void rcu_copy_mutex_unlock(void)
230 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
232 perror("Error in pthread mutex unlock");
239 * Source: http://burtleburtle.net/bob/c/lookup3.c
240 * Originally Public Domain
243 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
245 #define mix(a, b, c) \
247 a -= c; a ^= rot(c, 4); c += b; \
248 b -= a; b ^= rot(a, 6); a += c; \
249 c -= b; c ^= rot(b, 8); b += a; \
250 a -= c; a ^= rot(c, 16); c += b; \
251 b -= a; b ^= rot(a, 19); a += c; \
252 c -= b; c ^= rot(b, 4); b += a; \
255 #define final(a, b, c) \
257 c ^= b; c -= rot(b, 14); \
258 a ^= c; a -= rot(c, 11); \
259 b ^= a; b -= rot(a, 25); \
260 c ^= b; c -= rot(b, 16); \
261 a ^= c; a -= rot(c, 4);\
262 b ^= a; b -= rot(a, 14); \
263 c ^= b; c -= rot(b, 24); \
266 static __attribute__((unused
))
268 const uint32_t *k
, /* the key, an array of uint32_t values */
269 size_t length
, /* the length of the key, in uint32_ts */
270 uint32_t initval
) /* the previous hash, or an arbitrary value */
274 /* Set up the internal state */
275 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
277 /*----------------------------------------- handle most of the key */
287 /*----------------------------------- handle the last 3 uint32_t's */
288 switch (length
) { /* all the case statements fall through */
293 case 0: /* case 0: nothing left to add */
296 /*---------------------------------------------- report the result */
302 const uint32_t *k
, /* the key, an array of uint32_t values */
303 size_t length
, /* the length of the key, in uint32_ts */
304 uint32_t *pc
, /* IN: seed OUT: primary hash value */
305 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
309 /* Set up the internal state */
310 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
313 /*----------------------------------------- handle most of the key */
323 /*----------------------------------- handle the last 3 uint32_t's */
324 switch (length
) { /* all the case statements fall through */
329 case 0: /* case 0: nothing left to add */
332 /*---------------------------------------------- report the result */
337 #if (CAA_BITS_PER_LONG == 32)
339 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
341 unsigned long key
= (unsigned long) _key
;
344 assert(length
== sizeof(unsigned long));
345 return hash_u32(&v
, 1, seed
);
349 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
360 assert(length
== sizeof(unsigned long));
361 v
.v64
= (uint64_t) seed
;
362 key
.v64
= (uint64_t) _key
;
363 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
369 unsigned long test_compare(void *key1
, size_t key1_len
,
370 void *key2
, size_t key2_len
)
372 if (unlikely(key1_len
!= key2_len
))
374 assert(key1_len
== sizeof(unsigned long));
381 void *thr_reader(void *_count
)
383 unsigned long long *count
= _count
;
384 struct cds_lfht_node
*node
;
386 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
387 "reader", pthread_self(), (unsigned long)gettid());
391 rcu_register_thread();
400 node
= cds_lfht_lookup(test_ht
,
401 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
408 if (unlikely(rduration
))
409 loop_sleep(rduration
);
412 if (unlikely(!test_duration_read()))
414 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
415 rcu_quiescent_state();
418 rcu_unregister_thread();
421 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
422 "reader", pthread_self(), (unsigned long)gettid());
423 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
424 pthread_self(), lookup_fail
, lookup_ok
);
430 void free_node_cb(struct rcu_head
*head
)
432 struct cds_lfht_node
*node
=
433 caa_container_of(head
, struct cds_lfht_node
, head
);
437 void *thr_writer(void *_count
)
439 struct cds_lfht_node
*node
, *ret_node
;
440 struct wr_count
*count
= _count
;
443 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
444 "writer", pthread_self(), (unsigned long)gettid());
448 rcu_register_thread();
456 if ((addremove
== AR_ADD
|| add_only
)
457 || (addremove
== AR_RANDOM
&& rand_r(&rand_lookup
) & 1)) {
458 node
= malloc(sizeof(struct cds_lfht_node
));
460 cds_lfht_node_init(node
,
461 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
464 ret_node
= cds_lfht_add_unique(test_ht
, node
);
466 cds_lfht_add(test_ht
, node
);
468 if (add_unique
&& ret_node
!= node
) {
476 node
= cds_lfht_lookup(test_ht
,
477 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
480 ret
= cds_lfht_remove(test_ht
, node
);
485 call_rcu(&node
->head
, free_node_cb
);
491 //if (nr_writes % 100000 == 0) {
492 if (nr_writes
% 1000 == 0) {
494 if (rand_r(&rand_lookup
) & 1) {
495 ht_resize(test_ht
, 1);
497 ht_resize(test_ht
, -1);
503 if (unlikely(!test_duration_write()))
505 if (unlikely(wdelay
))
507 if (unlikely((nr_writes
& ((1 << 10) - 1)) == 0))
508 rcu_quiescent_state();
511 rcu_unregister_thread();
513 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
514 "writer", pthread_self(), (unsigned long)gettid());
515 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
516 "nr_delnoent %lu\n", pthread_self(), nr_add
,
517 nr_addexist
, nr_del
, nr_delnoent
);
518 count
->update_ops
= nr_writes
;
520 count
->add_exist
= nr_addexist
;
521 count
->remove
= nr_del
;
525 static int populate_hash(void)
527 struct cds_lfht_node
*node
, *ret_node
;
532 if (add_unique
&& init_populate
* 10 > rand_pool
) {
533 printf("WARNING: required to populate %lu nodes (-k), but random "
534 "pool is quite small (%lu values) and we are in add_unique (-u) mode. Try with a "
535 "larger random pool (-p option).\n", init_populate
, rand_pool
);
539 while (nr_add
< init_populate
) {
540 node
= malloc(sizeof(struct cds_lfht_node
));
541 cds_lfht_node_init(node
,
542 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
545 ret_node
= cds_lfht_add_unique(test_ht
, node
);
547 cds_lfht_add(test_ht
, node
);
548 if (add_unique
&& ret_node
!= node
) {
558 void show_usage(int argc
, char **argv
)
560 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
562 printf(" [-r] [-w] (yield reader and/or writer)");
564 printf(" [-d delay] (writer period (us))");
565 printf(" [-c duration] (reader C.S. duration (in loops))");
566 printf(" [-v] (verbose output)");
567 printf(" [-a cpu#] [-a cpu#]... (affinity)");
568 printf(" [-p size] (random key value pool size)");
569 printf(" [-h size] (initial hash table size)");
570 printf(" [-u] Uniquify add.");
571 printf(" [-i] Add only (no removal).");
572 printf(" [-k nr_nodes] Number of nodes to insert initially.");
573 printf(" [-A] Automatically resize hash table.");
577 int main(int argc
, char **argv
)
580 pthread_t
*tid_reader
, *tid_writer
;
582 unsigned long long *count_reader
;
583 struct wr_count
*count_writer
;
584 unsigned long long tot_reads
= 0, tot_writes
= 0,
585 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
586 unsigned long count
, removed
;
588 struct sigaction act
;
592 show_usage(argc
, argv
);
596 err
= sscanf(argv
[1], "%u", &nr_readers
);
598 show_usage(argc
, argv
);
602 err
= sscanf(argv
[2], "%u", &nr_writers
);
604 show_usage(argc
, argv
);
608 err
= sscanf(argv
[3], "%lu", &duration
);
610 show_usage(argc
, argv
);
614 for (i
= 4; i
< argc
; i
++) {
615 if (argv
[i
][0] != '-')
617 switch (argv
[i
][1]) {
620 yield_active
|= YIELD_READ
;
623 yield_active
|= YIELD_WRITE
;
628 show_usage(argc
, argv
);
632 cpu_affinities
[next_aff
++] = a
;
634 printf_verbose("Adding CPU %d affinity\n", a
);
638 show_usage(argc
, argv
);
641 rduration
= atol(argv
[++i
]);
645 show_usage(argc
, argv
);
648 wdelay
= atol(argv
[++i
]);
655 show_usage(argc
, argv
);
658 rand_pool
= atol(argv
[++i
]);
662 show_usage(argc
, argv
);
665 init_hash_size
= atol(argv
[++i
]);
674 init_populate
= atol(argv
[++i
]);
682 /* Check if hash size is power of 2 */
683 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
684 printf("Error: Hash table size %lu is not a power of 2.\n",
689 memset(&act
, 0, sizeof(act
));
690 ret
= sigemptyset(&act
.sa_mask
);
692 perror("sigemptyset");
695 act
.sa_handler
= sigusr1_handler
;
696 act
.sa_flags
= SA_RESTART
;
697 ret
= sigaction(SIGUSR1
, &act
, NULL
);
703 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
704 duration
, nr_readers
, nr_writers
);
705 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
706 printf_verbose("Reader duration : %lu loops.\n", rduration
);
707 printf_verbose("Random pool size : %lu.\n", rand_pool
);
708 printf_verbose("Mode:%s%s.\n",
709 add_only
? " add only" : " add/remove",
710 add_unique
? " uniquify" : "");
711 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size
);
712 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
713 "main", pthread_self(), (unsigned long)gettid());
715 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
716 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
717 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
718 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
719 test_ht
= cds_lfht_new(test_hash
, test_compare
, 0x42UL
,
721 opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0,
722 call_rcu
, synchronize_rcu
);
723 ret
= populate_hash();
725 err
= create_all_cpu_call_rcu_data(0);
730 for (i
= 0; i
< nr_readers
; i
++) {
731 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
736 for (i
= 0; i
< nr_writers
; i
++) {
737 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
749 remain
= sleep(remain
);
750 } while (remain
> 0);
754 for (i
= 0; i
< nr_readers
; i
++) {
755 err
= pthread_join(tid_reader
[i
], &tret
);
758 tot_reads
+= count_reader
[i
];
760 for (i
= 0; i
< nr_writers
; i
++) {
761 err
= pthread_join(tid_writer
[i
], &tret
);
764 tot_writes
+= count_writer
[i
].update_ops
;
765 tot_add
+= count_writer
[i
].add
;
766 tot_add_exist
+= count_writer
[i
].add_exist
;
767 tot_remove
+= count_writer
[i
].remove
;
769 printf("Counting nodes... ");
771 cds_lfht_count_nodes(test_ht
, &count
, &removed
);
773 if (count
|| removed
)
774 printf("WARNING: nodes left in the hash table upon destroy: "
775 "%lu nodes + %lu logically removed.\n", count
, removed
);
776 ret
= cds_lfht_destroy(test_ht
);
779 printf_verbose("final delete aborted\n");
781 printf_verbose("final delete success\n");
782 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
784 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
786 "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
787 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
788 argv
[0], duration
, nr_readers
, rduration
,
789 nr_writers
, wdelay
, rand_pool
, tot_reads
, tot_writes
,
790 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
791 (long long) tot_add
+ init_populate
- tot_remove
- count
);