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 10000
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>
73 static unsigned int __thread rand_lookup
;
74 static unsigned long __thread nr_add
;
75 static unsigned long __thread nr_addexist
;
76 static unsigned long __thread nr_del
;
77 static unsigned long __thread nr_delnoent
;
78 static unsigned long __thread lookup_fail
;
79 static unsigned long __thread lookup_ok
;
81 static struct rcu_ht
*test_ht
;
88 static volatile int test_go
, test_stop
;
90 static unsigned long wdelay
;
92 static unsigned long duration
;
94 /* read-side C.S. duration, in loops */
95 static unsigned long rduration
;
97 static inline void loop_sleep(unsigned long l
)
103 static int verbose_mode
;
105 #define printf_verbose(fmt, args...) \
111 static unsigned int cpu_affinities
[NR_CPUS
];
112 static unsigned int next_aff
= 0;
113 static int use_affinity
= 0;
115 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
117 static void set_affinity(void)
126 ret
= pthread_mutex_lock(&affinity_mutex
);
128 perror("Error in pthread mutex lock");
131 cpu
= cpu_affinities
[next_aff
++];
132 ret
= pthread_mutex_unlock(&affinity_mutex
);
134 perror("Error in pthread mutex unlock");
139 sched_setaffinity(0, sizeof(mask
), &mask
);
143 * returns 0 if test should end.
145 static int test_duration_write(void)
150 static int test_duration_read(void)
155 static unsigned long long __thread nr_writes
;
156 static unsigned long long __thread nr_reads
;
158 static unsigned int nr_readers
;
159 static unsigned int nr_writers
;
161 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
163 void rcu_copy_mutex_lock(void)
166 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
168 perror("Error in pthread mutex lock");
173 void rcu_copy_mutex_unlock(void)
177 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
179 perror("Error in pthread mutex unlock");
186 * Source: http://burtleburtle.net/bob/c/lookup3.c
187 * Originally Public Domain
190 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
192 #define mix(a, b, c) \
194 a -= c; a ^= rot(c, 4); c += b; \
195 b -= a; b ^= rot(a, 6); a += c; \
196 c -= b; c ^= rot(b, 8); b += a; \
197 a -= c; a ^= rot(c, 16); c += b; \
198 b -= a; b ^= rot(a, 19); a += c; \
199 c -= b; c ^= rot(b, 4); b += a; \
202 #define final(a, b, c) \
204 c ^= b; c -= rot(b, 14); \
205 a ^= c; a -= rot(c, 11); \
206 b ^= a; b -= rot(a, 25); \
207 c ^= b; c -= rot(b, 16); \
208 a ^= c; a -= rot(c, 4);\
209 b ^= a; b -= rot(a, 14); \
210 c ^= b; c -= rot(b, 24); \
213 static __attribute__((unused
))
215 const uint32_t *k
, /* the key, an array of uint32_t values */
216 size_t length
, /* the length of the key, in uint32_ts */
217 uint32_t initval
) /* the previous hash, or an arbitrary value */
221 /* Set up the internal state */
222 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
224 /*----------------------------------------- handle most of the key */
234 /*----------------------------------- handle the last 3 uint32_t's */
235 switch (length
) { /* all the case statements fall through */
240 case 0: /* case 0: nothing left to add */
243 /*---------------------------------------------- report the result */
249 const uint32_t *k
, /* the key, an array of uint32_t values */
250 size_t length
, /* the length of the key, in uint32_ts */
251 uint32_t *pc
, /* IN: seed OUT: primary hash value */
252 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
256 /* Set up the internal state */
257 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
260 /*----------------------------------------- handle most of the key */
270 /*----------------------------------- handle the last 3 uint32_t's */
271 switch (length
) { /* all the case statements fall through */
276 case 0: /* case 0: nothing left to add */
279 /*---------------------------------------------- report the result */
284 #if (CAA_BITS_PER_LONG == 32)
286 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
288 unsigned long key
= (unsigned long) _key
;
291 assert(length
== sizeof(unsigned long));
292 return hash_u32(&v
, 1, seed
);
296 unsigned long test_hash(void *_key
, size_t length
, unsigned long seed
)
307 assert(length
== sizeof(unsigned long));
308 v
.v64
= (uint64_t) seed
;
309 key
.v64
= (uint64_t) _key
;
310 hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
316 unsigned long test_compare(void *key1
, size_t key1_len
,
317 void *key2
, size_t key2_len
)
319 if (unlikely(key1_len
!= key2_len
))
321 assert(key1_len
== sizeof(unsigned long));
328 void *thr_reader(void *_count
)
330 unsigned long long *count
= _count
;
331 struct rcu_ht_node
*node
;
333 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
334 "reader", pthread_self(), (unsigned long)gettid());
338 rcu_register_thread();
347 node
= ht_lookup(test_ht
,
348 (void *)(unsigned long)(rand_r(&rand_lookup
) % RAND_POOL
),
355 if (unlikely(rduration
))
356 loop_sleep(rduration
);
359 if (unlikely(!test_duration_read()))
363 rcu_unregister_thread();
366 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
367 "reader", pthread_self(), (unsigned long)gettid());
368 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
369 pthread_self(), lookup_fail
, lookup_ok
);
375 void free_node_cb(struct rcu_head
*head
)
377 struct rcu_ht_node
*node
=
378 caa_container_of(head
, struct rcu_ht_node
, head
);
382 void *thr_writer(void *_count
)
384 struct rcu_ht_node
*node
;
385 unsigned long long *count
= _count
;
388 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
389 "writer", pthread_self(), (unsigned long)gettid());
393 rcu_register_thread();
401 if (rand_r(&rand_lookup
) & 1) {
402 node
= malloc(sizeof(struct rcu_ht_node
));
405 (void *)(unsigned long)(rand_r(&rand_lookup
) % RAND_POOL
),
408 ret
= ht_add_unique(test_ht
, node
);
418 node
= ht_lookup(test_ht
,
419 (void *)(unsigned long)(rand_r(&rand_lookup
) % RAND_POOL
),
422 ret
= ht_remove(test_ht
, node
);
427 call_rcu(&node
->head
, free_node_cb
);
433 //if (nr_writes % 100000 == 0) {
434 if (nr_writes
% 1000 == 0) {
436 if (rand_r(&rand_lookup
) & 1) {
437 ht_resize(test_ht
, 1);
439 ht_resize(test_ht
, -1);
445 if (unlikely(!test_duration_write()))
447 if (unlikely(wdelay
))
451 rcu_unregister_thread();
453 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
454 "writer", pthread_self(), (unsigned long)gettid());
455 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
456 "nr_delnoent %lu\n", pthread_self(), nr_add
,
457 nr_addexist
, nr_del
, nr_delnoent
);
462 void show_usage(int argc
, char **argv
)
464 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
466 printf(" [-r] [-w] (yield reader and/or writer)");
468 printf(" [-d delay] (writer period (us))");
469 printf(" [-c duration] (reader C.S. duration (in loops))");
470 printf(" [-v] (verbose output)");
471 printf(" [-a cpu#] [-a cpu#]... (affinity)");
475 int main(int argc
, char **argv
)
478 pthread_t
*tid_reader
, *tid_writer
;
480 unsigned long long *count_reader
, *count_writer
;
481 unsigned long long tot_reads
= 0, tot_writes
= 0;
485 show_usage(argc
, argv
);
489 err
= sscanf(argv
[1], "%u", &nr_readers
);
491 show_usage(argc
, argv
);
495 err
= sscanf(argv
[2], "%u", &nr_writers
);
497 show_usage(argc
, argv
);
501 err
= sscanf(argv
[3], "%lu", &duration
);
503 show_usage(argc
, argv
);
507 for (i
= 4; i
< argc
; i
++) {
508 if (argv
[i
][0] != '-')
510 switch (argv
[i
][1]) {
513 yield_active
|= YIELD_READ
;
516 yield_active
|= YIELD_WRITE
;
521 show_usage(argc
, argv
);
525 cpu_affinities
[next_aff
++] = a
;
527 printf_verbose("Adding CPU %d affinity\n", a
);
531 show_usage(argc
, argv
);
534 rduration
= atol(argv
[++i
]);
538 show_usage(argc
, argv
);
541 wdelay
= atol(argv
[++i
]);
549 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
550 duration
, nr_readers
, nr_writers
);
551 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
552 printf_verbose("Reader duration : %lu loops.\n", rduration
);
553 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
554 "main", pthread_self(), (unsigned long)gettid());
556 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
557 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
558 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
559 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
560 test_ht
= ht_new(test_hash
, test_compare
, 0x42UL
,
561 HASH_SIZE
, call_rcu
);
564 for (i
= 0; i
< nr_readers
; i
++) {
565 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
570 for (i
= 0; i
< nr_writers
; i
++) {
571 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
585 for (i
= 0; i
< nr_readers
; i
++) {
586 err
= pthread_join(tid_reader
[i
], &tret
);
589 tot_reads
+= count_reader
[i
];
591 for (i
= 0; i
< nr_writers
; i
++) {
592 err
= pthread_join(tid_writer
[i
], &tret
);
595 tot_writes
+= count_writer
[i
];
597 ret
= ht_destroy(test_ht
);
599 printf("WARNING: nodes left in the hash table upon destroy\n");
601 printf_verbose("final delete: %d items\n", ret
);
602 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
604 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
606 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
607 argv
[0], duration
, nr_readers
, rduration
,
608 nr_writers
, wdelay
, tot_reads
, tot_writes
,
609 tot_reads
+ tot_writes
);