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 */
49 #if defined(_syscall0)
50 _syscall0(pid_t
, gettid
)
51 #elif defined(__NR_gettid)
52 static inline pid_t
gettid(void)
54 return syscall(__NR_gettid
);
57 #warning "use pid as tid"
58 static inline pid_t
gettid(void)
64 #ifndef DYNAMIC_LINK_TEST
67 #define debug_yield_read()
69 #include <urcu-qsbr.h>
70 #include <urcu/rculfhash.h>
71 #include <urcu-call-rcu.h>
74 unsigned long update_ops
;
76 unsigned long add_exist
;
80 static unsigned int __thread rand_lookup
;
81 static unsigned long __thread nr_add
;
82 static unsigned long __thread nr_addexist
;
83 static unsigned long __thread nr_del
;
84 static unsigned long __thread nr_delnoent
;
85 static unsigned long __thread lookup_fail
;
86 static unsigned long __thread lookup_ok
;
88 static struct cds_lfht
*test_ht
;
95 static volatile int test_go
, test_stop
;
97 static unsigned long wdelay
;
99 static unsigned long duration
;
101 /* read-side C.S. duration, in loops */
102 static unsigned long rduration
;
104 static unsigned long init_hash_size
= DEFAULT_HASH_SIZE
;
105 static unsigned long init_populate
;
106 static unsigned long rand_pool
= DEFAULT_RAND_POOL
;
107 static int add_only
, add_unique
;
109 static inline void loop_sleep(unsigned long l
)
115 static int verbose_mode
;
117 #define printf_verbose(fmt, args...) \
120 printf(fmt, ## args); \
123 static unsigned int cpu_affinities
[NR_CPUS
];
124 static unsigned int next_aff
= 0;
125 static int use_affinity
= 0;
127 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
129 static void set_affinity(void)
138 ret
= pthread_mutex_lock(&affinity_mutex
);
140 perror("Error in pthread mutex lock");
143 cpu
= cpu_affinities
[next_aff
++];
144 ret
= pthread_mutex_unlock(&affinity_mutex
);
146 perror("Error in pthread mutex unlock");
151 sched_setaffinity(0, sizeof(mask
), &mask
);
155 * returns 0 if test should end.
157 static int test_duration_write(void)
162 static int test_duration_read(void)
167 static unsigned long long __thread nr_writes
;
168 static unsigned long long __thread nr_reads
;
170 static unsigned int nr_readers
;
171 static unsigned int nr_writers
;
173 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
175 void rcu_copy_mutex_lock(void)
178 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
180 perror("Error in pthread mutex lock");
185 void rcu_copy_mutex_unlock(void)
189 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
191 perror("Error in pthread mutex unlock");
198 * Source: http://burtleburtle.net/bob/c/lookup3.c
199 * Originally Public Domain
202 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
204 #define mix(a, b, c) \
206 a -= c; a ^= rot(c, 4); c += b; \
207 b -= a; b ^= rot(a, 6); a += c; \
208 c -= b; c ^= rot(b, 8); b += a; \
209 a -= c; a ^= rot(c, 16); c += b; \
210 b -= a; b ^= rot(a, 19); a += c; \
211 c -= b; c ^= rot(b, 4); b += a; \
214 #define final(a, b, c) \
216 c ^= b; c -= rot(b, 14); \
217 a ^= c; a -= rot(c, 11); \
218 b ^= a; b -= rot(a, 25); \
219 c ^= b; c -= rot(b, 16); \
220 a ^= c; a -= rot(c, 4);\
221 b ^= a; b -= rot(a, 14); \
222 c ^= b; c -= rot(b, 24); \
225 static __attribute__((unused
))
227 const uint32_t *k
, /* the key, an array of uint32_t values */
228 size_t length
, /* the length of the key, in uint32_ts */
229 uint32_t initval
) /* the previous hash, or an arbitrary value */
233 /* Set up the internal state */
234 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
236 /*----------------------------------------- handle most of the key */
246 /*----------------------------------- handle the last 3 uint32_t's */
247 switch (length
) { /* all the case statements fall through */
252 case 0: /* case 0: nothing left to add */
255 /*---------------------------------------------- report the result */
261 const uint32_t *k
, /* the key, an array of uint32_t values */
262 size_t length
, /* the length of the key, in uint32_ts */
263 uint32_t *pc
, /* IN: seed OUT: primary hash value */
264 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
268 /* Set up the internal state */
269 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
272 /*----------------------------------------- handle most of the key */
282 /*----------------------------------- handle the last 3 uint32_t's */
283 switch (length
) { /* all the case statements fall through */
288 case 0: /* case 0: nothing left to add */
291 /*---------------------------------------------- report the result */
296 #if (CAA_BITS_PER_LONG == 32)
298 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
300 unsigned long key
= (unsigned long) _key
;
303 assert(length
== sizeof(unsigned long));
304 return hash_u32(&v
, 1, seed
);
308 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
319 assert(length
== sizeof(unsigned long));
320 v
.v64
= (uint64_t) seed
;
321 key
.v64
= (uint64_t) _key
;
322 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
328 unsigned long test_compare(void *key1
, size_t key1_len
,
329 void *key2
, size_t key2_len
)
331 if (unlikely(key1_len
!= key2_len
))
333 assert(key1_len
== sizeof(unsigned long));
340 void *thr_reader(void *_count
)
342 unsigned long long *count
= _count
;
343 struct cds_lfht_node
*node
;
345 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
346 "reader", pthread_self(), (unsigned long)gettid());
350 rcu_register_thread();
359 node
= cds_lfht_lookup(test_ht
,
360 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
367 if (unlikely(rduration
))
368 loop_sleep(rduration
);
371 if (unlikely(!test_duration_read()))
373 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
374 rcu_quiescent_state();
377 rcu_unregister_thread();
380 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
381 "reader", pthread_self(), (unsigned long)gettid());
382 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
383 pthread_self(), lookup_fail
, lookup_ok
);
389 void free_node_cb(struct rcu_head
*head
)
391 struct cds_lfht_node
*node
=
392 caa_container_of(head
, struct cds_lfht_node
, head
);
396 void *thr_writer(void *_count
)
398 struct cds_lfht_node
*node
, *ret_node
;
399 struct wr_count
*count
= _count
;
402 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
403 "writer", pthread_self(), (unsigned long)gettid());
407 rcu_register_thread();
415 if (add_only
|| rand_r(&rand_lookup
) & 1) {
416 node
= malloc(sizeof(struct cds_lfht_node
));
418 cds_lfht_node_init(node
,
419 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
422 ret_node
= cds_lfht_add_unique(test_ht
, node
);
424 cds_lfht_add(test_ht
, node
);
426 if (add_unique
&& ret_node
!= node
) {
434 node
= cds_lfht_lookup(test_ht
,
435 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
438 ret
= cds_lfht_remove(test_ht
, node
);
443 call_rcu(&node
->head
, free_node_cb
);
449 //if (nr_writes % 100000 == 0) {
450 if (nr_writes
% 1000 == 0) {
452 if (rand_r(&rand_lookup
) & 1) {
453 ht_resize(test_ht
, 1);
455 ht_resize(test_ht
, -1);
461 if (unlikely(!test_duration_write()))
463 if (unlikely(wdelay
))
465 if (unlikely((nr_writes
& ((1 << 10) - 1)) == 0))
466 rcu_quiescent_state();
469 rcu_unregister_thread();
471 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
472 "writer", pthread_self(), (unsigned long)gettid());
473 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
474 "nr_delnoent %lu\n", pthread_self(), nr_add
,
475 nr_addexist
, nr_del
, nr_delnoent
);
476 count
->update_ops
= nr_writes
;
478 count
->add_exist
= nr_addexist
;
479 count
->remove
= nr_del
;
483 static void populate_hash(void)
485 struct cds_lfht_node
*node
, *ret_node
;
490 while (nr_add
< init_populate
) {
491 node
= malloc(sizeof(struct cds_lfht_node
));
492 cds_lfht_node_init(node
,
493 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
496 ret_node
= cds_lfht_add_unique(test_ht
, node
);
498 cds_lfht_add(test_ht
, node
);
499 if (add_unique
&& ret_node
!= node
) {
508 void show_usage(int argc
, char **argv
)
510 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
512 printf(" [-r] [-w] (yield reader and/or writer)");
514 printf(" [-d delay] (writer period (us))");
515 printf(" [-c duration] (reader C.S. duration (in loops))");
516 printf(" [-v] (verbose output)");
517 printf(" [-a cpu#] [-a cpu#]... (affinity)");
518 printf(" [-p size] (random key value pool size)");
519 printf(" [-h size] (initial hash table size)");
520 printf(" [-u] Uniquify add.");
521 printf(" [-i] Add only (no removal).");
522 printf(" [-k nr_nodes] Number of nodes to insert initially.");
526 int main(int argc
, char **argv
)
529 pthread_t
*tid_reader
, *tid_writer
;
531 unsigned long long *count_reader
;
532 struct wr_count
*count_writer
;
533 unsigned long long tot_reads
= 0, tot_writes
= 0,
534 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
535 unsigned long count
, removed
;
539 show_usage(argc
, argv
);
543 err
= sscanf(argv
[1], "%u", &nr_readers
);
545 show_usage(argc
, argv
);
549 err
= sscanf(argv
[2], "%u", &nr_writers
);
551 show_usage(argc
, argv
);
555 err
= sscanf(argv
[3], "%lu", &duration
);
557 show_usage(argc
, argv
);
561 for (i
= 4; i
< argc
; i
++) {
562 if (argv
[i
][0] != '-')
564 switch (argv
[i
][1]) {
567 yield_active
|= YIELD_READ
;
570 yield_active
|= YIELD_WRITE
;
575 show_usage(argc
, argv
);
579 cpu_affinities
[next_aff
++] = a
;
581 printf_verbose("Adding CPU %d affinity\n", a
);
585 show_usage(argc
, argv
);
588 rduration
= atol(argv
[++i
]);
592 show_usage(argc
, argv
);
595 wdelay
= atol(argv
[++i
]);
602 show_usage(argc
, argv
);
605 rand_pool
= atol(argv
[++i
]);
609 show_usage(argc
, argv
);
612 init_hash_size
= atol(argv
[++i
]);
621 init_populate
= atol(argv
[++i
]);
626 /* Check if hash size is power of 2 */
627 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
628 printf("Error: Hash table size %lu is not a power of 2.\n",
633 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
634 duration
, nr_readers
, nr_writers
);
635 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
636 printf_verbose("Reader duration : %lu loops.\n", rduration
);
637 printf_verbose("Random pool size : %lu.\n", rand_pool
);
638 printf_verbose("Mode:%s%s.\n",
639 add_only
? " add only" : " add/remove",
640 add_unique
? " uniquify" : "");
641 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size
);
642 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
643 "main", pthread_self(), (unsigned long)gettid());
645 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
646 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
647 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
648 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
649 test_ht
= cds_lfht_new(test_hash
, test_compare
, 0x42UL
,
650 init_hash_size
, call_rcu
);
652 err
= create_all_cpu_call_rcu_data(0);
657 for (i
= 0; i
< nr_readers
; i
++) {
658 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
663 for (i
= 0; i
< nr_writers
; i
++) {
664 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
678 for (i
= 0; i
< nr_readers
; i
++) {
679 err
= pthread_join(tid_reader
[i
], &tret
);
682 tot_reads
+= count_reader
[i
];
684 for (i
= 0; i
< nr_writers
; i
++) {
685 err
= pthread_join(tid_writer
[i
], &tret
);
688 tot_writes
+= count_writer
[i
].update_ops
;
689 tot_add
+= count_writer
[i
].add
;
690 tot_add_exist
+= count_writer
[i
].add_exist
;
691 tot_remove
+= count_writer
[i
].remove
;
693 printf("Counting nodes... ");
695 cds_lfht_count_nodes(test_ht
, &count
, &removed
);
697 if (count
|| removed
)
698 printf("WARNING: nodes left in the hash table upon destroy: "
699 "%lu nodes + %lu logically removed.\n", count
, removed
);
700 ret
= cds_lfht_destroy(test_ht
);
703 printf_verbose("final delete aborted\n");
705 printf_verbose("final delete success\n");
706 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
708 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
710 "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
711 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
712 argv
[0], duration
, nr_readers
, rduration
,
713 nr_writers
, wdelay
, rand_pool
, tot_reads
, tot_writes
,
714 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
715 (long long) tot_add
+ init_populate
- tot_remove
- count
);