4 * Userspace RCU library - test program (with baatch reclamation)
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
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.
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.
28 #include <sys/types.h>
33 #include <sys/syscall.h>
38 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
39 #define CACHE_LINE_SIZE 4096
41 #if defined(_syscall0)
42 _syscall0(pid_t
, gettid
)
43 #elif defined(__NR_gettid)
44 static inline pid_t
gettid(void)
46 return syscall(__NR_gettid
);
49 #warning "use pid as tid"
50 static inline pid_t
gettid(void)
57 #include "urcu-qsbr.h"
63 static volatile int test_go
, test_stop
;
65 static unsigned long wdelay
;
67 static struct test_array
*test_rcu_pointer
;
69 static unsigned long duration
;
71 /* read-side C.S. duration, in loops */
72 static unsigned long rduration
;
73 static int reclaim_batch
= 1;
75 struct reclaim_queue
{
76 void **queue
; /* Beginning of queue */
77 void **head
; /* Insert position */
80 static struct reclaim_queue
*pending_reclaims
;
83 static inline void loop_sleep(unsigned long l
)
89 static int verbose_mode
;
91 #define printf_verbose(fmt, args...) \
98 * returns 0 if test should end.
100 static int test_duration_write(void)
105 static int test_duration_read(void)
110 static unsigned long long __thread nr_writes
;
111 static unsigned long long __thread nr_reads
;
113 static unsigned int nr_readers
;
114 static unsigned int nr_writers
;
116 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
118 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE
))) *tot_nr_writes
;
121 void rcu_copy_mutex_lock(void)
124 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
126 perror("Error in pthread mutex lock");
131 void rcu_copy_mutex_unlock(void)
135 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
137 perror("Error in pthread mutex unlock");
142 void *thr_reader(void *_count
)
144 unsigned long long *count
= _count
;
145 struct test_array
*local_ptr
;
147 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
148 "reader", pthread_self(), (unsigned long)gettid());
150 rcu_register_thread();
159 local_ptr
= _rcu_dereference(test_rcu_pointer
);
162 assert(local_ptr
->a
== 8);
163 if (unlikely(rduration
))
164 loop_sleep(rduration
);
167 /* QS each 1024 reads */
168 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
169 _rcu_quiescent_state();
170 if (unlikely(!test_duration_read()))
174 rcu_unregister_thread();
177 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
178 "reader", pthread_self(), (unsigned long)gettid());
183 /* Using per-thread queue */
184 static void rcu_gc_reclaim(unsigned long wtidx
, void *old
)
189 *pending_reclaims
[wtidx
].head
= old
;
190 pending_reclaims
[wtidx
].head
++;
192 if (likely(pending_reclaims
[wtidx
].head
- pending_reclaims
[wtidx
].queue
196 /* If queue is full, wait for Q.S and empty queue */
199 for (p
= pending_reclaims
[wtidx
].queue
;
200 p
< pending_reclaims
[wtidx
].head
; p
++) {
203 ((struct test_array
*)*p
)->a
= 0;
206 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
209 static void rcu_gc_cleanup(unsigned long wtidx
)
213 /* Wait for Q.S and empty queue */
216 for (p
= pending_reclaims
[wtidx
].queue
;
217 p
< pending_reclaims
[wtidx
].head
; p
++) {
220 ((struct test_array
*)*p
)->a
= 0;
223 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
226 void *thr_writer(void *data
)
228 unsigned long wtidx
= (unsigned long)data
;
229 struct test_array
*new, *old
;
231 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
232 "writer", pthread_self(), (unsigned long)gettid());
240 new = malloc(sizeof(*new));
241 rcu_copy_mutex_lock();
242 old
= test_rcu_pointer
;
246 old
= _rcu_xchg_pointer(&test_rcu_pointer
, new);
247 rcu_copy_mutex_unlock();
248 rcu_gc_reclaim(wtidx
, old
);
250 if (unlikely(!test_duration_write()))
252 if (unlikely(wdelay
))
256 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
257 "writer", pthread_self(), (unsigned long)gettid());
258 tot_nr_writes
[wtidx
] = nr_writes
;
262 void show_usage(int argc
, char **argv
)
264 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
266 printf(" [-r] [-w] (yield reader and/or writer)");
268 printf(" [-d delay] (writer period (us))");
269 printf(" [-c duration] (reader C.S. duration (in loops))");
270 printf(" [-v] (verbose output)");
271 printf(" [-a cpu#] [-a cpu#]... (affinity)");
277 int main(int argc
, char **argv
)
280 pthread_t
*tid_reader
, *tid_writer
;
282 unsigned long long *count_reader
;
283 unsigned long long tot_reads
= 0, tot_writes
= 0;
285 int use_affinity
= 0;
288 show_usage(argc
, argv
);
292 err
= sscanf(argv
[1], "%u", &nr_readers
);
294 show_usage(argc
, argv
);
298 err
= sscanf(argv
[2], "%u", &nr_writers
);
300 show_usage(argc
, argv
);
304 err
= sscanf(argv
[3], "%lu", &duration
);
306 show_usage(argc
, argv
);
312 for (i
= 4; i
< argc
; i
++) {
313 if (argv
[i
][0] != '-')
315 switch (argv
[i
][1]) {
318 yield_active
|= YIELD_READ
;
321 yield_active
|= YIELD_WRITE
;
326 show_usage(argc
, argv
);
330 CPU_SET(a
, &affinity
);
332 printf_verbose("Adding CPU %d affinity\n", a
);
336 show_usage(argc
, argv
);
339 reclaim_batch
= atol(argv
[++i
]);
343 show_usage(argc
, argv
);
346 rduration
= atol(argv
[++i
]);
350 show_usage(argc
, argv
);
353 wdelay
= atol(argv
[++i
]);
361 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
362 duration
, nr_readers
, nr_writers
);
363 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
364 printf_verbose("Reader duration : %lu loops.\n", rduration
);
365 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
366 "main", pthread_self(), (unsigned long)gettid());
369 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
370 perror("sched_setaffinity");
374 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
375 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
376 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
377 tot_nr_writes
= malloc(sizeof(*tot_nr_writes
) * nr_writers
);
378 pending_reclaims
= malloc(sizeof(*pending_reclaims
) * nr_writers
);
379 if (reclaim_batch
* sizeof(*pending_reclaims
[i
].queue
)
381 for (i
= 0; i
< nr_writers
; i
++)
382 pending_reclaims
[i
].queue
= calloc(1, CACHE_LINE_SIZE
);
384 for (i
= 0; i
< nr_writers
; i
++)
385 pending_reclaims
[i
].queue
= calloc(reclaim_batch
,
386 sizeof(*pending_reclaims
[i
].queue
));
387 for (i
= 0; i
< nr_writers
; i
++)
388 pending_reclaims
[i
].head
= pending_reclaims
[i
].queue
;
390 for (i
= 0; i
< nr_readers
; i
++) {
391 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
396 for (i
= 0; i
< nr_writers
; i
++) {
397 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
411 for (i
= 0; i
< nr_readers
; i
++) {
412 err
= pthread_join(tid_reader
[i
], &tret
);
415 tot_reads
+= count_reader
[i
];
417 for (i
= 0; i
< nr_writers
; i
++) {
418 err
= pthread_join(tid_writer
[i
], &tret
);
421 tot_writes
+= tot_nr_writes
[i
];
425 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
427 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
429 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
430 argv
[0], duration
, nr_readers
, rduration
,
431 nr_writers
, wdelay
, tot_reads
, tot_writes
,
432 tot_reads
+ tot_writes
);
437 for (i
= 0; i
< nr_writers
; i
++)
438 free(pending_reclaims
[i
].queue
);
439 free(pending_reclaims
);