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
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");
90 sched_setaffinity(0, sizeof(mask
), &mask
);
91 #endif /* HAVE_SCHED_SETAFFINITY */
94 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
95 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
97 static unsigned int nr_readers
;
98 static unsigned int nr_writers
;
100 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
103 void *thr_reader(void *_count
)
105 unsigned long long *count
= _count
;
108 printf_verbose("thread_begin %s, tid %lu\n",
109 "reader", urcu_get_thread_id());
113 rcu_register_thread();
114 urcu_posix_assert(!rcu_read_ongoing());
120 urcu_posix_assert(rcu_read_ongoing());
121 local_ptr
= rcu_dereference(test_rcu_pointer
);
122 rcu_debug_yield_read();
124 urcu_posix_assert(*local_ptr
== 8);
125 if (caa_unlikely(rduration
))
126 loop_sleep(rduration
);
128 URCU_TLS(nr_reads
)++;
129 if (caa_unlikely(!test_duration_read()))
133 rcu_unregister_thread();
135 *count
= URCU_TLS(nr_reads
);
136 printf_verbose("thread_end %s, tid %lu\n",
137 "reader", urcu_get_thread_id());
143 void *thr_writer(void *_count
)
145 unsigned long long *count
= _count
;
148 printf_verbose("thread_begin %s, tid %lu\n",
149 "writer", urcu_get_thread_id());
156 new = malloc(sizeof(int));
158 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
159 if (caa_unlikely(wduration
))
160 loop_sleep(wduration
);
165 URCU_TLS(nr_writes
)++;
166 if (caa_unlikely(!test_duration_write()))
168 if (caa_unlikely(wdelay
))
172 printf_verbose("thread_end %s, tid %lu\n",
173 "writer", urcu_get_thread_id());
174 *count
= URCU_TLS(nr_writes
);
179 void show_usage(char **argv
)
181 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
183 printf("OPTIONS:\n");
184 printf(" [-r] [-w] (yield reader and/or writer)\n");
185 printf(" [-d delay] (writer period (us))\n");
186 printf(" [-c duration] (reader C.S. duration (in loops))\n");
187 printf(" [-e duration] (writer C.S. duration (in loops))\n");
188 printf(" [-v] (verbose output)\n");
189 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
193 int main(int argc
, char **argv
)
196 pthread_t
*tid_reader
, *tid_writer
;
198 unsigned long long *count_reader
, *count_writer
;
199 unsigned long long tot_reads
= 0, tot_writes
= 0;
208 err
= sscanf(argv
[1], "%u", &nr_readers
);
214 err
= sscanf(argv
[2], "%u", &nr_writers
);
220 err
= sscanf(argv
[3], "%lu", &duration
);
226 for (i
= 4; i
< argc
; i
++) {
227 if (argv
[i
][0] != '-')
229 switch (argv
[i
][1]) {
231 rcu_debug_yield_enable(RCU_YIELD_READ
);
234 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
242 cpu_affinities
[next_aff
++] = a
;
244 printf_verbose("Adding CPU %d affinity\n", a
);
251 rduration
= atol(argv
[++i
]);
258 wdelay
= atol(argv
[++i
]);
265 wduration
= atol(argv
[++i
]);
273 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
274 duration
, nr_readers
, nr_writers
);
275 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
276 printf_verbose("Reader duration : %lu loops.\n", rduration
);
277 printf_verbose("thread %-6s, tid %lu\n",
278 "main", urcu_get_thread_id());
280 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
281 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
282 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
283 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
287 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
288 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
289 &count_reader
[i_thr
]);
293 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
294 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
295 &count_writer
[i_thr
]);
302 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
303 err
= pthread_join(tid_reader
[i_thr
], &tret
);
306 tot_reads
+= count_reader
[i_thr
];
308 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
309 err
= pthread_join(tid_writer
[i_thr
], &tret
);
312 tot_writes
+= count_writer
[i_thr
];
315 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
317 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
319 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
320 argv
[0], duration
, nr_readers
, rduration
, wduration
,
321 nr_writers
, wdelay
, tot_reads
, tot_writes
,
322 tot_reads
+ tot_writes
);
324 free(test_rcu_pointer
);