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 unsigned long wdelay
;
39 static struct test_array
*test_rcu_pointer
;
41 static unsigned long duration
;
43 /* read-side C.S. duration, in loops */
44 static unsigned long rduration
;
46 /* write-side C.S. duration, in loops */
47 static unsigned long wduration
;
49 static inline void loop_sleep(unsigned long loops
)
55 static int verbose_mode
;
57 #define printf_verbose(fmt, args...) \
63 static unsigned int cpu_affinities
[NR_CPUS
];
64 static unsigned int next_aff
= 0;
65 static int use_affinity
= 0;
67 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
69 static void set_affinity(void)
71 #ifdef HAVE_SCHED_SETAFFINITY
74 #endif /* HAVE_SCHED_SETAFFINITY */
79 #ifdef HAVE_SCHED_SETAFFINITY
80 ret
= pthread_mutex_lock(&affinity_mutex
);
82 perror("Error in pthread mutex lock");
85 cpu
= cpu_affinities
[next_aff
++];
86 ret
= pthread_mutex_unlock(&affinity_mutex
);
88 perror("Error in pthread mutex unlock");
94 sched_setaffinity(0, sizeof(mask
), &mask
);
95 #endif /* HAVE_SCHED_SETAFFINITY */
98 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
99 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
101 static unsigned int nr_readers
;
102 static unsigned int nr_writers
;
104 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
107 void rcu_copy_mutex_lock(void)
110 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
112 perror("Error in pthread mutex lock");
118 void rcu_copy_mutex_unlock(void)
122 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
124 perror("Error in pthread mutex unlock");
130 * malloc/free are reusing memory areas too quickly, which does not let us
131 * test races appropriately. Use a large circular array for allocations.
132 * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across
133 * both alloc and free, which insures we never run over our tail.
135 #define ARRAY_SIZE (1048576 * nr_writers)
136 #define ARRAY_POISON (int) 0xDEADBEEF
137 static unsigned int array_index
;
138 static struct test_array
*test_array
;
140 static struct test_array
*test_array_alloc(void)
142 struct test_array
*ret
;
145 index
= array_index
% ARRAY_SIZE
;
146 urcu_posix_assert(test_array
[index
].a
== ARRAY_POISON
||
147 test_array
[index
].a
== 0);
148 ret
= &test_array
[index
];
150 if (array_index
== ARRAY_SIZE
)
155 static void test_array_free(struct test_array
*ptr
)
159 ptr
->a
= ARRAY_POISON
;
163 void *thr_reader(void *_count
)
165 unsigned long long *count
= _count
;
166 struct test_array
*local_ptr
;
168 printf_verbose("thread_begin %s, tid %lu\n",
169 "reader", urcu_get_thread_id());
173 rcu_register_thread();
179 local_ptr
= rcu_dereference(test_rcu_pointer
);
180 rcu_debug_yield_read();
182 urcu_posix_assert(local_ptr
->a
== 8);
183 if (caa_unlikely(rduration
))
184 loop_sleep(rduration
);
186 URCU_TLS(nr_reads
)++;
187 if (caa_unlikely(!test_duration_read()))
191 rcu_unregister_thread();
193 *count
= URCU_TLS(nr_reads
);
194 printf_verbose("thread_end %s, tid %lu\n",
195 "reader", urcu_get_thread_id());
201 void *thr_writer(void *_count
)
203 unsigned long long *count
= _count
;
204 struct test_array
*new, *old
;
206 printf_verbose("thread_begin %s, tid %lu\n",
207 "writer", urcu_get_thread_id());
214 rcu_copy_mutex_lock();
215 new = test_array_alloc();
217 old
= test_rcu_pointer
;
218 rcu_assign_pointer(test_rcu_pointer
, new);
219 if (caa_unlikely(wduration
))
220 loop_sleep(wduration
);
224 test_array_free(old
);
225 rcu_copy_mutex_unlock();
226 URCU_TLS(nr_writes
)++;
227 if (caa_unlikely(!test_duration_write()))
229 if (caa_unlikely(wdelay
))
233 printf_verbose("thread_end %s, tid %lu\n",
234 "writer", urcu_get_thread_id());
235 *count
= URCU_TLS(nr_writes
);
240 void show_usage(char **argv
)
242 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
244 printf("OPTIONS:\n");
245 printf(" [-r] [-w] (yield reader and/or writer)\n");
246 printf(" [-d delay] (writer period (us))\n");
247 printf(" [-c duration] (reader C.S. duration (in loops))\n");
248 printf(" [-e duration] (writer C.S. duration (in loops))\n");
249 printf(" [-v] (verbose output)\n");
250 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
254 int main(int argc
, char **argv
)
257 pthread_t
*tid_reader
, *tid_writer
;
259 unsigned long long *count_reader
, *count_writer
;
260 unsigned long long tot_reads
= 0, tot_writes
= 0;
269 err
= sscanf(argv
[1], "%u", &nr_readers
);
275 err
= sscanf(argv
[2], "%u", &nr_writers
);
281 err
= sscanf(argv
[3], "%lu", &duration
);
287 for (i
= 4; i
< argc
; i
++) {
288 if (argv
[i
][0] != '-')
290 switch (argv
[i
][1]) {
292 rcu_debug_yield_enable(RCU_YIELD_READ
);
295 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
303 cpu_affinities
[next_aff
++] = a
;
305 printf_verbose("Adding CPU %d affinity\n", a
);
312 rduration
= atol(argv
[++i
]);
319 wdelay
= atol(argv
[++i
]);
326 wduration
= atol(argv
[++i
]);
334 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
335 duration
, nr_readers
, nr_writers
);
336 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
337 printf_verbose("Reader duration : %lu loops.\n", rduration
);
338 printf_verbose("thread %-6s, tid %lu\n",
339 "main", urcu_get_thread_id());
341 test_array
= calloc(1, sizeof(*test_array
) * ARRAY_SIZE
);
342 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
343 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
344 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
345 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
349 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
350 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
351 &count_reader
[i_thr
]);
355 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
356 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
357 &count_writer
[i_thr
]);
364 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
365 err
= pthread_join(tid_reader
[i_thr
], &tret
);
368 tot_reads
+= count_reader
[i_thr
];
370 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
371 err
= pthread_join(tid_writer
[i_thr
], &tret
);
374 tot_writes
+= count_writer
[i_thr
];
377 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
379 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
381 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
382 argv
[0], duration
, nr_readers
, rduration
, wduration
,
383 nr_writers
, wdelay
, tot_reads
, tot_writes
,
384 tot_reads
+ tot_writes
);
386 test_array_free(test_rcu_pointer
);