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 unsigned long wdelay
;
44 static volatile struct test_array test_array
= { 8 };
46 static unsigned long duration
;
48 /* read-side C.S. duration, in loops */
49 static unsigned long rduration
;
51 /* write-side C.S. duration, in loops */
52 static unsigned long wduration
;
54 static inline void loop_sleep(unsigned long loops
)
60 static int verbose_mode
;
62 #define printf_verbose(fmt, args...) \
68 static unsigned int cpu_affinities
[NR_CPUS
];
69 static unsigned int next_aff
= 0;
70 static int use_affinity
= 0;
72 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
74 static void set_affinity(void)
76 #ifdef HAVE_SCHED_SETAFFINITY
79 #endif /* HAVE_SCHED_SETAFFINITY */
84 #ifdef HAVE_SCHED_SETAFFINITY
85 ret
= pthread_mutex_lock(&affinity_mutex
);
87 perror("Error in pthread mutex lock");
90 cpu
= cpu_affinities
[next_aff
++];
91 ret
= pthread_mutex_unlock(&affinity_mutex
);
93 perror("Error in pthread mutex unlock");
99 sched_setaffinity(0, sizeof(mask
), &mask
);
100 #endif /* HAVE_SCHED_SETAFFINITY */
103 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
104 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
106 static unsigned int nr_readers
;
107 static unsigned int nr_writers
;
109 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
112 void *thr_reader(void *_count
)
114 unsigned long long *count
= _count
;
116 printf_verbose("thread_begin %s, tid %lu\n",
117 "reader", urcu_get_thread_id());
126 ret
= pthread_rwlock_rdlock(&lock
);
128 fprintf(stderr
, "reader pthread_rwlock_rdlock: %s\n", strerror(ret
));
133 urcu_posix_assert(a
== 8);
134 if (caa_unlikely(rduration
))
135 loop_sleep(rduration
);
137 ret
= pthread_rwlock_unlock(&lock
);
139 fprintf(stderr
, "reader pthread_rwlock_unlock: %s\n", strerror(ret
));
143 URCU_TLS(nr_reads
)++;
144 if (caa_unlikely(!test_duration_read()))
148 *count
= URCU_TLS(nr_reads
);
150 printf_verbose("thread_end %s, tid %lu, count %llu\n",
151 "reader", urcu_get_thread_id(), *count
);
157 void *thr_writer(void *_count
)
159 unsigned long long *count
= _count
;
161 printf_verbose("thread_begin %s, tid %lu\n",
162 "writer", urcu_get_thread_id());
171 ret
= pthread_rwlock_wrlock(&lock
);
173 fprintf(stderr
, "writer pthread_rwlock_wrlock: %s\n", strerror(ret
));
179 if (caa_unlikely(wduration
))
180 loop_sleep(wduration
);
182 ret
= pthread_rwlock_unlock(&lock
);
184 fprintf(stderr
, "writer pthread_rwlock_unlock: %s\n", strerror(ret
));
188 URCU_TLS(nr_writes
)++;
189 if (caa_unlikely(!test_duration_write()))
191 if (caa_unlikely(wdelay
))
194 *count
= URCU_TLS(nr_writes
);
196 printf_verbose("thread_end %s, tid %lu, count %llu\n",
197 "writer", urcu_get_thread_id(), *count
);
202 void show_usage(char **argv
)
204 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
206 printf("OPTIONS:\n");
207 printf(" [-d delay] (writer period (us))\n");
208 printf(" [-c duration] (reader C.S. duration (in loops))\n");
209 printf(" [-e duration] (writer C.S. duration (in loops))\n");
210 printf(" [-v] (verbose output)\n");
211 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
215 int main(int argc
, char **argv
)
218 pthread_t
*tid_reader
, *tid_writer
;
220 unsigned long long *count_reader
, *count_writer
;
221 unsigned long long tot_reads
= 0, tot_writes
= 0;
231 err
= sscanf(argv
[1], "%u", &nr_readers
);
237 err
= sscanf(argv
[2], "%u", &nr_writers
);
243 err
= sscanf(argv
[3], "%lu", &duration
);
249 for (i
= 4; i
< argc
; i
++) {
250 if (argv
[i
][0] != '-')
252 switch (argv
[i
][1]) {
259 cpu_affinities
[next_aff
++] = a
;
261 printf_verbose("Adding CPU %d affinity\n", a
);
268 rduration
= atol(argv
[++i
]);
275 wdelay
= atol(argv
[++i
]);
282 wduration
= atol(argv
[++i
]);
290 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
291 duration
, nr_readers
, nr_writers
);
292 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
293 printf_verbose("Writer duration : %lu loops.\n", wduration
);
294 printf_verbose("Reader duration : %lu loops.\n", rduration
);
295 printf_verbose("thread %-6s, tid %lu\n",
296 "main", urcu_get_thread_id());
298 err
= pthread_rwlock_init(&lock
, NULL
);
300 fprintf(stderr
, "pthread_rwlock_init: (%d) %s\n", err
, strerror(err
));
304 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
305 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
306 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
307 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
311 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
312 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
313 &count_reader
[i_thr
]);
317 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
318 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
319 &count_writer
[i_thr
]);
326 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
327 err
= pthread_join(tid_reader
[i_thr
], &tret
);
330 tot_reads
+= count_reader
[i_thr
];
332 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
333 err
= pthread_join(tid_writer
[i_thr
], &tret
);
336 tot_writes
+= count_writer
[i_thr
];
339 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
341 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
343 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
344 argv
[0], duration
, nr_readers
, rduration
, wduration
,
345 nr_writers
, wdelay
, tot_reads
, tot_writes
,
346 tot_reads
+ tot_writes
);
348 err
= pthread_rwlock_destroy(&lock
);
350 fprintf(stderr
, "pthread_rwlock_destroy: (%d) %s\n", err
, strerror(err
));