tests: use SPDX identifiers
[urcu.git] / tests / benchmark / test_mutex.c
1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 //
3 // SPDX-License-Identifier: GPL-2.0-or-later
4
5 /*
6 * Userspace RCU library - test program
7 */
8
9 #include <stdio.h>
10 #include <pthread.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <errno.h>
18
19 #include <urcu/arch.h>
20 #include <urcu/assert.h>
21 #include <urcu/tls-compat.h>
22 #include "thread-id.h"
23
24 /* hardcoded number of CPUs */
25 #define NR_CPUS 16384
26
27 #ifndef DYNAMIC_LINK_TEST
28 #define _LGPL_SOURCE
29 #endif
30 #include <urcu.h>
31
32 struct test_array {
33 int a;
34 };
35
36 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
37
38 static volatile int test_go, test_stop;
39
40 static unsigned long wdelay;
41
42 static volatile struct test_array test_array = { 8 };
43
44 static unsigned long duration;
45
46 /* read-side C.S. duration, in loops */
47 static unsigned long rduration;
48
49 /* write-side C.S. duration, in loops */
50 static unsigned long wduration;
51
52 static inline void loop_sleep(unsigned long loops)
53 {
54 while (loops-- != 0)
55 caa_cpu_relax();
56 }
57
58 static int verbose_mode;
59
60 #define printf_verbose(fmt, args...) \
61 do { \
62 if (verbose_mode) \
63 printf(fmt, args); \
64 } while (0)
65
66 static unsigned int cpu_affinities[NR_CPUS];
67 static unsigned int next_aff = 0;
68 static int use_affinity = 0;
69
70 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
71
72 static void set_affinity(void)
73 {
74 #ifdef HAVE_SCHED_SETAFFINITY
75 cpu_set_t mask;
76 int cpu, ret;
77 #endif /* HAVE_SCHED_SETAFFINITY */
78
79 if (!use_affinity)
80 return;
81
82 #ifdef HAVE_SCHED_SETAFFINITY
83 ret = pthread_mutex_lock(&affinity_mutex);
84 if (ret) {
85 perror("Error in pthread mutex lock");
86 exit(-1);
87 }
88 cpu = cpu_affinities[next_aff++];
89 ret = pthread_mutex_unlock(&affinity_mutex);
90 if (ret) {
91 perror("Error in pthread mutex unlock");
92 exit(-1);
93 }
94 CPU_ZERO(&mask);
95 CPU_SET(cpu, &mask);
96 sched_setaffinity(0, sizeof(mask), &mask);
97 #endif /* HAVE_SCHED_SETAFFINITY */
98 }
99
100 /*
101 * returns 0 if test should end.
102 */
103 static int test_duration_write(void)
104 {
105 return !test_stop;
106 }
107
108 static int test_duration_read(void)
109 {
110 return !test_stop;
111 }
112
113 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
114 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
115
116 static
117 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
118 static
119 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_reads;
120
121 static unsigned int nr_readers;
122 static unsigned int nr_writers;
123
124 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
125
126 static
127 void *thr_reader(void *data)
128 {
129 unsigned long tidx = (unsigned long)data;
130
131 printf_verbose("thread_begin %s, tid %lu\n",
132 "reader", urcu_get_thread_id());
133
134 set_affinity();
135
136 while (!test_go)
137 {
138 }
139
140 for (;;) {
141 int v;
142
143 pthread_mutex_lock(&lock);
144 v = test_array.a;
145 urcu_posix_assert(v == 8);
146 if (caa_unlikely(rduration))
147 loop_sleep(rduration);
148 pthread_mutex_unlock(&lock);
149 URCU_TLS(nr_reads)++;
150 if (caa_unlikely(!test_duration_read()))
151 break;
152 }
153
154 tot_nr_reads[tidx] = URCU_TLS(nr_reads);
155 printf_verbose("thread_end %s, tid %lu\n",
156 "reader", urcu_get_thread_id());
157 return ((void*)1);
158
159 }
160
161 static
162 void *thr_writer(void *data)
163 {
164 unsigned long wtidx = (unsigned long)data;
165
166 printf_verbose("thread_begin %s, tid %lu\n",
167 "writer", urcu_get_thread_id());
168
169 set_affinity();
170
171 while (!test_go)
172 {
173 }
174 cmm_smp_mb();
175
176 for (;;) {
177 pthread_mutex_lock(&lock);
178 test_array.a = 0;
179 test_array.a = 8;
180 if (caa_unlikely(wduration))
181 loop_sleep(wduration);
182 pthread_mutex_unlock(&lock);
183 URCU_TLS(nr_writes)++;
184 if (caa_unlikely(!test_duration_write()))
185 break;
186 if (caa_unlikely(wdelay))
187 loop_sleep(wdelay);
188 }
189
190 printf_verbose("thread_end %s, tid %lu\n",
191 "writer", urcu_get_thread_id());
192 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
193 return ((void*)2);
194 }
195
196 static
197 void show_usage(char **argv)
198 {
199 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
200 argv[0]);
201 printf("OPTIONS:\n");
202 printf(" [-d delay] (writer period (us))\n");
203 printf(" [-c duration] (reader C.S. duration (in loops))\n");
204 printf(" [-e duration] (writer C.S. duration (in loops))\n");
205 printf(" [-v] (verbose output)\n");
206 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
207 printf("\n");
208 }
209
210 int main(int argc, char **argv)
211 {
212 int err;
213 pthread_t *tid_reader, *tid_writer;
214 void *tret;
215 unsigned long long *count_reader, *count_writer;
216 unsigned long long tot_reads = 0, tot_writes = 0;
217 int i, a;
218 unsigned int i_thr;
219
220 if (argc < 4) {
221 show_usage(argv);
222 return -1;
223 }
224 cmm_smp_mb();
225
226 err = sscanf(argv[1], "%u", &nr_readers);
227 if (err != 1) {
228 show_usage(argv);
229 return -1;
230 }
231
232 err = sscanf(argv[2], "%u", &nr_writers);
233 if (err != 1) {
234 show_usage(argv);
235 return -1;
236 }
237
238 err = sscanf(argv[3], "%lu", &duration);
239 if (err != 1) {
240 show_usage(argv);
241 return -1;
242 }
243
244 for (i = 4; i < argc; i++) {
245 if (argv[i][0] != '-')
246 continue;
247 switch (argv[i][1]) {
248 case 'a':
249 if (argc < i + 2) {
250 show_usage(argv);
251 return -1;
252 }
253 a = atoi(argv[++i]);
254 cpu_affinities[next_aff++] = a;
255 use_affinity = 1;
256 printf_verbose("Adding CPU %d affinity\n", a);
257 break;
258 case 'c':
259 if (argc < i + 2) {
260 show_usage(argv);
261 return -1;
262 }
263 rduration = atol(argv[++i]);
264 break;
265 case 'd':
266 if (argc < i + 2) {
267 show_usage(argv);
268 return -1;
269 }
270 wdelay = atol(argv[++i]);
271 break;
272 case 'e':
273 if (argc < i + 2) {
274 show_usage(argv);
275 return -1;
276 }
277 wduration = atol(argv[++i]);
278 break;
279 case 'v':
280 verbose_mode = 1;
281 break;
282 }
283 }
284
285 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
286 duration, nr_readers, nr_writers);
287 printf_verbose("Writer delay : %lu loops.\n", wdelay);
288 printf_verbose("Reader duration : %lu loops.\n", rduration);
289 printf_verbose("thread %-6s, tid %lu\n",
290 "main", urcu_get_thread_id());
291
292 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
293 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
294 count_reader = calloc(nr_readers, sizeof(*count_reader));
295 count_writer = calloc(nr_writers, sizeof(*count_writer));
296 tot_nr_reads = calloc(nr_readers, sizeof(*tot_nr_reads));
297 tot_nr_writes = calloc(nr_writers, sizeof(*tot_nr_writes));
298
299 next_aff = 0;
300
301 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
302 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
303 (void *)(long)i_thr);
304 if (err != 0)
305 exit(1);
306 }
307 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
308 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
309 (void *)(long)i_thr);
310 if (err != 0)
311 exit(1);
312 }
313
314 cmm_smp_mb();
315
316 test_go = 1;
317
318 sleep(duration);
319
320 test_stop = 1;
321
322 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
323 err = pthread_join(tid_reader[i_thr], &tret);
324 if (err != 0)
325 exit(1);
326 tot_reads += tot_nr_reads[i_thr];
327 }
328 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
329 err = pthread_join(tid_writer[i_thr], &tret);
330 if (err != 0)
331 exit(1);
332 tot_writes += tot_nr_writes[i_thr];
333 }
334
335 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
336 tot_writes);
337 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
338 "nr_writers %3u "
339 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
340 argv[0], duration, nr_readers, rduration, wduration,
341 nr_writers, wdelay, tot_reads, tot_writes,
342 tot_reads + tot_writes);
343
344 free(tid_reader);
345 free(tid_writer);
346 free(count_reader);
347 free(count_writer);
348 free(tot_nr_reads);
349 free(tot_nr_writes);
350 return 0;
351 }
This page took 0.037074 seconds and 5 git commands to generate.