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