Remove glibc < 2.4 compat code for sched_setaffinity
[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 "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 #if 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 #if 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 void rcu_copy_mutex_lock(void)
141 {
142 int ret;
143 ret = pthread_mutex_lock(&rcu_copy_mutex);
144 if (ret) {
145 perror("Error in pthread mutex lock");
146 exit(-1);
147 }
148 }
149
150 void rcu_copy_mutex_unlock(void)
151 {
152 int ret;
153
154 ret = pthread_mutex_unlock(&rcu_copy_mutex);
155 if (ret) {
156 perror("Error in pthread mutex unlock");
157 exit(-1);
158 }
159 }
160
161 void *thr_reader(void *_count)
162 {
163 unsigned long long *count = _count;
164
165 printf_verbose("thread_begin %s, tid %lu\n",
166 "reader", urcu_get_thread_id());
167
168 set_affinity();
169
170 while (!test_go)
171 {
172 }
173
174 for (;;) {
175 int a, ret;
176
177 ret = pthread_rwlock_rdlock(&lock);
178 if (ret) {
179 fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret));
180 abort();
181 }
182
183 a = test_array.a;
184 assert(a == 8);
185 if (caa_unlikely(rduration))
186 loop_sleep(rduration);
187
188 ret = pthread_rwlock_unlock(&lock);
189 if (ret) {
190 fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret));
191 abort();
192 }
193
194 URCU_TLS(nr_reads)++;
195 if (caa_unlikely(!test_duration_read()))
196 break;
197 }
198
199 *count = URCU_TLS(nr_reads);
200
201 printf_verbose("thread_end %s, tid %lu, count %llu\n",
202 "reader", urcu_get_thread_id(), *count);
203 return ((void*)1);
204
205 }
206
207 void *thr_writer(void *_count)
208 {
209 unsigned long long *count = _count;
210
211 printf_verbose("thread_begin %s, tid %lu\n",
212 "writer", urcu_get_thread_id());
213
214 set_affinity();
215
216 while (!test_go)
217 {
218 }
219 cmm_smp_mb();
220
221 for (;;) {
222 int ret;
223
224 ret = pthread_rwlock_wrlock(&lock);
225 if (ret) {
226 fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret));
227 abort();
228 }
229
230 test_array.a = 0;
231 test_array.a = 8;
232 if (caa_unlikely(wduration))
233 loop_sleep(wduration);
234
235 ret = pthread_rwlock_unlock(&lock);
236 if (ret) {
237 fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret));
238 abort();
239 }
240
241 URCU_TLS(nr_writes)++;
242 if (caa_unlikely(!test_duration_write()))
243 break;
244 if (caa_unlikely(wdelay))
245 loop_sleep(wdelay);
246 }
247 *count = URCU_TLS(nr_writes);
248
249 printf_verbose("thread_end %s, tid %lu, count %llu\n",
250 "writer", urcu_get_thread_id(), *count);
251 return ((void*)2);
252 }
253
254 void show_usage(int argc, char **argv)
255 {
256 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
257 argv[0]);
258 printf("OPTIONS:\n");
259 printf(" [-d delay] (writer period (us))\n");
260 printf(" [-c duration] (reader C.S. duration (in loops))\n");
261 printf(" [-e duration] (writer C.S. duration (in loops))\n");
262 printf(" [-v] (verbose output)\n");
263 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
264 printf("\n");
265 }
266
267 int main(int argc, char **argv)
268 {
269 int err;
270 pthread_t *tid_reader, *tid_writer;
271 void *tret;
272 unsigned long long *count_reader, *count_writer;
273 unsigned long long tot_reads = 0, tot_writes = 0;
274 int i, a;
275 unsigned int i_thr;
276
277 if (argc < 4) {
278 show_usage(argc, argv);
279 return -1;
280 }
281 cmm_smp_mb();
282
283 err = sscanf(argv[1], "%u", &nr_readers);
284 if (err != 1) {
285 show_usage(argc, argv);
286 return -1;
287 }
288
289 err = sscanf(argv[2], "%u", &nr_writers);
290 if (err != 1) {
291 show_usage(argc, argv);
292 return -1;
293 }
294
295 err = sscanf(argv[3], "%lu", &duration);
296 if (err != 1) {
297 show_usage(argc, argv);
298 return -1;
299 }
300
301 for (i = 4; i < argc; i++) {
302 if (argv[i][0] != '-')
303 continue;
304 switch (argv[i][1]) {
305 case 'a':
306 if (argc < i + 2) {
307 show_usage(argc, argv);
308 return -1;
309 }
310 a = atoi(argv[++i]);
311 cpu_affinities[next_aff++] = a;
312 use_affinity = 1;
313 printf_verbose("Adding CPU %d affinity\n", a);
314 break;
315 case 'c':
316 if (argc < i + 2) {
317 show_usage(argc, argv);
318 return -1;
319 }
320 rduration = atol(argv[++i]);
321 break;
322 case 'd':
323 if (argc < i + 2) {
324 show_usage(argc, argv);
325 return -1;
326 }
327 wdelay = atol(argv[++i]);
328 break;
329 case 'e':
330 if (argc < i + 2) {
331 show_usage(argc, argv);
332 return -1;
333 }
334 wduration = atol(argv[++i]);
335 break;
336 case 'v':
337 verbose_mode = 1;
338 break;
339 }
340 }
341
342 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
343 duration, nr_readers, nr_writers);
344 printf_verbose("Writer delay : %lu loops.\n", wdelay);
345 printf_verbose("Writer duration : %lu loops.\n", wduration);
346 printf_verbose("Reader duration : %lu loops.\n", rduration);
347 printf_verbose("thread %-6s, tid %lu\n",
348 "main", urcu_get_thread_id());
349
350 err = pthread_rwlock_init(&lock, NULL);
351 if (err != 0) {
352 fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err));
353 exit(1);
354 }
355
356 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
357 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
358 count_reader = calloc(nr_readers, sizeof(*count_reader));
359 count_writer = calloc(nr_writers, sizeof(*count_writer));
360
361 next_aff = 0;
362
363 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
364 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
365 &count_reader[i_thr]);
366 if (err != 0)
367 exit(1);
368 }
369 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
370 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
371 &count_writer[i_thr]);
372 if (err != 0)
373 exit(1);
374 }
375
376 cmm_smp_mb();
377
378 test_go = 1;
379
380 sleep(duration);
381
382 test_stop = 1;
383
384 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
385 err = pthread_join(tid_reader[i_thr], &tret);
386 if (err != 0)
387 exit(1);
388 tot_reads += count_reader[i_thr];
389 }
390 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
391 err = pthread_join(tid_writer[i_thr], &tret);
392 if (err != 0)
393 exit(1);
394 tot_writes += count_writer[i_thr];
395 }
396
397 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
398 tot_writes);
399 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
400 "nr_writers %3u "
401 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
402 argv[0], duration, nr_readers, rduration, wduration,
403 nr_writers, wdelay, tot_reads, tot_writes,
404 tot_reads + tot_writes);
405
406 err = pthread_rwlock_destroy(&lock);
407 if (err != 0) {
408 fprintf(stderr, "pthread_rwlock_destroy: (%d) %s\n", err, strerror(err));
409 exit(1);
410 }
411 free(tid_reader);
412 free(tid_writer);
413 free(count_reader);
414 free(count_writer);
415 return 0;
416 }
This page took 0.059217 seconds and 4 git commands to generate.