benchmark: Use uatomic for accessing global states
[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 unsigned long wdelay;
43
44 static volatile struct test_array test_array = { 8 };
45
46 static unsigned long duration;
47
48 /* read-side C.S. duration, in loops */
49 static unsigned long rduration;
50
51 /* write-side C.S. duration, in loops */
52 static unsigned long wduration;
53
54 static inline void loop_sleep(unsigned long loops)
55 {
56 while (loops-- != 0)
57 caa_cpu_relax();
58 }
59
60 static int verbose_mode;
61
62 #define printf_verbose(fmt, args...) \
63 do { \
64 if (verbose_mode) \
65 printf(fmt, args); \
66 } while (0)
67
68 static unsigned int cpu_affinities[NR_CPUS];
69 static unsigned int next_aff = 0;
70 static int use_affinity = 0;
71
72 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
73
74 static void set_affinity(void)
75 {
76 #ifdef HAVE_SCHED_SETAFFINITY
77 cpu_set_t mask;
78 int cpu, ret;
79 #endif /* HAVE_SCHED_SETAFFINITY */
80
81 if (!use_affinity)
82 return;
83
84 #ifdef HAVE_SCHED_SETAFFINITY
85 ret = pthread_mutex_lock(&affinity_mutex);
86 if (ret) {
87 perror("Error in pthread mutex lock");
88 exit(-1);
89 }
90 cpu = cpu_affinities[next_aff++];
91 ret = pthread_mutex_unlock(&affinity_mutex);
92 if (ret) {
93 perror("Error in pthread mutex unlock");
94 exit(-1);
95 }
96
97 CPU_ZERO(&mask);
98 CPU_SET(cpu, &mask);
99 sched_setaffinity(0, sizeof(mask), &mask);
100 #endif /* HAVE_SCHED_SETAFFINITY */
101 }
102
103 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
104 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
105
106 static unsigned int nr_readers;
107 static unsigned int nr_writers;
108
109 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
110
111 static
112 void *thr_reader(void *_count)
113 {
114 unsigned long long *count = _count;
115
116 printf_verbose("thread_begin %s, tid %lu\n",
117 "reader", urcu_get_thread_id());
118
119 set_affinity();
120
121 wait_until_go();
122
123 for (;;) {
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 }
131
132 a = test_array.a;
133 urcu_posix_assert(a == 8);
134 if (caa_unlikely(rduration))
135 loop_sleep(rduration);
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
143 URCU_TLS(nr_reads)++;
144 if (caa_unlikely(!test_duration_read()))
145 break;
146 }
147
148 *count = URCU_TLS(nr_reads);
149
150 printf_verbose("thread_end %s, tid %lu, count %llu\n",
151 "reader", urcu_get_thread_id(), *count);
152 return ((void*)1);
153
154 }
155
156 static
157 void *thr_writer(void *_count)
158 {
159 unsigned long long *count = _count;
160
161 printf_verbose("thread_begin %s, tid %lu\n",
162 "writer", urcu_get_thread_id());
163
164 set_affinity();
165
166 wait_until_go();
167
168 for (;;) {
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
177 test_array.a = 0;
178 test_array.a = 8;
179 if (caa_unlikely(wduration))
180 loop_sleep(wduration);
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
188 URCU_TLS(nr_writes)++;
189 if (caa_unlikely(!test_duration_write()))
190 break;
191 if (caa_unlikely(wdelay))
192 loop_sleep(wdelay);
193 }
194 *count = URCU_TLS(nr_writes);
195
196 printf_verbose("thread_end %s, tid %lu, count %llu\n",
197 "writer", urcu_get_thread_id(), *count);
198 return ((void*)2);
199 }
200
201 static
202 void show_usage(char **argv)
203 {
204 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
205 argv[0]);
206 printf("OPTIONS:\n");
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");
212 printf("\n");
213 }
214
215 int 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;
222 int i, a;
223 unsigned int i_thr;
224
225 if (argc < 4) {
226 show_usage(argv);
227 return -1;
228 }
229 cmm_smp_mb();
230
231 err = sscanf(argv[1], "%u", &nr_readers);
232 if (err != 1) {
233 show_usage(argv);
234 return -1;
235 }
236
237 err = sscanf(argv[2], "%u", &nr_writers);
238 if (err != 1) {
239 show_usage(argv);
240 return -1;
241 }
242
243 err = sscanf(argv[3], "%lu", &duration);
244 if (err != 1) {
245 show_usage(argv);
246 return -1;
247 }
248
249 for (i = 4; i < argc; i++) {
250 if (argv[i][0] != '-')
251 continue;
252 switch (argv[i][1]) {
253 case 'a':
254 if (argc < i + 2) {
255 show_usage(argv);
256 return -1;
257 }
258 a = atoi(argv[++i]);
259 cpu_affinities[next_aff++] = a;
260 use_affinity = 1;
261 printf_verbose("Adding CPU %d affinity\n", a);
262 break;
263 case 'c':
264 if (argc < i + 2) {
265 show_usage(argv);
266 return -1;
267 }
268 rduration = atol(argv[++i]);
269 break;
270 case 'd':
271 if (argc < i + 2) {
272 show_usage(argv);
273 return -1;
274 }
275 wdelay = atol(argv[++i]);
276 break;
277 case 'e':
278 if (argc < i + 2) {
279 show_usage(argv);
280 return -1;
281 }
282 wduration = atol(argv[++i]);
283 break;
284 case 'v':
285 verbose_mode = 1;
286 break;
287 }
288 }
289
290 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
291 duration, nr_readers, nr_writers);
292 printf_verbose("Writer delay : %lu loops.\n", wdelay);
293 printf_verbose("Writer duration : %lu loops.\n", wduration);
294 printf_verbose("Reader duration : %lu loops.\n", rduration);
295 printf_verbose("thread %-6s, tid %lu\n",
296 "main", urcu_get_thread_id());
297
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
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));
308
309 next_aff = 0;
310
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]);
314 if (err != 0)
315 exit(1);
316 }
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]);
320 if (err != 0)
321 exit(1);
322 }
323
324 test_for(duration);
325
326 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
327 err = pthread_join(tid_reader[i_thr], &tret);
328 if (err != 0)
329 exit(1);
330 tot_reads += count_reader[i_thr];
331 }
332 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
333 err = pthread_join(tid_writer[i_thr], &tret);
334 if (err != 0)
335 exit(1);
336 tot_writes += count_writer[i_thr];
337 }
338
339 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
340 tot_writes);
341 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
342 "nr_writers %3u "
343 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
344 argv[0], duration, nr_readers, rduration, wduration,
345 nr_writers, wdelay, tot_reads, tot_writes,
346 tot_reads + tot_writes);
347
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 }
353 free(tid_reader);
354 free(tid_writer);
355 free(count_reader);
356 free(count_writer);
357 return 0;
358 }
This page took 0.036777 seconds and 5 git commands to generate.