1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Userspace RCU library - test program
13 #include <sys/types.h>
19 #include <urcu/arch.h>
20 #include <urcu/assert.h>
21 #include <urcu/tls-compat.h>
22 #include "thread-id.h"
23 #include "../common/debug-yield.h"
25 /* hardcoded number of CPUs */
28 #ifndef DYNAMIC_LINK_TEST
37 static volatile int test_go
, test_stop
;
39 static unsigned long wdelay
;
41 static struct test_array
*test_rcu_pointer
;
43 static unsigned long duration
;
45 /* read-side C.S. duration, in loops */
46 static unsigned long rduration
;
48 /* write-side C.S. duration, in loops */
49 static unsigned long wduration
;
51 static inline void loop_sleep(unsigned long loops
)
57 static int verbose_mode
;
59 #define printf_verbose(fmt, args...) \
65 static unsigned int cpu_affinities
[NR_CPUS
];
66 static unsigned int next_aff
= 0;
67 static int use_affinity
= 0;
69 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
71 static void set_affinity(void)
73 #ifdef HAVE_SCHED_SETAFFINITY
76 #endif /* HAVE_SCHED_SETAFFINITY */
81 #ifdef HAVE_SCHED_SETAFFINITY
82 ret
= pthread_mutex_lock(&affinity_mutex
);
84 perror("Error in pthread mutex lock");
87 cpu
= cpu_affinities
[next_aff
++];
88 ret
= pthread_mutex_unlock(&affinity_mutex
);
90 perror("Error in pthread mutex unlock");
96 sched_setaffinity(0, sizeof(mask
), &mask
);
97 #endif /* HAVE_SCHED_SETAFFINITY */
101 * returns 0 if test should end.
103 static int test_duration_write(void)
108 static int test_duration_read(void)
113 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
114 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
116 static unsigned int nr_readers
;
117 static unsigned int nr_writers
;
119 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
122 void rcu_copy_mutex_lock(void)
125 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
127 perror("Error in pthread mutex lock");
133 void rcu_copy_mutex_unlock(void)
137 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
139 perror("Error in pthread mutex unlock");
145 * malloc/free are reusing memory areas too quickly, which does not let us
146 * test races appropriately. Use a large circular array for allocations.
147 * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across
148 * both alloc and free, which insures we never run over our tail.
150 #define ARRAY_SIZE (1048576 * nr_writers)
151 #define ARRAY_POISON (int) 0xDEADBEEF
152 static unsigned int array_index
;
153 static struct test_array
*test_array
;
155 static struct test_array
*test_array_alloc(void)
157 struct test_array
*ret
;
160 index
= array_index
% ARRAY_SIZE
;
161 urcu_posix_assert(test_array
[index
].a
== ARRAY_POISON
||
162 test_array
[index
].a
== 0);
163 ret
= &test_array
[index
];
165 if (array_index
== ARRAY_SIZE
)
170 static void test_array_free(struct test_array
*ptr
)
174 ptr
->a
= ARRAY_POISON
;
178 void *thr_reader(void *_count
)
180 unsigned long long *count
= _count
;
181 struct test_array
*local_ptr
;
183 printf_verbose("thread_begin %s, tid %lu\n",
184 "reader", urcu_get_thread_id());
188 rcu_register_thread();
197 local_ptr
= rcu_dereference(test_rcu_pointer
);
198 rcu_debug_yield_read();
200 urcu_posix_assert(local_ptr
->a
== 8);
201 if (caa_unlikely(rduration
))
202 loop_sleep(rduration
);
204 URCU_TLS(nr_reads
)++;
205 if (caa_unlikely(!test_duration_read()))
209 rcu_unregister_thread();
211 *count
= URCU_TLS(nr_reads
);
212 printf_verbose("thread_end %s, tid %lu\n",
213 "reader", urcu_get_thread_id());
219 void *thr_writer(void *_count
)
221 unsigned long long *count
= _count
;
222 struct test_array
*new, *old
;
224 printf_verbose("thread_begin %s, tid %lu\n",
225 "writer", urcu_get_thread_id());
235 rcu_copy_mutex_lock();
236 new = test_array_alloc();
238 old
= test_rcu_pointer
;
239 rcu_assign_pointer(test_rcu_pointer
, new);
240 if (caa_unlikely(wduration
))
241 loop_sleep(wduration
);
245 test_array_free(old
);
246 rcu_copy_mutex_unlock();
247 URCU_TLS(nr_writes
)++;
248 if (caa_unlikely(!test_duration_write()))
250 if (caa_unlikely(wdelay
))
254 printf_verbose("thread_end %s, tid %lu\n",
255 "writer", urcu_get_thread_id());
256 *count
= URCU_TLS(nr_writes
);
261 void show_usage(char **argv
)
263 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
265 printf("OPTIONS:\n");
266 printf(" [-r] [-w] (yield reader and/or writer)\n");
267 printf(" [-d delay] (writer period (us))\n");
268 printf(" [-c duration] (reader C.S. duration (in loops))\n");
269 printf(" [-e duration] (writer C.S. duration (in loops))\n");
270 printf(" [-v] (verbose output)\n");
271 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
275 int main(int argc
, char **argv
)
278 pthread_t
*tid_reader
, *tid_writer
;
280 unsigned long long *count_reader
, *count_writer
;
281 unsigned long long tot_reads
= 0, tot_writes
= 0;
290 err
= sscanf(argv
[1], "%u", &nr_readers
);
296 err
= sscanf(argv
[2], "%u", &nr_writers
);
302 err
= sscanf(argv
[3], "%lu", &duration
);
308 for (i
= 4; i
< argc
; i
++) {
309 if (argv
[i
][0] != '-')
311 switch (argv
[i
][1]) {
313 rcu_debug_yield_enable(RCU_YIELD_READ
);
316 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
324 cpu_affinities
[next_aff
++] = a
;
326 printf_verbose("Adding CPU %d affinity\n", a
);
333 rduration
= atol(argv
[++i
]);
340 wdelay
= atol(argv
[++i
]);
347 wduration
= atol(argv
[++i
]);
355 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
356 duration
, nr_readers
, nr_writers
);
357 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
358 printf_verbose("Reader duration : %lu loops.\n", rduration
);
359 printf_verbose("thread %-6s, tid %lu\n",
360 "main", urcu_get_thread_id());
362 test_array
= calloc(1, sizeof(*test_array
) * ARRAY_SIZE
);
363 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
364 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
365 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
366 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
370 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
371 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
372 &count_reader
[i_thr
]);
376 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
377 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
378 &count_writer
[i_thr
]);
391 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
392 err
= pthread_join(tid_reader
[i_thr
], &tret
);
395 tot_reads
+= count_reader
[i_thr
];
397 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
398 err
= pthread_join(tid_writer
[i_thr
], &tret
);
401 tot_writes
+= count_writer
[i_thr
];
404 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
406 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
408 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
409 argv
[0], duration
, nr_readers
, rduration
, wduration
,
410 nr_writers
, wdelay
, tot_reads
, tot_writes
,
411 tot_reads
+ tot_writes
);
413 test_array_free(test_rcu_pointer
);