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
31 #include "urcu-qsbr.h"
33 static volatile int test_go
, test_stop
;
35 static unsigned long wdelay
;
37 static int *test_rcu_pointer
;
39 static unsigned long duration
;
41 /* read-side C.S. duration, in loops */
42 static unsigned long rduration
;
44 /* write-side C.S. duration, in loops */
45 static unsigned long wduration
;
47 static inline void loop_sleep(unsigned long loops
)
53 static int verbose_mode
;
55 #define printf_verbose(fmt, args...) \
61 static unsigned int cpu_affinities
[NR_CPUS
];
62 static unsigned int next_aff
= 0;
63 static int use_affinity
= 0;
65 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
67 static void set_affinity(void)
69 #ifdef HAVE_SCHED_SETAFFINITY
72 #endif /* HAVE_SCHED_SETAFFINITY */
77 #ifdef HAVE_SCHED_SETAFFINITY
78 ret
= pthread_mutex_lock(&affinity_mutex
);
80 perror("Error in pthread mutex lock");
83 cpu
= cpu_affinities
[next_aff
++];
84 ret
= pthread_mutex_unlock(&affinity_mutex
);
86 perror("Error in pthread mutex unlock");
91 sched_setaffinity(0, sizeof(mask
), &mask
);
92 #endif /* HAVE_SCHED_SETAFFINITY */
96 * returns 0 if test should end.
98 static int test_duration_write(void)
103 static int test_duration_read(void)
108 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
109 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
111 static unsigned int nr_readers
;
112 static unsigned int nr_writers
;
114 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
117 void *thr_reader(void *_count
)
119 unsigned long long *count
= _count
;
122 printf_verbose("thread_begin %s, tid %lu\n",
123 "reader", urcu_get_thread_id());
127 rcu_register_thread();
129 urcu_posix_assert(rcu_read_ongoing());
130 rcu_thread_offline();
131 urcu_posix_assert(!rcu_read_ongoing());
141 urcu_posix_assert(rcu_read_ongoing());
142 local_ptr
= rcu_dereference(test_rcu_pointer
);
143 rcu_debug_yield_read();
145 urcu_posix_assert(*local_ptr
== 8);
146 if (caa_unlikely(rduration
))
147 loop_sleep(rduration
);
149 URCU_TLS(nr_reads
)++;
150 /* QS each 1024 reads */
151 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
152 rcu_quiescent_state();
153 if (caa_unlikely(!test_duration_read()))
157 rcu_unregister_thread();
159 /* test extra thread registration */
160 rcu_register_thread();
161 rcu_unregister_thread();
163 *count
= URCU_TLS(nr_reads
);
164 printf_verbose("thread_end %s, tid %lu\n",
165 "reader", urcu_get_thread_id());
171 void *thr_writer(void *_count
)
173 unsigned long long *count
= _count
;
176 printf_verbose("thread_begin %s, tid %lu\n",
177 "writer", urcu_get_thread_id());
187 new = malloc(sizeof(int));
188 urcu_posix_assert(new);
190 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
191 if (caa_unlikely(wduration
))
192 loop_sleep(wduration
);
197 URCU_TLS(nr_writes
)++;
198 if (caa_unlikely(!test_duration_write()))
200 if (caa_unlikely(wdelay
))
204 printf_verbose("thread_end %s, tid %lu\n",
205 "writer", urcu_get_thread_id());
206 *count
= URCU_TLS(nr_writes
);
211 void show_usage(char **argv
)
213 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
215 printf("OPTIONS:\n");
216 printf(" [-r] [-w] (yield reader and/or writer)\n");
217 printf(" [-d delay] (writer period (us))\n");
218 printf(" [-c duration] (reader C.S. duration (in loops))\n");
219 printf(" [-e duration] (writer C.S. duration (in loops))\n");
220 printf(" [-v] (verbose output)\n");
221 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
225 int main(int argc
, char **argv
)
228 pthread_t
*tid_reader
, *tid_writer
;
230 unsigned long long *count_reader
, *count_writer
;
231 unsigned long long tot_reads
= 0, tot_writes
= 0;
240 err
= sscanf(argv
[1], "%u", &nr_readers
);
246 err
= sscanf(argv
[2], "%u", &nr_writers
);
252 err
= sscanf(argv
[3], "%lu", &duration
);
258 for (i
= 4; i
< argc
; i
++) {
259 if (argv
[i
][0] != '-')
261 switch (argv
[i
][1]) {
263 rcu_debug_yield_enable(RCU_YIELD_READ
);
266 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
274 cpu_affinities
[next_aff
++] = a
;
276 printf_verbose("Adding CPU %d affinity\n", a
);
283 rduration
= atol(argv
[++i
]);
290 wdelay
= atol(argv
[++i
]);
297 wduration
= atol(argv
[++i
]);
305 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
306 duration
, nr_readers
, nr_writers
);
307 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
308 printf_verbose("Reader duration : %lu loops.\n", rduration
);
309 printf_verbose("thread %-6s, tid %lu\n",
310 "main", urcu_get_thread_id());
312 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
313 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
314 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
315 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
319 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
320 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
321 &count_reader
[i_thr
]);
325 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
326 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
327 &count_writer
[i_thr
]);
340 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
341 err
= pthread_join(tid_reader
[i_thr
], &tret
);
344 tot_reads
+= count_reader
[i_thr
];
346 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
347 err
= pthread_join(tid_writer
[i_thr
], &tret
);
350 tot_writes
+= count_writer
[i_thr
];
353 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
355 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
357 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
358 argv
[0], duration
, nr_readers
, rduration
, wduration
,
359 nr_writers
, wdelay
, tot_reads
, tot_writes
,
360 tot_reads
+ tot_writes
);
361 free(test_rcu_pointer
);