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