Fix: pthread_rwlock initialization on Cygwin
[urcu.git] / tests / benchmark / test_urcu_gc.c
CommitLineData
a813abf8
MD
1/*
2 * test_urcu_gc.c
3 *
ffa11a18 4 * Userspace RCU library - test program (with batch reclamation)
a813abf8 5 *
6982d6d7 6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
a813abf8
MD
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
a813abf8
MD
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>
94b343fd 32#include <errno.h>
a813abf8 33
ec4e58a3 34#include <urcu/arch.h>
bd252a04 35#include <urcu/tls-compat.h>
2953b501 36#include "cpuset.h"
94df6318 37#include "thread-id.h"
2650042a 38#include "../common/debug-yield.h"
65fcc7e9 39
2a7ac59d
MD
40/* hardcoded number of CPUs */
41#define NR_CPUS 16384
42
a813abf8
MD
43#ifndef DYNAMIC_LINK_TEST
44#define _LGPL_SOURCE
a813abf8 45#endif
ec4e58a3 46#include <urcu.h>
a813abf8
MD
47
48struct test_array {
49 int a;
50};
51
52static volatile int test_go, test_stop;
53
54static unsigned long wdelay;
55
56static struct test_array *test_rcu_pointer;
57
4a3e0004 58static unsigned int reclaim_batch = 1;
a813abf8
MD
59
60struct reclaim_queue {
61 void **queue; /* Beginning of queue */
62 void **head; /* Insert position */
63};
64
65static struct reclaim_queue *pending_reclaims;
66
67static unsigned long duration;
68
69/* read-side C.S. duration, in loops */
70static unsigned long rduration;
71
31b598e0
MD
72/* write-side C.S. duration, in loops */
73static unsigned long wduration;
74
ab0aacbe 75static inline void loop_sleep(unsigned long loops)
a813abf8 76{
ab0aacbe 77 while (loops-- != 0)
06f22bdb 78 caa_cpu_relax();
a813abf8
MD
79}
80
81static int verbose_mode;
82
83#define printf_verbose(fmt, args...) \
84 do { \
85 if (verbose_mode) \
86 printf(fmt, args); \
87 } while (0)
88
2a7ac59d
MD
89static unsigned int cpu_affinities[NR_CPUS];
90static unsigned int next_aff = 0;
91static int use_affinity = 0;
92
6af882ba
MD
93pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
94
2a7ac59d
MD
95static void set_affinity(void)
96{
95bc7fb9 97#if HAVE_SCHED_SETAFFINITY
2a7ac59d 98 cpu_set_t mask;
95bc7fb9
MD
99 int cpu, ret;
100#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
101
102 if (!use_affinity)
103 return;
104
d8540fc5 105#if HAVE_SCHED_SETAFFINITY
6af882ba
MD
106 ret = pthread_mutex_lock(&affinity_mutex);
107 if (ret) {
108 perror("Error in pthread mutex lock");
109 exit(-1);
110 }
2a7ac59d 111 cpu = cpu_affinities[next_aff++];
6af882ba
MD
112 ret = pthread_mutex_unlock(&affinity_mutex);
113 if (ret) {
114 perror("Error in pthread mutex unlock");
115 exit(-1);
116 }
d8540fc5 117
2a7ac59d
MD
118 CPU_ZERO(&mask);
119 CPU_SET(cpu, &mask);
d8540fc5
PA
120#if SCHED_SETAFFINITY_ARGS == 2
121 sched_setaffinity(0, &mask);
122#else
2a7ac59d 123 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5
PA
124#endif
125#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
126}
127
a813abf8
MD
128/*
129 * returns 0 if test should end.
130 */
131static int test_duration_write(void)
132{
133 return !test_stop;
134}
135
136static int test_duration_read(void)
137{
138 return !test_stop;
139}
140
bd252a04
MD
141static DEFINE_URCU_TLS(unsigned long long, nr_writes);
142static DEFINE_URCU_TLS(unsigned long long, nr_reads);
a813abf8
MD
143
144static
06f22bdb 145unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
a813abf8
MD
146
147static unsigned int nr_readers;
148static unsigned int nr_writers;
149
150pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
151
152void rcu_copy_mutex_lock(void)
153{
154 int ret;
155 ret = pthread_mutex_lock(&rcu_copy_mutex);
156 if (ret) {
157 perror("Error in pthread mutex lock");
158 exit(-1);
159 }
160}
161
162void rcu_copy_mutex_unlock(void)
163{
164 int ret;
165
166 ret = pthread_mutex_unlock(&rcu_copy_mutex);
167 if (ret) {
168 perror("Error in pthread mutex unlock");
169 exit(-1);
170 }
171}
172
173void *thr_reader(void *_count)
174{
175 unsigned long long *count = _count;
176 struct test_array *local_ptr;
177
94df6318
MD
178 printf_verbose("thread_begin %s, tid %lu\n",
179 "reader", urcu_get_thread_id());
a813abf8 180
2a7ac59d
MD
181 set_affinity();
182
a813abf8
MD
183 rcu_register_thread();
184
185 while (!test_go)
186 {
187 }
5481ddb3 188 cmm_smp_mb();
a813abf8
MD
189
190 for (;;) {
191 rcu_read_lock();
192 local_ptr = rcu_dereference(test_rcu_pointer);
1de4df4b 193 rcu_debug_yield_read();
a813abf8
MD
194 if (local_ptr)
195 assert(local_ptr->a == 8);
a0b7f7ea 196 if (caa_unlikely(rduration))
a813abf8
MD
197 loop_sleep(rduration);
198 rcu_read_unlock();
bd252a04 199 URCU_TLS(nr_reads)++;
a0b7f7ea 200 if (caa_unlikely(!test_duration_read()))
a813abf8
MD
201 break;
202 }
203
204 rcu_unregister_thread();
205
bd252a04 206 *count = URCU_TLS(nr_reads);
94df6318
MD
207 printf_verbose("thread_end %s, tid %lu\n",
208 "reader", urcu_get_thread_id());
a813abf8
MD
209 return ((void*)1);
210
211}
212
53091fe5 213static void rcu_gc_clear_queue(unsigned long wtidx)
a813abf8
MD
214{
215 void **p;
216
53091fe5 217 /* Wait for Q.S and empty queue */
a813abf8
MD
218 synchronize_rcu();
219
220 for (p = pending_reclaims[wtidx].queue;
221 p < pending_reclaims[wtidx].head; p++) {
222 /* poison */
223 if (*p)
224 ((struct test_array *)*p)->a = 0;
225 free(*p);
226 }
227 pending_reclaims[wtidx].head = pending_reclaims[wtidx].queue;
228}
229
53091fe5
MD
230/* Using per-thread queue */
231static void rcu_gc_reclaim(unsigned long wtidx, void *old)
a813abf8 232{
53091fe5
MD
233 /* Queue pointer */
234 *pending_reclaims[wtidx].head = old;
235 pending_reclaims[wtidx].head++;
a813abf8 236
a0b7f7ea 237 if (caa_likely(pending_reclaims[wtidx].head - pending_reclaims[wtidx].queue
53091fe5
MD
238 < reclaim_batch))
239 return;
a813abf8 240
53091fe5 241 rcu_gc_clear_queue(wtidx);
a813abf8
MD
242}
243
244void *thr_writer(void *data)
245{
246 unsigned long wtidx = (unsigned long)data;
321e29d9
MD
247#ifdef TEST_LOCAL_GC
248 struct test_array *old = NULL;
249#else
a813abf8 250 struct test_array *new, *old;
321e29d9 251#endif
a813abf8 252
94df6318
MD
253 printf_verbose("thread_begin %s, tid %lu\n",
254 "writer", urcu_get_thread_id());
a813abf8 255
2a7ac59d
MD
256 set_affinity();
257
a813abf8
MD
258 while (!test_go)
259 {
260 }
5481ddb3 261 cmm_smp_mb();
a813abf8
MD
262
263 for (;;) {
321e29d9 264#ifndef TEST_LOCAL_GC
a813abf8 265 new = malloc(sizeof(*new));
a813abf8
MD
266 new->a = 8;
267 old = rcu_xchg_pointer(&test_rcu_pointer, new);
321e29d9 268#endif
a0b7f7ea 269 if (caa_unlikely(wduration))
31b598e0 270 loop_sleep(wduration);
a813abf8 271 rcu_gc_reclaim(wtidx, old);
bd252a04 272 URCU_TLS(nr_writes)++;
a0b7f7ea 273 if (caa_unlikely(!test_duration_write()))
a813abf8 274 break;
a0b7f7ea 275 if (caa_unlikely(wdelay))
a813abf8
MD
276 loop_sleep(wdelay);
277 }
278
94df6318
MD
279 printf_verbose("thread_end %s, tid %lu\n",
280 "writer", urcu_get_thread_id());
bd252a04 281 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
a813abf8
MD
282 return ((void*)2);
283}
284
285void show_usage(int argc, char **argv)
286{
06637138
MD
287 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
288 argv[0]);
289 printf("OPTIONS:\n");
06637138 290 printf(" [-r] [-w] (yield reader and/or writer)\n");
06637138
MD
291 printf(" [-d delay] (writer period (us))\n");
292 printf(" [-c duration] (reader C.S. duration (in loops))\n");
293 printf(" [-e duration] (writer C.S. duration (in loops))\n");
294 printf(" [-v] (verbose output)\n");
295 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
a813abf8
MD
296 printf("\n");
297}
298
a813abf8
MD
299int main(int argc, char **argv)
300{
301 int err;
302 pthread_t *tid_reader, *tid_writer;
303 void *tret;
304 unsigned long long *count_reader;
305 unsigned long long tot_reads = 0, tot_writes = 0;
306 int i, a;
a813abf8
MD
307
308 if (argc < 4) {
309 show_usage(argc, argv);
310 return -1;
311 }
312
313 err = sscanf(argv[1], "%u", &nr_readers);
314 if (err != 1) {
315 show_usage(argc, argv);
316 return -1;
317 }
318
319 err = sscanf(argv[2], "%u", &nr_writers);
320 if (err != 1) {
321 show_usage(argc, argv);
322 return -1;
323 }
324
325 err = sscanf(argv[3], "%lu", &duration);
326 if (err != 1) {
327 show_usage(argc, argv);
328 return -1;
329 }
330
a813abf8
MD
331 for (i = 4; i < argc; i++) {
332 if (argv[i][0] != '-')
333 continue;
334 switch (argv[i][1]) {
a813abf8 335 case 'r':
2650042a 336 rcu_debug_yield_enable(RCU_YIELD_READ);
a813abf8
MD
337 break;
338 case 'w':
2650042a 339 rcu_debug_yield_enable(RCU_YIELD_WRITE);
a813abf8 340 break;
a813abf8
MD
341 case 'a':
342 if (argc < i + 2) {
343 show_usage(argc, argv);
344 return -1;
345 }
346 a = atoi(argv[++i]);
2a7ac59d 347 cpu_affinities[next_aff++] = a;
a813abf8
MD
348 use_affinity = 1;
349 printf_verbose("Adding CPU %d affinity\n", a);
350 break;
351 case 'b':
352 if (argc < i + 2) {
353 show_usage(argc, argv);
354 return -1;
355 }
356 reclaim_batch = atol(argv[++i]);
357 break;
358 case 'c':
359 if (argc < i + 2) {
360 show_usage(argc, argv);
361 return -1;
362 }
363 rduration = atol(argv[++i]);
364 break;
365 case 'd':
366 if (argc < i + 2) {
367 show_usage(argc, argv);
368 return -1;
369 }
370 wdelay = atol(argv[++i]);
371 break;
31b598e0
MD
372 case 'e':
373 if (argc < i + 2) {
374 show_usage(argc, argv);
375 return -1;
376 }
377 wduration = atol(argv[++i]);
378 break;
a813abf8
MD
379 case 'v':
380 verbose_mode = 1;
381 break;
382 }
383 }
384
385 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
386 duration, nr_readers, nr_writers);
387 printf_verbose("Writer delay : %lu loops.\n", wdelay);
388 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
389 printf_verbose("thread %-6s, tid %lu\n",
390 "main", urcu_get_thread_id());
a813abf8 391
9aa14175
MD
392 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
393 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
394 count_reader = calloc(nr_readers, sizeof(*count_reader));
395 tot_nr_writes = calloc(nr_writers, sizeof(*tot_nr_writes));
396 pending_reclaims = calloc(nr_writers, sizeof(*pending_reclaims));
a813abf8 397 if (reclaim_batch * sizeof(*pending_reclaims[i].queue)
06f22bdb 398 < CAA_CACHE_LINE_SIZE)
a813abf8 399 for (i = 0; i < nr_writers; i++)
06f22bdb 400 pending_reclaims[i].queue = calloc(1, CAA_CACHE_LINE_SIZE);
a813abf8
MD
401 else
402 for (i = 0; i < nr_writers; i++)
403 pending_reclaims[i].queue = calloc(reclaim_batch,
404 sizeof(*pending_reclaims[i].queue));
405 for (i = 0; i < nr_writers; i++)
406 pending_reclaims[i].head = pending_reclaims[i].queue;
407
2a7ac59d
MD
408 next_aff = 0;
409
a813abf8
MD
410 for (i = 0; i < nr_readers; i++) {
411 err = pthread_create(&tid_reader[i], NULL, thr_reader,
412 &count_reader[i]);
413 if (err != 0)
414 exit(1);
415 }
416 for (i = 0; i < nr_writers; i++) {
417 err = pthread_create(&tid_writer[i], NULL, thr_writer,
418 (void *)(long)i);
419 if (err != 0)
420 exit(1);
421 }
422
5481ddb3 423 cmm_smp_mb();
a813abf8
MD
424
425 test_go = 1;
426
427 sleep(duration);
428
429 test_stop = 1;
430
431 for (i = 0; i < nr_readers; i++) {
432 err = pthread_join(tid_reader[i], &tret);
433 if (err != 0)
434 exit(1);
435 tot_reads += count_reader[i];
436 }
437 for (i = 0; i < nr_writers; i++) {
438 err = pthread_join(tid_writer[i], &tret);
439 if (err != 0)
440 exit(1);
441 tot_writes += tot_nr_writes[i];
53091fe5 442 rcu_gc_clear_queue(i);
a813abf8
MD
443 }
444
445 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
446 tot_writes);
a50a7b43 447 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
a813abf8 448 "nr_writers %3u "
4a3e0004
MD
449 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
450 "batch %u\n",
a50a7b43 451 argv[0], duration, nr_readers, rduration, wduration,
a813abf8 452 nr_writers, wdelay, tot_reads, tot_writes,
4a3e0004 453 tot_reads + tot_writes, reclaim_batch);
a813abf8
MD
454 free(tid_reader);
455 free(tid_writer);
456 free(count_reader);
457 free(tot_nr_writes);
458 for (i = 0; i < nr_writers; i++)
459 free(pending_reclaims[i].queue);
460 free(pending_reclaims);
461
462 return 0;
463}
This page took 0.057908 seconds and 4 git commands to generate.