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 unsigned long wdelay
;
35 static int *test_rcu_pointer
;
37 static unsigned long duration
;
39 /* read-side C.S. duration, in loops */
40 static unsigned long rduration
;
42 /* write-side C.S. duration, in loops */
43 static unsigned long wduration
;
45 static inline void loop_sleep(unsigned long loops
)
51 static int verbose_mode
;
53 #define printf_verbose(fmt, args...) \
59 static unsigned int cpu_affinities
[NR_CPUS
];
60 static unsigned int next_aff
= 0;
61 static int use_affinity
= 0;
63 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
65 static void set_affinity(void)
67 #ifdef HAVE_SCHED_SETAFFINITY
70 #endif /* HAVE_SCHED_SETAFFINITY */
75 #ifdef HAVE_SCHED_SETAFFINITY
76 ret
= pthread_mutex_lock(&affinity_mutex
);
78 perror("Error in pthread mutex lock");
81 cpu
= cpu_affinities
[next_aff
++];
82 ret
= pthread_mutex_unlock(&affinity_mutex
);
84 perror("Error in pthread mutex unlock");
89 sched_setaffinity(0, sizeof(mask
), &mask
);
90 #endif /* HAVE_SCHED_SETAFFINITY */
93 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
94 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
96 static unsigned int nr_readers
;
97 static unsigned int nr_writers
;
99 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
102 void *thr_reader(void *_count
)
104 unsigned long long *count
= _count
;
107 printf_verbose("thread_begin %s, tid %lu\n",
108 "reader", urcu_get_thread_id());
112 rcu_register_thread();
114 urcu_posix_assert(rcu_read_ongoing());
115 rcu_thread_offline();
116 urcu_posix_assert(!rcu_read_ongoing());
123 urcu_posix_assert(rcu_read_ongoing());
124 local_ptr
= rcu_dereference(test_rcu_pointer
);
125 rcu_debug_yield_read();
127 urcu_posix_assert(*local_ptr
== 8);
128 if (caa_unlikely(rduration
))
129 loop_sleep(rduration
);
131 URCU_TLS(nr_reads
)++;
132 /* QS each 1024 reads */
133 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
134 rcu_quiescent_state();
135 if (caa_unlikely(!test_duration_read()))
139 rcu_unregister_thread();
141 /* test extra thread registration */
142 rcu_register_thread();
143 rcu_unregister_thread();
145 *count
= URCU_TLS(nr_reads
);
146 printf_verbose("thread_end %s, tid %lu\n",
147 "reader", urcu_get_thread_id());
153 void *thr_writer(void *_count
)
155 unsigned long long *count
= _count
;
158 printf_verbose("thread_begin %s, tid %lu\n",
159 "writer", urcu_get_thread_id());
166 new = malloc(sizeof(int));
167 urcu_posix_assert(new);
169 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
170 if (caa_unlikely(wduration
))
171 loop_sleep(wduration
);
176 URCU_TLS(nr_writes
)++;
177 if (caa_unlikely(!test_duration_write()))
179 if (caa_unlikely(wdelay
))
183 printf_verbose("thread_end %s, tid %lu\n",
184 "writer", urcu_get_thread_id());
185 *count
= URCU_TLS(nr_writes
);
190 void show_usage(char **argv
)
192 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
194 printf("OPTIONS:\n");
195 printf(" [-r] [-w] (yield reader and/or writer)\n");
196 printf(" [-d delay] (writer period (us))\n");
197 printf(" [-c duration] (reader C.S. duration (in loops))\n");
198 printf(" [-e duration] (writer C.S. duration (in loops))\n");
199 printf(" [-v] (verbose output)\n");
200 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
204 int main(int argc
, char **argv
)
207 pthread_t
*tid_reader
, *tid_writer
;
209 unsigned long long *count_reader
, *count_writer
;
210 unsigned long long tot_reads
= 0, tot_writes
= 0;
219 err
= sscanf(argv
[1], "%u", &nr_readers
);
225 err
= sscanf(argv
[2], "%u", &nr_writers
);
231 err
= sscanf(argv
[3], "%lu", &duration
);
237 for (i
= 4; i
< argc
; i
++) {
238 if (argv
[i
][0] != '-')
240 switch (argv
[i
][1]) {
242 rcu_debug_yield_enable(RCU_YIELD_READ
);
245 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
253 cpu_affinities
[next_aff
++] = a
;
255 printf_verbose("Adding CPU %d affinity\n", a
);
262 rduration
= atol(argv
[++i
]);
269 wdelay
= atol(argv
[++i
]);
276 wduration
= atol(argv
[++i
]);
284 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
285 duration
, nr_readers
, nr_writers
);
286 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
287 printf_verbose("Reader duration : %lu loops.\n", rduration
);
288 printf_verbose("thread %-6s, tid %lu\n",
289 "main", urcu_get_thread_id());
291 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
292 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
293 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
294 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
298 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
299 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
300 &count_reader
[i_thr
]);
304 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
305 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
306 &count_writer
[i_thr
]);
313 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
314 err
= pthread_join(tid_reader
[i_thr
], &tret
);
317 tot_reads
+= count_reader
[i_thr
];
319 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
320 err
= pthread_join(tid_writer
[i_thr
], &tret
);
323 tot_writes
+= count_writer
[i_thr
];
326 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
328 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
330 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
331 argv
[0], duration
, nr_readers
, rduration
, wduration
,
332 nr_writers
, wdelay
, tot_reads
, tot_writes
,
333 tot_reads
+ tot_writes
);
334 free(test_rcu_pointer
);