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"
24 /* hardcoded number of CPUs */
27 #ifndef DYNAMIC_LINK_TEST
36 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
38 static volatile int test_go
, test_stop
;
40 static unsigned long wdelay
;
42 static volatile struct test_array test_array
= { 8 };
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");
96 sched_setaffinity(0, sizeof(mask
), &mask
);
97 #endif /* HAVE_SCHED_SETAFFINITY */
101 * returns 0 if test should end.
103 static int test_duration_write(void)
108 static int test_duration_read(void)
113 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
114 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
117 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_writes
;
119 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_reads
;
121 static unsigned int nr_readers
;
122 static unsigned int nr_writers
;
124 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
127 void *thr_reader(void *data
)
129 unsigned long tidx
= (unsigned long)data
;
131 printf_verbose("thread_begin %s, tid %lu\n",
132 "reader", urcu_get_thread_id());
143 pthread_mutex_lock(&lock
);
145 urcu_posix_assert(v
== 8);
146 if (caa_unlikely(rduration
))
147 loop_sleep(rduration
);
148 pthread_mutex_unlock(&lock
);
149 URCU_TLS(nr_reads
)++;
150 if (caa_unlikely(!test_duration_read()))
154 tot_nr_reads
[tidx
] = URCU_TLS(nr_reads
);
155 printf_verbose("thread_end %s, tid %lu\n",
156 "reader", urcu_get_thread_id());
162 void *thr_writer(void *data
)
164 unsigned long wtidx
= (unsigned long)data
;
166 printf_verbose("thread_begin %s, tid %lu\n",
167 "writer", urcu_get_thread_id());
177 pthread_mutex_lock(&lock
);
180 if (caa_unlikely(wduration
))
181 loop_sleep(wduration
);
182 pthread_mutex_unlock(&lock
);
183 URCU_TLS(nr_writes
)++;
184 if (caa_unlikely(!test_duration_write()))
186 if (caa_unlikely(wdelay
))
190 printf_verbose("thread_end %s, tid %lu\n",
191 "writer", urcu_get_thread_id());
192 tot_nr_writes
[wtidx
] = URCU_TLS(nr_writes
);
197 void show_usage(char **argv
)
199 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
201 printf("OPTIONS:\n");
202 printf(" [-d delay] (writer period (us))\n");
203 printf(" [-c duration] (reader C.S. duration (in loops))\n");
204 printf(" [-e duration] (writer C.S. duration (in loops))\n");
205 printf(" [-v] (verbose output)\n");
206 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
210 int main(int argc
, char **argv
)
213 pthread_t
*tid_reader
, *tid_writer
;
215 unsigned long long *count_reader
, *count_writer
;
216 unsigned long long tot_reads
= 0, tot_writes
= 0;
226 err
= sscanf(argv
[1], "%u", &nr_readers
);
232 err
= sscanf(argv
[2], "%u", &nr_writers
);
238 err
= sscanf(argv
[3], "%lu", &duration
);
244 for (i
= 4; i
< argc
; i
++) {
245 if (argv
[i
][0] != '-')
247 switch (argv
[i
][1]) {
254 cpu_affinities
[next_aff
++] = a
;
256 printf_verbose("Adding CPU %d affinity\n", a
);
263 rduration
= atol(argv
[++i
]);
270 wdelay
= atol(argv
[++i
]);
277 wduration
= atol(argv
[++i
]);
285 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
286 duration
, nr_readers
, nr_writers
);
287 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
288 printf_verbose("Reader duration : %lu loops.\n", rduration
);
289 printf_verbose("thread %-6s, tid %lu\n",
290 "main", urcu_get_thread_id());
292 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
293 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
294 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
295 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
296 tot_nr_reads
= calloc(nr_readers
, sizeof(*tot_nr_reads
));
297 tot_nr_writes
= calloc(nr_writers
, sizeof(*tot_nr_writes
));
301 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
302 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
303 (void *)(long)i_thr
);
307 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
308 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
309 (void *)(long)i_thr
);
322 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
323 err
= pthread_join(tid_reader
[i_thr
], &tret
);
326 tot_reads
+= tot_nr_reads
[i_thr
];
328 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
329 err
= pthread_join(tid_writer
[i_thr
], &tret
);
332 tot_writes
+= tot_nr_writes
[i_thr
];
335 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
337 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
339 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
340 argv
[0], duration
, nr_readers
, rduration
, wduration
,
341 nr_writers
, wdelay
, tot_reads
, tot_writes
,
342 tot_reads
+ tot_writes
);