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
;
79 static unsigned int __thread rand_lookup
;
80 static unsigned long __thread nr_add
;
81 static unsigned long __thread nr_addexist
;
82 static unsigned long __thread nr_del
;
83 static unsigned long __thread nr_delnoent
;
84 static unsigned long __thread lookup_fail
;
85 static unsigned long __thread lookup_ok
;
87 static struct rcu_ht
*test_ht
;
94 static volatile int test_go
, test_stop
;
96 static unsigned long wdelay
;
98 static unsigned long duration
;
100 /* read-side C.S. duration, in loops */
101 static unsigned long rduration
;
103 static unsigned long init_hash_size
= DEFAULT_HASH_SIZE
;
104 static unsigned long rand_pool
= DEFAULT_RAND_POOL
;
105 static int add_only
, add_unique
;
107 static inline void loop_sleep(unsigned long l
)
113 static int verbose_mode
;
115 #define printf_verbose(fmt, args...) \
121 static unsigned int cpu_affinities
[NR_CPUS
];
122 static unsigned int next_aff
= 0;
123 static int use_affinity
= 0;
125 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
127 static void set_affinity(void)
136 ret
= pthread_mutex_lock(&affinity_mutex
);
138 perror("Error in pthread mutex lock");
141 cpu
= cpu_affinities
[next_aff
++];
142 ret
= pthread_mutex_unlock(&affinity_mutex
);
144 perror("Error in pthread mutex unlock");
149 sched_setaffinity(0, sizeof(mask
), &mask
);
153 * returns 0 if test should end.
155 static int test_duration_write(void)
160 static int test_duration_read(void)
165 static unsigned long long __thread nr_writes
;
166 static unsigned long long __thread nr_reads
;
168 static unsigned int nr_readers
;
169 static unsigned int nr_writers
;
171 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
173 void rcu_copy_mutex_lock(void)
176 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
178 perror("Error in pthread mutex lock");
183 void rcu_copy_mutex_unlock(void)
187 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
189 perror("Error in pthread mutex unlock");
196 * Source: http://burtleburtle.net/bob/c/lookup3.c
197 * Originally Public Domain
200 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
202 #define mix(a, b, c) \
204 a -= c; a ^= rot(c, 4); c += b; \
205 b -= a; b ^= rot(a, 6); a += c; \
206 c -= b; c ^= rot(b, 8); b += a; \
207 a -= c; a ^= rot(c, 16); c += b; \
208 b -= a; b ^= rot(a, 19); a += c; \
209 c -= b; c ^= rot(b, 4); b += a; \
212 #define final(a, b, c) \
214 c ^= b; c -= rot(b, 14); \
215 a ^= c; a -= rot(c, 11); \
216 b ^= a; b -= rot(a, 25); \
217 c ^= b; c -= rot(b, 16); \
218 a ^= c; a -= rot(c, 4);\
219 b ^= a; b -= rot(a, 14); \
220 c ^= b; c -= rot(b, 24); \
223 static __attribute__((unused
))
225 const uint32_t *k
, /* the key, an array of uint32_t values */
226 size_t length
, /* the length of the key, in uint32_ts */
227 uint32_t initval
) /* the previous hash, or an arbitrary value */
231 /* Set up the internal state */
232 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
234 /*----------------------------------------- handle most of the key */
244 /*----------------------------------- handle the last 3 uint32_t's */
245 switch (length
) { /* all the case statements fall through */
250 case 0: /* case 0: nothing left to add */
253 /*---------------------------------------------- report the result */
259 const uint32_t *k
, /* the key, an array of uint32_t values */
260 size_t length
, /* the length of the key, in uint32_ts */
261 uint32_t *pc
, /* IN: seed OUT: primary hash value */
262 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
266 /* Set up the internal state */
267 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
270 /*----------------------------------------- handle most of the key */
280 /*----------------------------------- handle the last 3 uint32_t's */
281 switch (length
) { /* all the case statements fall through */
286 case 0: /* case 0: nothing left to add */
289 /*---------------------------------------------- report the result */
294 #if (CAA_BITS_PER_LONG == 32)
296 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
298 unsigned long key
= (unsigned long) _key
;
301 assert(length
== sizeof(unsigned long));
302 return hash_u32(&v
, 1, seed
);
306 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
317 assert(length
== sizeof(unsigned long));
318 v
.v64
= (uint64_t) seed
;
319 key
.v64
= (uint64_t) _key
;
320 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
326 unsigned long test_compare(void *key1
, size_t key1_len
,
327 void *key2
, size_t key2_len
)
329 if (unlikely(key1_len
!= key2_len
))
331 assert(key1_len
== sizeof(unsigned long));
338 void *thr_reader(void *_count
)
340 unsigned long long *count
= _count
;
341 struct rcu_ht_node
*node
;
343 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
344 "reader", pthread_self(), (unsigned long)gettid());
348 rcu_register_thread();
357 node
= ht_lookup(test_ht
,
358 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
365 if (unlikely(rduration
))
366 loop_sleep(rduration
);
369 if (unlikely(!test_duration_read()))
373 rcu_unregister_thread();
376 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
377 "reader", pthread_self(), (unsigned long)gettid());
378 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
379 pthread_self(), lookup_fail
, lookup_ok
);
385 void free_node_cb(struct rcu_head
*head
)
387 struct rcu_ht_node
*node
=
388 caa_container_of(head
, struct rcu_ht_node
, head
);
392 void *thr_writer(void *_count
)
394 struct rcu_ht_node
*node
, *ret_node
;
395 struct wr_count
*count
= _count
;
398 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
399 "writer", pthread_self(), (unsigned long)gettid());
403 rcu_register_thread();
411 if (add_only
|| rand_r(&rand_lookup
) & 1) {
412 node
= malloc(sizeof(struct rcu_ht_node
));
415 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
418 ret_node
= ht_add_unique(test_ht
, node
);
420 ht_add(test_ht
, node
);
422 if (add_unique
&& ret_node
!= node
) {
430 node
= ht_lookup(test_ht
,
431 (void *)(unsigned long)(rand_r(&rand_lookup
) % rand_pool
),
434 ret
= ht_remove(test_ht
, node
);
439 call_rcu(&node
->head
, free_node_cb
);
445 //if (nr_writes % 100000 == 0) {
446 if (nr_writes
% 1000 == 0) {
448 if (rand_r(&rand_lookup
) & 1) {
449 ht_resize(test_ht
, 1);
451 ht_resize(test_ht
, -1);
457 if (unlikely(!test_duration_write()))
459 if (unlikely(wdelay
))
463 rcu_unregister_thread();
465 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
466 "writer", pthread_self(), (unsigned long)gettid());
467 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
468 "nr_delnoent %lu\n", pthread_self(), nr_add
,
469 nr_addexist
, nr_del
, nr_delnoent
);
470 count
->update_ops
= nr_writes
;
472 count
->remove
= nr_del
;
476 void show_usage(int argc
, char **argv
)
478 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
480 printf(" [-r] [-w] (yield reader and/or writer)");
482 printf(" [-d delay] (writer period (us))");
483 printf(" [-c duration] (reader C.S. duration (in loops))");
484 printf(" [-v] (verbose output)");
485 printf(" [-a cpu#] [-a cpu#]... (affinity)");
486 printf(" [-p size] (random key value pool size)");
487 printf(" [-h size] (initial hash table size)");
488 printf(" [-u] Uniquify add.");
489 printf(" [-i] Add only (no removal).");
493 int main(int argc
, char **argv
)
496 pthread_t
*tid_reader
, *tid_writer
;
498 unsigned long long *count_reader
;
499 struct wr_count
*count_writer
;
500 unsigned long long tot_reads
= 0, tot_writes
= 0,
501 tot_add
= 0, tot_remove
= 0;
502 unsigned long count
, removed
;
506 show_usage(argc
, argv
);
510 err
= sscanf(argv
[1], "%u", &nr_readers
);
512 show_usage(argc
, argv
);
516 err
= sscanf(argv
[2], "%u", &nr_writers
);
518 show_usage(argc
, argv
);
522 err
= sscanf(argv
[3], "%lu", &duration
);
524 show_usage(argc
, argv
);
528 for (i
= 4; i
< argc
; i
++) {
529 if (argv
[i
][0] != '-')
531 switch (argv
[i
][1]) {
534 yield_active
|= YIELD_READ
;
537 yield_active
|= YIELD_WRITE
;
542 show_usage(argc
, argv
);
546 cpu_affinities
[next_aff
++] = a
;
548 printf_verbose("Adding CPU %d affinity\n", a
);
552 show_usage(argc
, argv
);
555 rduration
= atol(argv
[++i
]);
559 show_usage(argc
, argv
);
562 wdelay
= atol(argv
[++i
]);
569 show_usage(argc
, argv
);
572 rand_pool
= atol(argv
[++i
]);
576 show_usage(argc
, argv
);
579 init_hash_size
= atol(argv
[++i
]);
590 /* Check if hash size is power of 2 */
591 if (init_hash_size
&& init_hash_size
& (init_hash_size
- 1)) {
592 printf("Error: Hash table size %lu is not a power of 2.\n",
597 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
598 duration
, nr_readers
, nr_writers
);
599 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
600 printf_verbose("Reader duration : %lu loops.\n", rduration
);
601 printf_verbose("Random pool size : %lu.\n", rand_pool
);
602 printf_verbose("Mode:%s%s.\n",
603 add_only
? " add only" : " add/remove",
604 add_unique
? " uniquify" : "");
605 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size
);
606 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
607 "main", pthread_self(), (unsigned long)gettid());
609 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
610 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
611 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
612 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
613 test_ht
= ht_new(test_hash
, test_compare
, 0x42UL
,
614 init_hash_size
, call_rcu
);
616 err
= create_all_cpu_call_rcu_data(0);
621 for (i
= 0; i
< nr_readers
; i
++) {
622 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
627 for (i
= 0; i
< nr_writers
; i
++) {
628 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
642 for (i
= 0; i
< nr_readers
; i
++) {
643 err
= pthread_join(tid_reader
[i
], &tret
);
646 tot_reads
+= count_reader
[i
];
648 for (i
= 0; i
< nr_writers
; i
++) {
649 err
= pthread_join(tid_writer
[i
], &tret
);
652 tot_writes
+= count_writer
[i
].update_ops
;
653 tot_add
+= count_writer
[i
].add
;
654 tot_remove
+= count_writer
[i
].remove
;
656 ht_count_nodes(test_ht
, &count
, &removed
);
657 if (count
|| removed
)
658 printf("WARNING: nodes left in the hash table upon destroy: "
659 "%lu nodes + %lu logically removed.\n", count
, removed
);
660 (void) ht_destroy(test_ht
);
662 printf_verbose("final delete: %d items\n", ret
);
663 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
665 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
667 "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
668 "nr_add %12llu nr_remove %12llu nr_leaked %12llu\n",
669 argv
[0], duration
, nr_readers
, rduration
,
670 nr_writers
, wdelay
, rand_pool
, tot_reads
, tot_writes
,
671 tot_reads
+ tot_writes
, tot_add
, tot_remove
,
672 tot_add
- tot_remove
- count
);