6908ea4ca5b4a7f315d76a5212640adface5073a
[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 <errno.h>
32
33 #include <urcu/arch.h>
34 #include <urcu/assert.h>
35 #include <urcu/tls-compat.h>
36 #include "thread-id.h"
37
38 /* hardcoded number of CPUs */
39 #define NR_CPUS 16384
40
41 #ifndef DYNAMIC_LINK_TEST
42 #define _LGPL_SOURCE
43 #endif
44 #include <urcu.h>
45
46 struct test_array {
47 int a;
48 };
49
50 /*
51 * static rwlock initializer is broken on Cygwin. Use runtime
52 * initialization.
53 */
54 pthread_rwlock_t lock;
55
56 static volatile int test_go, test_stop;
57
58 static unsigned long wdelay;
59
60 static volatile struct test_array test_array = { 8 };
61
62 static unsigned long duration;
63
64 /* read-side C.S. duration, in loops */
65 static unsigned long rduration;
66
67 /* write-side C.S. duration, in loops */
68 static unsigned long wduration;
69
70 static inline void loop_sleep(unsigned long loops)
71 {
72 while (loops-- != 0)
73 caa_cpu_relax();
74 }
75
76 static int verbose_mode;
77
78 #define printf_verbose(fmt, args...) \
79 do { \
80 if (verbose_mode) \
81 printf(fmt, args); \
82 } while (0)
83
84 static unsigned int cpu_affinities[NR_CPUS];
85 static unsigned int next_aff = 0;
86 static int use_affinity = 0;
87
88 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
89
90 static void set_affinity(void)
91 {
92 #ifdef HAVE_SCHED_SETAFFINITY
93 cpu_set_t mask;
94 int cpu, ret;
95 #endif /* HAVE_SCHED_SETAFFINITY */
96
97 if (!use_affinity)
98 return;
99
100 #ifdef HAVE_SCHED_SETAFFINITY
101 ret = pthread_mutex_lock(&affinity_mutex);
102 if (ret) {
103 perror("Error in pthread mutex lock");
104 exit(-1);
105 }
106 cpu = cpu_affinities[next_aff++];
107 ret = pthread_mutex_unlock(&affinity_mutex);
108 if (ret) {
109 perror("Error in pthread mutex unlock");
110 exit(-1);
111 }
112
113 CPU_ZERO(&mask);
114 CPU_SET(cpu, &mask);
115 sched_setaffinity(0, sizeof(mask), &mask);
116 #endif /* HAVE_SCHED_SETAFFINITY */
117 }
118
119 /*
120 * returns 0 if test should end.
121 */
122 static int test_duration_write(void)
123 {
124 return !test_stop;
125 }
126
127 static int test_duration_read(void)
128 {
129 return !test_stop;
130 }
131
132 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
133 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
134
135 static unsigned int nr_readers;
136 static unsigned int nr_writers;
137
138 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
139
140 static
141 void *thr_reader(void *_count)
142 {
143 unsigned long long *count = _count;
144
145 printf_verbose("thread_begin %s, tid %lu\n",
146 "reader", urcu_get_thread_id());
147
148 set_affinity();
149
150 while (!test_go)
151 {
152 }
153
154 for (;;) {
155 int a, ret;
156
157 ret = pthread_rwlock_rdlock(&lock);
158 if (ret) {
159 fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));
160 abort();
161 }
162
163 a = test_array.a;
164 urcu_posix_assert(a == 8);
165 if (caa_unlikely(rduration))
166 loop_sleep(rduration);
167
168 ret = pthread_rwlock_unlock(&lock);
169 if (ret) {
170 fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
171 abort();
172 }
173
174 URCU_TLS(nr_reads)++;
175 if (caa_unlikely(!test_duration_read()))
176 break;
177 }
178
179 *count = URCU_TLS(nr_reads);
180
181 printf_verbose("thread_end %s, tid %lu, count %llu\n",
182 "reader", urcu_get_thread_id(), *count);
183 return ((void*)1);
184
185 }
186
187 static
188 void *thr_writer(void *_count)
189 {
190 unsigned long long *count = _count;
191
192 printf_verbose("thread_begin %s, tid %lu\n",
193 "writer", urcu_get_thread_id());
194
195 set_affinity();
196
197 while (!test_go)
198 {
199 }
200 cmm_smp_mb();
201
202 for (;;) {
203 int ret;
204
205 ret = pthread_rwlock_wrlock(&lock);
206 if (ret) {
207 fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
208 abort();
209 }
210
211 test_array.a = 0;
212 test_array.a = 8;
213 if (caa_unlikely(wduration))
214 loop_sleep(wduration);
215
216 ret = pthread_rwlock_unlock(&lock);
217 if (ret) {
218 fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
219 abort();
220 }
221
222 URCU_TLS(nr_writes)++;
223 if (caa_unlikely(!test_duration_write()))
224 break;
225 if (caa_unlikely(wdelay))
226 loop_sleep(wdelay);
227 }
228 *count = URCU_TLS(nr_writes);
229
230 printf_verbose("thread_end %s, tid %lu, count %llu\n",
231 "writer", urcu_get_thread_id(), *count);
232 return ((void*)2);
233 }
234
235 static
236 void show_usage(char **argv)
237 {
238 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
239 argv[0]);
240 printf("OPTIONS:\n");
241 printf(" [-d delay] (writer period (us))\n");
242 printf(" [-c duration] (reader C.S. duration (in loops))\n");
243 printf(" [-e duration] (writer C.S. duration (in loops))\n");
244 printf(" [-v] (verbose output)\n");
245 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
246 printf("\n");
247 }
248
249 int main(int argc, char **argv)
250 {
251 int err;
252 pthread_t *tid_reader, *tid_writer;
253 void *tret;
254 unsigned long long *count_reader, *count_writer;
255 unsigned long long tot_reads = 0, tot_writes = 0;
256 int i, a;
257 unsigned int i_thr;
258
259 if (argc < 4) {
260 show_usage(argv);
261 return -1;
262 }
263 cmm_smp_mb();
264
265 err = sscanf(argv[1], "%u", &nr_readers);
266 if (err != 1) {
267 show_usage(argv);
268 return -1;
269 }
270
271 err = sscanf(argv[2], "%u", &nr_writers);
272 if (err != 1) {
273 show_usage(argv);
274 return -1;
275 }
276
277 err = sscanf(argv[3], "%lu", &duration);
278 if (err != 1) {
279 show_usage(argv);
280 return -1;
281 }
282
283 for (i = 4; i < argc; i++) {
284 if (argv[i][0] != '-')
285 continue;
286 switch (argv[i][1]) {
287 case 'a':
288 if (argc < i + 2) {
289 show_usage(argv);
290 return -1;
291 }
292 a = atoi(argv[++i]);
293 cpu_affinities[next_aff++] = a;
294 use_affinity = 1;
295 printf_verbose("Adding CPU %d affinity\n", a);
296 break;
297 case 'c':
298 if (argc < i + 2) {
299 show_usage(argv);
300 return -1;
301 }
302 rduration = atol(argv[++i]);
303 break;
304 case 'd':
305 if (argc < i + 2) {
306 show_usage(argv);
307 return -1;
308 }
309 wdelay = atol(argv[++i]);
310 break;
311 case 'e':
312 if (argc < i + 2) {
313 show_usage(argv);
314 return -1;
315 }
316 wduration = atol(argv[++i]);
317 break;
318 case 'v':
319 verbose_mode = 1;
320 break;
321 }
322 }
323
324 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
325 duration, nr_readers, nr_writers);
326 printf_verbose("Writer delay : %lu loops.\n", wdelay);
327 printf_verbose("Writer duration : %lu loops.\n", wduration);
328 printf_verbose("Reader duration : %lu loops.\n", rduration);
329 printf_verbose("thread %-6s, tid %lu\n",
330 "main", urcu_get_thread_id());
331
332 err = pthread_rwlock_init(&lock, NULL);
333 if (err != 0) {
334 fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
335 exit(1);
336 }
337
338 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
339 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
340 count_reader = calloc(nr_readers, sizeof(*count_reader));
341 count_writer = calloc(nr_writers, sizeof(*count_writer));
342
343 next_aff = 0;
344
345 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
346 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
347 &count_reader[i_thr]);
348 if (err != 0)
349 exit(1);
350 }
351 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
352 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
353 &count_writer[i_thr]);
354 if (err != 0)
355 exit(1);
356 }
357
358 cmm_smp_mb();
359
360 test_go = 1;
361
362 sleep(duration);
363
364 test_stop = 1;
365
366 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
367 err = pthread_join(tid_reader[i_thr], &tret);
368 if (err != 0)
369 exit(1);
370 tot_reads += count_reader[i_thr];
371 }
372 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
373 err = pthread_join(tid_writer[i_thr], &tret);
374 if (err != 0)
375 exit(1);
376 tot_writes += count_writer[i_thr];
377 }
378
379 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
380 tot_writes);
381 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
382 "nr_writers %3u "
383 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
384 argv[0], duration, nr_readers, rduration, wduration,
385 nr_writers, wdelay, tot_reads, tot_writes,
386 tot_reads + tot_writes);
387
388 err = pthread_rwlock_destroy(&lock);
389 if (err != 0) {
390 fprintf(stderr, "pthread_rwlock_destroy: (%d) %s\n", err, strerror(err));
391 exit(1);
392 }
393 free(tid_reader);
394 free(tid_writer);
395 free(count_reader);
396 free(count_writer);
397 return 0;
398 }
This page took 0.04892 seconds and 4 git commands to generate.