4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
27 #include <sys/types.h>
34 #include <urcu/arch.h>
35 #include <urcu/tls-compat.h>
37 #include "thread-id.h"
38 #include "../common/debug-yield.h"
40 /* hardcoded number of CPUs */
43 #ifndef DYNAMIC_LINK_TEST
52 static volatile int test_go
, test_stop
;
54 static unsigned long wdelay
;
56 static struct test_array
*test_rcu_pointer
;
58 static unsigned long duration
;
60 /* read-side C.S. duration, in loops */
61 static unsigned long rduration
;
63 /* write-side C.S. duration, in loops */
64 static unsigned long wduration
;
66 static inline void loop_sleep(unsigned long loops
)
72 static int verbose_mode
;
74 #define printf_verbose(fmt, args...) \
80 static unsigned int cpu_affinities
[NR_CPUS
];
81 static unsigned int next_aff
= 0;
82 static int use_affinity
= 0;
84 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
86 static void set_affinity(void)
88 #if HAVE_SCHED_SETAFFINITY
91 #endif /* HAVE_SCHED_SETAFFINITY */
96 #if HAVE_SCHED_SETAFFINITY
97 ret
= pthread_mutex_lock(&affinity_mutex
);
99 perror("Error in pthread mutex lock");
102 cpu
= cpu_affinities
[next_aff
++];
103 ret
= pthread_mutex_unlock(&affinity_mutex
);
105 perror("Error in pthread mutex unlock");
111 #if SCHED_SETAFFINITY_ARGS == 2
112 sched_setaffinity(0, &mask
);
114 sched_setaffinity(0, sizeof(mask
), &mask
);
116 #endif /* HAVE_SCHED_SETAFFINITY */
120 * returns 0 if test should end.
122 static int test_duration_write(void)
127 static int test_duration_read(void)
132 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
133 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
135 static unsigned int nr_readers
;
136 static unsigned int nr_writers
;
138 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
140 void rcu_copy_mutex_lock(void)
143 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
145 perror("Error in pthread mutex lock");
150 void rcu_copy_mutex_unlock(void)
154 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
156 perror("Error in pthread mutex unlock");
162 * malloc/free are reusing memory areas too quickly, which does not let us
163 * test races appropriately. Use a large circular array for allocations.
164 * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across
165 * both alloc and free, which insures we never run over our tail.
167 #define ARRAY_SIZE (1048576 * nr_writers)
168 #define ARRAY_POISON (int) 0xDEADBEEF
169 static unsigned int array_index
;
170 static struct test_array
*test_array
;
172 static struct test_array
*test_array_alloc(void)
174 struct test_array
*ret
;
177 index
= array_index
% ARRAY_SIZE
;
178 assert(test_array
[index
].a
== ARRAY_POISON
||
179 test_array
[index
].a
== 0);
180 ret
= &test_array
[index
];
182 if (array_index
== ARRAY_SIZE
)
187 static void test_array_free(struct test_array
*ptr
)
191 ptr
->a
= ARRAY_POISON
;
194 void *thr_reader(void *_count
)
196 unsigned long long *count
= _count
;
197 struct test_array
*local_ptr
;
199 printf_verbose("thread_begin %s, tid %lu\n",
200 "reader", urcu_get_thread_id());
204 rcu_register_thread();
213 local_ptr
= rcu_dereference(test_rcu_pointer
);
214 rcu_debug_yield_read();
216 assert(local_ptr
->a
== 8);
217 if (caa_unlikely(rduration
))
218 loop_sleep(rduration
);
220 URCU_TLS(nr_reads
)++;
221 if (caa_unlikely(!test_duration_read()))
225 rcu_unregister_thread();
227 *count
= URCU_TLS(nr_reads
);
228 printf_verbose("thread_end %s, tid %lu\n",
229 "reader", urcu_get_thread_id());
234 void *thr_writer(void *_count
)
236 unsigned long long *count
= _count
;
237 struct test_array
*new, *old
;
239 printf_verbose("thread_begin %s, tid %lu\n",
240 "writer", urcu_get_thread_id());
250 rcu_copy_mutex_lock();
251 new = test_array_alloc();
253 old
= test_rcu_pointer
;
254 rcu_assign_pointer(test_rcu_pointer
, new);
255 if (caa_unlikely(wduration
))
256 loop_sleep(wduration
);
260 test_array_free(old
);
261 rcu_copy_mutex_unlock();
262 URCU_TLS(nr_writes
)++;
263 if (caa_unlikely(!test_duration_write()))
265 if (caa_unlikely(wdelay
))
269 printf_verbose("thread_end %s, tid %lu\n",
270 "writer", urcu_get_thread_id());
271 *count
= URCU_TLS(nr_writes
);
275 void show_usage(int argc
, char **argv
)
277 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
279 printf("OPTIONS:\n");
280 printf(" [-r] [-w] (yield reader and/or writer)\n");
281 printf(" [-d delay] (writer period (us))\n");
282 printf(" [-c duration] (reader C.S. duration (in loops))\n");
283 printf(" [-e duration] (writer C.S. duration (in loops))\n");
284 printf(" [-v] (verbose output)\n");
285 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
289 int main(int argc
, char **argv
)
292 pthread_t
*tid_reader
, *tid_writer
;
294 unsigned long long *count_reader
, *count_writer
;
295 unsigned long long tot_reads
= 0, tot_writes
= 0;
300 show_usage(argc
, argv
);
304 err
= sscanf(argv
[1], "%u", &nr_readers
);
306 show_usage(argc
, argv
);
310 err
= sscanf(argv
[2], "%u", &nr_writers
);
312 show_usage(argc
, argv
);
316 err
= sscanf(argv
[3], "%lu", &duration
);
318 show_usage(argc
, argv
);
322 for (i
= 4; i
< argc
; i
++) {
323 if (argv
[i
][0] != '-')
325 switch (argv
[i
][1]) {
327 rcu_debug_yield_enable(RCU_YIELD_READ
);
330 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
334 show_usage(argc
, argv
);
338 cpu_affinities
[next_aff
++] = a
;
340 printf_verbose("Adding CPU %d affinity\n", a
);
344 show_usage(argc
, argv
);
347 rduration
= atol(argv
[++i
]);
351 show_usage(argc
, argv
);
354 wdelay
= atol(argv
[++i
]);
358 show_usage(argc
, argv
);
361 wduration
= atol(argv
[++i
]);
369 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
370 duration
, nr_readers
, nr_writers
);
371 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
372 printf_verbose("Reader duration : %lu loops.\n", rduration
);
373 printf_verbose("thread %-6s, tid %lu\n",
374 "main", urcu_get_thread_id());
376 test_array
= calloc(1, sizeof(*test_array
) * ARRAY_SIZE
);
377 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
378 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
379 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
380 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
384 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
385 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
386 &count_reader
[i_thr
]);
390 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
391 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
392 &count_writer
[i_thr
]);
405 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
406 err
= pthread_join(tid_reader
[i_thr
], &tret
);
409 tot_reads
+= count_reader
[i_thr
];
411 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
412 err
= pthread_join(tid_writer
[i_thr
], &tret
);
415 tot_writes
+= count_writer
[i_thr
];
418 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
420 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
422 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
423 argv
[0], duration
, nr_readers
, rduration
, wduration
,
424 nr_writers
, wdelay
, tot_reads
, tot_writes
,
425 tot_reads
+ tot_writes
);
427 test_array_free(test_rcu_pointer
);