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 struct per_thread_lock
{
38 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
))); /* cache-line aligned */
40 static struct per_thread_lock
*per_thread_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
);
90 perror("Error in pthread mutex lock");
93 cpu
= cpu_affinities
[next_aff
++];
94 ret
= pthread_mutex_unlock(&affinity_mutex
);
97 perror("Error in pthread mutex unlock");
102 sched_setaffinity(0, sizeof(mask
), &mask
);
103 #endif /* HAVE_SCHED_SETAFFINITY */
107 * returns 0 if test should end.
109 static int test_duration_write(void)
114 static int test_duration_read(void)
119 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
120 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
123 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_writes
;
125 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_reads
;
127 static unsigned int nr_readers
;
128 static unsigned int nr_writers
;
130 static void urcu_mutex_lock(pthread_mutex_t
*lock
)
134 ret
= pthread_mutex_lock(lock
);
137 perror("Error in pthread mutex lock");
142 static void urcu_mutex_unlock(pthread_mutex_t
*lock
)
146 ret
= pthread_mutex_unlock(lock
);
149 perror("Error in pthread mutex unlock");
155 void *thr_reader(void *data
)
157 unsigned long tidx
= (unsigned long)data
;
159 printf_verbose("thread_begin %s, tid %lu\n",
160 "reader", urcu_get_thread_id());
171 urcu_mutex_lock(&per_thread_lock
[tidx
].lock
);
173 urcu_posix_assert(v
== 8);
174 if (caa_unlikely(rduration
))
175 loop_sleep(rduration
);
176 urcu_mutex_unlock(&per_thread_lock
[tidx
].lock
);
177 URCU_TLS(nr_reads
)++;
178 if (caa_unlikely(!test_duration_read()))
182 tot_nr_reads
[tidx
] = URCU_TLS(nr_reads
);
183 printf_verbose("thread_end %s, tid %lu\n",
184 "reader", urcu_get_thread_id());
190 void *thr_writer(void *data
)
192 unsigned long wtidx
= (unsigned long)data
;
195 printf_verbose("thread_begin %s, tid %lu\n",
196 "writer", urcu_get_thread_id());
206 for (tidx
= 0; tidx
< (long)nr_readers
; tidx
++) {
207 urcu_mutex_lock(&per_thread_lock
[tidx
].lock
);
211 if (caa_unlikely(wduration
))
212 loop_sleep(wduration
);
213 for (tidx
= (long)nr_readers
- 1; tidx
>= 0; tidx
--) {
214 urcu_mutex_unlock(&per_thread_lock
[tidx
].lock
);
216 URCU_TLS(nr_writes
)++;
217 if (caa_unlikely(!test_duration_write()))
219 if (caa_unlikely(wdelay
))
223 printf_verbose("thread_end %s, tid %lu\n",
224 "writer", urcu_get_thread_id());
225 tot_nr_writes
[wtidx
] = URCU_TLS(nr_writes
);
230 void show_usage(char **argv
)
232 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
234 printf("OPTIONS:\n");
235 printf(" [-d delay] (writer period (us))\n");
236 printf(" [-c duration] (reader C.S. duration (in loops))\n");
237 printf(" [-e duration] (writer C.S. duration (in loops))\n");
238 printf(" [-v] (verbose output)\n");
239 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
243 int main(int argc
, char **argv
)
246 pthread_t
*tid_reader
, *tid_writer
;
248 unsigned long long tot_reads
= 0, tot_writes
= 0;
258 err
= sscanf(argv
[1], "%u", &nr_readers
);
264 err
= sscanf(argv
[2], "%u", &nr_writers
);
270 err
= sscanf(argv
[3], "%lu", &duration
);
276 for (i
= 4; i
< argc
; i
++) {
277 if (argv
[i
][0] != '-')
279 switch (argv
[i
][1]) {
286 cpu_affinities
[next_aff
++] = a
;
288 printf_verbose("Adding CPU %d affinity\n", a
);
295 rduration
= atol(argv
[++i
]);
302 wdelay
= atol(argv
[++i
]);
309 wduration
= atol(argv
[++i
]);
317 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
318 duration
, nr_readers
, nr_writers
);
319 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
320 printf_verbose("Reader duration : %lu loops.\n", rduration
);
321 printf_verbose("thread %-6s, tid %lu\n",
322 "main", urcu_get_thread_id());
324 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
325 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
326 tot_nr_reads
= calloc(nr_readers
, sizeof(*tot_nr_reads
));
327 tot_nr_writes
= calloc(nr_writers
, sizeof(*tot_nr_writes
));
328 per_thread_lock
= calloc(nr_readers
, sizeof(*per_thread_lock
));
329 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
330 pthread_mutex_init(&per_thread_lock
[i_thr
].lock
, NULL
);
335 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
336 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
337 (void *)(long)i_thr
);
341 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
342 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
343 (void *)(long)i_thr
);
356 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
357 err
= pthread_join(tid_reader
[i_thr
], &tret
);
360 tot_reads
+= tot_nr_reads
[i_thr
];
362 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
363 err
= pthread_join(tid_writer
[i_thr
], &tret
);
366 tot_writes
+= tot_nr_writes
[i_thr
];
369 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
371 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
373 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
374 argv
[0], duration
, nr_readers
, rduration
, wduration
,
375 nr_writers
, wdelay
, tot_reads
, tot_writes
,
376 tot_reads
+ tot_writes
);
382 free(per_thread_lock
);