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 unsigned long wdelay
;
40 static volatile struct test_array test_array
= { 8 };
42 static unsigned long duration
;
44 /* read-side C.S. duration, in loops */
45 static unsigned long rduration
;
47 /* write-side C.S. duration, in loops */
48 static unsigned long wduration
;
50 static inline void loop_sleep(unsigned long loops
)
56 static int verbose_mode
;
58 #define printf_verbose(fmt, args...) \
64 static unsigned int cpu_affinities
[NR_CPUS
];
65 static unsigned int next_aff
= 0;
66 static int use_affinity
= 0;
68 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
70 static void set_affinity(void)
72 #ifdef HAVE_SCHED_SETAFFINITY
75 #endif /* HAVE_SCHED_SETAFFINITY */
80 #ifdef HAVE_SCHED_SETAFFINITY
81 ret
= pthread_mutex_lock(&affinity_mutex
);
83 perror("Error in pthread mutex lock");
86 cpu
= cpu_affinities
[next_aff
++];
87 ret
= pthread_mutex_unlock(&affinity_mutex
);
89 perror("Error in pthread mutex unlock");
94 sched_setaffinity(0, sizeof(mask
), &mask
);
95 #endif /* HAVE_SCHED_SETAFFINITY */
98 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
99 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
102 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_writes
;
104 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_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 *data
)
114 unsigned long tidx
= (unsigned long)data
;
116 printf_verbose("thread_begin %s, tid %lu\n",
117 "reader", urcu_get_thread_id());
126 pthread_mutex_lock(&lock
);
128 urcu_posix_assert(v
== 8);
129 if (caa_unlikely(rduration
))
130 loop_sleep(rduration
);
131 pthread_mutex_unlock(&lock
);
132 URCU_TLS(nr_reads
)++;
133 if (caa_unlikely(!test_duration_read()))
137 tot_nr_reads
[tidx
] = URCU_TLS(nr_reads
);
138 printf_verbose("thread_end %s, tid %lu\n",
139 "reader", urcu_get_thread_id());
145 void *thr_writer(void *data
)
147 unsigned long wtidx
= (unsigned long)data
;
149 printf_verbose("thread_begin %s, tid %lu\n",
150 "writer", urcu_get_thread_id());
157 pthread_mutex_lock(&lock
);
160 if (caa_unlikely(wduration
))
161 loop_sleep(wduration
);
162 pthread_mutex_unlock(&lock
);
163 URCU_TLS(nr_writes
)++;
164 if (caa_unlikely(!test_duration_write()))
166 if (caa_unlikely(wdelay
))
170 printf_verbose("thread_end %s, tid %lu\n",
171 "writer", urcu_get_thread_id());
172 tot_nr_writes
[wtidx
] = URCU_TLS(nr_writes
);
177 void show_usage(char **argv
)
179 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
181 printf("OPTIONS:\n");
182 printf(" [-d delay] (writer period (us))\n");
183 printf(" [-c duration] (reader C.S. duration (in loops))\n");
184 printf(" [-e duration] (writer C.S. duration (in loops))\n");
185 printf(" [-v] (verbose output)\n");
186 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
190 int main(int argc
, char **argv
)
193 pthread_t
*tid_reader
, *tid_writer
;
195 unsigned long long *count_reader
, *count_writer
;
196 unsigned long long tot_reads
= 0, tot_writes
= 0;
206 err
= sscanf(argv
[1], "%u", &nr_readers
);
212 err
= sscanf(argv
[2], "%u", &nr_writers
);
218 err
= sscanf(argv
[3], "%lu", &duration
);
224 for (i
= 4; i
< argc
; i
++) {
225 if (argv
[i
][0] != '-')
227 switch (argv
[i
][1]) {
234 cpu_affinities
[next_aff
++] = a
;
236 printf_verbose("Adding CPU %d affinity\n", a
);
243 rduration
= atol(argv
[++i
]);
250 wdelay
= atol(argv
[++i
]);
257 wduration
= atol(argv
[++i
]);
265 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
266 duration
, nr_readers
, nr_writers
);
267 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
268 printf_verbose("Reader duration : %lu loops.\n", rduration
);
269 printf_verbose("thread %-6s, tid %lu\n",
270 "main", urcu_get_thread_id());
272 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
273 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
274 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
275 count_writer
= calloc(nr_writers
, sizeof(*count_writer
));
276 tot_nr_reads
= calloc(nr_readers
, sizeof(*tot_nr_reads
));
277 tot_nr_writes
= calloc(nr_writers
, sizeof(*tot_nr_writes
));
281 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
282 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
283 (void *)(long)i_thr
);
287 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
288 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
289 (void *)(long)i_thr
);
296 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
297 err
= pthread_join(tid_reader
[i_thr
], &tret
);
300 tot_reads
+= tot_nr_reads
[i_thr
];
302 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
303 err
= pthread_join(tid_writer
[i_thr
], &tret
);
306 tot_writes
+= tot_nr_writes
[i_thr
];
309 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
311 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
313 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
314 argv
[0], duration
, nr_readers
, rduration
, wduration
,
315 nr_writers
, wdelay
, tot_reads
, tot_writes
,
316 tot_reads
+ tot_writes
);