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
37 * static rwlock initializer is broken on Cygwin. Use runtime
40 pthread_rwlock_t lock
;
42 static volatile int test_go
, test_stop
;
44 static unsigned long wdelay
;
46 static volatile struct test_array test_array
= { 8 };
48 static unsigned long duration
;
50 /* read-side C.S. duration, in loops */
51 static unsigned long rduration
;
53 /* write-side C.S. duration, in loops */
54 static unsigned long wduration
;
56 static inline void loop_sleep(unsigned long loops
)
62 static int verbose_mode
;
64 #define printf_verbose(fmt, args...) \
70 static unsigned int cpu_affinities
[NR_CPUS
];
71 static unsigned int next_aff
= 0;
72 static int use_affinity
= 0;
74 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
76 static void set_affinity(void)
78 #ifdef HAVE_SCHED_SETAFFINITY
81 #endif /* HAVE_SCHED_SETAFFINITY */
86 #ifdef HAVE_SCHED_SETAFFINITY
87 ret
= pthread_mutex_lock(&affinity_mutex
);
89 perror("Error in pthread mutex lock");
92 cpu
= cpu_affinities
[next_aff
++];
93 ret
= pthread_mutex_unlock(&affinity_mutex
);
95 perror("Error in pthread mutex unlock");
101 sched_setaffinity(0, sizeof(mask
), &mask
);
102 #endif /* HAVE_SCHED_SETAFFINITY */
106 * returns 0 if test should end.
108 static int test_duration_write(void)
113 static int test_duration_read(void)
118 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
119 static DEFINE_URCU_TLS(unsigned long long, 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 *_count
)
129 unsigned long long *count
= _count
;
131 printf_verbose("thread_begin %s, tid %lu\n",
132 "reader", urcu_get_thread_id());
143 ret
= pthread_rwlock_rdlock(&lock
);
145 fprintf(stderr
, "reader pthread_rwlock_rdlock: %s\n", strerror(ret
));
150 urcu_posix_assert(a
== 8);
151 if (caa_unlikely(rduration
))
152 loop_sleep(rduration
);
154 ret
= pthread_rwlock_unlock(&lock
);
156 fprintf(stderr
, "reader pthread_rwlock_unlock: %s\n", strerror(ret
));
160 URCU_TLS(nr_reads
)++;
161 if (caa_unlikely(!test_duration_read()))
165 *count
= URCU_TLS(nr_reads
);
167 printf_verbose("thread_end %s, tid %lu, count %llu\n",
168 "reader", urcu_get_thread_id(), *count
);
174 void *thr_writer(void *_count
)
176 unsigned long long *count
= _count
;
178 printf_verbose("thread_begin %s, tid %lu\n",
179 "writer", urcu_get_thread_id());
191 ret
= pthread_rwlock_wrlock(&lock
);
193 fprintf(stderr
, "writer pthread_rwlock_wrlock: %s\n", strerror(ret
));
199 if (caa_unlikely(wduration
))
200 loop_sleep(wduration
);
202 ret
= pthread_rwlock_unlock(&lock
);
204 fprintf(stderr
, "writer pthread_rwlock_unlock: %s\n", strerror(ret
));
208 URCU_TLS(nr_writes
)++;
209 if (caa_unlikely(!test_duration_write()))
211 if (caa_unlikely(wdelay
))
214 *count
= URCU_TLS(nr_writes
);
216 printf_verbose("thread_end %s, tid %lu, count %llu\n",
217 "writer", urcu_get_thread_id(), *count
);
222 void show_usage(char **argv
)
224 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
226 printf("OPTIONS:\n");
227 printf(" [-d delay] (writer period (us))\n");
228 printf(" [-c duration] (reader C.S. duration (in loops))\n");
229 printf(" [-e duration] (writer C.S. duration (in loops))\n");
230 printf(" [-v] (verbose output)\n");
231 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
235 int main(int argc
, char **argv
)
238 pthread_t
*tid_reader
, *tid_writer
;
240 unsigned long long *count_reader
, *count_writer
;
241 unsigned long long tot_reads
= 0, tot_writes
= 0;
251 err
= sscanf(argv
[1], "%u", &nr_readers
);
257 err
= sscanf(argv
[2], "%u", &nr_writers
);
263 err
= sscanf(argv
[3], "%lu", &duration
);
269 for (i
= 4; i
< argc
; i
++) {
270 if (argv
[i
][0] != '-')
272 switch (argv
[i
][1]) {
279 cpu_affinities
[next_aff
++] = a
;
281 printf_verbose("Adding CPU %d affinity\n", a
);
288 rduration
= atol(argv
[++i
]);
295 wdelay
= atol(argv
[++i
]);
302 wduration
= atol(argv
[++i
]);
310 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
311 duration
, nr_readers
, nr_writers
);
312 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
313 printf_verbose("Writer duration : %lu loops.\n", wduration
);
314 printf_verbose("Reader duration : %lu loops.\n", rduration
);
315 printf_verbose("thread %-6s, tid %lu\n",
316 "main", urcu_get_thread_id());
318 err
= pthread_rwlock_init(&lock
, NULL
);
320 fprintf(stderr
, "pthread_rwlock_init: (%d) %s\n", err
, strerror(err
));
324 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
325 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
326 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
327 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
331 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
332 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
333 &count_reader
[i_thr
]);
337 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
338 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
339 &count_writer
[i_thr
]);
352 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
353 err
= pthread_join(tid_reader
[i_thr
], &tret
);
356 tot_reads
+= count_reader
[i_thr
];
358 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
359 err
= pthread_join(tid_writer
[i_thr
], &tret
);
362 tot_writes
+= count_writer
[i_thr
];
365 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
367 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
369 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
370 argv
[0], duration
, nr_readers
, rduration
, wduration
,
371 nr_writers
, wdelay
, tot_reads
, tot_writes
,
372 tot_reads
+ tot_writes
);
374 err
= pthread_rwlock_destroy(&lock
);
376 fprintf(stderr
, "pthread_rwlock_destroy: (%d) %s\n", err
, strerror(err
));