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 unsigned 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 static void rcu_gc_clear_queue(unsigned long wtidx
)
188 /* Wait for Q.S and empty queue */
191 for (p
= pending_reclaims
[wtidx
].queue
;
192 p
< pending_reclaims
[wtidx
].head
; p
++) {
195 ((struct test_array
*)*p
)->a
= 0;
198 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
201 /* Using per-thread queue */
202 static void rcu_gc_reclaim(unsigned long wtidx
, void *old
)
205 *pending_reclaims
[wtidx
].head
= old
;
206 pending_reclaims
[wtidx
].head
++;
208 if (likely(pending_reclaims
[wtidx
].head
- pending_reclaims
[wtidx
].queue
212 rcu_gc_clear_queue(wtidx
);
215 void *thr_writer(void *data
)
217 unsigned long wtidx
= (unsigned long)data
;
218 struct test_array
*new, *old
;
220 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
221 "writer", pthread_self(), (unsigned long)gettid());
229 new = malloc(sizeof(*new));
230 rcu_copy_mutex_lock();
231 old
= test_rcu_pointer
;
235 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
236 rcu_copy_mutex_unlock();
237 rcu_gc_reclaim(wtidx
, old
);
239 if (unlikely(!test_duration_write()))
241 if (unlikely(wdelay
))
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
;
251 void show_usage(int argc
, char **argv
)
253 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
255 printf(" [-r] [-w] (yield reader and/or writer)");
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)");
266 int main(int argc
, char **argv
)
269 pthread_t
*tid_reader
, *tid_writer
;
271 unsigned long long *count_reader
;
272 unsigned long long tot_reads
= 0, tot_writes
= 0;
274 int use_affinity
= 0;
277 show_usage(argc
, argv
);
281 err
= sscanf(argv
[1], "%u", &nr_readers
);
283 show_usage(argc
, argv
);
287 err
= sscanf(argv
[2], "%u", &nr_writers
);
289 show_usage(argc
, argv
);
293 err
= sscanf(argv
[3], "%lu", &duration
);
295 show_usage(argc
, argv
);
301 for (i
= 4; i
< argc
; i
++) {
302 if (argv
[i
][0] != '-')
304 switch (argv
[i
][1]) {
307 yield_active
|= YIELD_READ
;
310 yield_active
|= YIELD_WRITE
;
315 show_usage(argc
, argv
);
319 CPU_SET(a
, &affinity
);
321 printf_verbose("Adding CPU %d affinity\n", a
);
325 show_usage(argc
, argv
);
328 reclaim_batch
= atol(argv
[++i
]);
332 show_usage(argc
, argv
);
335 rduration
= atol(argv
[++i
]);
339 show_usage(argc
, argv
);
342 wdelay
= atol(argv
[++i
]);
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());
358 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
359 perror("sched_setaffinity");
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
)
370 for (i
= 0; i
< nr_writers
; i
++)
371 pending_reclaims
[i
].queue
= calloc(1, CACHE_LINE_SIZE
);
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
;
379 for (i
= 0; i
< nr_readers
; i
++) {
380 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
385 for (i
= 0; i
< nr_writers
; i
++) {
386 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
400 for (i
= 0; i
< nr_readers
; i
++) {
401 err
= pthread_join(tid_reader
[i
], &tret
);
404 tot_reads
+= count_reader
[i
];
406 for (i
= 0; i
< nr_writers
; i
++) {
407 err
= pthread_join(tid_writer
[i
], &tret
);
410 tot_writes
+= tot_nr_writes
[i
];
411 rcu_gc_clear_queue(i
);
414 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
416 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
418 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
420 argv
[0], duration
, nr_readers
, rduration
,
421 nr_writers
, wdelay
, tot_reads
, tot_writes
,
422 tot_reads
+ tot_writes
, reclaim_batch
);
427 for (i
= 0; i
< nr_writers
; i
++)
428 free(pending_reclaims
[i
].queue
);
429 free(pending_reclaims
);