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 (with automatic reclamation)
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
32 #include <urcu-defer.h>
38 static volatile int test_go
, test_stop
;
40 static unsigned long wdelay
;
42 static struct test_array
*test_rcu_pointer
;
44 static unsigned long duration
;
46 /* read-side C.S. duration, in loops */
47 static unsigned long rduration
;
49 /* write-side C.S. duration, in loops */
50 static unsigned long wduration
;
52 static inline void loop_sleep(unsigned long loops
)
58 static int verbose_mode
;
60 #define printf_verbose(fmt, args...) \
66 static unsigned int cpu_affinities
[NR_CPUS
];
67 static unsigned int next_aff
= 0;
68 static int use_affinity
= 0;
70 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
72 static void set_affinity(void)
74 #ifdef HAVE_SCHED_SETAFFINITY
77 #endif /* HAVE_SCHED_SETAFFINITY */
82 #ifdef HAVE_SCHED_SETAFFINITY
83 ret
= pthread_mutex_lock(&affinity_mutex
);
85 perror("Error in pthread mutex lock");
88 cpu
= cpu_affinities
[next_aff
++];
89 ret
= pthread_mutex_unlock(&affinity_mutex
);
91 perror("Error in pthread mutex unlock");
97 sched_setaffinity(0, sizeof(mask
), &mask
);
98 #endif /* HAVE_SCHED_SETAFFINITY */
102 * returns 0 if test should end.
104 static int test_duration_write(void)
109 static int test_duration_read(void)
114 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
115 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
118 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_writes
;
120 static unsigned int nr_readers
;
121 static unsigned int nr_writers
;
123 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
126 void *thr_reader(void *_count
)
128 unsigned long long *count
= _count
;
129 struct test_array
*local_ptr
;
131 printf_verbose("thread_begin %s, tid %lu\n",
132 "reader", urcu_get_thread_id());
136 rcu_register_thread();
145 local_ptr
= rcu_dereference(test_rcu_pointer
);
146 rcu_debug_yield_read();
148 urcu_posix_assert(local_ptr
->a
== 8);
149 if (caa_unlikely(rduration
))
150 loop_sleep(rduration
);
152 URCU_TLS(nr_reads
)++;
153 if (caa_unlikely(!test_duration_read()))
157 rcu_unregister_thread();
159 *count
= URCU_TLS(nr_reads
);
160 printf_verbose("thread_end %s, tid %lu\n",
161 "reader", urcu_get_thread_id());
166 static void test_cb2(void *data
__attribute__((unused
)))
170 static void test_cb1(void *data
__attribute__((unused
)))
175 void *thr_writer(void *data
)
177 unsigned long wtidx
= (unsigned long)data
;
178 struct test_array
*new, *old
= NULL
;
181 printf_verbose("thread_begin %s, tid %lu\n",
182 "writer", urcu_get_thread_id());
186 ret
= rcu_defer_register_thread();
188 printf("Error in rcu_defer_register_thread\n");
198 new = malloc(sizeof(*new));
200 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
201 if (caa_unlikely(wduration
))
202 loop_sleep(wduration
);
203 defer_rcu(free
, old
);
204 defer_rcu(test_cb1
, old
);
205 defer_rcu(test_cb1
, (void *)-2L);
206 defer_rcu(test_cb1
, (void *)-2L);
207 defer_rcu(test_cb1
, old
);
208 defer_rcu(test_cb2
, (void *)-2L);
209 defer_rcu(test_cb2
, (void *)-4L);
210 defer_rcu(test_cb2
, (void *)-2L);
211 URCU_TLS(nr_writes
)++;
212 if (caa_unlikely(!test_duration_write()))
214 if (caa_unlikely(wdelay
))
218 rcu_defer_unregister_thread();
220 printf_verbose("thread_end %s, tid %lu\n",
221 "writer", urcu_get_thread_id());
222 tot_nr_writes
[wtidx
] = URCU_TLS(nr_writes
);
227 void show_usage(char **argv
)
229 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
231 printf("OPTIONS:\n");
232 printf(" [-r] [-w] (yield reader and/or writer)\n");
233 printf(" [-d delay] (writer period (us))\n");
234 printf(" [-c duration] (reader C.S. duration (in loops))\n");
235 printf(" [-e duration] (writer C.S. duration (in loops))\n");
236 printf(" [-v] (verbose output)\n");
237 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
241 int main(int argc
, char **argv
)
244 pthread_t
*tid_reader
, *tid_writer
;
246 unsigned long long *count_reader
;
247 unsigned long long tot_reads
= 0, tot_writes
= 0;
256 err
= sscanf(argv
[1], "%u", &nr_readers
);
262 err
= sscanf(argv
[2], "%u", &nr_writers
);
268 err
= sscanf(argv
[3], "%lu", &duration
);
274 for (i
= 4; i
< argc
; i
++) {
275 if (argv
[i
][0] != '-')
277 switch (argv
[i
][1]) {
279 rcu_debug_yield_enable(RCU_YIELD_READ
);
282 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
290 cpu_affinities
[next_aff
++] = a
;
292 printf_verbose("Adding CPU %d affinity\n", a
);
299 rduration
= atol(argv
[++i
]);
306 wdelay
= atol(argv
[++i
]);
313 wduration
= atol(argv
[++i
]);
321 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
322 duration
, nr_readers
, nr_writers
);
323 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
324 printf_verbose("Reader duration : %lu loops.\n", rduration
);
325 printf_verbose("thread %-6s, tid %lu\n",
326 "main", urcu_get_thread_id());
328 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
329 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
330 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
331 tot_nr_writes
= calloc(nr_writers
, sizeof(*tot_nr_writes
));
335 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
336 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
337 &count_reader
[i_thr
]);
341 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
342 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
343 (void *)(long)i_thr
);
356 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
357 err
= pthread_join(tid_reader
[i_thr
], &tret
);
360 tot_reads
+= count_reader
[i_thr
];
362 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
363 err
= pthread_join(tid_writer
[i_thr
], &tret
);
366 tot_writes
+= tot_nr_writes
[i_thr
];
369 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
371 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
373 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
374 argv
[0], duration
, nr_readers
, rduration
, wduration
,
375 nr_writers
, wdelay
, tot_reads
, tot_writes
,
376 tot_reads
+ tot_writes
);