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