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 unsigned long rand_pool
= DEFAULT_RAND_POOL
;
119 static int opt_auto_resize
;
120 static int add_only
, add_unique
;
122 static unsigned long init_pool_offset
, lookup_pool_offset
, write_pool_offset
;
124 static inline void loop_sleep(unsigned long l
)
130 static int verbose_mode
;
132 #define printf_verbose(fmt, args...) \
135 printf(fmt, ## args); \
138 static unsigned int cpu_affinities
[NR_CPUS
];
139 static unsigned int next_aff
= 0;
140 static int use_affinity
= 0;
142 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
144 static void set_affinity(void)
153 ret
= pthread_mutex_lock(&affinity_mutex
);
155 perror("Error in pthread mutex lock");
158 cpu
= cpu_affinities
[next_aff
++];
159 ret
= pthread_mutex_unlock(&affinity_mutex
);
161 perror("Error in pthread mutex unlock");
166 sched_setaffinity(0, sizeof(mask
), &mask
);
173 } addremove
; /* 1: add, -1 remove, 0: random */
176 void sigusr1_handler(int signo
)
180 printf("Add/Remove: random.\n");
181 addremove
= AR_RANDOM
;
184 printf("Add/Remove: remove only.\n");
185 addremove
= AR_REMOVE
;
188 printf("Add/Remove: add only.\n");
195 * returns 0 if test should end.
197 static int test_duration_write(void)
202 static int test_duration_read(void)
207 static unsigned long long __thread nr_writes
;
208 static unsigned long long __thread nr_reads
;
210 static unsigned int nr_readers
;
211 static unsigned int nr_writers
;
213 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
215 void rcu_copy_mutex_lock(void)
218 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
220 perror("Error in pthread mutex lock");
225 void rcu_copy_mutex_unlock(void)
229 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
231 perror("Error in pthread mutex unlock");
238 * Source: http://burtleburtle.net/bob/c/lookup3.c
239 * Originally Public Domain
242 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
244 #define mix(a, b, c) \
246 a -= c; a ^= rot(c, 4); c += b; \
247 b -= a; b ^= rot(a, 6); a += c; \
248 c -= b; c ^= rot(b, 8); b += a; \
249 a -= c; a ^= rot(c, 16); c += b; \
250 b -= a; b ^= rot(a, 19); a += c; \
251 c -= b; c ^= rot(b, 4); b += a; \
254 #define final(a, b, c) \
256 c ^= b; c -= rot(b, 14); \
257 a ^= c; a -= rot(c, 11); \
258 b ^= a; b -= rot(a, 25); \
259 c ^= b; c -= rot(b, 16); \
260 a ^= c; a -= rot(c, 4);\
261 b ^= a; b -= rot(a, 14); \
262 c ^= b; c -= rot(b, 24); \
265 static __attribute__((unused
))
267 const uint32_t *k
, /* the key, an array of uint32_t values */
268 size_t length
, /* the length of the key, in uint32_ts */
269 uint32_t initval
) /* the previous hash, or an arbitrary value */
273 /* Set up the internal state */
274 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
276 /*----------------------------------------- handle most of the key */
286 /*----------------------------------- handle the last 3 uint32_t's */
287 switch (length
) { /* all the case statements fall through */
292 case 0: /* case 0: nothing left to add */
295 /*---------------------------------------------- report the result */
301 const uint32_t *k
, /* the key, an array of uint32_t values */
302 size_t length
, /* the length of the key, in uint32_ts */
303 uint32_t *pc
, /* IN: seed OUT: primary hash value */
304 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
308 /* Set up the internal state */
309 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
312 /*----------------------------------------- handle most of the key */
322 /*----------------------------------- handle the last 3 uint32_t's */
323 switch (length
) { /* all the case statements fall through */
328 case 0: /* case 0: nothing left to add */
331 /*---------------------------------------------- report the result */
336 #if (CAA_BITS_PER_LONG == 32)
338 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
340 unsigned long key
= (unsigned long) _key
;
343 assert(length
== sizeof(unsigned long));
344 return hash_u32(&v
, 1, seed
);
348 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
359 assert(length
== sizeof(unsigned long));
360 v
.v64
= (uint64_t) seed
;
361 key
.v64
= (uint64_t) _key
;
362 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
368 unsigned long test_compare(void *key1
, size_t key1_len
,
369 void *key2
, size_t key2_len
)
371 if (unlikely(key1_len
!= key2_len
))
373 assert(key1_len
== sizeof(unsigned long));
380 void *thr_reader(void *_count
)
382 unsigned long long *count
= _count
;
383 struct cds_lfht_node
*node
;
385 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
386 "reader", pthread_self(), (unsigned long)gettid());
390 rcu_register_thread();
399 node
= cds_lfht_lookup(test_ht
,
400 (void *)(unsigned long)((rand_r(&rand_lookup
) % rand_pool
) + lookup_pool_offset
),
407 if (unlikely(rduration
))
408 loop_sleep(rduration
);
411 if (unlikely(!test_duration_read()))
413 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
414 rcu_quiescent_state();
417 rcu_unregister_thread();
420 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
421 "reader", pthread_self(), (unsigned long)gettid());
422 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
423 pthread_self(), lookup_fail
, lookup_ok
);
429 void free_node_cb(struct rcu_head
*head
)
431 struct cds_lfht_node
*node
=
432 caa_container_of(head
, struct cds_lfht_node
, head
);
436 void *thr_writer(void *_count
)
438 struct cds_lfht_node
*node
, *ret_node
;
439 struct wr_count
*count
= _count
;
442 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
443 "writer", pthread_self(), (unsigned long)gettid());
447 rcu_register_thread();
455 if ((addremove
== AR_ADD
|| add_only
)
456 || (addremove
== AR_RANDOM
&& rand_r(&rand_lookup
) & 1)) {
457 node
= malloc(sizeof(struct cds_lfht_node
));
459 cds_lfht_node_init(node
,
460 (void *)(unsigned long)((rand_r(&rand_lookup
) % rand_pool
) + write_pool_offset
),
463 ret_node
= cds_lfht_add_unique(test_ht
, node
);
465 cds_lfht_add(test_ht
, node
);
467 if (add_unique
&& ret_node
!= node
) {
475 node
= cds_lfht_lookup(test_ht
,
476 (void *)(unsigned long)((rand_r(&rand_lookup
) % rand_pool
) + write_pool_offset
),
479 ret
= cds_lfht_remove(test_ht
, node
);
484 call_rcu(&node
->head
, free_node_cb
);
490 //if (nr_writes % 100000 == 0) {
491 if (nr_writes
% 1000 == 0) {
493 if (rand_r(&rand_lookup
) & 1) {
494 ht_resize(test_ht
, 1);
496 ht_resize(test_ht
, -1);
502 if (unlikely(!test_duration_write()))
504 if (unlikely(wdelay
))
506 if (unlikely((nr_writes
& ((1 << 10) - 1)) == 0))
507 rcu_quiescent_state();
510 rcu_unregister_thread();
512 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
513 "writer", pthread_self(), (unsigned long)gettid());
514 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
515 "nr_delnoent %lu\n", pthread_self(), nr_add
,
516 nr_addexist
, nr_del
, nr_delnoent
);
517 count
->update_ops
= nr_writes
;
519 count
->add_exist
= nr_addexist
;
520 count
->remove
= nr_del
;
524 static int populate_hash(void)
526 struct cds_lfht_node
*node
, *ret_node
;
531 if (add_unique
&& init_populate
* 10 > rand_pool
) {
532 printf("WARNING: required to populate %lu nodes (-k), but random "
533 "pool is quite small (%lu values) and we are in add_unique (-u) mode. Try with a "
534 "larger random pool (-p option).\n", init_populate
, rand_pool
);
538 while (nr_add
< init_populate
) {
539 node
= malloc(sizeof(struct cds_lfht_node
));
540 cds_lfht_node_init(node
,
541 (void *)(unsigned long)((rand_r(&rand_lookup
) % rand_pool
) + init_pool_offset
),
544 ret_node
= cds_lfht_add_unique(test_ht
, node
);
546 cds_lfht_add(test_ht
, node
);
547 if (add_unique
&& ret_node
!= node
) {
557 void show_usage(int argc
, char **argv
)
559 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
561 printf(" [-r] [-w] (yield reader and/or writer)");
563 printf(" [-d delay] (writer period (us))");
564 printf(" [-c duration] (reader C.S. duration (in loops))");
565 printf(" [-v] (verbose output)");
566 printf(" [-a cpu#] [-a cpu#]... (affinity)");
567 printf(" [-p size] (random key value pool size)");
568 printf(" [-h size] (initial hash table size)");
569 printf(" [-u] Uniquify add.");
570 printf(" [-i] Add only (no removal).");
571 printf(" [-k nr_nodes] Number of nodes to insert initially.");
572 printf(" [-A] Automatically resize hash table.");
573 printf(" [-R offset] Lookup pool offset\n");
574 printf(" [-S offset] Write pool offset\n");
575 printf(" [-T offset] Init pool offset\n");
579 int main(int argc
, char **argv
)
582 pthread_t
*tid_reader
, *tid_writer
;
584 unsigned long long *count_reader
;
585 struct wr_count
*count_writer
;
586 unsigned long long tot_reads
= 0, tot_writes
= 0,
587 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
588 unsigned long count
, removed
;
590 struct sigaction act
;
594 show_usage(argc
, argv
);
598 err
= sscanf(argv
[1], "%u", &nr_readers
);
600 show_usage(argc
, argv
);
604 err
= sscanf(argv
[2], "%u", &nr_writers
);
606 show_usage(argc
, argv
);
610 err
= sscanf(argv
[3], "%lu", &duration
);
612 show_usage(argc
, argv
);
616 for (i
= 4; i
< argc
; i
++) {
617 if (argv
[i
][0] != '-')
619 switch (argv
[i
][1]) {
622 yield_active
|= YIELD_READ
;
625 yield_active
|= YIELD_WRITE
;
630 show_usage(argc
, argv
);
634 cpu_affinities
[next_aff
++] = a
;
636 printf_verbose("Adding CPU %d affinity\n", a
);
640 show_usage(argc
, argv
);
643 rduration
= atol(argv
[++i
]);
647 show_usage(argc
, argv
);
650 wdelay
= atol(argv
[++i
]);
657 show_usage(argc
, argv
);
660 rand_pool
= atol(argv
[++i
]);
664 show_usage(argc
, argv
);
667 init_hash_size
= atol(argv
[++i
]);
676 init_populate
= atol(argv
[++i
]);
682 lookup_pool_offset
= atol(argv
[++i
]);
685 write_pool_offset
= atol(argv
[++i
]);
688 init_pool_offset
= atol(argv
[++i
]);
694 /* Check if hash size is power of 2 */
695 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
696 printf("Error: Hash table size %lu is not a power of 2.\n",
701 memset(&act
, 0, sizeof(act
));
702 ret
= sigemptyset(&act
.sa_mask
);
704 perror("sigemptyset");
707 act
.sa_handler
= sigusr1_handler
;
708 act
.sa_flags
= SA_RESTART
;
709 ret
= sigaction(SIGUSR1
, &act
, NULL
);
715 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
716 duration
, nr_readers
, nr_writers
);
717 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
718 printf_verbose("Reader duration : %lu loops.\n", rduration
);
719 printf_verbose("Random pool size : %lu.\n", rand_pool
);
720 printf_verbose("Mode:%s%s.\n",
721 add_only
? " add only" : " add/remove",
722 add_unique
? " uniquify" : "");
723 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size
);
724 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
725 "main", pthread_self(), (unsigned long)gettid());
727 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
728 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
729 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
730 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
731 test_ht
= cds_lfht_new(test_hash
, test_compare
, 0x42UL
,
733 opt_auto_resize
? CDS_LFHT_AUTO_RESIZE
: 0,
734 call_rcu
, synchronize_rcu
, rcu_read_lock
,
735 rcu_read_unlock
, rcu_thread_offline
,
737 ret
= populate_hash();
739 err
= create_all_cpu_call_rcu_data(0);
744 for (i
= 0; i
< nr_readers
; i
++) {
745 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
750 for (i
= 0; i
< nr_writers
; i
++) {
751 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
763 remain
= sleep(remain
);
764 } while (remain
> 0);
768 for (i
= 0; i
< nr_readers
; i
++) {
769 err
= pthread_join(tid_reader
[i
], &tret
);
772 tot_reads
+= count_reader
[i
];
774 for (i
= 0; i
< nr_writers
; i
++) {
775 err
= pthread_join(tid_writer
[i
], &tret
);
778 tot_writes
+= count_writer
[i
].update_ops
;
779 tot_add
+= count_writer
[i
].add
;
780 tot_add_exist
+= count_writer
[i
].add_exist
;
781 tot_remove
+= count_writer
[i
].remove
;
783 printf("Counting nodes... ");
785 cds_lfht_count_nodes(test_ht
, &count
, &removed
);
787 if (count
|| removed
)
788 printf("WARNING: nodes left in the hash table upon destroy: "
789 "%lu nodes + %lu logically removed.\n", count
, removed
);
790 ret
= cds_lfht_destroy(test_ht
);
793 printf_verbose("final delete aborted\n");
795 printf_verbose("final delete success\n");
796 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
798 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
800 "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
801 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
802 argv
[0], duration
, nr_readers
, rduration
,
803 nr_writers
, wdelay
, rand_pool
, tot_reads
, tot_writes
,
804 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
805 (long long) tot_add
+ init_populate
- tot_remove
- count
);
806 free_all_cpu_call_rcu_data();