4 * Userspace RCU library - test program (with batch reclamation)
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
27 #include <sys/types.h>
34 #include <urcu/arch.h>
35 #include <urcu/tls-compat.h>
36 #include "thread-id.h"
37 #include "../common/debug-yield.h"
39 /* hardcoded number of CPUs */
42 #ifndef DYNAMIC_LINK_TEST
51 static volatile int test_go
, test_stop
;
53 static unsigned long wdelay
;
55 static struct test_array
*test_rcu_pointer
;
57 static long reclaim_batch
= 1;
59 struct reclaim_queue
{
60 void **queue
; /* Beginning of queue */
61 void **head
; /* Insert position */
64 static struct reclaim_queue
*pending_reclaims
;
66 static unsigned long duration
;
68 /* read-side C.S. duration, in loops */
69 static unsigned long rduration
;
71 /* write-side C.S. duration, in loops */
72 static unsigned long wduration
;
74 static inline void loop_sleep(unsigned long loops
)
80 static int verbose_mode
;
82 #define printf_verbose(fmt, args...) \
88 static unsigned int cpu_affinities
[NR_CPUS
];
89 static unsigned int next_aff
= 0;
90 static int use_affinity
= 0;
92 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
94 static void set_affinity(void)
96 #if HAVE_SCHED_SETAFFINITY
99 #endif /* HAVE_SCHED_SETAFFINITY */
104 #if HAVE_SCHED_SETAFFINITY
105 ret
= pthread_mutex_lock(&affinity_mutex
);
107 perror("Error in pthread mutex lock");
110 cpu
= cpu_affinities
[next_aff
++];
111 ret
= pthread_mutex_unlock(&affinity_mutex
);
113 perror("Error in pthread mutex unlock");
119 sched_setaffinity(0, sizeof(mask
), &mask
);
120 #endif /* HAVE_SCHED_SETAFFINITY */
124 * returns 0 if test should end.
126 static int test_duration_write(void)
131 static int test_duration_read(void)
136 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
137 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
140 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_writes
;
142 static unsigned int nr_readers
;
143 static unsigned int nr_writers
;
145 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
147 void rcu_copy_mutex_lock(void)
150 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
152 perror("Error in pthread mutex lock");
157 void rcu_copy_mutex_unlock(void)
161 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
163 perror("Error in pthread mutex unlock");
168 void *thr_reader(void *_count
)
170 unsigned long long *count
= _count
;
171 struct test_array
*local_ptr
;
173 printf_verbose("thread_begin %s, tid %lu\n",
174 "reader", urcu_get_thread_id());
178 rcu_register_thread();
187 local_ptr
= rcu_dereference(test_rcu_pointer
);
188 rcu_debug_yield_read();
190 assert(local_ptr
->a
== 8);
191 if (caa_unlikely(rduration
))
192 loop_sleep(rduration
);
194 URCU_TLS(nr_reads
)++;
195 if (caa_unlikely(!test_duration_read()))
199 rcu_unregister_thread();
201 *count
= URCU_TLS(nr_reads
);
202 printf_verbose("thread_end %s, tid %lu\n",
203 "reader", urcu_get_thread_id());
208 static void rcu_gc_clear_queue(unsigned long wtidx
)
212 /* Wait for Q.S and empty queue */
215 for (p
= pending_reclaims
[wtidx
].queue
;
216 p
< pending_reclaims
[wtidx
].head
; p
++) {
219 ((struct test_array
*)*p
)->a
= 0;
222 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
225 /* Using per-thread queue */
226 static void rcu_gc_reclaim(unsigned long wtidx
, void *old
)
229 *pending_reclaims
[wtidx
].head
= old
;
230 pending_reclaims
[wtidx
].head
++;
232 if (caa_likely(pending_reclaims
[wtidx
].head
- pending_reclaims
[wtidx
].queue
236 rcu_gc_clear_queue(wtidx
);
239 void *thr_writer(void *data
)
241 unsigned long wtidx
= (unsigned long)data
;
243 struct test_array
*old
= NULL
;
245 struct test_array
*new, *old
;
248 printf_verbose("thread_begin %s, tid %lu\n",
249 "writer", urcu_get_thread_id());
259 #ifndef TEST_LOCAL_GC
260 new = malloc(sizeof(*new));
262 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
264 if (caa_unlikely(wduration
))
265 loop_sleep(wduration
);
266 rcu_gc_reclaim(wtidx
, old
);
267 URCU_TLS(nr_writes
)++;
268 if (caa_unlikely(!test_duration_write()))
270 if (caa_unlikely(wdelay
))
274 printf_verbose("thread_end %s, tid %lu\n",
275 "writer", urcu_get_thread_id());
276 tot_nr_writes
[wtidx
] = URCU_TLS(nr_writes
);
280 void show_usage(int argc
, char **argv
)
282 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
284 printf("OPTIONS:\n");
285 printf(" [-r] [-w] (yield reader and/or writer)\n");
286 printf(" [-d delay] (writer period (us))\n");
287 printf(" [-c duration] (reader C.S. duration (in loops))\n");
288 printf(" [-e duration] (writer C.S. duration (in loops))\n");
289 printf(" [-v] (verbose output)\n");
290 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
294 int main(int argc
, char **argv
)
297 pthread_t
*tid_reader
, *tid_writer
;
299 unsigned long long *count_reader
;
300 unsigned long long tot_reads
= 0, tot_writes
= 0;
305 show_usage(argc
, argv
);
309 err
= sscanf(argv
[1], "%u", &nr_readers
);
311 show_usage(argc
, argv
);
315 err
= sscanf(argv
[2], "%u", &nr_writers
);
317 show_usage(argc
, argv
);
321 err
= sscanf(argv
[3], "%lu", &duration
);
323 show_usage(argc
, argv
);
327 for (i
= 4; i
< argc
; i
++) {
328 if (argv
[i
][0] != '-')
330 switch (argv
[i
][1]) {
332 rcu_debug_yield_enable(RCU_YIELD_READ
);
335 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
339 show_usage(argc
, argv
);
343 cpu_affinities
[next_aff
++] = a
;
345 printf_verbose("Adding CPU %d affinity\n", a
);
349 show_usage(argc
, argv
);
352 reclaim_batch
= atol(argv
[++i
]);
356 show_usage(argc
, argv
);
359 rduration
= atol(argv
[++i
]);
363 show_usage(argc
, argv
);
366 wdelay
= atol(argv
[++i
]);
370 show_usage(argc
, argv
);
373 wduration
= atol(argv
[++i
]);
381 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
382 duration
, nr_readers
, nr_writers
);
383 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
384 printf_verbose("Reader duration : %lu loops.\n", rduration
);
385 printf_verbose("thread %-6s, tid %lu\n",
386 "main", urcu_get_thread_id());
388 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
389 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
390 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
391 tot_nr_writes
= calloc(nr_writers
, sizeof(*tot_nr_writes
));
392 pending_reclaims
= calloc(nr_writers
, sizeof(*pending_reclaims
));
394 if (reclaim_batch
* sizeof(*pending_reclaims
[0].queue
)
395 < CAA_CACHE_LINE_SIZE
)
396 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
397 pending_reclaims
[i_thr
].queue
= calloc(1, CAA_CACHE_LINE_SIZE
);
399 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
400 pending_reclaims
[i_thr
].queue
= calloc(reclaim_batch
,
401 sizeof(*pending_reclaims
[i_thr
].queue
));
402 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
403 pending_reclaims
[i_thr
].head
= pending_reclaims
[i_thr
].queue
;
407 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
408 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
409 &count_reader
[i_thr
]);
413 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
414 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
415 (void *)(long)i_thr
);
428 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
429 err
= pthread_join(tid_reader
[i_thr
], &tret
);
432 tot_reads
+= count_reader
[i_thr
];
434 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
435 err
= pthread_join(tid_writer
[i_thr
], &tret
);
438 tot_writes
+= tot_nr_writes
[i_thr
];
439 rcu_gc_clear_queue(i_thr
);
442 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
444 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
446 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
448 argv
[0], duration
, nr_readers
, rduration
, wduration
,
449 nr_writers
, wdelay
, tot_reads
, tot_writes
,
450 tot_reads
+ tot_writes
, reclaim_batch
);
457 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
458 free(pending_reclaims
[i_thr
].queue
);
459 free(pending_reclaims
);