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 };
203 write(count_pipe
[1], msg
, 1); /* wakeup thread */
207 * returns 0 if test should end.
209 static int test_duration_write(void)
214 static int test_duration_read(void)
219 static unsigned long long __thread nr_writes
;
220 static unsigned long long __thread nr_reads
;
222 static unsigned int nr_readers
;
223 static unsigned int nr_writers
;
225 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
227 void rcu_copy_mutex_lock(void)
230 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
232 perror("Error in pthread mutex lock");
237 void rcu_copy_mutex_unlock(void)
241 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
243 perror("Error in pthread mutex unlock");
250 * Source: http://burtleburtle.net/bob/c/lookup3.c
251 * Originally Public Domain
254 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
256 #define mix(a, b, c) \
258 a -= c; a ^= rot(c, 4); c += b; \
259 b -= a; b ^= rot(a, 6); a += c; \
260 c -= b; c ^= rot(b, 8); b += a; \
261 a -= c; a ^= rot(c, 16); c += b; \
262 b -= a; b ^= rot(a, 19); a += c; \
263 c -= b; c ^= rot(b, 4); b += a; \
266 #define final(a, b, c) \
268 c ^= b; c -= rot(b, 14); \
269 a ^= c; a -= rot(c, 11); \
270 b ^= a; b -= rot(a, 25); \
271 c ^= b; c -= rot(b, 16); \
272 a ^= c; a -= rot(c, 4);\
273 b ^= a; b -= rot(a, 14); \
274 c ^= b; c -= rot(b, 24); \
277 static __attribute__((unused
))
279 const uint32_t *k
, /* the key, an array of uint32_t values */
280 size_t length
, /* the length of the key, in uint32_ts */
281 uint32_t initval
) /* the previous hash, or an arbitrary value */
285 /* Set up the internal state */
286 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
288 /*----------------------------------------- handle most of the key */
298 /*----------------------------------- handle the last 3 uint32_t's */
299 switch (length
) { /* all the case statements fall through */
304 case 0: /* case 0: nothing left to add */
307 /*---------------------------------------------- report the result */
313 const uint32_t *k
, /* the key, an array of uint32_t values */
314 size_t length
, /* the length of the key, in uint32_ts */
315 uint32_t *pc
, /* IN: seed OUT: primary hash value */
316 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
320 /* Set up the internal state */
321 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
324 /*----------------------------------------- handle most of the key */
334 /*----------------------------------- handle the last 3 uint32_t's */
335 switch (length
) { /* all the case statements fall through */
340 case 0: /* case 0: nothing left to add */
343 /*---------------------------------------------- report the result */
348 #if (CAA_BITS_PER_LONG == 32)
350 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
352 unsigned long key
= (unsigned long) _key
;
355 assert(length
== sizeof(unsigned long));
356 return hash_u32(&v
, 1, seed
);
360 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
371 assert(length
== sizeof(unsigned long));
372 v
.v64
= (uint64_t) seed
;
373 key
.v64
= (uint64_t) _key
;
374 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
380 unsigned long test_compare(void *key1
, size_t key1_len
,
381 void *key2
, size_t key2_len
)
383 if (unlikely(key1_len
!= key2_len
))
385 assert(key1_len
== sizeof(unsigned long));
392 void *thr_count(void *arg
)
394 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
395 "counter", pthread_self(), (unsigned long)gettid());
397 rcu_register_thread();
400 unsigned long count
, removed
, approx_before
, approx_after
;
404 rcu_thread_offline();
405 len
= read(count_pipe
[0], buf
, 1);
407 if (unlikely(!test_duration_read()))
412 printf("Counting nodes... ");
415 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &removed
,
419 printf("Approximation before node accounting: %lu nodes.\n",
421 printf("Accounting of nodes in the hash table: "
422 "%lu nodes + %lu logically removed.\n",
424 printf("Approximation after node accounting: %lu nodes.\n",
427 rcu_unregister_thread();
431 void *thr_reader(void *_count
)
433 unsigned long long *count
= _count
;
434 struct cds_lfht_node
*node
;
435 struct cds_lfht_iter iter
;
437 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
438 "reader", pthread_self(), (unsigned long)gettid());
442 rcu_register_thread();
451 cds_lfht_lookup(test_ht
,
452 (void *)(((unsigned long) rand_r(&rand_lookup
) % lookup_pool_size
) + lookup_pool_offset
),
453 sizeof(void *), &iter
);
454 node
= cds_lfht_iter_get_node(&iter
);
456 if (validate_lookup
) {
457 printf("[ERROR] Lookup cannot find initial node.\n");
465 if (unlikely(rduration
))
466 loop_sleep(rduration
);
469 if (unlikely(!test_duration_read()))
471 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
472 rcu_quiescent_state();
475 rcu_unregister_thread();
478 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
479 "reader", pthread_self(), (unsigned long)gettid());
480 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
481 pthread_self(), lookup_fail
, lookup_ok
);
487 void free_node_cb(struct rcu_head
*head
)
489 struct cds_lfht_node
*node
=
490 caa_container_of(head
, struct cds_lfht_node
, head
);
494 void *thr_writer(void *_count
)
496 struct cds_lfht_node
*node
, *ret_node
;
497 struct cds_lfht_iter iter
;
498 struct wr_count
*count
= _count
;
501 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
502 "writer", pthread_self(), (unsigned long)gettid());
506 rcu_register_thread();
514 if ((addremove
== AR_ADD
|| add_only
)
515 || (addremove
== AR_RANDOM
&& rand_r(&rand_lookup
) & 1)) {
516 node
= malloc(sizeof(struct cds_lfht_node
));
518 cds_lfht_node_init(node
,
519 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
522 ret_node
= cds_lfht_add_unique(test_ht
, node
);
525 ret_node
= cds_lfht_replace(test_ht
, node
);
527 cds_lfht_add(test_ht
, node
);
530 if (add_unique
&& ret_node
!= node
) {
534 if (add_replace
&& ret_node
) {
535 call_rcu(&ret_node
->head
, free_node_cb
);
544 cds_lfht_lookup(test_ht
,
545 (void *)(((unsigned long) rand_r(&rand_lookup
) % write_pool_size
) + write_pool_offset
),
546 sizeof(void *), &iter
);
547 node
= cds_lfht_iter_get_node(&iter
);
549 ret
= cds_lfht_del(test_ht
, node
);
554 call_rcu(&node
->head
, free_node_cb
);
560 //if (nr_writes % 100000 == 0) {
561 if (nr_writes
% 1000 == 0) {
563 if (rand_r(&rand_lookup
) & 1) {
564 ht_resize(test_ht
, 1);
566 ht_resize(test_ht
, -1);
572 if (unlikely(!test_duration_write()))
574 if (unlikely(wdelay
))
576 if (unlikely((nr_writes
& ((1 << 10) - 1)) == 0))
577 rcu_quiescent_state();
580 rcu_unregister_thread();
582 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
583 "writer", pthread_self(), (unsigned long)gettid());
584 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
585 "nr_delnoent %lu\n", pthread_self(), nr_add
,
586 nr_addexist
, nr_del
, nr_delnoent
);
587 count
->update_ops
= nr_writes
;
589 count
->add_exist
= nr_addexist
;
590 count
->remove
= nr_del
;
594 static int populate_hash(void)
596 struct cds_lfht_node
*node
, *ret_node
;
601 if ((add_unique
|| add_replace
) && init_populate
* 10 > init_pool_size
) {
602 printf("WARNING: required to populate %lu nodes (-k), but random "
603 "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a "
604 "larger random pool (-p option). This may take a while...\n", init_populate
, init_pool_size
);
607 while (nr_add
< init_populate
) {
608 node
= malloc(sizeof(struct cds_lfht_node
));
609 cds_lfht_node_init(node
,
610 (void *)(((unsigned long) rand_r(&rand_lookup
) % init_pool_size
) + init_pool_offset
),
614 ret_node
= cds_lfht_add_unique(test_ht
, node
);
617 ret_node
= cds_lfht_replace(test_ht
, node
);
619 cds_lfht_add(test_ht
, node
);
622 if (add_unique
&& ret_node
!= node
) {
626 if (add_replace
&& ret_node
) {
627 call_rcu(&ret_node
->head
, free_node_cb
);
638 void show_usage(int argc
, char **argv
)
640 printf("Usage : %s nr_readers nr_writers duration (s)\n", argv
[0]);
642 printf(" [-r] [-w] (yield reader and/or writer)\n");
644 printf(" [-d delay] (writer period (us))\n");
645 printf(" [-c duration] (reader C.S. duration (in loops))\n");
646 printf(" [-v] (verbose output)\n");
647 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
648 printf(" [-h size] (initial hash table size)\n");
649 printf(" [not -u nor -s] Add entries (supports redundant keys).\n");
650 printf(" [-u] Uniquify add (no redundant keys).\n");
651 printf(" [-s] Replace (swap) entries.\n");
652 printf(" [-i] Add only (no removal).\n");
653 printf(" [-k nr_nodes] Number of nodes to insert initially.\n");
654 printf(" [-A] Automatically resize hash table.\n");
655 printf(" [-R offset] Lookup pool offset.\n");
656 printf(" [-S offset] Write pool offset.\n");
657 printf(" [-T offset] Init pool offset.\n");
658 printf(" [-M size] Lookup pool size.\n");
659 printf(" [-N size] Write pool size.\n");
660 printf(" [-O size] Init pool size.\n");
661 printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n");
665 int main(int argc
, char **argv
)
668 pthread_t
*tid_reader
, *tid_writer
;
671 unsigned long long *count_reader
;
672 struct wr_count
*count_writer
;
673 unsigned long long tot_reads
= 0, tot_writes
= 0,
674 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
675 unsigned long count
, removed
, approx_before
, approx_after
;
677 struct sigaction act
;
681 show_usage(argc
, argv
);
685 err
= sscanf(argv
[1], "%u", &nr_readers
);
687 show_usage(argc
, argv
);
691 err
= sscanf(argv
[2], "%u", &nr_writers
);
693 show_usage(argc
, argv
);
697 err
= sscanf(argv
[3], "%lu", &duration
);
699 show_usage(argc
, argv
);
703 for (i
= 4; i
< argc
; i
++) {
704 if (argv
[i
][0] != '-')
706 switch (argv
[i
][1]) {
709 yield_active
|= YIELD_READ
;
712 yield_active
|= YIELD_WRITE
;
717 show_usage(argc
, argv
);
721 cpu_affinities
[next_aff
++] = a
;
723 printf_verbose("Adding CPU %d affinity\n", a
);
727 show_usage(argc
, argv
);
730 rduration
= atol(argv
[++i
]);
734 show_usage(argc
, argv
);
737 wdelay
= atol(argv
[++i
]);
744 show_usage(argc
, argv
);
747 init_hash_size
= atol(argv
[++i
]);
751 printf("Please specify at most one of -s or -u.\n");
758 printf("Please specify at most one of -s or -u.\n");
767 init_populate
= atol(argv
[++i
]);
773 lookup_pool_offset
= atol(argv
[++i
]);
776 write_pool_offset
= atol(argv
[++i
]);
779 init_pool_offset
= atol(argv
[++i
]);
782 lookup_pool_size
= atol(argv
[++i
]);
785 write_pool_size
= atol(argv
[++i
]);
788 init_pool_size
= atol(argv
[++i
]);
797 /* Check if hash size is power of 2 */
798 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
799 printf("Error: Hash table size %lu is not a power of 2.\n",
804 memset(&act
, 0, sizeof(act
));
805 ret
= sigemptyset(&act
.sa_mask
);
807 perror("sigemptyset");
810 act
.sa_handler
= sigusr1_handler
;
811 act
.sa_flags
= SA_RESTART
;
812 ret
= sigaction(SIGUSR1
, &act
, NULL
);
818 ret
= pipe(count_pipe
);
824 /* spawn counter thread */
825 err
= pthread_create(&tid_count
, NULL
, thr_count
,
830 act
.sa_handler
= sigusr2_handler
;
831 act
.sa_flags
= SA_RESTART
;
832 ret
= sigaction(SIGUSR2
, &act
, NULL
);
838 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
839 duration
, nr_readers
, nr_writers
);
840 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
841 printf_verbose("Reader duration : %lu loops.\n", rduration
);
842 printf_verbose("Mode:%s%s.\n",
843 add_only
? " add only" : " add/remove",
844 add_unique
? " uniquify" : ( add_replace
? " replace" : " insert"));
845 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size
);
846 printf_verbose("Init pool size offset %lu size %lu.\n",
847 init_pool_offset
, init_pool_size
);
848 printf_verbose("Lookup pool size offset %lu size %lu.\n",
849 lookup_pool_offset
, lookup_pool_size
);
850 printf_verbose("Update pool size offset %lu size %lu.\n",
851 write_pool_offset
, write_pool_size
);
852 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
853 "main", pthread_self(), (unsigned long)gettid());
855 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
856 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
857 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
858 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
860 err
= create_all_cpu_call_rcu_data(0);
864 * Hash creation and population needs to be seen as a RCU reader
865 * thread from the point of view of resize.
867 rcu_register_thread();
868 test_ht
= cds_lfht_new(test_hash
, test_compare
, 0x42UL
,
870 opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0, NULL
);
871 ret
= populate_hash();
873 rcu_unregister_thread();
877 for (i
= 0; i
< nr_readers
; i
++) {
878 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
883 for (i
= 0; i
< nr_writers
; i
++) {
884 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
896 remain
= sleep(remain
);
897 } while (remain
> 0);
901 for (i
= 0; i
< nr_readers
; i
++) {
902 err
= pthread_join(tid_reader
[i
], &tret
);
905 tot_reads
+= count_reader
[i
];
907 for (i
= 0; i
< nr_writers
; i
++) {
908 err
= pthread_join(tid_writer
[i
], &tret
);
911 tot_writes
+= count_writer
[i
].update_ops
;
912 tot_add
+= count_writer
[i
].add
;
913 tot_add_exist
+= count_writer
[i
].add_exist
;
914 tot_remove
+= count_writer
[i
].remove
;
917 /* teardown counter thread */
918 act
.sa_handler
= SIG_IGN
;
919 act
.sa_flags
= SA_RESTART
;
920 ret
= sigaction(SIGUSR2
, &act
, NULL
);
926 char msg
[1] = { 0x42 };
927 write(count_pipe
[1], msg
, 1); /* wakeup thread */
929 err
= pthread_join(tid_count
, &tret
);
933 printf("Counting nodes... ");
935 cds_lfht_count_nodes(test_ht
, &approx_before
, &count
, &removed
,
938 if (count
|| removed
) {
939 printf("Approximation before node accounting: %lu nodes.\n",
941 printf("WARNING: nodes left in the hash table upon destroy: "
942 "%lu nodes + %lu logically removed.\n",
944 printf("Approximation after node accounting: %lu nodes.\n",
947 ret
= cds_lfht_destroy(test_ht
, NULL
);
950 printf_verbose("final delete aborted\n");
952 printf_verbose("final delete success\n");
953 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
955 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
957 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
958 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
959 argv
[0], duration
, nr_readers
, rduration
,
960 nr_writers
, wdelay
, tot_reads
, tot_writes
,
961 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
962 (long long) tot_add
+ init_populate
- tot_remove
- count
);
963 free_all_cpu_call_rcu_data();