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