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