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