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 void sigusr2_handler(int signo
)
200 unsigned long count
, removed
, approx_before
, approx_after
;
203 printf("Counting nodes... ");
205 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &removed
,
208 printf("Approximation before node accounting: %lu nodes.\n",
210 printf("Accounting of nodes in the hash table: "
211 "%lu nodes + %lu logically removed.\n",
213 printf("Approximation after node accounting: %lu nodes.\n",
218 * returns 0 if test should end.
220 static int test_duration_write(void)
225 static int test_duration_read(void)
230 static unsigned long long __thread nr_writes
;
231 static unsigned long long __thread nr_reads
;
233 static unsigned int nr_readers
;
234 static unsigned int nr_writers
;
236 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
238 void rcu_copy_mutex_lock(void)
241 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
243 perror("Error in pthread mutex lock");
248 void rcu_copy_mutex_unlock(void)
252 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
254 perror("Error in pthread mutex unlock");
261 * Source: http://burtleburtle.net/bob/c/lookup3.c
262 * Originally Public Domain
265 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
267 #define mix(a, b, c) \
269 a -= c; a ^= rot(c, 4); c += b; \
270 b -= a; b ^= rot(a, 6); a += c; \
271 c -= b; c ^= rot(b, 8); b += a; \
272 a -= c; a ^= rot(c, 16); c += b; \
273 b -= a; b ^= rot(a, 19); a += c; \
274 c -= b; c ^= rot(b, 4); b += a; \
277 #define final(a, b, c) \
279 c ^= b; c -= rot(b, 14); \
280 a ^= c; a -= rot(c, 11); \
281 b ^= a; b -= rot(a, 25); \
282 c ^= b; c -= rot(b, 16); \
283 a ^= c; a -= rot(c, 4);\
284 b ^= a; b -= rot(a, 14); \
285 c ^= b; c -= rot(b, 24); \
288 static __attribute__((unused
))
290 const uint32_t *k
, /* the key, an array of uint32_t values */
291 size_t length
, /* the length of the key, in uint32_ts */
292 uint32_t initval
) /* the previous hash, or an arbitrary value */
296 /* Set up the internal state */
297 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
299 /*----------------------------------------- handle most of the key */
309 /*----------------------------------- handle the last 3 uint32_t's */
310 switch (length
) { /* all the case statements fall through */
315 case 0: /* case 0: nothing left to add */
318 /*---------------------------------------------- report the result */
324 const uint32_t *k
, /* the key, an array of uint32_t values */
325 size_t length
, /* the length of the key, in uint32_ts */
326 uint32_t *pc
, /* IN: seed OUT: primary hash value */
327 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
331 /* Set up the internal state */
332 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
335 /*----------------------------------------- handle most of the key */
345 /*----------------------------------- handle the last 3 uint32_t's */
346 switch (length
) { /* all the case statements fall through */
351 case 0: /* case 0: nothing left to add */
354 /*---------------------------------------------- report the result */
359 #if (CAA_BITS_PER_LONG == 32)
361 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
363 unsigned long key
= (unsigned long) _key
;
366 assert(length
== sizeof(unsigned long));
367 return hash_u32(&v
, 1, seed
);
371 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
382 assert(length
== sizeof(unsigned long));
383 v
.v64
= (uint64_t) seed
;
384 key
.v64
= (uint64_t) _key
;
385 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
391 unsigned long test_compare(void *key1
, size_t key1_len
,
392 void *key2
, size_t key2_len
)
394 if (unlikely(key1_len
!= key2_len
))
396 assert(key1_len
== sizeof(unsigned long));
403 void *thr_reader(void *_count
)
405 unsigned long long *count
= _count
;
406 struct cds_lfht_node
*node
;
407 struct cds_lfht_iter iter
;
409 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
410 "reader", pthread_self(), (unsigned long)gettid());
414 rcu_register_thread();
423 cds_lfht_lookup(test_ht
,
424 (void *)(((unsigned long) rand_r(&rand_lookup
) % lookup_pool_size
) + lookup_pool_offset
),
425 sizeof(void *), &iter
);
426 node
= cds_lfht_iter_get_node(&iter
);
428 if (validate_lookup
) {
429 printf("[ERROR] Lookup cannot find initial node.\n");
437 if (unlikely(rduration
))
438 loop_sleep(rduration
);
441 if (unlikely(!test_duration_read()))
443 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
444 rcu_quiescent_state();
447 rcu_unregister_thread();
450 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
451 "reader", pthread_self(), (unsigned long)gettid());
452 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
453 pthread_self(), lookup_fail
, lookup_ok
);
459 void free_node_cb(struct rcu_head
*head
)
461 struct cds_lfht_node
*node
=
462 caa_container_of(head
, struct cds_lfht_node
, head
);
466 void *thr_writer(void *_count
)
468 struct cds_lfht_node
*node
, *ret_node
;
469 struct cds_lfht_iter iter
;
470 struct wr_count
*count
= _count
;
473 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
474 "writer", pthread_self(), (unsigned long)gettid());
478 rcu_register_thread();
486 if ((addremove
== AR_ADD
|| add_only
)
487 || (addremove
== AR_RANDOM
&& rand_r(&rand_lookup
) & 1)) {
488 node
= malloc(sizeof(struct cds_lfht_node
));
490 cds_lfht_node_init(node
,
491 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
494 ret_node
= cds_lfht_add_unique(test_ht
, node
);
497 ret_node
= cds_lfht_replace(test_ht
, node
);
499 cds_lfht_add(test_ht
, node
);
502 if (add_unique
&& ret_node
!= node
) {
506 if (add_replace
&& ret_node
) {
507 call_rcu(&ret_node
->head
, free_node_cb
);
516 cds_lfht_lookup(test_ht
,
517 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
518 sizeof(void *), &iter
);
519 node
= cds_lfht_iter_get_node(&iter
);
521 ret
= cds_lfht_del(test_ht
, node
);
526 call_rcu(&node
->head
, free_node_cb
);
532 //if (nr_writes % 100000 == 0) {
533 if (nr_writes
% 1000 == 0) {
535 if (rand_r(&rand_lookup
) & 1) {
536 ht_resize(test_ht
, 1);
538 ht_resize(test_ht
, -1);
544 if (unlikely(!test_duration_write()))
546 if (unlikely(wdelay
))
548 if (unlikely((nr_writes
& ((1 << 10) - 1)) == 0))
549 rcu_quiescent_state();
552 rcu_unregister_thread();
554 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
555 "writer", pthread_self(), (unsigned long)gettid());
556 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
557 "nr_delnoent %lu\n", pthread_self(), nr_add
,
558 nr_addexist
, nr_del
, nr_delnoent
);
559 count
->update_ops
= nr_writes
;
561 count
->add_exist
= nr_addexist
;
562 count
->remove
= nr_del
;
566 static int populate_hash(void)
568 struct cds_lfht_node
*node
, *ret_node
;
573 if ((add_unique
|| add_replace
) && init_populate
* 10 > init_pool_size
) {
574 printf("WARNING: required to populate %lu nodes (-k), but random "
575 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
576 "larger random pool (-p option). This may take a while...\n", init_populate
, init_pool_size
);
579 while (nr_add
< init_populate
) {
580 node
= malloc(sizeof(struct cds_lfht_node
));
581 cds_lfht_node_init(node
,
582 (void *)(((unsigned long) rand_r(&rand_lookup
) % init_pool_size
) + init_pool_offset
),
586 ret_node
= cds_lfht_add_unique(test_ht
, node
);
589 ret_node
= cds_lfht_replace(test_ht
, node
);
591 cds_lfht_add(test_ht
, node
);
594 if (add_unique
&& ret_node
!= node
) {
598 if (add_replace
&& ret_node
) {
599 call_rcu(&ret_node
->head
, free_node_cb
);
610 void show_usage(int argc
, char **argv
)
612 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
614 printf(" [-r] [-w] (yield reader and/or writer)\n");
616 printf(" [-d delay] (writer period (us))\n");
617 printf(" [-c duration] (reader C.S. duration (in loops))\n");
618 printf(" [-v] (verbose output)\n");
619 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
620 printf(" [-h size] (initial hash table size)\n");
621 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
622 printf(" [-u] Uniquify add (no redundant keys).\n");
623 printf(" [-s] Replace (swap) entries.\n");
624 printf(" [-i] Add only (no removal).\n");
625 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
626 printf(" [-A] Automatically resize hash table.\n");
627 printf(" [-R offset] Lookup pool offset.\n");
628 printf(" [-S offset] Write pool offset.\n");
629 printf(" [-T offset] Init pool offset.\n");
630 printf(" [-M size] Lookup pool size.\n");
631 printf(" [-N size] Write pool size.\n");
632 printf(" [-O size] Init pool size.\n");
633 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
637 int main(int argc
, char **argv
)
640 pthread_t
*tid_reader
, *tid_writer
;
642 unsigned long long *count_reader
;
643 struct wr_count
*count_writer
;
644 unsigned long long tot_reads
= 0, tot_writes
= 0,
645 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
646 unsigned long count
, removed
, approx_before
, approx_after
;
648 struct sigaction act
;
652 show_usage(argc
, argv
);
656 err
= sscanf(argv
[1], "%u", &nr_readers
);
658 show_usage(argc
, argv
);
662 err
= sscanf(argv
[2], "%u", &nr_writers
);
664 show_usage(argc
, argv
);
668 err
= sscanf(argv
[3], "%lu", &duration
);
670 show_usage(argc
, argv
);
674 for (i
= 4; i
< argc
; i
++) {
675 if (argv
[i
][0] != '-')
677 switch (argv
[i
][1]) {
680 yield_active
|= YIELD_READ
;
683 yield_active
|= YIELD_WRITE
;
688 show_usage(argc
, argv
);
692 cpu_affinities
[next_aff
++] = a
;
694 printf_verbose("Adding CPU %d affinity\n", a
);
698 show_usage(argc
, argv
);
701 rduration
= atol(argv
[++i
]);
705 show_usage(argc
, argv
);
708 wdelay
= atol(argv
[++i
]);
715 show_usage(argc
, argv
);
718 init_hash_size
= atol(argv
[++i
]);
722 printf("Please specify at most one of -s or -u.\n");
729 printf("Please specify at most one of -s or -u.\n");
738 init_populate
= atol(argv
[++i
]);
744 lookup_pool_offset
= atol(argv
[++i
]);
747 write_pool_offset
= atol(argv
[++i
]);
750 init_pool_offset
= atol(argv
[++i
]);
753 lookup_pool_size
= atol(argv
[++i
]);
756 write_pool_size
= atol(argv
[++i
]);
759 init_pool_size
= atol(argv
[++i
]);
768 /* Check if hash size is power of 2 */
769 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
770 printf("Error: Hash table size %lu is not a power of 2.\n",
775 memset(&act
, 0, sizeof(act
));
776 ret
= sigemptyset(&act
.sa_mask
);
778 perror("sigemptyset");
781 act
.sa_handler
= sigusr1_handler
;
782 act
.sa_flags
= SA_RESTART
;
783 ret
= sigaction(SIGUSR1
, &act
, NULL
);
788 act
.sa_handler
= sigusr2_handler
;
789 act
.sa_flags
= SA_RESTART
;
790 ret
= sigaction(SIGUSR2
, &act
, NULL
);
796 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
797 duration
, nr_readers
, nr_writers
);
798 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
799 printf_verbose("Reader duration : %lu loops.\n", rduration
);
800 printf_verbose("Mode:%s%s.\n",
801 add_only
? " add only" : " add/remove",
802 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
803 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size
);
804 printf_verbose("Init pool size offset %lu size %lu.\n",
805 init_pool_offset
, init_pool_size
);
806 printf_verbose("Lookup pool size offset %lu size %lu.\n",
807 lookup_pool_offset
, lookup_pool_size
);
808 printf_verbose("Update pool size offset %lu size %lu.\n",
809 write_pool_offset
, write_pool_size
);
810 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
811 "main", pthread_self(), (unsigned long)gettid());
813 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
814 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
815 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
816 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
818 err
= create_all_cpu_call_rcu_data(0);
822 * Hash creation and population needs to be seen as a RCU reader
823 * thread from the point of view of resize.
825 rcu_register_thread();
826 test_ht
= cds_lfht_new(test_hash
, test_compare
, 0x42UL
,
828 opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0, NULL
);
829 ret
= populate_hash();
831 rcu_unregister_thread();
835 for (i
= 0; i
< nr_readers
; i
++) {
836 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
841 for (i
= 0; i
< nr_writers
; i
++) {
842 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
854 remain
= sleep(remain
);
855 } while (remain
> 0);
859 for (i
= 0; i
< nr_readers
; i
++) {
860 err
= pthread_join(tid_reader
[i
], &tret
);
863 tot_reads
+= count_reader
[i
];
865 for (i
= 0; i
< nr_writers
; i
++) {
866 err
= pthread_join(tid_writer
[i
], &tret
);
869 tot_writes
+= count_writer
[i
].update_ops
;
870 tot_add
+= count_writer
[i
].add
;
871 tot_add_exist
+= count_writer
[i
].add_exist
;
872 tot_remove
+= count_writer
[i
].remove
;
874 printf("Counting nodes... ");
876 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &removed
,
879 if (count
|| removed
) {
880 printf("Approximation before node accounting: %lu nodes.\n",
882 printf("WARNING: nodes left in the hash table upon destroy: "
883 "%lu nodes + %lu logically removed.\n",
885 printf("Approximation after node accounting: %lu nodes.\n",
888 ret
= cds_lfht_destroy(test_ht
, NULL
);
891 printf_verbose("final delete aborted\n");
893 printf_verbose("final delete success\n");
894 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
896 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
898 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
899 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
900 argv
[0], duration
, nr_readers
, rduration
,
901 nr_writers
, wdelay
, tot_reads
, tot_writes
,
902 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
903 (long long) tot_add
+ init_populate
- tot_remove
- count
);
904 free_all_cpu_call_rcu_data();