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>
33 #include <sys/syscall.h>
36 #include <urcu-defer.h>
42 #define RAND_POOL 1000
44 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
45 #define CACHE_LINE_SIZE 4096
47 /* hardcoded number of CPUs */
50 #if defined(_syscall0)
51 _syscall0(pid_t
, gettid
)
52 #elif defined(__NR_gettid)
53 static inline pid_t
gettid(void)
55 return syscall(__NR_gettid
);
58 #warning "use pid as tid"
59 static inline pid_t
gettid(void)
65 #ifndef DYNAMIC_LINK_TEST
68 #define debug_yield_read()
72 static unsigned int __thread rand_lookup
;
73 static unsigned long __thread nr_add
;
74 static unsigned long __thread nr_addexist
;
75 static unsigned long __thread nr_del
;
76 static unsigned long __thread nr_delnoent
;
77 static unsigned long __thread lookup_fail
;
78 static unsigned long __thread lookup_ok
;
80 static struct rcu_ht
*test_ht
;
87 static volatile int test_go
, test_stop
;
89 static unsigned long wdelay
;
91 static unsigned long duration
;
93 /* read-side C.S. duration, in loops */
94 static unsigned long rduration
;
96 static inline void loop_sleep(unsigned long l
)
102 static int verbose_mode
;
104 #define printf_verbose(fmt, args...) \
110 static unsigned int cpu_affinities
[NR_CPUS
];
111 static unsigned int next_aff
= 0;
112 static int use_affinity
= 0;
114 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
116 static void set_affinity(void)
125 ret
= pthread_mutex_lock(&affinity_mutex
);
127 perror("Error in pthread mutex lock");
130 cpu
= cpu_affinities
[next_aff
++];
131 ret
= pthread_mutex_unlock(&affinity_mutex
);
133 perror("Error in pthread mutex unlock");
138 sched_setaffinity(0, sizeof(mask
), &mask
);
142 * returns 0 if test should end.
144 static int test_duration_write(void)
149 static int test_duration_read(void)
154 static unsigned long long __thread nr_writes
;
155 static unsigned long long __thread nr_reads
;
157 static unsigned int nr_readers
;
158 static unsigned int nr_writers
;
160 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
162 void rcu_copy_mutex_lock(void)
165 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
167 perror("Error in pthread mutex lock");
172 void rcu_copy_mutex_unlock(void)
176 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
178 perror("Error in pthread mutex unlock");
183 #define ARRAY_POISON 0xDEADBEEF
185 void *thr_reader(void *_count
)
187 unsigned long long *count
= _count
;
188 struct test_data
*local_ptr
;
190 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
191 "reader", pthread_self(), (unsigned long)gettid());
195 rcu_register_thread();
204 local_ptr
= ht_lookup(test_ht
,
205 (void *)(unsigned long)(rand_r(&rand_lookup
) % RAND_POOL
));
206 if (local_ptr
== NULL
)
211 if (unlikely(rduration
))
212 loop_sleep(rduration
);
215 if (unlikely(!test_duration_read()))
219 rcu_unregister_thread();
222 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
223 "reader", pthread_self(), (unsigned long)gettid());
224 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
225 pthread_self(), lookup_fail
, lookup_ok
);
230 void *thr_writer(void *_count
)
232 unsigned long long *count
= _count
;
233 struct test_data
*data
;
236 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
237 "writer", pthread_self(), (unsigned long)gettid());
241 rcu_register_thread();
242 rcu_defer_register_thread();
250 if (rand_r(&rand_lookup
) & 1) {
251 data
= malloc(sizeof(struct test_data
));
252 //rcu_copy_mutex_lock();
253 ret
= ht_add(test_ht
,
254 (void *)(unsigned long)(rand_r(&rand_lookup
) % RAND_POOL
),
256 if (ret
== -EEXIST
) {
262 //rcu_copy_mutex_unlock();
265 //rcu_copy_mutex_lock();
266 ret
= ht_delete(test_ht
,
267 (void *)(unsigned long)(rand_r(&rand_lookup
) % RAND_POOL
));
272 //rcu_copy_mutex_unlock();
274 //if (nr_writes % 100000 == 0) {
275 if (nr_writes
% 1000 == 0) {
276 if (rand_r(&rand_lookup
) & 1) {
277 ht_resize(test_ht
, 1);
279 ht_resize(test_ht
, -1);
283 if (unlikely(!test_duration_write()))
285 if (unlikely(wdelay
))
289 rcu_defer_unregister_thread();
290 rcu_unregister_thread();
292 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
293 "writer", pthread_self(), (unsigned long)gettid());
294 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
295 "nr_delnoent %lu\n", pthread_self(), nr_add
,
296 nr_addexist
, nr_del
, nr_delnoent
);
301 void show_usage(int argc
, char **argv
)
303 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
305 printf(" [-r] [-w] (yield reader and/or writer)");
307 printf(" [-d delay] (writer period (us))");
308 printf(" [-c duration] (reader C.S. duration (in loops))");
309 printf(" [-v] (verbose output)");
310 printf(" [-a cpu#] [-a cpu#]... (affinity)");
314 int main(int argc
, char **argv
)
317 pthread_t
*tid_reader
, *tid_writer
;
319 unsigned long long *count_reader
, *count_writer
;
320 unsigned long long tot_reads
= 0, tot_writes
= 0;
324 show_usage(argc
, argv
);
328 err
= sscanf(argv
[1], "%u", &nr_readers
);
330 show_usage(argc
, argv
);
334 err
= sscanf(argv
[2], "%u", &nr_writers
);
336 show_usage(argc
, argv
);
340 err
= sscanf(argv
[3], "%lu", &duration
);
342 show_usage(argc
, argv
);
346 for (i
= 4; i
< argc
; i
++) {
347 if (argv
[i
][0] != '-')
349 switch (argv
[i
][1]) {
352 yield_active
|= YIELD_READ
;
355 yield_active
|= YIELD_WRITE
;
360 show_usage(argc
, argv
);
364 cpu_affinities
[next_aff
++] = a
;
366 printf_verbose("Adding CPU %d affinity\n", a
);
370 show_usage(argc
, argv
);
373 rduration
= atol(argv
[++i
]);
377 show_usage(argc
, argv
);
380 wdelay
= atol(argv
[++i
]);
388 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
389 duration
, nr_readers
, nr_writers
);
390 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
391 printf_verbose("Reader duration : %lu loops.\n", rduration
);
392 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
393 "main", pthread_self(), (unsigned long)gettid());
395 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
396 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
397 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
398 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
399 test_ht
= ht_new(ht_jhash
, free
, HASH_SIZE
, sizeof(unsigned long),
403 for (i
= 0; i
< nr_readers
; i
++) {
404 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
409 for (i
= 0; i
< nr_writers
; i
++) {
410 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
424 for (i
= 0; i
< nr_readers
; i
++) {
425 err
= pthread_join(tid_reader
[i
], &tret
);
428 tot_reads
+= count_reader
[i
];
430 for (i
= 0; i
< nr_writers
; i
++) {
431 err
= pthread_join(tid_writer
[i
], &tret
);
434 tot_writes
+= count_writer
[i
];
436 rcu_register_thread();
437 rcu_defer_register_thread();
438 ret
= ht_destroy(test_ht
);
439 rcu_defer_unregister_thread();
440 rcu_unregister_thread();
442 printf_verbose("final delete: %d items\n", ret
);
443 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
445 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
447 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
448 argv
[0], duration
, nr_readers
, rduration
,
449 nr_writers
, wdelay
, tot_reads
, tot_writes
,
450 tot_reads
+ tot_writes
);