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