tests: use SPDX identifiers
[urcu.git] / tests / benchmark / test_perthreadlock.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 struct per_thread_lock {
37 pthread_mutex_t lock;
38 } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); /* cache-line aligned */
39
40 static struct per_thread_lock *per_thread_lock;
41
42 static volatile int test_go, test_stop;
43
44 static unsigned long wdelay;
45
46 static volatile struct test_array test_array = { 8 };
47
48 static unsigned long duration;
49
50 /* read-side C.S. duration, in loops */
51 static unsigned long rduration;
52
53 /* write-side C.S. duration, in loops */
54 static unsigned long wduration;
55
56 static inline void loop_sleep(unsigned long loops)
57 {
58 while (loops-- != 0)
59 caa_cpu_relax();
60 }
61
62 static int verbose_mode;
63
64 #define printf_verbose(fmt, args...) \
65 do { \
66 if (verbose_mode) \
67 printf(fmt, args); \
68 } while (0)
69
70 static unsigned int cpu_affinities[NR_CPUS];
71 static unsigned int next_aff = 0;
72 static int use_affinity = 0;
73
74 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
75
76 static void set_affinity(void)
77 {
78 #ifdef HAVE_SCHED_SETAFFINITY
79 cpu_set_t mask;
80 int cpu, ret;
81 #endif /* HAVE_SCHED_SETAFFINITY */
82
83 if (!use_affinity)
84 return;
85
86 #ifdef HAVE_SCHED_SETAFFINITY
87 ret = pthread_mutex_lock(&affinity_mutex);
88 if (ret) {
89 errno = ret;
90 perror("Error in pthread mutex lock");
91 abort();
92 }
93 cpu = cpu_affinities[next_aff++];
94 ret = pthread_mutex_unlock(&affinity_mutex);
95 if (ret) {
96 errno = ret;
97 perror("Error in pthread mutex unlock");
98 abort();
99 }
100 CPU_ZERO(&mask);
101 CPU_SET(cpu, &mask);
102 sched_setaffinity(0, sizeof(mask), &mask);
103 #endif /* HAVE_SCHED_SETAFFINITY */
104 }
105
106 /*
107 * returns 0 if test should end.
108 */
109 static int test_duration_write(void)
110 {
111 return !test_stop;
112 }
113
114 static int test_duration_read(void)
115 {
116 return !test_stop;
117 }
118
119 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
120 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
121
122 static
123 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
124 static
125 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_reads;
126
127 static unsigned int nr_readers;
128 static unsigned int nr_writers;
129
130 static void urcu_mutex_lock(pthread_mutex_t *lock)
131 {
132 int ret;
133
134 ret = pthread_mutex_lock(lock);
135 if (ret) {
136 errno = ret;
137 perror("Error in pthread mutex lock");
138 abort();
139 }
140 }
141
142 static void urcu_mutex_unlock(pthread_mutex_t *lock)
143 {
144 int ret;
145
146 ret = pthread_mutex_unlock(lock);
147 if (ret) {
148 errno = ret;
149 perror("Error in pthread mutex unlock");
150 abort();
151 }
152 }
153
154 static
155 void *thr_reader(void *data)
156 {
157 unsigned long tidx = (unsigned long)data;
158
159 printf_verbose("thread_begin %s, tid %lu\n",
160 "reader", urcu_get_thread_id());
161
162 set_affinity();
163
164 while (!test_go)
165 {
166 }
167
168 for (;;) {
169 int v;
170
171 urcu_mutex_lock(&per_thread_lock[tidx].lock);
172 v = test_array.a;
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()))
179 break;
180 }
181
182 tot_nr_reads[tidx] = URCU_TLS(nr_reads);
183 printf_verbose("thread_end %s, tid %lu\n",
184 "reader", urcu_get_thread_id());
185 return ((void*)1);
186
187 }
188
189 static
190 void *thr_writer(void *data)
191 {
192 unsigned long wtidx = (unsigned long)data;
193 long tidx;
194
195 printf_verbose("thread_begin %s, tid %lu\n",
196 "writer", urcu_get_thread_id());
197
198 set_affinity();
199
200 while (!test_go)
201 {
202 }
203 cmm_smp_mb();
204
205 for (;;) {
206 for (tidx = 0; tidx < (long)nr_readers; tidx++) {
207 urcu_mutex_lock(&per_thread_lock[tidx].lock);
208 }
209 test_array.a = 0;
210 test_array.a = 8;
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);
215 }
216 URCU_TLS(nr_writes)++;
217 if (caa_unlikely(!test_duration_write()))
218 break;
219 if (caa_unlikely(wdelay))
220 loop_sleep(wdelay);
221 }
222
223 printf_verbose("thread_end %s, tid %lu\n",
224 "writer", urcu_get_thread_id());
225 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
226 return ((void*)2);
227 }
228
229 static
230 void show_usage(char **argv)
231 {
232 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
233 argv[0]);
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");
240 printf("\n");
241 }
242
243 int main(int argc, char **argv)
244 {
245 int err;
246 pthread_t *tid_reader, *tid_writer;
247 void *tret;
248 unsigned long long tot_reads = 0, tot_writes = 0;
249 int i, a;
250 unsigned int i_thr;
251
252 if (argc < 4) {
253 show_usage(argv);
254 return -1;
255 }
256 cmm_smp_mb();
257
258 err = sscanf(argv[1], "%u", &nr_readers);
259 if (err != 1) {
260 show_usage(argv);
261 return -1;
262 }
263
264 err = sscanf(argv[2], "%u", &nr_writers);
265 if (err != 1) {
266 show_usage(argv);
267 return -1;
268 }
269
270 err = sscanf(argv[3], "%lu", &duration);
271 if (err != 1) {
272 show_usage(argv);
273 return -1;
274 }
275
276 for (i = 4; i < argc; i++) {
277 if (argv[i][0] != '-')
278 continue;
279 switch (argv[i][1]) {
280 case 'a':
281 if (argc < i + 2) {
282 show_usage(argv);
283 return -1;
284 }
285 a = atoi(argv[++i]);
286 cpu_affinities[next_aff++] = a;
287 use_affinity = 1;
288 printf_verbose("Adding CPU %d affinity\n", a);
289 break;
290 case 'c':
291 if (argc < i + 2) {
292 show_usage(argv);
293 return -1;
294 }
295 rduration = atol(argv[++i]);
296 break;
297 case 'd':
298 if (argc < i + 2) {
299 show_usage(argv);
300 return -1;
301 }
302 wdelay = atol(argv[++i]);
303 break;
304 case 'e':
305 if (argc < i + 2) {
306 show_usage(argv);
307 return -1;
308 }
309 wduration = atol(argv[++i]);
310 break;
311 case 'v':
312 verbose_mode = 1;
313 break;
314 }
315 }
316
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());
323
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);
331 }
332
333 next_aff = 0;
334
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);
338 if (err != 0)
339 exit(1);
340 }
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);
344 if (err != 0)
345 exit(1);
346 }
347
348 cmm_smp_mb();
349
350 test_go = 1;
351
352 sleep(duration);
353
354 test_stop = 1;
355
356 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
357 err = pthread_join(tid_reader[i_thr], &tret);
358 if (err != 0)
359 exit(1);
360 tot_reads += tot_nr_reads[i_thr];
361 }
362 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
363 err = pthread_join(tid_writer[i_thr], &tret);
364 if (err != 0)
365 exit(1);
366 tot_writes += tot_nr_writes[i_thr];
367 }
368
369 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
370 tot_writes);
371 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
372 "nr_writers %3u "
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);
377
378 free(tid_reader);
379 free(tid_writer);
380 free(tot_nr_reads);
381 free(tot_nr_writes);
382 free(per_thread_lock);
383 return 0;
384 }
This page took 0.037263 seconds and 5 git commands to generate.