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 int count_pipe
[2];
129 static inline void loop_sleep(unsigned long l
)
135 static int verbose_mode
;
137 #define printf_verbose(fmt, args...) \
140 printf(fmt, ## args); \
143 static unsigned int cpu_affinities
[NR_CPUS
];
144 static unsigned int next_aff
= 0;
145 static int use_affinity
= 0;
147 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
149 static void set_affinity(void)
158 ret
= pthread_mutex_lock(&affinity_mutex
);
160 perror("Error in pthread mutex lock");
163 cpu
= cpu_affinities
[next_aff
++];
164 ret
= pthread_mutex_unlock(&affinity_mutex
);
166 perror("Error in pthread mutex unlock");
171 sched_setaffinity(0, sizeof(mask
), &mask
);
178 } addremove
; /* 1: add, -1 remove, 0: random */
181 void sigusr1_handler(int signo
)
185 printf("Add/Remove: random.\n");
186 addremove
= AR_RANDOM
;
189 printf("Add/Remove: remove only.\n");
190 addremove
= AR_REMOVE
;
193 printf("Add/Remove: add only.\n");
200 void sigusr2_handler(int signo
)
202 char msg
[1] = { 0x42 };
206 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
207 } while (ret
== -1L && errno
== EINTR
);
211 * returns 0 if test should end.
213 static int test_duration_write(void)
218 static int test_duration_read(void)
223 static unsigned long long __thread nr_writes
;
224 static unsigned long long __thread nr_reads
;
226 static unsigned int nr_readers
;
227 static unsigned int nr_writers
;
229 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
231 void rcu_copy_mutex_lock(void)
234 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
236 perror("Error in pthread mutex lock");
241 void rcu_copy_mutex_unlock(void)
245 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
247 perror("Error in pthread mutex unlock");
254 * Source: http://burtleburtle.net/bob/c/lookup3.c
255 * Originally Public Domain
258 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
260 #define mix(a, b, c) \
262 a -= c; a ^= rot(c, 4); c += b; \
263 b -= a; b ^= rot(a, 6); a += c; \
264 c -= b; c ^= rot(b, 8); b += a; \
265 a -= c; a ^= rot(c, 16); c += b; \
266 b -= a; b ^= rot(a, 19); a += c; \
267 c -= b; c ^= rot(b, 4); b += a; \
270 #define final(a, b, c) \
272 c ^= b; c -= rot(b, 14); \
273 a ^= c; a -= rot(c, 11); \
274 b ^= a; b -= rot(a, 25); \
275 c ^= b; c -= rot(b, 16); \
276 a ^= c; a -= rot(c, 4);\
277 b ^= a; b -= rot(a, 14); \
278 c ^= b; c -= rot(b, 24); \
281 static __attribute__((unused
))
283 const uint32_t *k
, /* the key, an array of uint32_t values */
284 size_t length
, /* the length of the key, in uint32_ts */
285 uint32_t initval
) /* the previous hash, or an arbitrary value */
289 /* Set up the internal state */
290 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
292 /*----------------------------------------- handle most of the key */
302 /*----------------------------------- handle the last 3 uint32_t's */
303 switch (length
) { /* all the case statements fall through */
308 case 0: /* case 0: nothing left to add */
311 /*---------------------------------------------- report the result */
317 const uint32_t *k
, /* the key, an array of uint32_t values */
318 size_t length
, /* the length of the key, in uint32_ts */
319 uint32_t *pc
, /* IN: seed OUT: primary hash value */
320 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
324 /* Set up the internal state */
325 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
328 /*----------------------------------------- handle most of the key */
338 /*----------------------------------- handle the last 3 uint32_t's */
339 switch (length
) { /* all the case statements fall through */
344 case 0: /* case 0: nothing left to add */
347 /*---------------------------------------------- report the result */
352 #if (CAA_BITS_PER_LONG == 32)
354 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
356 unsigned int key
= (unsigned int) _key
;
358 assert(length
== sizeof(unsigned int));
359 return hash_u32(&key
, 1, seed
);
363 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
374 assert(length
== sizeof(unsigned long));
375 v
.v64
= (uint64_t) seed
;
376 key
.v64
= (uint64_t) _key
;
377 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
383 unsigned long test_compare(void *key1
, size_t key1_len
,
384 void *key2
, size_t key2_len
)
386 if (unlikely(key1_len
!= key2_len
))
388 assert(key1_len
== sizeof(unsigned long));
395 void *thr_count(void *arg
)
397 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
398 "counter", pthread_self(), (unsigned long)gettid());
400 rcu_register_thread();
403 unsigned long count
, removed
;
404 long approx_before
, approx_after
;
408 rcu_thread_offline();
409 len
= read(count_pipe
[0], buf
, 1);
411 if (unlikely(!test_duration_read()))
416 printf("Counting nodes... ");
419 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &removed
,
423 printf("Approximation before node accounting: %ld nodes.\n",
425 printf("Accounting of nodes in the hash table: "
426 "%lu nodes + %lu logically removed.\n",
428 printf("Approximation after node accounting: %ld nodes.\n",
431 rcu_unregister_thread();
435 void *thr_reader(void *_count
)
437 unsigned long long *count
= _count
;
438 struct cds_lfht_node
*node
;
439 struct cds_lfht_iter iter
;
441 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
442 "reader", pthread_self(), (unsigned long)gettid());
446 rcu_register_thread();
455 cds_lfht_lookup(test_ht
,
456 (void *)(((unsigned long) rand_r(&rand_lookup
) % lookup_pool_size
) + lookup_pool_offset
),
457 sizeof(void *), &iter
);
458 node
= cds_lfht_iter_get_node(&iter
);
460 if (validate_lookup
) {
461 printf("[ERROR] Lookup cannot find initial node.\n");
469 if (unlikely(rduration
))
470 loop_sleep(rduration
);
473 if (unlikely(!test_duration_read()))
475 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
476 rcu_quiescent_state();
479 rcu_unregister_thread();
482 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
483 "reader", pthread_self(), (unsigned long)gettid());
484 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
485 pthread_self(), lookup_fail
, lookup_ok
);
491 void free_node_cb(struct rcu_head
*head
)
493 struct cds_lfht_node
*node
=
494 caa_container_of(head
, struct cds_lfht_node
, head
);
498 void *thr_writer(void *_count
)
500 struct cds_lfht_node
*node
, *ret_node
;
501 struct cds_lfht_iter iter
;
502 struct wr_count
*count
= _count
;
505 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
506 "writer", pthread_self(), (unsigned long)gettid());
510 rcu_register_thread();
518 if ((addremove
== AR_ADD
|| add_only
)
519 || (addremove
== AR_RANDOM
&& rand_r(&rand_lookup
) & 1)) {
520 node
= malloc(sizeof(struct cds_lfht_node
));
521 cds_lfht_node_init(node
,
522 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
526 ret_node
= cds_lfht_add_unique(test_ht
, node
);
529 ret_node
= cds_lfht_add_replace(test_ht
, node
);
531 cds_lfht_add(test_ht
, node
);
534 if (add_unique
&& ret_node
!= node
) {
538 if (add_replace
&& ret_node
) {
539 call_rcu(&ret_node
->head
, free_node_cb
);
548 cds_lfht_lookup(test_ht
,
549 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
550 sizeof(void *), &iter
);
551 ret
= cds_lfht_del(test_ht
, &iter
);
554 node
= cds_lfht_iter_get_node(&iter
);
555 call_rcu(&node
->head
, free_node_cb
);
561 //if (nr_writes % 100000 == 0) {
562 if (nr_writes
% 1000 == 0) {
564 if (rand_r(&rand_lookup
) & 1) {
565 ht_resize(test_ht
, 1);
567 ht_resize(test_ht
, -1);
573 if (unlikely(!test_duration_write()))
575 if (unlikely(wdelay
))
577 if (unlikely((nr_writes
& ((1 << 10) - 1)) == 0))
578 rcu_quiescent_state();
581 rcu_unregister_thread();
583 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
584 "writer", pthread_self(), (unsigned long)gettid());
585 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
586 "nr_delnoent %lu\n", pthread_self(), nr_add
,
587 nr_addexist
, nr_del
, nr_delnoent
);
588 count
->update_ops
= nr_writes
;
590 count
->add_exist
= nr_addexist
;
591 count
->remove
= nr_del
;
595 static int populate_hash(void)
597 struct cds_lfht_node
*node
, *ret_node
;
602 if ((add_unique
|| add_replace
) && init_populate
* 10 > init_pool_size
) {
603 printf("WARNING: required to populate %lu nodes (-k), but random "
604 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
605 "larger random pool (-p option). This may take a while...\n", init_populate
, init_pool_size
);
608 while (nr_add
< init_populate
) {
609 node
= malloc(sizeof(struct cds_lfht_node
));
610 cds_lfht_node_init(node
,
611 (void *)(((unsigned long) rand_r(&rand_lookup
) % init_pool_size
) + init_pool_offset
),
615 ret_node
= cds_lfht_add_unique(test_ht
, node
);
618 ret_node
= cds_lfht_add_replace(test_ht
, node
);
620 cds_lfht_add(test_ht
, node
);
623 if (add_unique
&& ret_node
!= node
) {
627 if (add_replace
&& ret_node
) {
628 call_rcu(&ret_node
->head
, free_node_cb
);
640 void test_delete_all_nodes(struct cds_lfht
*ht
)
642 struct cds_lfht_iter iter
;
643 struct cds_lfht_node
*node
;
644 unsigned long count
= 0;
646 cds_lfht_first(ht
, &iter
);
647 while ((node
= cds_lfht_iter_get_node(&iter
)) != NULL
) {
650 ret
= cds_lfht_del(test_ht
, &iter
);
652 call_rcu(&node
->head
, free_node_cb
);
653 cds_lfht_next(ht
, &iter
);
656 printf("deleted %lu nodes.\n", count
);
659 void show_usage(int argc
, char **argv
)
661 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
663 printf(" [-r] [-w] (yield reader and/or writer)\n");
665 printf(" [-d delay] (writer period (us))\n");
666 printf(" [-c duration] (reader C.S. duration (in loops))\n");
667 printf(" [-v] (verbose output)\n");
668 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
669 printf(" [-h size] (initial hash table size)\n");
670 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
671 printf(" [-u] Uniquify add (no redundant keys).\n");
672 printf(" [-s] Replace (swap) entries.\n");
673 printf(" [-i] Add only (no removal).\n");
674 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
675 printf(" [-A] Automatically resize hash table.\n");
676 printf(" [-R offset] Lookup pool offset.\n");
677 printf(" [-S offset] Write pool offset.\n");
678 printf(" [-T offset] Init pool offset.\n");
679 printf(" [-M size] Lookup pool size.\n");
680 printf(" [-N size] Write pool size.\n");
681 printf(" [-O size] Init pool size.\n");
682 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
686 int main(int argc
, char **argv
)
689 pthread_t
*tid_reader
, *tid_writer
;
692 unsigned long long *count_reader
;
693 struct wr_count
*count_writer
;
694 unsigned long long tot_reads
= 0, tot_writes
= 0,
695 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
696 unsigned long count
, removed
;
697 long approx_before
, approx_after
;
699 struct sigaction act
;
703 show_usage(argc
, argv
);
707 err
= sscanf(argv
[1], "%u", &nr_readers
);
709 show_usage(argc
, argv
);
713 err
= sscanf(argv
[2], "%u", &nr_writers
);
715 show_usage(argc
, argv
);
719 err
= sscanf(argv
[3], "%lu", &duration
);
721 show_usage(argc
, argv
);
725 for (i
= 4; i
< argc
; i
++) {
726 if (argv
[i
][0] != '-')
728 switch (argv
[i
][1]) {
731 yield_active
|= YIELD_READ
;
734 yield_active
|= YIELD_WRITE
;
739 show_usage(argc
, argv
);
743 cpu_affinities
[next_aff
++] = a
;
745 printf_verbose("Adding CPU %d affinity\n", a
);
749 show_usage(argc
, argv
);
752 rduration
= atol(argv
[++i
]);
756 show_usage(argc
, argv
);
759 wdelay
= atol(argv
[++i
]);
766 show_usage(argc
, argv
);
769 init_hash_size
= atol(argv
[++i
]);
773 printf("Please specify at most one of -s or -u.\n");
780 printf("Please specify at most one of -s or -u.\n");
789 init_populate
= atol(argv
[++i
]);
795 lookup_pool_offset
= atol(argv
[++i
]);
798 write_pool_offset
= atol(argv
[++i
]);
801 init_pool_offset
= atol(argv
[++i
]);
804 lookup_pool_size
= atol(argv
[++i
]);
807 write_pool_size
= atol(argv
[++i
]);
810 init_pool_size
= atol(argv
[++i
]);
819 /* Check if hash size is power of 2 */
820 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
821 printf("Error: Hash table size %lu is not a power of 2.\n",
826 memset(&act
, 0, sizeof(act
));
827 ret
= sigemptyset(&act
.sa_mask
);
829 perror("sigemptyset");
832 act
.sa_handler
= sigusr1_handler
;
833 act
.sa_flags
= SA_RESTART
;
834 ret
= sigaction(SIGUSR1
, &act
, NULL
);
840 ret
= pipe(count_pipe
);
846 /* spawn counter thread */
847 err
= pthread_create(&tid_count
, NULL
, thr_count
,
852 act
.sa_handler
= sigusr2_handler
;
853 act
.sa_flags
= SA_RESTART
;
854 ret
= sigaction(SIGUSR2
, &act
, NULL
);
860 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
861 duration
, nr_readers
, nr_writers
);
862 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
863 printf_verbose("Reader duration : %lu loops.\n", rduration
);
864 printf_verbose("Mode:%s%s.\n",
865 add_only
? " add only" : " add/remove",
866 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
867 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size
);
868 printf_verbose("Init pool size offset %lu size %lu.\n",
869 init_pool_offset
, init_pool_size
);
870 printf_verbose("Lookup pool size offset %lu size %lu.\n",
871 lookup_pool_offset
, lookup_pool_size
);
872 printf_verbose("Update pool size offset %lu size %lu.\n",
873 write_pool_offset
, write_pool_size
);
874 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
875 "main", pthread_self(), (unsigned long)gettid());
877 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
878 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
879 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
880 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
882 err
= create_all_cpu_call_rcu_data(0);
886 * Hash creation and population needs to be seen as a RCU reader
887 * thread from the point of view of resize.
889 rcu_register_thread();
890 test_ht
= cds_lfht_new(test_hash
, test_compare
, 0x42UL
,
892 opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0, NULL
);
893 ret
= populate_hash();
896 rcu_thread_offline();
900 for (i
= 0; i
< nr_readers
; i
++) {
901 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
906 for (i
= 0; i
< nr_writers
; i
++) {
907 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
919 remain
= sleep(remain
);
920 } while (remain
> 0);
924 for (i
= 0; i
< nr_readers
; i
++) {
925 err
= pthread_join(tid_reader
[i
], &tret
);
928 tot_reads
+= count_reader
[i
];
930 for (i
= 0; i
< nr_writers
; i
++) {
931 err
= pthread_join(tid_writer
[i
], &tret
);
934 tot_writes
+= count_writer
[i
].update_ops
;
935 tot_add
+= count_writer
[i
].add
;
936 tot_add_exist
+= count_writer
[i
].add_exist
;
937 tot_remove
+= count_writer
[i
].remove
;
940 /* teardown counter thread */
941 act
.sa_handler
= SIG_IGN
;
942 act
.sa_flags
= SA_RESTART
;
943 ret
= sigaction(SIGUSR2
, &act
, NULL
);
949 char msg
[1] = { 0x42 };
953 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
954 } while (ret
== -1L && errno
== EINTR
);
956 err
= pthread_join(tid_count
, &tret
);
963 printf("Counting nodes... ");
964 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &removed
,
967 test_delete_all_nodes(test_ht
);
969 rcu_thread_offline();
970 if (count
|| removed
) {
971 printf("Approximation before node accounting: %ld nodes.\n",
973 printf("Nodes deleted from hash table before destroy: "
974 "%lu nodes + %lu logically removed.\n",
976 printf("Approximation after node accounting: %ld nodes.\n",
979 ret
= cds_lfht_destroy(test_ht
, NULL
);
981 printf_verbose("final delete aborted\n");
983 printf_verbose("final delete success\n");
984 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
986 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
988 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
989 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
990 argv
[0], duration
, nr_readers
, rduration
,
991 nr_writers
, wdelay
, tot_reads
, tot_writes
,
992 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
993 (long long) tot_add
+ init_populate
- tot_remove
- count
);
994 rcu_unregister_thread();
995 free_all_cpu_call_rcu_data();