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()
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 rand_pool
= DEFAULT_RAND_POOL
;
106 static int add_only
, add_unique
;
108 static inline void loop_sleep(unsigned long l
)
114 static int verbose_mode
;
116 #define printf_verbose(fmt, args...) \
119 printf(fmt, ## args); \
122 static unsigned int cpu_affinities
[NR_CPUS
];
123 static unsigned int next_aff
= 0;
124 static int use_affinity
= 0;
126 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
128 static void set_affinity(void)
137 ret
= pthread_mutex_lock(&affinity_mutex
);
139 perror("Error in pthread mutex lock");
142 cpu
= cpu_affinities
[next_aff
++];
143 ret
= pthread_mutex_unlock(&affinity_mutex
);
145 perror("Error in pthread mutex unlock");
150 sched_setaffinity(0, sizeof(mask
), &mask
);
154 * returns 0 if test should end.
156 static int test_duration_write(void)
161 static int test_duration_read(void)
166 static unsigned long long __thread nr_writes
;
167 static unsigned long long __thread nr_reads
;
169 static unsigned int nr_readers
;
170 static unsigned int nr_writers
;
172 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
174 void rcu_copy_mutex_lock(void)
177 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
179 perror("Error in pthread mutex lock");
184 void rcu_copy_mutex_unlock(void)
188 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
190 perror("Error in pthread mutex unlock");
197 * Source: http://burtleburtle.net/bob/c/lookup3.c
198 * Originally Public Domain
201 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
203 #define mix(a, b, c) \
205 a -= c; a ^= rot(c, 4); c += b; \
206 b -= a; b ^= rot(a, 6); a += c; \
207 c -= b; c ^= rot(b, 8); b += a; \
208 a -= c; a ^= rot(c, 16); c += b; \
209 b -= a; b ^= rot(a, 19); a += c; \
210 c -= b; c ^= rot(b, 4); b += a; \
213 #define final(a, b, c) \
215 c ^= b; c -= rot(b, 14); \
216 a ^= c; a -= rot(c, 11); \
217 b ^= a; b -= rot(a, 25); \
218 c ^= b; c -= rot(b, 16); \
219 a ^= c; a -= rot(c, 4);\
220 b ^= a; b -= rot(a, 14); \
221 c ^= b; c -= rot(b, 24); \
224 static __attribute__((unused
))
226 const uint32_t *k
, /* the key, an array of uint32_t values */
227 size_t length
, /* the length of the key, in uint32_ts */
228 uint32_t initval
) /* the previous hash, or an arbitrary value */
232 /* Set up the internal state */
233 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
235 /*----------------------------------------- handle most of the key */
245 /*----------------------------------- handle the last 3 uint32_t's */
246 switch (length
) { /* all the case statements fall through */
251 case 0: /* case 0: nothing left to add */
254 /*---------------------------------------------- report the result */
260 const uint32_t *k
, /* the key, an array of uint32_t values */
261 size_t length
, /* the length of the key, in uint32_ts */
262 uint32_t *pc
, /* IN: seed OUT: primary hash value */
263 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
267 /* Set up the internal state */
268 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
271 /*----------------------------------------- handle most of the key */
281 /*----------------------------------- handle the last 3 uint32_t's */
282 switch (length
) { /* all the case statements fall through */
287 case 0: /* case 0: nothing left to add */
290 /*---------------------------------------------- report the result */
295 #if (CAA_BITS_PER_LONG == 32)
297 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
299 unsigned long key
= (unsigned long) _key
;
302 assert(length
== sizeof(unsigned long));
303 return hash_u32(&v
, 1, seed
);
307 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
318 assert(length
== sizeof(unsigned long));
319 v
.v64
= (uint64_t) seed
;
320 key
.v64
= (uint64_t) _key
;
321 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
327 unsigned long test_compare(void *key1
, size_t key1_len
,
328 void *key2
, size_t key2_len
)
330 if (unlikely(key1_len
!= key2_len
))
332 assert(key1_len
== sizeof(unsigned long));
339 void *thr_reader(void *_count
)
341 unsigned long long *count
= _count
;
342 struct cds_lfht_node
*node
;
344 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
345 "reader", pthread_self(), (unsigned long)gettid());
349 rcu_register_thread();
358 node
= cds_lfht_lookup(test_ht
,
359 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
366 if (unlikely(rduration
))
367 loop_sleep(rduration
);
370 if (unlikely(!test_duration_read()))
374 rcu_unregister_thread();
377 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
378 "reader", pthread_self(), (unsigned long)gettid());
379 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
380 pthread_self(), lookup_fail
, lookup_ok
);
386 void free_node_cb(struct rcu_head
*head
)
388 struct cds_lfht_node
*node
=
389 caa_container_of(head
, struct cds_lfht_node
, head
);
393 void *thr_writer(void *_count
)
395 struct cds_lfht_node
*node
, *ret_node
;
396 struct wr_count
*count
= _count
;
399 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
400 "writer", pthread_self(), (unsigned long)gettid());
404 rcu_register_thread();
412 if (add_only
|| rand_r(&rand_lookup
) & 1) {
413 node
= malloc(sizeof(struct cds_lfht_node
));
415 cds_lfht_node_init(node
,
416 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
419 ret_node
= cds_lfht_add_unique(test_ht
, node
);
421 cds_lfht_add(test_ht
, node
);
423 if (add_unique
&& ret_node
!= node
) {
431 node
= cds_lfht_lookup(test_ht
,
432 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
435 ret
= cds_lfht_remove(test_ht
, node
);
440 call_rcu(&node
->head
, free_node_cb
);
446 //if (nr_writes % 100000 == 0) {
447 if (nr_writes
% 1000 == 0) {
449 if (rand_r(&rand_lookup
) & 1) {
450 ht_resize(test_ht
, 1);
452 ht_resize(test_ht
, -1);
458 if (unlikely(!test_duration_write()))
460 if (unlikely(wdelay
))
464 rcu_unregister_thread();
466 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
467 "writer", pthread_self(), (unsigned long)gettid());
468 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
469 "nr_delnoent %lu\n", pthread_self(), nr_add
,
470 nr_addexist
, nr_del
, nr_delnoent
);
471 count
->update_ops
= nr_writes
;
473 count
->add_exist
= nr_addexist
;
474 count
->remove
= nr_del
;
478 void show_usage(int argc
, char **argv
)
480 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
482 printf(" [-r] [-w] (yield reader and/or writer)");
484 printf(" [-d delay] (writer period (us))");
485 printf(" [-c duration] (reader C.S. duration (in loops))");
486 printf(" [-v] (verbose output)");
487 printf(" [-a cpu#] [-a cpu#]... (affinity)");
488 printf(" [-p size] (random key value pool size)");
489 printf(" [-h size] (initial hash table size)");
490 printf(" [-u] Uniquify add.");
491 printf(" [-i] Add only (no removal).");
495 int main(int argc
, char **argv
)
498 pthread_t
*tid_reader
, *tid_writer
;
500 unsigned long long *count_reader
;
501 struct wr_count
*count_writer
;
502 unsigned long long tot_reads
= 0, tot_writes
= 0,
503 tot_add
= 0, tot_add_exist
= 0, tot_remove
= 0;
504 unsigned long count
, removed
;
508 show_usage(argc
, argv
);
512 err
= sscanf(argv
[1], "%u", &nr_readers
);
514 show_usage(argc
, argv
);
518 err
= sscanf(argv
[2], "%u", &nr_writers
);
520 show_usage(argc
, argv
);
524 err
= sscanf(argv
[3], "%lu", &duration
);
526 show_usage(argc
, argv
);
530 for (i
= 4; i
< argc
; i
++) {
531 if (argv
[i
][0] != '-')
533 switch (argv
[i
][1]) {
536 yield_active
|= YIELD_READ
;
539 yield_active
|= YIELD_WRITE
;
544 show_usage(argc
, argv
);
548 cpu_affinities
[next_aff
++] = a
;
550 printf_verbose("Adding CPU %d affinity\n", a
);
554 show_usage(argc
, argv
);
557 rduration
= atol(argv
[++i
]);
561 show_usage(argc
, argv
);
564 wdelay
= atol(argv
[++i
]);
571 show_usage(argc
, argv
);
574 rand_pool
= atol(argv
[++i
]);
578 show_usage(argc
, argv
);
581 init_hash_size
= atol(argv
[++i
]);
592 /* Check if hash size is power of 2 */
593 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
594 printf("Error: Hash table size %lu is not a power of 2.\n",
599 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
600 duration
, nr_readers
, nr_writers
);
601 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
602 printf_verbose("Reader duration : %lu loops.\n", rduration
);
603 printf_verbose("Random pool size : %lu.\n", rand_pool
);
604 printf_verbose("Mode:%s%s.\n",
605 add_only
? " add only" : " add/remove",
606 add_unique
? " uniquify" : "");
607 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size
);
608 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
609 "main", pthread_self(), (unsigned long)gettid());
611 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
612 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
613 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
614 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
615 test_ht
= cds_lfht_new(test_hash
, test_compare
, 0x42UL
,
616 init_hash_size
, call_rcu
);
618 err
= create_all_cpu_call_rcu_data(0);
623 for (i
= 0; i
< nr_readers
; i
++) {
624 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
629 for (i
= 0; i
< nr_writers
; i
++) {
630 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
644 for (i
= 0; i
< nr_readers
; i
++) {
645 err
= pthread_join(tid_reader
[i
], &tret
);
648 tot_reads
+= count_reader
[i
];
650 for (i
= 0; i
< nr_writers
; i
++) {
651 err
= pthread_join(tid_writer
[i
], &tret
);
654 tot_writes
+= count_writer
[i
].update_ops
;
655 tot_add
+= count_writer
[i
].add
;
656 tot_add_exist
+= count_writer
[i
].add_exist
;
657 tot_remove
+= count_writer
[i
].remove
;
659 printf("Counting nodes... ");
661 cds_lfht_count_nodes(test_ht
, &count
, &removed
);
663 if (count
|| removed
)
664 printf("WARNING: nodes left in the hash table upon destroy: "
665 "%lu nodes + %lu logically removed.\n", count
, removed
);
666 ret
= cds_lfht_destroy(test_ht
);
669 printf_verbose("final delete aborted\n");
671 printf_verbose("final delete success\n");
672 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
674 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
676 "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
677 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12llu\n",
678 argv
[0], duration
, nr_readers
, rduration
,
679 nr_writers
, wdelay
, rand_pool
, tot_reads
, tot_writes
,
680 tot_reads
+ tot_writes
, tot_add
, tot_add_exist
, tot_remove
,
681 tot_add
- tot_remove
- count
);