Fix: uatomic arm32: add missing release barrier before uatomic_xchg
[urcu.git] / tests / benchmark / test_rwlock.c
1 /*
2 * test_urcu.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include <stdio.h>
24 #include <pthread.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <errno.h>
33
34 #include <urcu/arch.h>
35 #include <urcu/tls-compat.h>
36 #include "cpuset.h"
37 #include "thread-id.h"
38
39 /* hardcoded number of CPUs */
40 #define NR_CPUS 16384
41
42 #ifndef DYNAMIC_LINK_TEST
43 #define _LGPL_SOURCE
44 #endif
45 #include <urcu.h>
46
47 struct test_array {
48 int a;
49 };
50
51 pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
52
53 static volatile int test_go, test_stop;
54
55 static unsigned long wdelay;
56
57 static volatile struct test_array test_array = { 8 };
58
59 static unsigned long duration;
60
61 /* read-side C.S. duration, in loops */
62 static unsigned long rduration;
63
64 /* write-side C.S. duration, in loops */
65 static unsigned long wduration;
66
67 static inline void loop_sleep(unsigned long loops)
68 {
69 while (loops-- != 0)
70 caa_cpu_relax();
71 }
72
73 static int verbose_mode;
74
75 #define printf_verbose(fmt, args...) \
76 do { \
77 if (verbose_mode) \
78 printf(fmt, args); \
79 } while (0)
80
81 static unsigned int cpu_affinities[NR_CPUS];
82 static unsigned int next_aff = 0;
83 static int use_affinity = 0;
84
85 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
86
87 static void set_affinity(void)
88 {
89 #if HAVE_SCHED_SETAFFINITY
90 cpu_set_t mask;
91 int cpu, ret;
92 #endif /* HAVE_SCHED_SETAFFINITY */
93
94 if (!use_affinity)
95 return;
96
97 #if HAVE_SCHED_SETAFFINITY
98 ret = pthread_mutex_lock(&affinity_mutex);
99 if (ret) {
100 perror("Error in pthread mutex lock");
101 exit(-1);
102 }
103 cpu = cpu_affinities[next_aff++];
104 ret = pthread_mutex_unlock(&affinity_mutex);
105 if (ret) {
106 perror("Error in pthread mutex unlock");
107 exit(-1);
108 }
109
110 CPU_ZERO(&mask);
111 CPU_SET(cpu, &mask);
112 #if SCHED_SETAFFINITY_ARGS == 2
113 sched_setaffinity(0, &mask);
114 #else
115 sched_setaffinity(0, sizeof(mask), &mask);
116 #endif
117 #endif /* HAVE_SCHED_SETAFFINITY */
118 }
119
120 /*
121 * returns 0 if test should end.
122 */
123 static int test_duration_write(void)
124 {
125 return !test_stop;
126 }
127
128 static int test_duration_read(void)
129 {
130 return !test_stop;
131 }
132
133 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
134 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
135
136 static unsigned int nr_readers;
137 static unsigned int nr_writers;
138
139 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
140
141 void rcu_copy_mutex_lock(void)
142 {
143 int ret;
144 ret = pthread_mutex_lock(&rcu_copy_mutex);
145 if (ret) {
146 perror("Error in pthread mutex lock");
147 exit(-1);
148 }
149 }
150
151 void rcu_copy_mutex_unlock(void)
152 {
153 int ret;
154
155 ret = pthread_mutex_unlock(&rcu_copy_mutex);
156 if (ret) {
157 perror("Error in pthread mutex unlock");
158 exit(-1);
159 }
160 }
161
162 void *thr_reader(void *_count)
163 {
164 unsigned long long *count = _count;
165
166 printf_verbose("thread_begin %s, tid %lu\n",
167 "reader", urcu_get_thread_id());
168
169 set_affinity();
170
171 while (!test_go)
172 {
173 }
174
175 for (;;) {
176 int a;
177
178 pthread_rwlock_rdlock(&lock);
179 a = test_array.a;
180 assert(a == 8);
181 if (caa_unlikely(rduration))
182 loop_sleep(rduration);
183 pthread_rwlock_unlock(&lock);
184 URCU_TLS(nr_reads)++;
185 if (caa_unlikely(!test_duration_read()))
186 break;
187 }
188
189 *count = URCU_TLS(nr_reads);
190 printf_verbose("thread_end %s, tid %lu\n",
191 "reader", urcu_get_thread_id());
192 return ((void*)1);
193
194 }
195
196 void *thr_writer(void *_count)
197 {
198 unsigned long long *count = _count;
199
200 printf_verbose("thread_begin %s, tid %lu\n",
201 "writer", urcu_get_thread_id());
202
203 set_affinity();
204
205 while (!test_go)
206 {
207 }
208 cmm_smp_mb();
209
210 for (;;) {
211 pthread_rwlock_wrlock(&lock);
212 test_array.a = 0;
213 test_array.a = 8;
214 if (caa_unlikely(wduration))
215 loop_sleep(wduration);
216 pthread_rwlock_unlock(&lock);
217 URCU_TLS(nr_writes)++;
218 if (caa_unlikely(!test_duration_write()))
219 break;
220 if (caa_unlikely(wdelay))
221 loop_sleep(wdelay);
222 }
223
224 printf_verbose("thread_end %s, tid %lu\n",
225 "writer", urcu_get_thread_id());
226 *count = URCU_TLS(nr_writes);
227 return ((void*)2);
228 }
229
230 void show_usage(int argc, 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 *count_reader, *count_writer;
249 unsigned long long tot_reads = 0, tot_writes = 0;
250 int i, a;
251
252 if (argc < 4) {
253 show_usage(argc, 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(argc, argv);
261 return -1;
262 }
263
264 err = sscanf(argv[2], "%u", &nr_writers);
265 if (err != 1) {
266 show_usage(argc, argv);
267 return -1;
268 }
269
270 err = sscanf(argv[3], "%lu", &duration);
271 if (err != 1) {
272 show_usage(argc, 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(argc, 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(argc, argv);
293 return -1;
294 }
295 rduration = atol(argv[++i]);
296 break;
297 case 'd':
298 if (argc < i + 2) {
299 show_usage(argc, argv);
300 return -1;
301 }
302 wdelay = atol(argv[++i]);
303 break;
304 case 'e':
305 if (argc < i + 2) {
306 show_usage(argc, 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 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 = 0; i < nr_readers; i++) {
332 err = pthread_create(&tid_reader[i], NULL, thr_reader,
333 &count_reader[i]);
334 if (err != 0)
335 exit(1);
336 }
337 for (i = 0; i < nr_writers; i++) {
338 err = pthread_create(&tid_writer[i], NULL, thr_writer,
339 &count_writer[i]);
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 = 0; i < nr_readers; i++) {
353 err = pthread_join(tid_reader[i], &tret);
354 if (err != 0)
355 exit(1);
356 tot_reads += count_reader[i];
357 }
358 for (i = 0; i < nr_writers; i++) {
359 err = pthread_join(tid_writer[i], &tret);
360 if (err != 0)
361 exit(1);
362 tot_writes += count_writer[i];
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 free(tid_reader);
375 free(tid_writer);
376 free(count_reader);
377 free(count_writer);
378 return 0;
379 }
This page took 0.036354 seconds and 4 git commands to generate.