tests: use SPDX identifiers
[urcu.git] / tests / benchmark / test_rwlock.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 /*
37 * static rwlock initializer is broken on Cygwin. Use runtime
38 * initialization.
39 */
40 pthread_rwlock_t 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 perror("Error in pthread mutex lock");
90 exit(-1);
91 }
92 cpu = cpu_affinities[next_aff++];
93 ret = pthread_mutex_unlock(&affinity_mutex);
94 if (ret) {
95 perror("Error in pthread mutex unlock");
96 exit(-1);
97 }
98
99 CPU_ZERO(&mask);
100 CPU_SET(cpu, &mask);
101 sched_setaffinity(0, sizeof(mask), &mask);
102 #endif /* HAVE_SCHED_SETAFFINITY */
103 }
104
105 /*
106 * returns 0 if test should end.
107 */
108 static int test_duration_write(void)
109 {
110 return !test_stop;
111 }
112
113 static int test_duration_read(void)
114 {
115 return !test_stop;
116 }
117
118 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
119 static DEFINE_URCU_TLS(unsigned long long, 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 *_count)
128 {
129 unsigned long long *count = _count;
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 a, ret;
142
143 ret = pthread_rwlock_rdlock(&lock);
144 if (ret) {
145 fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));
146 abort();
147 }
148
149 a = test_array.a;
150 urcu_posix_assert(a == 8);
151 if (caa_unlikely(rduration))
152 loop_sleep(rduration);
153
154 ret = pthread_rwlock_unlock(&lock);
155 if (ret) {
156 fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
157 abort();
158 }
159
160 URCU_TLS(nr_reads)++;
161 if (caa_unlikely(!test_duration_read()))
162 break;
163 }
164
165 *count = URCU_TLS(nr_reads);
166
167 printf_verbose("thread_end %s, tid %lu, count %llu\n",
168 "reader", urcu_get_thread_id(), *count);
169 return ((void*)1);
170
171 }
172
173 static
174 void *thr_writer(void *_count)
175 {
176 unsigned long long *count = _count;
177
178 printf_verbose("thread_begin %s, tid %lu\n",
179 "writer", urcu_get_thread_id());
180
181 set_affinity();
182
183 while (!test_go)
184 {
185 }
186 cmm_smp_mb();
187
188 for (;;) {
189 int ret;
190
191 ret = pthread_rwlock_wrlock(&lock);
192 if (ret) {
193 fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
194 abort();
195 }
196
197 test_array.a = 0;
198 test_array.a = 8;
199 if (caa_unlikely(wduration))
200 loop_sleep(wduration);
201
202 ret = pthread_rwlock_unlock(&lock);
203 if (ret) {
204 fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
205 abort();
206 }
207
208 URCU_TLS(nr_writes)++;
209 if (caa_unlikely(!test_duration_write()))
210 break;
211 if (caa_unlikely(wdelay))
212 loop_sleep(wdelay);
213 }
214 *count = URCU_TLS(nr_writes);
215
216 printf_verbose("thread_end %s, tid %lu, count %llu\n",
217 "writer", urcu_get_thread_id(), *count);
218 return ((void*)2);
219 }
220
221 static
222 void show_usage(char **argv)
223 {
224 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
225 argv[0]);
226 printf("OPTIONS:\n");
227 printf(" [-d delay] (writer period (us))\n");
228 printf(" [-c duration] (reader C.S. duration (in loops))\n");
229 printf(" [-e duration] (writer C.S. duration (in loops))\n");
230 printf(" [-v] (verbose output)\n");
231 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
232 printf("\n");
233 }
234
235 int main(int argc, char **argv)
236 {
237 int err;
238 pthread_t *tid_reader, *tid_writer;
239 void *tret;
240 unsigned long long *count_reader, *count_writer;
241 unsigned long long tot_reads = 0, tot_writes = 0;
242 int i, a;
243 unsigned int i_thr;
244
245 if (argc < 4) {
246 show_usage(argv);
247 return -1;
248 }
249 cmm_smp_mb();
250
251 err = sscanf(argv[1], "%u", &nr_readers);
252 if (err != 1) {
253 show_usage(argv);
254 return -1;
255 }
256
257 err = sscanf(argv[2], "%u", &nr_writers);
258 if (err != 1) {
259 show_usage(argv);
260 return -1;
261 }
262
263 err = sscanf(argv[3], "%lu", &duration);
264 if (err != 1) {
265 show_usage(argv);
266 return -1;
267 }
268
269 for (i = 4; i < argc; i++) {
270 if (argv[i][0] != '-')
271 continue;
272 switch (argv[i][1]) {
273 case 'a':
274 if (argc < i + 2) {
275 show_usage(argv);
276 return -1;
277 }
278 a = atoi(argv[++i]);
279 cpu_affinities[next_aff++] = a;
280 use_affinity = 1;
281 printf_verbose("Adding CPU %d affinity\n", a);
282 break;
283 case 'c':
284 if (argc < i + 2) {
285 show_usage(argv);
286 return -1;
287 }
288 rduration = atol(argv[++i]);
289 break;
290 case 'd':
291 if (argc < i + 2) {
292 show_usage(argv);
293 return -1;
294 }
295 wdelay = atol(argv[++i]);
296 break;
297 case 'e':
298 if (argc < i + 2) {
299 show_usage(argv);
300 return -1;
301 }
302 wduration = atol(argv[++i]);
303 break;
304 case 'v':
305 verbose_mode = 1;
306 break;
307 }
308 }
309
310 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
311 duration, nr_readers, nr_writers);
312 printf_verbose("Writer delay : %lu loops.\n", wdelay);
313 printf_verbose("Writer duration : %lu loops.\n", wduration);
314 printf_verbose("Reader duration : %lu loops.\n", rduration);
315 printf_verbose("thread %-6s, tid %lu\n",
316 "main", urcu_get_thread_id());
317
318 err = pthread_rwlock_init(&lock, NULL);
319 if (err != 0) {
320 fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
321 exit(1);
322 }
323
324 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
325 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
326 count_reader = calloc(nr_readers, sizeof(*count_reader));
327 count_writer = calloc(nr_writers, sizeof(*count_writer));
328
329 next_aff = 0;
330
331 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
332 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
333 &count_reader[i_thr]);
334 if (err != 0)
335 exit(1);
336 }
337 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
338 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
339 &count_writer[i_thr]);
340 if (err != 0)
341 exit(1);
342 }
343
344 cmm_smp_mb();
345
346 test_go = 1;
347
348 sleep(duration);
349
350 test_stop = 1;
351
352 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
353 err = pthread_join(tid_reader[i_thr], &tret);
354 if (err != 0)
355 exit(1);
356 tot_reads += count_reader[i_thr];
357 }
358 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
359 err = pthread_join(tid_writer[i_thr], &tret);
360 if (err != 0)
361 exit(1);
362 tot_writes += count_writer[i_thr];
363 }
364
365 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
366 tot_writes);
367 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
368 "nr_writers %3u "
369 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
370 argv[0], duration, nr_readers, rduration, wduration,
371 nr_writers, wdelay, tot_reads, tot_writes,
372 tot_reads + tot_writes);
373
374 err = pthread_rwlock_destroy(&lock);
375 if (err != 0) {
376 fprintf(stderr, "pthread_rwlock_destroy: (%d) %s\n", err, strerror(err));
377 exit(1);
378 }
379 free(tid_reader);
380 free(tid_writer);
381 free(count_reader);
382 free(count_writer);
383 return 0;
384 }
This page took 0.038434 seconds and 5 git commands to generate.