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_MIN_ALLOC_SIZE 1
42 #define DEFAULT_RAND_POOL 1000000
44 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
45 #define CACHE_LINE_SIZE 4096
47 /* hardcoded number of CPUs */
51 #define poison_free(ptr) \
53 memset(ptr, 0x42, sizeof(*(ptr))); \
57 #define poison_free(ptr) free(ptr)
62 #if defined(_syscall0)
63 _syscall0(pid_t
, gettid
)
64 #elif defined(__NR_gettid)
65 static inline pid_t
gettid(void)
67 return syscall(__NR_gettid
);
70 #warning "use pid as tid"
71 static inline pid_t
gettid(void)
77 #ifndef DYNAMIC_LINK_TEST
80 #define debug_yield_read()
82 #include <urcu-qsbr.h>
83 #include <urcu/rculfhash.h>
84 #include <urcu-call-rcu.h>
87 unsigned long update_ops
;
89 unsigned long add_exist
;
93 static unsigned int __thread rand_lookup
;
94 static unsigned long __thread nr_add
;
95 static unsigned long __thread nr_addexist
;
96 static unsigned long __thread nr_del
;
97 static unsigned long __thread nr_delnoent
;
98 static unsigned long __thread lookup_fail
;
99 static unsigned long __thread lookup_ok
;
101 static struct cds_lfht
*test_ht
;
108 static volatile int test_go
, test_stop
;
110 static unsigned long wdelay
;
112 static unsigned long duration
;
114 /* read-side C.S. duration, in loops */
115 static unsigned long rduration
;
117 static unsigned long init_hash_size
= DEFAULT_HASH_SIZE
;
118 static unsigned long min_hash_alloc_size
= DEFAULT_MIN_ALLOC_SIZE
;
119 static unsigned long init_populate
;
120 static int opt_auto_resize
;
121 static int add_only
, add_unique
, add_replace
;
123 static unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
124 static unsigned long init_pool_size
= DEFAULT_RAND_POOL
,
125 lookup_pool_size
= DEFAULT_RAND_POOL
,
126 write_pool_size
= DEFAULT_RAND_POOL
;
127 static int validate_lookup
;
129 static int count_pipe
[2];
131 static inline void loop_sleep(unsigned long l
)
137 static int verbose_mode
;
139 #define printf_verbose(fmt, args...) \
142 printf(fmt, ## args); \
145 static unsigned int cpu_affinities
[NR_CPUS
];
146 static unsigned int next_aff
= 0;
147 static int use_affinity
= 0;
149 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
151 static void set_affinity(void)
160 ret
= pthread_mutex_lock(&affinity_mutex
);
162 perror("Error in pthread mutex lock");
165 cpu
= cpu_affinities
[next_aff
++];
166 ret
= pthread_mutex_unlock(&affinity_mutex
);
168 perror("Error in pthread mutex unlock");
173 sched_setaffinity(0, sizeof(mask
), &mask
);
180 } addremove
; /* 1: add, -1 remove, 0: random */
183 void sigusr1_handler(int signo
)
187 printf("Add/Remove: random.\n");
188 addremove
= AR_RANDOM
;
191 printf("Add/Remove: remove only.\n");
192 addremove
= AR_REMOVE
;
195 printf("Add/Remove: add only.\n");
202 void sigusr2_handler(int signo
)
204 char msg
[1] = { 0x42 };
208 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
209 } while (ret
== -1L && errno
== EINTR
);
213 * returns 0 if test should end.
215 static int test_duration_write(void)
220 static int test_duration_read(void)
225 static unsigned long long __thread nr_writes
;
226 static unsigned long long __thread nr_reads
;
228 static unsigned int nr_readers
;
229 static unsigned int nr_writers
;
231 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
233 void rcu_copy_mutex_lock(void)
236 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
238 perror("Error in pthread mutex lock");
243 void rcu_copy_mutex_unlock(void)
247 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
249 perror("Error in pthread mutex unlock");
256 * Source: http://burtleburtle.net/bob/c/lookup3.c
257 * Originally Public Domain
260 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
262 #define mix(a, b, c) \
264 a -= c; a ^= rot(c, 4); c += b; \
265 b -= a; b ^= rot(a, 6); a += c; \
266 c -= b; c ^= rot(b, 8); b += a; \
267 a -= c; a ^= rot(c, 16); c += b; \
268 b -= a; b ^= rot(a, 19); a += c; \
269 c -= b; c ^= rot(b, 4); b += a; \
272 #define final(a, b, c) \
274 c ^= b; c -= rot(b, 14); \
275 a ^= c; a -= rot(c, 11); \
276 b ^= a; b -= rot(a, 25); \
277 c ^= b; c -= rot(b, 16); \
278 a ^= c; a -= rot(c, 4);\
279 b ^= a; b -= rot(a, 14); \
280 c ^= b; c -= rot(b, 24); \
283 static __attribute__((unused
))
285 const uint32_t *k
, /* the key, an array of uint32_t values */
286 size_t length
, /* the length of the key, in uint32_ts */
287 uint32_t initval
) /* the previous hash, or an arbitrary value */
291 /* Set up the internal state */
292 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
294 /*----------------------------------------- handle most of the key */
304 /*----------------------------------- handle the last 3 uint32_t's */
305 switch (length
) { /* all the case statements fall through */
310 case 0: /* case 0: nothing left to add */
313 /*---------------------------------------------- report the result */
319 const uint32_t *k
, /* the key, an array of uint32_t values */
320 size_t length
, /* the length of the key, in uint32_ts */
321 uint32_t *pc
, /* IN: seed OUT: primary hash value */
322 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
326 /* Set up the internal state */
327 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
330 /*----------------------------------------- handle most of the key */
340 /*----------------------------------- handle the last 3 uint32_t's */
341 switch (length
) { /* all the case statements fall through */
346 case 0: /* case 0: nothing left to add */
349 /*---------------------------------------------- report the result */
354 #if (CAA_BITS_PER_LONG == 32)
356 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
358 unsigned int key
= (unsigned int) _key
;
360 assert(length
== sizeof(unsigned int));
361 return hash_u32(&key
, 1, seed
);
365 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
376 assert(length
== sizeof(unsigned long));
377 v
.v64
= (uint64_t) seed
;
378 key
.v64
= (uint64_t) _key
;
379 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
385 unsigned long test_compare(void *key1
, size_t key1_len
,
386 void *key2
, size_t key2_len
)
388 if (caa_unlikely(key1_len
!= key2_len
))
390 assert(key1_len
== sizeof(unsigned long));
397 void *thr_count(void *arg
)
399 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
400 "counter", pthread_self(), (unsigned long)gettid());
402 rcu_register_thread();
405 unsigned long count
, removed
;
406 long approx_before
, approx_after
;
410 rcu_thread_offline();
411 len
= read(count_pipe
[0], buf
, 1);
413 if (caa_unlikely(!test_duration_read()))
418 printf("Counting nodes... ");
421 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &removed
,
425 printf("Approximation before node accounting: %ld nodes.\n",
427 printf("Accounting of nodes in the hash table: "
428 "%lu nodes + %lu logically removed.\n",
430 printf("Approximation after node accounting: %ld nodes.\n",
433 rcu_unregister_thread();
437 void *thr_reader(void *_count
)
439 unsigned long long *count
= _count
;
440 struct cds_lfht_node
*node
;
441 struct cds_lfht_iter iter
;
443 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
444 "reader", pthread_self(), (unsigned long)gettid());
448 rcu_register_thread();
457 cds_lfht_lookup(test_ht
,
458 (void *)(((unsigned long) rand_r(&rand_lookup
) % lookup_pool_size
) + lookup_pool_offset
),
459 sizeof(void *), &iter
);
460 node
= cds_lfht_iter_get_node(&iter
);
462 if (validate_lookup
) {
463 printf("[ERROR] Lookup cannot find initial node.\n");
471 if (caa_unlikely(rduration
))
472 loop_sleep(rduration
);
475 if (caa_unlikely(!test_duration_read()))
477 if (caa_unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
478 rcu_quiescent_state();
481 rcu_unregister_thread();
484 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
485 "reader", pthread_self(), (unsigned long)gettid());
486 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
487 pthread_self(), lookup_fail
, lookup_ok
);
493 void free_node_cb(struct rcu_head
*head
)
495 struct cds_lfht_node
*node
=
496 caa_container_of(head
, struct cds_lfht_node
, head
);
500 void *thr_writer(void *_count
)
502 struct cds_lfht_node
*node
, *ret_node
;
503 struct cds_lfht_iter iter
;
504 struct wr_count
*count
= _count
;
507 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
508 "writer", pthread_self(), (unsigned long)gettid());
512 rcu_register_thread();
520 if ((addremove
== AR_ADD
|| add_only
)
521 || (addremove
== AR_RANDOM
&& rand_r(&rand_lookup
) & 1)) {
522 node
= malloc(sizeof(struct cds_lfht_node
));
523 cds_lfht_node_init(node
,
524 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
528 ret_node
= cds_lfht_add_unique(test_ht
, node
);
531 ret_node
= cds_lfht_add_replace(test_ht
, node
);
533 cds_lfht_add(test_ht
, node
);
536 if (add_unique
&& ret_node
!= node
) {
540 if (add_replace
&& ret_node
) {
541 call_rcu(&ret_node
->head
, free_node_cb
);
550 cds_lfht_lookup(test_ht
,
551 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
552 sizeof(void *), &iter
);
553 ret
= cds_lfht_del(test_ht
, &iter
);
556 node
= cds_lfht_iter_get_node(&iter
);
557 call_rcu(&node
->head
, free_node_cb
);
563 //if (nr_writes % 100000 == 0) {
564 if (nr_writes
% 1000 == 0) {
566 if (rand_r(&rand_lookup
) & 1) {
567 ht_resize(test_ht
, 1);
569 ht_resize(test_ht
, -1);
575 if (caa_unlikely(!test_duration_write()))
577 if (caa_unlikely(wdelay
))
579 if (caa_unlikely((nr_writes
& ((1 << 10) - 1)) == 0))
580 rcu_quiescent_state();
583 rcu_unregister_thread();
585 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
586 "writer", pthread_self(), (unsigned long)gettid());
587 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
588 "nr_delnoent %lu\n", pthread_self(), nr_add
,
589 nr_addexist
, nr_del
, nr_delnoent
);
590 count
->update_ops
= nr_writes
;
592 count
->add_exist
= nr_addexist
;
593 count
->remove
= nr_del
;
597 static int populate_hash(void)
599 struct cds_lfht_node
*node
, *ret_node
;
604 if ((add_unique
|| add_replace
) && init_populate
* 10 > init_pool_size
) {
605 printf("WARNING: required to populate %lu nodes (-k), but random "
606 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
607 "larger random pool (-p option). This may take a while...\n", init_populate
, init_pool_size
);
610 while (nr_add
< init_populate
) {
611 node
= malloc(sizeof(struct cds_lfht_node
));
612 cds_lfht_node_init(node
,
613 (void *)(((unsigned long) rand_r(&rand_lookup
) % init_pool_size
) + init_pool_offset
),
617 ret_node
= cds_lfht_add_unique(test_ht
, node
);
620 ret_node
= cds_lfht_add_replace(test_ht
, node
);
622 cds_lfht_add(test_ht
, node
);
625 if (add_unique
&& ret_node
!= node
) {
629 if (add_replace
&& ret_node
) {
630 call_rcu(&ret_node
->head
, free_node_cb
);
642 void test_delete_all_nodes(struct cds_lfht
*ht
)
644 struct cds_lfht_iter iter
;
645 struct cds_lfht_node
*node
;
646 unsigned long count
= 0;
648 cds_lfht_first(ht
, &iter
);
649 while ((node
= cds_lfht_iter_get_node(&iter
)) != NULL
) {
652 ret
= cds_lfht_del(test_ht
, &iter
);
654 call_rcu(&node
->head
, free_node_cb
);
655 cds_lfht_next(ht
, &iter
);
658 printf("deleted %lu nodes.\n", count
);
661 void show_usage(int argc
, char **argv
)
663 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
665 printf(" [-r] [-w] (yield reader and/or writer)\n");
667 printf(" [-d delay] (writer period (us))\n");
668 printf(" [-c duration] (reader C.S. duration (in loops))\n");
669 printf(" [-v] (verbose output)\n");
670 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
671 printf(" [-h size] (initial hash table size)\n");
672 printf(" [-m size] (minimum hash alloc size)\n");
673 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
674 printf(" [-u] Uniquify add (no redundant keys).\n");
675 printf(" [-s] Replace (swap) entries.\n");
676 printf(" [-i] Add only (no removal).\n");
677 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
678 printf(" [-A] Automatically resize hash table.\n");
679 printf(" [-R offset] Lookup pool offset.\n");
680 printf(" [-S offset] Write pool offset.\n");
681 printf(" [-T offset] Init pool offset.\n");
682 printf(" [-M size] Lookup pool size.\n");
683 printf(" [-N size] Write pool size.\n");
684 printf(" [-O size] Init pool size.\n");
685 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
689 int main(int argc
, char **argv
)
692 pthread_t
*tid_reader
, *tid_writer
;
695 unsigned long long *count_reader
;
696 struct wr_count
*count_writer
;
697 unsigned long long tot_reads
= 0, tot_writes
= 0,
698 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
699 unsigned long count
, removed
;
700 long approx_before
, approx_after
;
702 struct sigaction act
;
706 show_usage(argc
, argv
);
710 err
= sscanf(argv
[1], "%u", &nr_readers
);
712 show_usage(argc
, argv
);
716 err
= sscanf(argv
[2], "%u", &nr_writers
);
718 show_usage(argc
, argv
);
722 err
= sscanf(argv
[3], "%lu", &duration
);
724 show_usage(argc
, argv
);
728 for (i
= 4; i
< argc
; i
++) {
729 if (argv
[i
][0] != '-')
731 switch (argv
[i
][1]) {
734 yield_active
|= YIELD_READ
;
737 yield_active
|= YIELD_WRITE
;
742 show_usage(argc
, argv
);
746 cpu_affinities
[next_aff
++] = a
;
748 printf_verbose("Adding CPU %d affinity\n", a
);
752 show_usage(argc
, argv
);
755 rduration
= atol(argv
[++i
]);
759 show_usage(argc
, argv
);
762 wdelay
= atol(argv
[++i
]);
769 show_usage(argc
, argv
);
772 init_hash_size
= atol(argv
[++i
]);
776 show_usage(argc
, argv
);
779 min_hash_alloc_size
= atol(argv
[++i
]);
783 printf("Please specify at most one of -s or -u.\n");
790 printf("Please specify at most one of -s or -u.\n");
799 init_populate
= atol(argv
[++i
]);
805 lookup_pool_offset
= atol(argv
[++i
]);
808 write_pool_offset
= atol(argv
[++i
]);
811 init_pool_offset
= atol(argv
[++i
]);
814 lookup_pool_size
= atol(argv
[++i
]);
817 write_pool_size
= atol(argv
[++i
]);
820 init_pool_size
= atol(argv
[++i
]);
829 /* Check if hash size is power of 2 */
830 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
831 printf("Error: Hash table size %lu is not a power of 2.\n",
836 if (min_hash_alloc_size
&& min_hash_alloc_size
* (min_hash_alloc_size
- 1)) {
837 printf("Error: Min hash alloc size %lu is not a power of 2.\n",
838 min_hash_alloc_size
);
842 memset(&act
, 0, sizeof(act
));
843 ret
= sigemptyset(&act
.sa_mask
);
845 perror("sigemptyset");
848 act
.sa_handler
= sigusr1_handler
;
849 act
.sa_flags
= SA_RESTART
;
850 ret
= sigaction(SIGUSR1
, &act
, NULL
);
856 ret
= pipe(count_pipe
);
862 /* spawn counter thread */
863 err
= pthread_create(&tid_count
, NULL
, thr_count
,
868 act
.sa_handler
= sigusr2_handler
;
869 act
.sa_flags
= SA_RESTART
;
870 ret
= sigaction(SIGUSR2
, &act
, NULL
);
876 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
877 duration
, nr_readers
, nr_writers
);
878 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
879 printf_verbose("Reader duration : %lu loops.\n", rduration
);
880 printf_verbose("Mode:%s%s.\n",
881 add_only
? " add only" : " add/remove",
882 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
883 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size
);
884 printf_verbose("Minimum hash alloc size: %lu buckets.\n", min_hash_alloc_size
);
885 printf_verbose("Init pool size offset %lu size %lu.\n",
886 init_pool_offset
, init_pool_size
);
887 printf_verbose("Lookup pool size offset %lu size %lu.\n",
888 lookup_pool_offset
, lookup_pool_size
);
889 printf_verbose("Update pool size offset %lu size %lu.\n",
890 write_pool_offset
, write_pool_size
);
891 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
892 "main", pthread_self(), (unsigned long)gettid());
894 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
895 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
896 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
897 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
899 err
= create_all_cpu_call_rcu_data(0);
903 * Hash creation and population needs to be seen as a RCU reader
904 * thread from the point of view of resize.
906 rcu_register_thread();
907 test_ht
= cds_lfht_new(test_hash
, test_compare
, 0x42UL
,
908 init_hash_size
, min_hash_alloc_size
,
909 (opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0) |
910 CDS_LFHT_ACCOUNTING
, NULL
);
911 ret
= populate_hash();
914 rcu_thread_offline();
918 for (i
= 0; i
< nr_readers
; i
++) {
919 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
924 for (i
= 0; i
< nr_writers
; i
++) {
925 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
937 remain
= sleep(remain
);
938 } while (remain
> 0);
942 for (i
= 0; i
< nr_readers
; i
++) {
943 err
= pthread_join(tid_reader
[i
], &tret
);
946 tot_reads
+= count_reader
[i
];
948 for (i
= 0; i
< nr_writers
; i
++) {
949 err
= pthread_join(tid_writer
[i
], &tret
);
952 tot_writes
+= count_writer
[i
].update_ops
;
953 tot_add
+= count_writer
[i
].add
;
954 tot_add_exist
+= count_writer
[i
].add_exist
;
955 tot_remove
+= count_writer
[i
].remove
;
958 /* teardown counter thread */
959 act
.sa_handler
= SIG_IGN
;
960 act
.sa_flags
= SA_RESTART
;
961 ret
= sigaction(SIGUSR2
, &act
, NULL
);
967 char msg
[1] = { 0x42 };
971 ret
= write(count_pipe
[1], msg
, 1); /* wakeup thread */
972 } while (ret
== -1L && errno
== EINTR
);
974 err
= pthread_join(tid_count
, &tret
);
981 printf("Counting nodes... ");
982 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &removed
,
985 test_delete_all_nodes(test_ht
);
987 rcu_thread_offline();
988 if (count
|| removed
) {
989 printf("Approximation before node accounting: %ld nodes.\n",
991 printf("Nodes deleted from hash table before destroy: "
992 "%lu nodes + %lu logically removed.\n",
994 printf("Approximation after node accounting: %ld nodes.\n",
997 ret
= cds_lfht_destroy(test_ht
, NULL
);
999 printf_verbose("final delete aborted\n");
1001 printf_verbose("final delete success\n");
1002 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
1004 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
1006 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
1007 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
1008 argv
[0], duration
, nr_readers
, rduration
,
1009 nr_writers
, wdelay
, tot_reads
, tot_writes
,
1010 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
1011 (long long) tot_add
+ init_populate
- tot_remove
- count
);
1012 rcu_unregister_thread();
1013 free_all_cpu_call_rcu_data();