Deal with kernel affinity bug for tests
[urcu.git] / test_qsbr_gc.c
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
41 /* hardcoded number of CPUs */
42 #define NR_CPUS 16384
43
44 #if defined(_syscall0)
45 _syscall0(pid_t, gettid)
46 #elif defined(__NR_gettid)
47 static inline pid_t gettid(void)
48 {
49 return syscall(__NR_gettid);
50 }
51 #else
52 #warning "use pid as tid"
53 static inline pid_t gettid(void)
54 {
55 return getpid();
56 }
57 #endif
58
59 #define _LGPL_SOURCE
60 #include "urcu-qsbr.h"
61
62 struct test_array {
63 int a;
64 };
65
66 static volatile int test_go, test_stop;
67
68 static unsigned long wdelay;
69
70 static struct test_array *test_rcu_pointer;
71
72 static unsigned long duration;
73
74 /* read-side C.S. duration, in loops */
75 static unsigned long rduration;
76 static unsigned int reclaim_batch = 1;
77
78 struct reclaim_queue {
79 void **queue; /* Beginning of queue */
80 void **head; /* Insert position */
81 };
82
83 static struct reclaim_queue *pending_reclaims;
84
85
86 static inline void loop_sleep(unsigned long l)
87 {
88 while(l-- != 0)
89 cpu_relax();
90 }
91
92 static int verbose_mode;
93
94 #define printf_verbose(fmt, args...) \
95 do { \
96 if (verbose_mode) \
97 printf(fmt, args); \
98 } while (0)
99
100 static unsigned int cpu_affinities[NR_CPUS];
101 static unsigned int next_aff = 0;
102 static int use_affinity = 0;
103
104 static 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
120 /*
121 * returns 0 if test should end.
122 */
123 static int test_duration_write(void)
124 {
125 return !test_stop;
126 }
127
128 static int test_duration_read(void)
129 {
130 return !test_stop;
131 }
132
133 static unsigned long long __thread nr_writes;
134 static unsigned long long __thread nr_reads;
135
136 static unsigned int nr_readers;
137 static unsigned int nr_writers;
138
139 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
140 static
141 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_writes;
142
143
144 void 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
154 void 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
165 void *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
173 set_affinity();
174
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
208 static void rcu_gc_clear_queue(unsigned long wtidx)
209 {
210 void **p;
211
212 /* Wait for Q.S and empty queue */
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
225 /* Using per-thread queue */
226 static void rcu_gc_reclaim(unsigned long wtidx, void *old)
227 {
228 /* Queue pointer */
229 *pending_reclaims[wtidx].head = old;
230 pending_reclaims[wtidx].head++;
231
232 if (likely(pending_reclaims[wtidx].head - pending_reclaims[wtidx].queue
233 < reclaim_batch))
234 return;
235
236 rcu_gc_clear_queue(wtidx);
237 }
238
239 void *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
247 set_affinity();
248
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
277 void 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
290 int 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;
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
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]);
340 cpu_affinities[next_aff++] = a;
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
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
394 next_aff = 0;
395
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];
428 rcu_gc_clear_queue(i);
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 "
435 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
436 "batch %u\n",
437 argv[0], duration, nr_readers, rduration,
438 nr_writers, wdelay, tot_reads, tot_writes,
439 tot_reads + tot_writes, reclaim_batch);
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.037192 seconds and 4 git commands to generate.