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.
29 #include <sys/types.h>
36 #include <urcu/arch.h>
37 #include <urcu/tls-compat.h>
39 #include "thread-id.h"
40 #include "../common/debug-yield.h"
42 /* hardcoded number of CPUs */
45 #ifndef DYNAMIC_LINK_TEST
54 static volatile int test_go
, test_stop
;
56 static unsigned long wdelay
;
58 static struct test_array
*test_rcu_pointer
;
60 static unsigned long duration
;
62 /* read-side C.S. duration, in loops */
63 static unsigned long rduration
;
65 /* write-side C.S. duration, in loops */
66 static unsigned long wduration
;
68 static inline void loop_sleep(unsigned long loops
)
74 static int verbose_mode
;
76 #define printf_verbose(fmt, args...) \
82 static unsigned int cpu_affinities
[NR_CPUS
];
83 static unsigned int next_aff
= 0;
84 static int use_affinity
= 0;
86 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
88 static void set_affinity(void)
90 #if HAVE_SCHED_SETAFFINITY
93 #endif /* HAVE_SCHED_SETAFFINITY */
98 #if HAVE_SCHED_SETAFFINITY
99 ret
= pthread_mutex_lock(&affinity_mutex
);
101 perror("Error in pthread mutex lock");
104 cpu
= cpu_affinities
[next_aff
++];
105 ret
= pthread_mutex_unlock(&affinity_mutex
);
107 perror("Error in pthread mutex unlock");
113 #if SCHED_SETAFFINITY_ARGS == 2
114 sched_setaffinity(0, &mask
);
116 sched_setaffinity(0, sizeof(mask
), &mask
);
118 #endif /* HAVE_SCHED_SETAFFINITY */
122 * returns 0 if test should end.
124 static int test_duration_write(void)
129 static int test_duration_read(void)
134 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
135 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
137 static unsigned int nr_readers
;
138 static unsigned int nr_writers
;
140 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
142 void rcu_copy_mutex_lock(void)
145 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
147 perror("Error in pthread mutex lock");
152 void rcu_copy_mutex_unlock(void)
156 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
158 perror("Error in pthread mutex unlock");
164 * malloc/free are reusing memory areas too quickly, which does not let us
165 * test races appropriately. Use a large circular array for allocations.
166 * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across
167 * both alloc and free, which insures we never run over our tail.
169 #define ARRAY_SIZE (1048576 * nr_writers)
170 #define ARRAY_POISON 0xDEADBEEF
171 static int array_index
;
172 static struct test_array
*test_array
;
174 static struct test_array
*test_array_alloc(void)
176 struct test_array
*ret
;
179 index
= array_index
% ARRAY_SIZE
;
180 assert(test_array
[index
].a
== ARRAY_POISON
||
181 test_array
[index
].a
== 0);
182 ret
= &test_array
[index
];
184 if (array_index
== ARRAY_SIZE
)
189 static void test_array_free(struct test_array
*ptr
)
193 ptr
->a
= ARRAY_POISON
;
196 void *thr_reader(void *_count
)
198 unsigned long long *count
= _count
;
199 struct test_array
*local_ptr
;
201 printf_verbose("thread_begin %s, tid %lu\n",
202 "reader", urcu_get_thread_id());
206 rcu_register_thread();
215 local_ptr
= rcu_dereference(test_rcu_pointer
);
216 rcu_debug_yield_read();
218 assert(local_ptr
->a
== 8);
219 if (caa_unlikely(rduration
))
220 loop_sleep(rduration
);
222 URCU_TLS(nr_reads
)++;
223 if (caa_unlikely(!test_duration_read()))
227 rcu_unregister_thread();
229 *count
= URCU_TLS(nr_reads
);
230 printf_verbose("thread_end %s, tid %lu\n",
231 "reader", urcu_get_thread_id());
236 void *thr_writer(void *_count
)
238 unsigned long long *count
= _count
;
239 struct test_array
*new, *old
;
241 printf_verbose("thread_begin %s, tid %lu\n",
242 "writer", urcu_get_thread_id());
252 rcu_copy_mutex_lock();
253 new = test_array_alloc();
255 old
= test_rcu_pointer
;
256 rcu_assign_pointer(test_rcu_pointer
, new);
257 if (caa_unlikely(wduration
))
258 loop_sleep(wduration
);
262 test_array_free(old
);
263 rcu_copy_mutex_unlock();
264 URCU_TLS(nr_writes
)++;
265 if (caa_unlikely(!test_duration_write()))
267 if (caa_unlikely(wdelay
))
271 printf_verbose("thread_end %s, tid %lu\n",
272 "writer", urcu_get_thread_id());
273 *count
= URCU_TLS(nr_writes
);
277 void show_usage(int argc
, char **argv
)
279 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
281 printf("OPTIONS:\n");
282 printf(" [-r] [-w] (yield reader and/or writer)\n");
283 printf(" [-d delay] (writer period (us))\n");
284 printf(" [-c duration] (reader C.S. duration (in loops))\n");
285 printf(" [-e duration] (writer C.S. duration (in loops))\n");
286 printf(" [-v] (verbose output)\n");
287 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
291 int main(int argc
, char **argv
)
294 pthread_t
*tid_reader
, *tid_writer
;
296 unsigned long long *count_reader
, *count_writer
;
297 unsigned long long tot_reads
= 0, tot_writes
= 0;
301 show_usage(argc
, argv
);
305 err
= sscanf(argv
[1], "%u", &nr_readers
);
307 show_usage(argc
, argv
);
311 err
= sscanf(argv
[2], "%u", &nr_writers
);
313 show_usage(argc
, argv
);
317 err
= sscanf(argv
[3], "%lu", &duration
);
319 show_usage(argc
, argv
);
323 for (i
= 4; i
< argc
; i
++) {
324 if (argv
[i
][0] != '-')
326 switch (argv
[i
][1]) {
328 rcu_debug_yield_enable(RCU_YIELD_READ
);
331 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
335 show_usage(argc
, argv
);
339 cpu_affinities
[next_aff
++] = a
;
341 printf_verbose("Adding CPU %d affinity\n", a
);
345 show_usage(argc
, argv
);
348 rduration
= atol(argv
[++i
]);
352 show_usage(argc
, argv
);
355 wdelay
= atol(argv
[++i
]);
359 show_usage(argc
, argv
);
362 wduration
= atol(argv
[++i
]);
370 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
371 duration
, nr_readers
, nr_writers
);
372 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
373 printf_verbose("Reader duration : %lu loops.\n", rduration
);
374 printf_verbose("thread %-6s, tid %lu\n",
375 "main", urcu_get_thread_id());
377 test_array
= calloc(1, sizeof(*test_array
) * ARRAY_SIZE
);
378 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
379 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
380 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
381 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
385 for (i
= 0; i
< nr_readers
; i
++) {
386 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
391 for (i
= 0; i
< nr_writers
; i
++) {
392 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
406 for (i
= 0; i
< nr_readers
; i
++) {
407 err
= pthread_join(tid_reader
[i
], &tret
);
410 tot_reads
+= count_reader
[i
];
412 for (i
= 0; i
< nr_writers
; i
++) {
413 err
= pthread_join(tid_writer
[i
], &tret
);
416 tot_writes
+= count_writer
[i
];
419 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
421 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
423 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
424 argv
[0], duration
, nr_readers
, rduration
, wduration
,
425 nr_writers
, wdelay
, tot_reads
, tot_writes
,
426 tot_reads
+ tot_writes
);
427 test_array_free(test_rcu_pointer
);