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