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>
37 #include <urcu/arch.h>
39 /* hardcoded number of CPUs */
42 #if defined(_syscall0)
43 _syscall0(pid_t
, gettid
)
44 #elif defined(__NR_gettid)
45 static inline pid_t
gettid(void)
47 return syscall(__NR_gettid
);
50 #warning "use pid as tid"
51 static inline pid_t
gettid(void)
57 #ifndef DYNAMIC_LINK_TEST
60 #define debug_yield_read()
68 static volatile int test_go
, test_stop
;
70 static unsigned long wdelay
;
72 static struct test_array
*test_rcu_pointer
;
74 static unsigned int reclaim_batch
= 1;
76 struct reclaim_queue
{
77 void **queue
; /* Beginning of queue */
78 void **head
; /* Insert position */
81 static struct reclaim_queue
*pending_reclaims
;
83 static unsigned long duration
;
85 /* read-side C.S. duration, in loops */
86 static unsigned long rduration
;
88 static inline void loop_sleep(unsigned long l
)
94 static int verbose_mode
;
96 #define printf_verbose(fmt, args...) \
102 static unsigned int cpu_affinities
[NR_CPUS
];
103 static unsigned int next_aff
= 0;
104 static int use_affinity
= 0;
106 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
108 static void set_affinity(void)
117 ret
= pthread_mutex_lock(&affinity_mutex
);
119 perror("Error in pthread mutex lock");
122 cpu
= cpu_affinities
[next_aff
++];
123 ret
= pthread_mutex_unlock(&affinity_mutex
);
125 perror("Error in pthread mutex unlock");
130 sched_setaffinity(0, sizeof(mask
), &mask
);
134 * returns 0 if test should end.
136 static int test_duration_write(void)
141 static int test_duration_read(void)
146 static unsigned long long __thread nr_writes
;
147 static unsigned long long __thread nr_reads
;
150 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE
))) *tot_nr_writes
;
152 static unsigned int nr_readers
;
153 static unsigned int nr_writers
;
155 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
157 void rcu_copy_mutex_lock(void)
160 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
162 perror("Error in pthread mutex lock");
167 void rcu_copy_mutex_unlock(void)
171 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
173 perror("Error in pthread mutex unlock");
178 void *thr_reader(void *_count
)
180 unsigned long long *count
= _count
;
181 struct test_array
*local_ptr
;
183 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
184 "reader", pthread_self(), (unsigned long)gettid());
188 rcu_register_thread();
197 local_ptr
= rcu_dereference(test_rcu_pointer
);
200 assert(local_ptr
->a
== 8);
201 if (unlikely(rduration
))
202 loop_sleep(rduration
);
205 if (unlikely(!test_duration_read()))
209 rcu_unregister_thread();
212 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
213 "reader", pthread_self(), (unsigned long)gettid());
218 static void rcu_gc_clear_queue(unsigned long wtidx
)
222 /* Wait for Q.S and empty queue */
225 for (p
= pending_reclaims
[wtidx
].queue
;
226 p
< pending_reclaims
[wtidx
].head
; p
++) {
229 ((struct test_array
*)*p
)->a
= 0;
232 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
235 /* Using per-thread queue */
236 static void rcu_gc_reclaim(unsigned long wtidx
, void *old
)
239 *pending_reclaims
[wtidx
].head
= old
;
240 pending_reclaims
[wtidx
].head
++;
242 if (likely(pending_reclaims
[wtidx
].head
- pending_reclaims
[wtidx
].queue
246 rcu_gc_clear_queue(wtidx
);
249 void *thr_writer(void *data
)
251 unsigned long wtidx
= (unsigned long)data
;
253 struct test_array
*old
= NULL
;
255 struct test_array
*new, *old
;
258 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
259 "writer", pthread_self(), (unsigned long)gettid());
269 #ifndef TEST_LOCAL_GC
270 new = malloc(sizeof(*new));
272 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
274 rcu_gc_reclaim(wtidx
, old
);
276 if (unlikely(!test_duration_write()))
278 if (unlikely(wdelay
))
282 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
283 "writer", pthread_self(), (unsigned long)gettid());
284 tot_nr_writes
[wtidx
] = nr_writes
;
288 void show_usage(int argc
, char **argv
)
290 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
292 printf(" [-r] [-w] (yield reader and/or writer)");
294 printf(" [-d delay] (writer period (us))");
295 printf(" [-c duration] (reader C.S. duration (in loops))");
296 printf(" [-v] (verbose output)");
297 printf(" [-a cpu#] [-a cpu#]... (affinity)");
301 int main(int argc
, char **argv
)
304 pthread_t
*tid_reader
, *tid_writer
;
306 unsigned long long *count_reader
;
307 unsigned long long tot_reads
= 0, tot_writes
= 0;
311 show_usage(argc
, argv
);
315 err
= sscanf(argv
[1], "%u", &nr_readers
);
317 show_usage(argc
, argv
);
321 err
= sscanf(argv
[2], "%u", &nr_writers
);
323 show_usage(argc
, argv
);
327 err
= sscanf(argv
[3], "%lu", &duration
);
329 show_usage(argc
, argv
);
333 for (i
= 4; i
< argc
; i
++) {
334 if (argv
[i
][0] != '-')
336 switch (argv
[i
][1]) {
339 yield_active
|= YIELD_READ
;
342 yield_active
|= YIELD_WRITE
;
347 show_usage(argc
, argv
);
351 cpu_affinities
[next_aff
++] = a
;
353 printf_verbose("Adding CPU %d affinity\n", a
);
357 show_usage(argc
, argv
);
360 reclaim_batch
= atol(argv
[++i
]);
364 show_usage(argc
, argv
);
367 rduration
= atol(argv
[++i
]);
371 show_usage(argc
, argv
);
374 wdelay
= atol(argv
[++i
]);
382 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
383 duration
, nr_readers
, nr_writers
);
384 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
385 printf_verbose("Reader duration : %lu loops.\n", rduration
);
386 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
387 "main", pthread_self(), (unsigned long)gettid());
389 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
390 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
391 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
392 tot_nr_writes
= malloc(sizeof(*tot_nr_writes
) * nr_writers
);
393 pending_reclaims
= malloc(sizeof(*pending_reclaims
) * nr_writers
);
394 if (reclaim_batch
* sizeof(*pending_reclaims
[i
].queue
)
396 for (i
= 0; i
< nr_writers
; i
++)
397 pending_reclaims
[i
].queue
= calloc(1, CACHE_LINE_SIZE
);
399 for (i
= 0; i
< nr_writers
; i
++)
400 pending_reclaims
[i
].queue
= calloc(reclaim_batch
,
401 sizeof(*pending_reclaims
[i
].queue
));
402 for (i
= 0; i
< nr_writers
; i
++)
403 pending_reclaims
[i
].head
= pending_reclaims
[i
].queue
;
407 for (i
= 0; i
< nr_readers
; i
++) {
408 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
413 for (i
= 0; i
< nr_writers
; i
++) {
414 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
428 for (i
= 0; i
< nr_readers
; i
++) {
429 err
= pthread_join(tid_reader
[i
], &tret
);
432 tot_reads
+= count_reader
[i
];
434 for (i
= 0; i
< nr_writers
; i
++) {
435 err
= pthread_join(tid_writer
[i
], &tret
);
438 tot_writes
+= tot_nr_writes
[i
];
439 rcu_gc_clear_queue(i
);
442 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
444 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
446 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
448 argv
[0], duration
, nr_readers
, rduration
,
449 nr_writers
, wdelay
, tot_reads
, tot_writes
,
450 tot_reads
+ tot_writes
, reclaim_batch
);
455 for (i
= 0; i
< nr_writers
; i
++)
456 free(pending_reclaims
[i
].queue
);
457 free(pending_reclaims
);