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>
41 #define 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 inline void loop_sleep(unsigned long l
)
109 static int verbose_mode
;
111 #define printf_verbose(fmt, args...) \
117 static unsigned int cpu_affinities
[NR_CPUS
];
118 static unsigned int next_aff
= 0;
119 static int use_affinity
= 0;
121 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
123 static void set_affinity(void)
132 ret
= pthread_mutex_lock(&affinity_mutex
);
134 perror("Error in pthread mutex lock");
137 cpu
= cpu_affinities
[next_aff
++];
138 ret
= pthread_mutex_unlock(&affinity_mutex
);
140 perror("Error in pthread mutex unlock");
145 sched_setaffinity(0, sizeof(mask
), &mask
);
149 * returns 0 if test should end.
151 static int test_duration_write(void)
156 static int test_duration_read(void)
161 static unsigned long long __thread nr_writes
;
162 static unsigned long long __thread nr_reads
;
164 static unsigned int nr_readers
;
165 static unsigned int nr_writers
;
167 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
169 void rcu_copy_mutex_lock(void)
172 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
174 perror("Error in pthread mutex lock");
179 void rcu_copy_mutex_unlock(void)
183 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
185 perror("Error in pthread mutex unlock");
192 * Source: http://burtleburtle.net/bob/c/lookup3.c
193 * Originally Public Domain
196 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
198 #define mix(a, b, c) \
200 a -= c; a ^= rot(c, 4); c += b; \
201 b -= a; b ^= rot(a, 6); a += c; \
202 c -= b; c ^= rot(b, 8); b += a; \
203 a -= c; a ^= rot(c, 16); c += b; \
204 b -= a; b ^= rot(a, 19); a += c; \
205 c -= b; c ^= rot(b, 4); b += a; \
208 #define final(a, b, c) \
210 c ^= b; c -= rot(b, 14); \
211 a ^= c; a -= rot(c, 11); \
212 b ^= a; b -= rot(a, 25); \
213 c ^= b; c -= rot(b, 16); \
214 a ^= c; a -= rot(c, 4);\
215 b ^= a; b -= rot(a, 14); \
216 c ^= b; c -= rot(b, 24); \
219 static __attribute__((unused
))
221 const uint32_t *k
, /* the key, an array of uint32_t values */
222 size_t length
, /* the length of the key, in uint32_ts */
223 uint32_t initval
) /* the previous hash, or an arbitrary value */
227 /* Set up the internal state */
228 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
230 /*----------------------------------------- handle most of the key */
240 /*----------------------------------- handle the last 3 uint32_t's */
241 switch (length
) { /* all the case statements fall through */
246 case 0: /* case 0: nothing left to add */
249 /*---------------------------------------------- report the result */
255 const uint32_t *k
, /* the key, an array of uint32_t values */
256 size_t length
, /* the length of the key, in uint32_ts */
257 uint32_t *pc
, /* IN: seed OUT: primary hash value */
258 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
262 /* Set up the internal state */
263 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
266 /*----------------------------------------- handle most of the key */
276 /*----------------------------------- handle the last 3 uint32_t's */
277 switch (length
) { /* all the case statements fall through */
282 case 0: /* case 0: nothing left to add */
285 /*---------------------------------------------- report the result */
290 #if (CAA_BITS_PER_LONG == 32)
292 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
294 unsigned long key
= (unsigned long) _key
;
297 assert(length
== sizeof(unsigned long));
298 return hash_u32(&v
, 1, seed
);
302 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
313 assert(length
== sizeof(unsigned long));
314 v
.v64
= (uint64_t) seed
;
315 key
.v64
= (uint64_t) _key
;
316 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
322 unsigned long test_compare(void *key1
, size_t key1_len
,
323 void *key2
, size_t key2_len
)
325 if (unlikely(key1_len
!= key2_len
))
327 assert(key1_len
== sizeof(unsigned long));
334 void *thr_reader(void *_count
)
336 unsigned long long *count
= _count
;
337 struct rcu_ht_node
*node
;
339 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
340 "reader", pthread_self(), (unsigned long)gettid());
344 rcu_register_thread();
353 node
= ht_lookup(test_ht
,
354 (void *)(unsigned long)(rand_r(&rand_lookup
) % RAND_POOL
),
361 if (unlikely(rduration
))
362 loop_sleep(rduration
);
365 if (unlikely(!test_duration_read()))
369 rcu_unregister_thread();
372 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
373 "reader", pthread_self(), (unsigned long)gettid());
374 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
375 pthread_self(), lookup_fail
, lookup_ok
);
381 void free_node_cb(struct rcu_head
*head
)
383 struct rcu_ht_node
*node
=
384 caa_container_of(head
, struct rcu_ht_node
, head
);
388 void *thr_writer(void *_count
)
390 struct rcu_ht_node
*node
;
391 struct wr_count
*count
= _count
;
394 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
395 "writer", pthread_self(), (unsigned long)gettid());
399 rcu_register_thread();
407 if (rand_r(&rand_lookup
) & 1) {
408 node
= malloc(sizeof(struct rcu_ht_node
));
411 (void *)(unsigned long)(rand_r(&rand_lookup
) % RAND_POOL
),
413 ret
= ht_add_unique(test_ht
, node
);
423 node
= ht_lookup(test_ht
,
424 (void *)(unsigned long)(rand_r(&rand_lookup
) % RAND_POOL
),
427 ret
= ht_remove(test_ht
, node
);
432 call_rcu(&node
->head
, free_node_cb
);
438 //if (nr_writes % 100000 == 0) {
439 if (nr_writes
% 1000 == 0) {
441 if (rand_r(&rand_lookup
) & 1) {
442 ht_resize(test_ht
, 1);
444 ht_resize(test_ht
, -1);
450 if (unlikely(!test_duration_write()))
452 if (unlikely(wdelay
))
456 rcu_unregister_thread();
458 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
459 "writer", pthread_self(), (unsigned long)gettid());
460 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
461 "nr_delnoent %lu\n", pthread_self(), nr_add
,
462 nr_addexist
, nr_del
, nr_delnoent
);
463 count
->update_ops
= nr_writes
;
465 count
->remove
= nr_del
;
469 void show_usage(int argc
, char **argv
)
471 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
473 printf(" [-r] [-w] (yield reader and/or writer)");
475 printf(" [-d delay] (writer period (us))");
476 printf(" [-c duration] (reader C.S. duration (in loops))");
477 printf(" [-v] (verbose output)");
478 printf(" [-a cpu#] [-a cpu#]... (affinity)");
482 int main(int argc
, char **argv
)
485 pthread_t
*tid_reader
, *tid_writer
;
487 unsigned long long *count_reader
;
488 struct wr_count
*count_writer
;
489 unsigned long long tot_reads
= 0, tot_writes
= 0,
490 tot_add
= 0, tot_remove
= 0;
491 unsigned long count
, removed
;
495 show_usage(argc
, argv
);
499 err
= sscanf(argv
[1], "%u", &nr_readers
);
501 show_usage(argc
, argv
);
505 err
= sscanf(argv
[2], "%u", &nr_writers
);
507 show_usage(argc
, argv
);
511 err
= sscanf(argv
[3], "%lu", &duration
);
513 show_usage(argc
, argv
);
517 for (i
= 4; i
< argc
; i
++) {
518 if (argv
[i
][0] != '-')
520 switch (argv
[i
][1]) {
523 yield_active
|= YIELD_READ
;
526 yield_active
|= YIELD_WRITE
;
531 show_usage(argc
, argv
);
535 cpu_affinities
[next_aff
++] = a
;
537 printf_verbose("Adding CPU %d affinity\n", a
);
541 show_usage(argc
, argv
);
544 rduration
= atol(argv
[++i
]);
548 show_usage(argc
, argv
);
551 wdelay
= atol(argv
[++i
]);
559 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
560 duration
, nr_readers
, nr_writers
);
561 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
562 printf_verbose("Reader duration : %lu loops.\n", rduration
);
563 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
564 "main", pthread_self(), (unsigned long)gettid());
566 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
567 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
568 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
569 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
570 test_ht
= ht_new(test_hash
, test_compare
, 0x42UL
,
571 HASH_SIZE
, call_rcu
);
574 for (i
= 0; i
< nr_readers
; i
++) {
575 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
580 for (i
= 0; i
< nr_writers
; i
++) {
581 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
595 for (i
= 0; i
< nr_readers
; i
++) {
596 err
= pthread_join(tid_reader
[i
], &tret
);
599 tot_reads
+= count_reader
[i
];
601 for (i
= 0; i
< nr_writers
; i
++) {
602 err
= pthread_join(tid_writer
[i
], &tret
);
605 tot_writes
+= count_writer
[i
].update_ops
;
606 tot_add
+= count_writer
[i
].add
;
607 tot_remove
+= count_writer
[i
].remove
;
609 ht_count_nodes(test_ht
, &count
, &removed
);
610 if (count
|| removed
)
611 printf("WARNING: nodes left in the hash table upon destroy: "
612 "%lu nodes + %lu logically removed.\n", count
, removed
);
613 (void) ht_destroy(test_ht
);
615 printf_verbose("final delete: %d items\n", ret
);
616 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
618 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
620 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
621 "nr_add %12llu nr_remove %12llu nr_leaked %12llu\n",
622 argv
[0], duration
, nr_readers
, rduration
,
623 nr_writers
, wdelay
, tot_reads
, tot_writes
,
624 tot_reads
+ tot_writes
, tot_add
, tot_remove
,
625 tot_add
- tot_remove
- count
);