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 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
);
88 perror("Error in pthread mutex lock");
91 cpu
= cpu_affinities
[next_aff
++];
92 ret
= pthread_mutex_unlock(&affinity_mutex
);
95 perror("Error in pthread mutex unlock");
100 sched_setaffinity(0, sizeof(mask
), &mask
);
101 #endif /* HAVE_SCHED_SETAFFINITY */
104 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
105 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
108 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_writes
;
110 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_reads
;
112 static unsigned int nr_readers
;
113 static unsigned int nr_writers
;
115 static void urcu_mutex_lock(pthread_mutex_t
*lock
)
119 ret
= pthread_mutex_lock(lock
);
122 perror("Error in pthread mutex lock");
127 static void urcu_mutex_unlock(pthread_mutex_t
*lock
)
131 ret
= pthread_mutex_unlock(lock
);
134 perror("Error in pthread mutex unlock");
140 void *thr_reader(void *data
)
142 unsigned long tidx
= (unsigned long)data
;
144 printf_verbose("thread_begin %s, tid %lu\n",
145 "reader", urcu_get_thread_id());
154 urcu_mutex_lock(&per_thread_lock
[tidx
].lock
);
156 urcu_posix_assert(v
== 8);
157 if (caa_unlikely(rduration
))
158 loop_sleep(rduration
);
159 urcu_mutex_unlock(&per_thread_lock
[tidx
].lock
);
160 URCU_TLS(nr_reads
)++;
161 if (caa_unlikely(!test_duration_read()))
165 tot_nr_reads
[tidx
] = URCU_TLS(nr_reads
);
166 printf_verbose("thread_end %s, tid %lu\n",
167 "reader", urcu_get_thread_id());
173 void *thr_writer(void *data
)
175 unsigned long wtidx
= (unsigned long)data
;
178 printf_verbose("thread_begin %s, tid %lu\n",
179 "writer", urcu_get_thread_id());
186 for (tidx
= 0; tidx
< (long)nr_readers
; tidx
++) {
187 urcu_mutex_lock(&per_thread_lock
[tidx
].lock
);
191 if (caa_unlikely(wduration
))
192 loop_sleep(wduration
);
193 for (tidx
= (long)nr_readers
- 1; tidx
>= 0; tidx
--) {
194 urcu_mutex_unlock(&per_thread_lock
[tidx
].lock
);
196 URCU_TLS(nr_writes
)++;
197 if (caa_unlikely(!test_duration_write()))
199 if (caa_unlikely(wdelay
))
203 printf_verbose("thread_end %s, tid %lu\n",
204 "writer", urcu_get_thread_id());
205 tot_nr_writes
[wtidx
] = URCU_TLS(nr_writes
);
210 void show_usage(char **argv
)
212 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
214 printf("OPTIONS:\n");
215 printf(" [-d delay] (writer period (us))\n");
216 printf(" [-c duration] (reader C.S. duration (in loops))\n");
217 printf(" [-e duration] (writer C.S. duration (in loops))\n");
218 printf(" [-v] (verbose output)\n");
219 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
223 int main(int argc
, char **argv
)
226 pthread_t
*tid_reader
, *tid_writer
;
228 unsigned long long tot_reads
= 0, tot_writes
= 0;
238 err
= sscanf(argv
[1], "%u", &nr_readers
);
244 err
= sscanf(argv
[2], "%u", &nr_writers
);
250 err
= sscanf(argv
[3], "%lu", &duration
);
256 for (i
= 4; i
< argc
; i
++) {
257 if (argv
[i
][0] != '-')
259 switch (argv
[i
][1]) {
266 cpu_affinities
[next_aff
++] = a
;
268 printf_verbose("Adding CPU %d affinity\n", a
);
275 rduration
= atol(argv
[++i
]);
282 wdelay
= atol(argv
[++i
]);
289 wduration
= atol(argv
[++i
]);
297 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
298 duration
, nr_readers
, nr_writers
);
299 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
300 printf_verbose("Reader duration : %lu loops.\n", rduration
);
301 printf_verbose("thread %-6s, tid %lu\n",
302 "main", urcu_get_thread_id());
304 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
305 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
306 tot_nr_reads
= calloc(nr_readers
, sizeof(*tot_nr_reads
));
307 tot_nr_writes
= calloc(nr_writers
, sizeof(*tot_nr_writes
));
308 per_thread_lock
= calloc(nr_readers
, sizeof(*per_thread_lock
));
309 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
310 pthread_mutex_init(&per_thread_lock
[i_thr
].lock
, NULL
);
315 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
316 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
317 (void *)(long)i_thr
);
321 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
322 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
323 (void *)(long)i_thr
);
330 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
331 err
= pthread_join(tid_reader
[i_thr
], &tret
);
334 tot_reads
+= tot_nr_reads
[i_thr
];
336 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
337 err
= pthread_join(tid_writer
[i_thr
], &tret
);
340 tot_writes
+= tot_nr_writes
[i_thr
];
343 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
345 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
347 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
348 argv
[0], duration
, nr_readers
, rduration
, wduration
,
349 nr_writers
, wdelay
, tot_reads
, tot_writes
,
350 tot_reads
+ tot_writes
);
356 free(per_thread_lock
);