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 unsigned long wdelay
;
40 static struct test_array
*test_rcu_pointer
;
42 static unsigned long duration
;
44 /* read-side C.S. duration, in loops */
45 static unsigned long rduration
;
47 /* write-side C.S. duration, in loops */
48 static unsigned long wduration
;
50 static inline void loop_sleep(unsigned long loops
)
56 static int verbose_mode
;
58 #define printf_verbose(fmt, args...) \
64 static unsigned int cpu_affinities
[NR_CPUS
];
65 static unsigned int next_aff
= 0;
66 static int use_affinity
= 0;
68 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
70 static void set_affinity(void)
72 #ifdef HAVE_SCHED_SETAFFINITY
75 #endif /* HAVE_SCHED_SETAFFINITY */
80 #ifdef HAVE_SCHED_SETAFFINITY
81 ret
= pthread_mutex_lock(&affinity_mutex
);
83 perror("Error in pthread mutex lock");
86 cpu
= cpu_affinities
[next_aff
++];
87 ret
= pthread_mutex_unlock(&affinity_mutex
);
89 perror("Error in pthread mutex unlock");
95 sched_setaffinity(0, sizeof(mask
), &mask
);
96 #endif /* HAVE_SCHED_SETAFFINITY */
99 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
100 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
103 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_writes
;
105 static unsigned int nr_readers
;
106 static unsigned int nr_writers
;
108 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
111 void *thr_reader(void *_count
)
113 unsigned long long *count
= _count
;
114 struct test_array
*local_ptr
;
116 printf_verbose("thread_begin %s, tid %lu\n",
117 "reader", urcu_get_thread_id());
121 rcu_register_thread();
127 local_ptr
= rcu_dereference(test_rcu_pointer
);
128 rcu_debug_yield_read();
130 urcu_posix_assert(local_ptr
->a
== 8);
131 if (caa_unlikely(rduration
))
132 loop_sleep(rduration
);
134 URCU_TLS(nr_reads
)++;
135 if (caa_unlikely(!test_duration_read()))
139 rcu_unregister_thread();
141 *count
= URCU_TLS(nr_reads
);
142 printf_verbose("thread_end %s, tid %lu\n",
143 "reader", urcu_get_thread_id());
148 static void test_cb2(void *data
__attribute__((unused
)))
152 static void test_cb1(void *data
__attribute__((unused
)))
157 void *thr_writer(void *data
)
159 unsigned long wtidx
= (unsigned long)data
;
160 struct test_array
*new, *old
= NULL
;
163 printf_verbose("thread_begin %s, tid %lu\n",
164 "writer", urcu_get_thread_id());
168 ret
= rcu_defer_register_thread();
170 printf("Error in rcu_defer_register_thread\n");
177 new = malloc(sizeof(*new));
179 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
180 if (caa_unlikely(wduration
))
181 loop_sleep(wduration
);
182 defer_rcu(free
, old
);
183 defer_rcu(test_cb1
, old
);
184 defer_rcu(test_cb1
, (void *)-2L);
185 defer_rcu(test_cb1
, (void *)-2L);
186 defer_rcu(test_cb1
, old
);
187 defer_rcu(test_cb2
, (void *)-2L);
188 defer_rcu(test_cb2
, (void *)-4L);
189 defer_rcu(test_cb2
, (void *)-2L);
190 URCU_TLS(nr_writes
)++;
191 if (caa_unlikely(!test_duration_write()))
193 if (caa_unlikely(wdelay
))
197 rcu_defer_unregister_thread();
199 printf_verbose("thread_end %s, tid %lu\n",
200 "writer", urcu_get_thread_id());
201 tot_nr_writes
[wtidx
] = URCU_TLS(nr_writes
);
206 void show_usage(char **argv
)
208 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
210 printf("OPTIONS:\n");
211 printf(" [-r] [-w] (yield reader and/or writer)\n");
212 printf(" [-d delay] (writer period (us))\n");
213 printf(" [-c duration] (reader C.S. duration (in loops))\n");
214 printf(" [-e duration] (writer C.S. duration (in loops))\n");
215 printf(" [-v] (verbose output)\n");
216 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
220 int main(int argc
, char **argv
)
223 pthread_t
*tid_reader
, *tid_writer
;
225 unsigned long long *count_reader
;
226 unsigned long long tot_reads
= 0, tot_writes
= 0;
235 err
= sscanf(argv
[1], "%u", &nr_readers
);
241 err
= sscanf(argv
[2], "%u", &nr_writers
);
247 err
= sscanf(argv
[3], "%lu", &duration
);
253 for (i
= 4; i
< argc
; i
++) {
254 if (argv
[i
][0] != '-')
256 switch (argv
[i
][1]) {
258 rcu_debug_yield_enable(RCU_YIELD_READ
);
261 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
269 cpu_affinities
[next_aff
++] = a
;
271 printf_verbose("Adding CPU %d affinity\n", a
);
278 rduration
= atol(argv
[++i
]);
285 wdelay
= atol(argv
[++i
]);
292 wduration
= atol(argv
[++i
]);
300 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
301 duration
, nr_readers
, nr_writers
);
302 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
303 printf_verbose("Reader duration : %lu loops.\n", rduration
);
304 printf_verbose("thread %-6s, tid %lu\n",
305 "main", urcu_get_thread_id());
307 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
308 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
309 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
310 tot_nr_writes
= calloc(nr_writers
, sizeof(*tot_nr_writes
));
314 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
315 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
316 &count_reader
[i_thr
]);
320 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
321 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
322 (void *)(long)i_thr
);
329 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
330 err
= pthread_join(tid_reader
[i_thr
], &tret
);
333 tot_reads
+= count_reader
[i_thr
];
335 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
336 err
= pthread_join(tid_writer
[i_thr
], &tret
);
339 tot_writes
+= tot_nr_writes
[i_thr
];
342 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
344 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
346 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
347 argv
[0], duration
, nr_readers
, rduration
, wduration
,
348 nr_writers
, wdelay
, tot_reads
, tot_writes
,
349 tot_reads
+ tot_writes
);