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