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)
56 #ifndef DYNAMIC_LINK_TEST
59 #define debug_yield_read()
67 static volatile int test_go
, test_stop
;
69 static unsigned long wdelay
;
71 static struct test_array
*test_rcu_pointer
;
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
;
82 static unsigned long duration
;
84 /* read-side C.S. duration, in loops */
85 static unsigned long rduration
;
87 static inline void loop_sleep(unsigned long l
)
93 static int verbose_mode
;
95 #define printf_verbose(fmt, args...) \
102 * returns 0 if test should end.
104 static int test_duration_write(void)
109 static int test_duration_read(void)
114 static unsigned long long __thread nr_writes
;
115 static unsigned long long __thread nr_reads
;
118 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE
))) *tot_nr_writes
;
120 static unsigned int nr_readers
;
121 static unsigned int nr_writers
;
123 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
125 void rcu_copy_mutex_lock(void)
128 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
130 perror("Error in pthread mutex lock");
135 void rcu_copy_mutex_unlock(void)
139 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
141 perror("Error in pthread mutex unlock");
146 void *thr_reader(void *_count
)
148 unsigned long long *count
= _count
;
149 struct test_array
*local_ptr
;
151 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
152 "reader", pthread_self(), (unsigned long)gettid());
154 rcu_register_thread();
163 local_ptr
= rcu_dereference(test_rcu_pointer
);
166 assert(local_ptr
->a
== 8);
167 if (unlikely(rduration
))
168 loop_sleep(rduration
);
171 if (unlikely(!test_duration_read()))
175 rcu_unregister_thread();
178 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
179 "reader", pthread_self(), (unsigned long)gettid());
184 /* Using per-thread queue */
185 static void rcu_gc_reclaim(unsigned long wtidx
, void *old
)
190 *pending_reclaims
[wtidx
].head
= old
;
191 pending_reclaims
[wtidx
].head
++;
193 if (likely(pending_reclaims
[wtidx
].head
- pending_reclaims
[wtidx
].queue
197 /* If queue is full, wait for Q.S and empty queue */
200 for (p
= pending_reclaims
[wtidx
].queue
;
201 p
< pending_reclaims
[wtidx
].head
; p
++) {
204 ((struct test_array
*)*p
)->a
= 0;
207 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
210 static void rcu_gc_cleanup(unsigned long wtidx
)
214 /* Wait for Q.S and empty queue */
217 for (p
= pending_reclaims
[wtidx
].queue
;
218 p
< pending_reclaims
[wtidx
].head
; p
++) {
221 ((struct test_array
*)*p
)->a
= 0;
224 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
227 void *thr_writer(void *data
)
229 unsigned long wtidx
= (unsigned long)data
;
230 struct test_array
*new, *old
;
232 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
233 "writer", pthread_self(), (unsigned long)gettid());
241 new = malloc(sizeof(*new));
242 rcu_copy_mutex_lock();
243 old
= test_rcu_pointer
;
247 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
248 rcu_copy_mutex_unlock();
249 rcu_gc_reclaim(wtidx
, old
);
251 if (unlikely(!test_duration_write()))
253 if (unlikely(wdelay
))
257 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
258 "writer", pthread_self(), (unsigned long)gettid());
259 tot_nr_writes
[wtidx
] = nr_writes
;
263 void show_usage(int argc
, char **argv
)
265 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
267 printf(" [-r] [-w] (yield reader and/or writer)");
269 printf(" [-d delay] (writer period (us))");
270 printf(" [-c duration] (reader C.S. duration (in loops))");
271 printf(" [-v] (verbose output)");
272 printf(" [-a cpu#] [-a cpu#]... (affinity)");
278 int main(int argc
, char **argv
)
281 pthread_t
*tid_reader
, *tid_writer
;
283 unsigned long long *count_reader
;
284 unsigned long long tot_reads
= 0, tot_writes
= 0;
286 int use_affinity
= 0;
289 show_usage(argc
, argv
);
293 err
= sscanf(argv
[1], "%u", &nr_readers
);
295 show_usage(argc
, argv
);
299 err
= sscanf(argv
[2], "%u", &nr_writers
);
301 show_usage(argc
, argv
);
305 err
= sscanf(argv
[3], "%lu", &duration
);
307 show_usage(argc
, argv
);
313 for (i
= 4; i
< argc
; i
++) {
314 if (argv
[i
][0] != '-')
316 switch (argv
[i
][1]) {
319 yield_active
|= YIELD_READ
;
322 yield_active
|= YIELD_WRITE
;
327 show_usage(argc
, argv
);
331 CPU_SET(a
, &affinity
);
333 printf_verbose("Adding CPU %d affinity\n", a
);
337 show_usage(argc
, argv
);
340 reclaim_batch
= atol(argv
[++i
]);
344 show_usage(argc
, argv
);
347 rduration
= atol(argv
[++i
]);
351 show_usage(argc
, argv
);
354 wdelay
= atol(argv
[++i
]);
362 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
363 duration
, nr_readers
, nr_writers
);
364 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
365 printf_verbose("Reader duration : %lu loops.\n", rduration
);
366 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
367 "main", pthread_self(), (unsigned long)gettid());
370 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
371 perror("sched_setaffinity");
375 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
376 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
377 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
378 tot_nr_writes
= malloc(sizeof(*tot_nr_writes
) * nr_writers
);
379 pending_reclaims
= malloc(sizeof(*pending_reclaims
) * nr_writers
);
380 if (reclaim_batch
* sizeof(*pending_reclaims
[i
].queue
)
382 for (i
= 0; i
< nr_writers
; i
++)
383 pending_reclaims
[i
].queue
= calloc(1, CACHE_LINE_SIZE
);
385 for (i
= 0; i
< nr_writers
; i
++)
386 pending_reclaims
[i
].queue
= calloc(reclaim_batch
,
387 sizeof(*pending_reclaims
[i
].queue
));
388 for (i
= 0; i
< nr_writers
; i
++)
389 pending_reclaims
[i
].head
= pending_reclaims
[i
].queue
;
391 for (i
= 0; i
< nr_readers
; i
++) {
392 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
397 for (i
= 0; i
< nr_writers
; i
++) {
398 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
412 for (i
= 0; i
< nr_readers
; i
++) {
413 err
= pthread_join(tid_reader
[i
], &tret
);
416 tot_reads
+= count_reader
[i
];
418 for (i
= 0; i
< nr_writers
; i
++) {
419 err
= pthread_join(tid_writer
[i
], &tret
);
422 tot_writes
+= tot_nr_writes
[i
];
426 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
428 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
430 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
431 argv
[0], duration
, nr_readers
, rduration
,
432 nr_writers
, wdelay
, tot_reads
, tot_writes
,
433 tot_reads
+ tot_writes
);
438 for (i
= 0; i
< nr_writers
; i
++)
439 free(pending_reclaims
[i
].queue
);
440 free(pending_reclaims
);