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 */
50 #define poison_free(ptr) \
52 memset(ptr, 0x42, sizeof(*(ptr))); \
56 #define poison_free(ptr) free(ptr)
61 #if defined(_syscall0)
62 _syscall0(pid_t
, gettid
)
63 #elif defined(__NR_gettid)
64 static inline pid_t
gettid(void)
66 return syscall(__NR_gettid
);
69 #warning "use pid as tid"
70 static inline pid_t
gettid(void)
76 #ifndef DYNAMIC_LINK_TEST
79 #define debug_yield_read()
81 #include <urcu-qsbr.h>
82 #include <urcu/rculfhash.h>
83 #include <urcu-call-rcu.h>
86 unsigned long update_ops
;
88 unsigned long add_exist
;
92 static unsigned int __thread rand_lookup
;
93 static unsigned long __thread nr_add
;
94 static unsigned long __thread nr_addexist
;
95 static unsigned long __thread nr_del
;
96 static unsigned long __thread nr_delnoent
;
97 static unsigned long __thread lookup_fail
;
98 static unsigned long __thread lookup_ok
;
100 static struct cds_lfht
*test_ht
;
107 static volatile int test_go
, test_stop
;
109 static unsigned long wdelay
;
111 static unsigned long duration
;
113 /* read-side C.S. duration, in loops */
114 static unsigned long rduration
;
116 static unsigned long init_hash_size
= DEFAULT_HASH_SIZE
;
117 static unsigned long init_populate
;
118 static int opt_auto_resize
;
119 static int add_only
, add_unique
, add_replace
;
121 static unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
122 static unsigned long init_pool_size
= DEFAULT_RAND_POOL
,
123 lookup_pool_size
= DEFAULT_RAND_POOL
,
124 write_pool_size
= DEFAULT_RAND_POOL
;
125 static int validate_lookup
;
127 static inline void loop_sleep(unsigned long l
)
133 static int verbose_mode
;
135 #define printf_verbose(fmt, args...) \
138 printf(fmt, ## args); \
141 static unsigned int cpu_affinities
[NR_CPUS
];
142 static unsigned int next_aff
= 0;
143 static int use_affinity
= 0;
145 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
147 static void set_affinity(void)
156 ret
= pthread_mutex_lock(&affinity_mutex
);
158 perror("Error in pthread mutex lock");
161 cpu
= cpu_affinities
[next_aff
++];
162 ret
= pthread_mutex_unlock(&affinity_mutex
);
164 perror("Error in pthread mutex unlock");
169 sched_setaffinity(0, sizeof(mask
), &mask
);
176 } addremove
; /* 1: add, -1 remove, 0: random */
179 void sigusr1_handler(int signo
)
183 printf("Add/Remove: random.\n");
184 addremove
= AR_RANDOM
;
187 printf("Add/Remove: remove only.\n");
188 addremove
= AR_REMOVE
;
191 printf("Add/Remove: add only.\n");
198 * returns 0 if test should end.
200 static int test_duration_write(void)
205 static int test_duration_read(void)
210 static unsigned long long __thread nr_writes
;
211 static unsigned long long __thread nr_reads
;
213 static unsigned int nr_readers
;
214 static unsigned int nr_writers
;
216 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
218 void rcu_copy_mutex_lock(void)
221 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
223 perror("Error in pthread mutex lock");
228 void rcu_copy_mutex_unlock(void)
232 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
234 perror("Error in pthread mutex unlock");
241 * Source: http://burtleburtle.net/bob/c/lookup3.c
242 * Originally Public Domain
245 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
247 #define mix(a, b, c) \
249 a -= c; a ^= rot(c, 4); c += b; \
250 b -= a; b ^= rot(a, 6); a += c; \
251 c -= b; c ^= rot(b, 8); b += a; \
252 a -= c; a ^= rot(c, 16); c += b; \
253 b -= a; b ^= rot(a, 19); a += c; \
254 c -= b; c ^= rot(b, 4); b += a; \
257 #define final(a, b, c) \
259 c ^= b; c -= rot(b, 14); \
260 a ^= c; a -= rot(c, 11); \
261 b ^= a; b -= rot(a, 25); \
262 c ^= b; c -= rot(b, 16); \
263 a ^= c; a -= rot(c, 4);\
264 b ^= a; b -= rot(a, 14); \
265 c ^= b; c -= rot(b, 24); \
268 static __attribute__((unused
))
270 const uint32_t *k
, /* the key, an array of uint32_t values */
271 size_t length
, /* the length of the key, in uint32_ts */
272 uint32_t initval
) /* the previous hash, or an arbitrary value */
276 /* Set up the internal state */
277 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
279 /*----------------------------------------- handle most of the key */
289 /*----------------------------------- handle the last 3 uint32_t's */
290 switch (length
) { /* all the case statements fall through */
295 case 0: /* case 0: nothing left to add */
298 /*---------------------------------------------- report the result */
304 const uint32_t *k
, /* the key, an array of uint32_t values */
305 size_t length
, /* the length of the key, in uint32_ts */
306 uint32_t *pc
, /* IN: seed OUT: primary hash value */
307 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
311 /* Set up the internal state */
312 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
315 /*----------------------------------------- handle most of the key */
325 /*----------------------------------- handle the last 3 uint32_t's */
326 switch (length
) { /* all the case statements fall through */
331 case 0: /* case 0: nothing left to add */
334 /*---------------------------------------------- report the result */
339 #if (CAA_BITS_PER_LONG == 32)
341 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
343 unsigned long key
= (unsigned long) _key
;
346 assert(length
== sizeof(unsigned long));
347 return hash_u32(&v
, 1, seed
);
351 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
362 assert(length
== sizeof(unsigned long));
363 v
.v64
= (uint64_t) seed
;
364 key
.v64
= (uint64_t) _key
;
365 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
371 unsigned long test_compare(void *key1
, size_t key1_len
,
372 void *key2
, size_t key2_len
)
374 if (unlikely(key1_len
!= key2_len
))
376 assert(key1_len
== sizeof(unsigned long));
383 void *thr_reader(void *_count
)
385 unsigned long long *count
= _count
;
386 struct cds_lfht_node
*node
;
387 struct cds_lfht_iter iter
;
389 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
390 "reader", pthread_self(), (unsigned long)gettid());
394 rcu_register_thread();
403 cds_lfht_lookup(test_ht
,
404 (void *)(((unsigned long) rand_r(&rand_lookup
) % lookup_pool_size
) + lookup_pool_offset
),
405 sizeof(void *), &iter
);
406 node
= cds_lfht_iter_get_node(&iter
);
408 if (validate_lookup
) {
409 printf("[ERROR] Lookup cannot find initial node.\n");
417 if (unlikely(rduration
))
418 loop_sleep(rduration
);
421 if (unlikely(!test_duration_read()))
423 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
424 rcu_quiescent_state();
427 rcu_unregister_thread();
430 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
431 "reader", pthread_self(), (unsigned long)gettid());
432 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
433 pthread_self(), lookup_fail
, lookup_ok
);
439 void free_node_cb(struct rcu_head
*head
)
441 struct cds_lfht_node
*node
=
442 caa_container_of(head
, struct cds_lfht_node
, head
);
446 void *thr_writer(void *_count
)
448 struct cds_lfht_node
*node
, *ret_node
;
449 struct cds_lfht_iter iter
;
450 struct wr_count
*count
= _count
;
453 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
454 "writer", pthread_self(), (unsigned long)gettid());
458 rcu_register_thread();
466 if ((addremove
== AR_ADD
|| add_only
)
467 || (addremove
== AR_RANDOM
&& rand_r(&rand_lookup
) & 1)) {
468 node
= malloc(sizeof(struct cds_lfht_node
));
470 cds_lfht_node_init(node
,
471 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
474 ret_node
= cds_lfht_add_unique(test_ht
, node
);
477 ret_node
= cds_lfht_replace(test_ht
, node
);
479 cds_lfht_add(test_ht
, node
);
482 if (add_unique
&& ret_node
!= node
) {
486 if (add_replace
&& ret_node
) {
487 call_rcu(&ret_node
->head
, free_node_cb
);
496 cds_lfht_lookup(test_ht
,
497 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
498 sizeof(void *), &iter
);
499 node
= cds_lfht_iter_get_node(&iter
);
501 ret
= cds_lfht_del(test_ht
, node
);
506 call_rcu(&node
->head
, free_node_cb
);
512 //if (nr_writes % 100000 == 0) {
513 if (nr_writes
% 1000 == 0) {
515 if (rand_r(&rand_lookup
) & 1) {
516 ht_resize(test_ht
, 1);
518 ht_resize(test_ht
, -1);
524 if (unlikely(!test_duration_write()))
526 if (unlikely(wdelay
))
528 if (unlikely((nr_writes
& ((1 << 10) - 1)) == 0))
529 rcu_quiescent_state();
532 rcu_unregister_thread();
534 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
535 "writer", pthread_self(), (unsigned long)gettid());
536 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
537 "nr_delnoent %lu\n", pthread_self(), nr_add
,
538 nr_addexist
, nr_del
, nr_delnoent
);
539 count
->update_ops
= nr_writes
;
541 count
->add_exist
= nr_addexist
;
542 count
->remove
= nr_del
;
546 static int populate_hash(void)
548 struct cds_lfht_node
*node
, *ret_node
;
553 if ((add_unique
|| add_replace
) && init_populate
* 10 > init_pool_size
) {
554 printf("WARNING: required to populate %lu nodes (-k), but random "
555 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
556 "larger random pool (-p option). This may take a while...\n", init_populate
, init_pool_size
);
559 while (nr_add
< init_populate
) {
560 node
= malloc(sizeof(struct cds_lfht_node
));
561 cds_lfht_node_init(node
,
562 (void *)(((unsigned long) rand_r(&rand_lookup
) % init_pool_size
) + init_pool_offset
),
566 ret_node
= cds_lfht_add_unique(test_ht
, node
);
569 ret_node
= cds_lfht_replace(test_ht
, node
);
571 cds_lfht_add(test_ht
, node
);
574 if (add_unique
&& ret_node
!= node
) {
578 if (add_replace
&& ret_node
) {
579 call_rcu(&ret_node
->head
, free_node_cb
);
590 void show_usage(int argc
, char **argv
)
592 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
594 printf(" [-r] [-w] (yield reader and/or writer)\n");
596 printf(" [-d delay] (writer period (us))\n");
597 printf(" [-c duration] (reader C.S. duration (in loops))\n");
598 printf(" [-v] (verbose output)\n");
599 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
600 printf(" [-h size] (initial hash table size)\n");
601 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
602 printf(" [-u] Uniquify add (no redundant keys).\n");
603 printf(" [-s] Replace (swap) entries.\n");
604 printf(" [-i] Add only (no removal).\n");
605 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
606 printf(" [-A] Automatically resize hash table.\n");
607 printf(" [-R offset] Lookup pool offset.\n");
608 printf(" [-S offset] Write pool offset.\n");
609 printf(" [-T offset] Init pool offset.\n");
610 printf(" [-M size] Lookup pool size.\n");
611 printf(" [-N size] Write pool size.\n");
612 printf(" [-O size] Init pool size.\n");
613 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
617 int main(int argc
, char **argv
)
620 pthread_t
*tid_reader
, *tid_writer
;
622 unsigned long long *count_reader
;
623 struct wr_count
*count_writer
;
624 unsigned long long tot_reads
= 0, tot_writes
= 0,
625 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
626 unsigned long count
, removed
;
628 struct sigaction act
;
632 show_usage(argc
, argv
);
636 err
= sscanf(argv
[1], "%u", &nr_readers
);
638 show_usage(argc
, argv
);
642 err
= sscanf(argv
[2], "%u", &nr_writers
);
644 show_usage(argc
, argv
);
648 err
= sscanf(argv
[3], "%lu", &duration
);
650 show_usage(argc
, argv
);
654 for (i
= 4; i
< argc
; i
++) {
655 if (argv
[i
][0] != '-')
657 switch (argv
[i
][1]) {
660 yield_active
|= YIELD_READ
;
663 yield_active
|= YIELD_WRITE
;
668 show_usage(argc
, argv
);
672 cpu_affinities
[next_aff
++] = a
;
674 printf_verbose("Adding CPU %d affinity\n", a
);
678 show_usage(argc
, argv
);
681 rduration
= atol(argv
[++i
]);
685 show_usage(argc
, argv
);
688 wdelay
= atol(argv
[++i
]);
695 show_usage(argc
, argv
);
698 init_hash_size
= atol(argv
[++i
]);
702 printf("Please specify at most one of -s or -u.\n");
709 printf("Please specify at most one of -s or -u.\n");
718 init_populate
= atol(argv
[++i
]);
724 lookup_pool_offset
= atol(argv
[++i
]);
727 write_pool_offset
= atol(argv
[++i
]);
730 init_pool_offset
= atol(argv
[++i
]);
733 lookup_pool_size
= atol(argv
[++i
]);
736 write_pool_size
= atol(argv
[++i
]);
739 init_pool_size
= atol(argv
[++i
]);
748 /* Check if hash size is power of 2 */
749 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
750 printf("Error: Hash table size %lu is not a power of 2.\n",
755 memset(&act
, 0, sizeof(act
));
756 ret
= sigemptyset(&act
.sa_mask
);
758 perror("sigemptyset");
761 act
.sa_handler
= sigusr1_handler
;
762 act
.sa_flags
= SA_RESTART
;
763 ret
= sigaction(SIGUSR1
, &act
, NULL
);
769 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
770 duration
, nr_readers
, nr_writers
);
771 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
772 printf_verbose("Reader duration : %lu loops.\n", rduration
);
773 printf_verbose("Mode:%s%s.\n",
774 add_only
? " add only" : " add/remove",
775 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
776 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size
);
777 printf_verbose("Init pool size offset %lu size %lu.\n",
778 init_pool_offset
, init_pool_size
);
779 printf_verbose("Lookup pool size offset %lu size %lu.\n",
780 lookup_pool_offset
, lookup_pool_size
);
781 printf_verbose("Update pool size offset %lu size %lu.\n",
782 write_pool_offset
, write_pool_size
);
783 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
784 "main", pthread_self(), (unsigned long)gettid());
786 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
787 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
788 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
789 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
791 err
= create_all_cpu_call_rcu_data(0);
795 * Hash creation and population needs to be seen as a RCU reader
796 * thread from the point of view of resize.
798 rcu_register_thread();
799 test_ht
= cds_lfht_new(test_hash
, test_compare
, 0x42UL
,
801 opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0, NULL
);
802 ret
= populate_hash();
804 rcu_unregister_thread();
808 for (i
= 0; i
< nr_readers
; i
++) {
809 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
814 for (i
= 0; i
< nr_writers
; i
++) {
815 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
827 remain
= sleep(remain
);
828 } while (remain
> 0);
832 for (i
= 0; i
< nr_readers
; i
++) {
833 err
= pthread_join(tid_reader
[i
], &tret
);
836 tot_reads
+= count_reader
[i
];
838 for (i
= 0; i
< nr_writers
; i
++) {
839 err
= pthread_join(tid_writer
[i
], &tret
);
842 tot_writes
+= count_writer
[i
].update_ops
;
843 tot_add
+= count_writer
[i
].add
;
844 tot_add_exist
+= count_writer
[i
].add_exist
;
845 tot_remove
+= count_writer
[i
].remove
;
847 printf("Counting nodes... ");
849 cds_lfht_count_nodes(test_ht
, &count
, &removed
);
851 if (count
|| removed
)
852 printf("WARNING: nodes left in the hash table upon destroy: "
853 "%lu nodes + %lu logically removed.\n", count
, removed
);
854 ret
= cds_lfht_destroy(test_ht
, NULL
);
857 printf_verbose("final delete aborted\n");
859 printf_verbose("final delete success\n");
860 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
862 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
864 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
865 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
866 argv
[0], duration
, nr_readers
, rduration
,
867 nr_writers
, wdelay
, tot_reads
, tot_writes
,
868 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
869 (long long) tot_add
+ init_populate
- tot_remove
- count
);
870 free_all_cpu_call_rcu_data();