1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: GPL-2.0-or-later
6 * Userspace RCU library - test program (with baatch reclamation)
13 #include <sys/types.h>
19 #include <urcu/arch.h>
20 #include <urcu/assert.h>
21 #include <urcu/tls-compat.h>
22 #include "thread-id.h"
23 #include "../common/debug-yield.h"
25 /* hardcoded number of CPUs */
29 #include <urcu-qsbr.h>
35 static volatile int test_go
, test_stop
;
37 static unsigned long wdelay
;
39 static struct test_array
*test_rcu_pointer
;
41 static unsigned long duration
;
43 /* read-side C.S. duration, in loops */
44 static unsigned long rduration
;
45 static long reclaim_batch
= 1;
47 struct reclaim_queue
{
48 void **queue
; /* Beginning of queue */
49 void **head
; /* Insert position */
52 static struct reclaim_queue
*pending_reclaims
;
55 /* write-side C.S. duration, in loops */
56 static unsigned long wduration
;
58 static inline void loop_sleep(unsigned long loops
)
64 static int verbose_mode
;
66 #define printf_verbose(fmt, args...) \
72 static unsigned int cpu_affinities
[NR_CPUS
];
73 static unsigned int next_aff
= 0;
74 static int use_affinity
= 0;
76 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
78 static void set_affinity(void)
80 #ifdef HAVE_SCHED_SETAFFINITY
83 #endif /* HAVE_SCHED_SETAFFINITY */
88 #ifdef HAVE_SCHED_SETAFFINITY
89 ret
= pthread_mutex_lock(&affinity_mutex
);
91 perror("Error in pthread mutex lock");
94 cpu
= cpu_affinities
[next_aff
++];
95 ret
= pthread_mutex_unlock(&affinity_mutex
);
97 perror("Error in pthread mutex unlock");
103 sched_setaffinity(0, sizeof(mask
), &mask
);
104 #endif /* HAVE_SCHED_SETAFFINITY */
108 * returns 0 if test should end.
110 static int test_duration_write(void)
115 static int test_duration_read(void)
120 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
121 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
123 static unsigned int nr_readers
;
124 static unsigned int nr_writers
;
126 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
128 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_writes
;
131 void *thr_reader(void *_count
)
133 unsigned long long *count
= _count
;
134 struct test_array
*local_ptr
;
136 printf_verbose("thread_begin %s, tid %lu\n",
137 "reader", urcu_get_thread_id());
141 rcu_register_thread();
150 local_ptr
= _rcu_dereference(test_rcu_pointer
);
151 rcu_debug_yield_read();
153 urcu_posix_assert(local_ptr
->a
== 8);
154 if (caa_unlikely(rduration
))
155 loop_sleep(rduration
);
157 URCU_TLS(nr_reads
)++;
158 /* QS each 1024 reads */
159 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
160 _rcu_quiescent_state();
161 if (caa_unlikely(!test_duration_read()))
165 rcu_unregister_thread();
167 *count
= URCU_TLS(nr_reads
);
168 printf_verbose("thread_end %s, tid %lu\n",
169 "reader", urcu_get_thread_id());
174 static void rcu_gc_clear_queue(unsigned long wtidx
)
178 /* Wait for Q.S and empty queue */
181 for (p
= pending_reclaims
[wtidx
].queue
;
182 p
< pending_reclaims
[wtidx
].head
; p
++) {
185 ((struct test_array
*)*p
)->a
= 0;
188 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
191 /* Using per-thread queue */
192 static void rcu_gc_reclaim(unsigned long wtidx
, void *old
)
195 *pending_reclaims
[wtidx
].head
= old
;
196 pending_reclaims
[wtidx
].head
++;
198 if (caa_likely(pending_reclaims
[wtidx
].head
- pending_reclaims
[wtidx
].queue
202 rcu_gc_clear_queue(wtidx
);
206 void *thr_writer(void *data
)
208 unsigned long wtidx
= (unsigned long)data
;
210 struct test_array
*old
= NULL
;
212 struct test_array
*new, *old
;
215 printf_verbose("thread_begin %s, tid %lu\n",
216 "writer", urcu_get_thread_id());
226 #ifndef TEST_LOCAL_GC
227 new = malloc(sizeof(*new));
229 old
= _rcu_xchg_pointer(&test_rcu_pointer
, new);
231 if (caa_unlikely(wduration
))
232 loop_sleep(wduration
);
233 rcu_gc_reclaim(wtidx
, old
);
234 URCU_TLS(nr_writes
)++;
235 if (caa_unlikely(!test_duration_write()))
237 if (caa_unlikely(wdelay
))
241 printf_verbose("thread_end %s, tid %lu\n",
242 "writer", urcu_get_thread_id());
243 tot_nr_writes
[wtidx
] = URCU_TLS(nr_writes
);
248 void show_usage(char **argv
)
250 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
252 printf("OPTIONS:\n");
253 printf(" [-r] [-w] (yield reader and/or writer)\n");
254 printf(" [-b batch] (batch reclaim)\n");
255 printf(" [-d delay] (writer period (us))\n");
256 printf(" [-c duration] (reader C.S. duration (in loops))\n");
257 printf(" [-e duration] (writer C.S. duration (in loops))\n");
258 printf(" [-v] (verbose output)\n");
259 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
263 int main(int argc
, char **argv
)
266 pthread_t
*tid_reader
, *tid_writer
;
268 unsigned long long *count_reader
;
269 unsigned long long tot_reads
= 0, tot_writes
= 0;
278 err
= sscanf(argv
[1], "%u", &nr_readers
);
284 err
= sscanf(argv
[2], "%u", &nr_writers
);
290 err
= sscanf(argv
[3], "%lu", &duration
);
296 for (i
= 4; i
< argc
; i
++) {
297 if (argv
[i
][0] != '-')
299 switch (argv
[i
][1]) {
301 rcu_debug_yield_enable(RCU_YIELD_READ
);
304 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
312 cpu_affinities
[next_aff
++] = a
;
314 printf_verbose("Adding CPU %d affinity\n", a
);
321 reclaim_batch
= atol(argv
[++i
]);
328 rduration
= atol(argv
[++i
]);
335 wdelay
= atol(argv
[++i
]);
342 wduration
= 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, tid %lu\n",
355 "main", urcu_get_thread_id());
357 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
358 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
359 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
360 tot_nr_writes
= calloc(nr_writers
, sizeof(*tot_nr_writes
));
361 pending_reclaims
= calloc(nr_writers
, sizeof(*pending_reclaims
));
362 if (reclaim_batch
* sizeof(*pending_reclaims
[0].queue
)
363 < CAA_CACHE_LINE_SIZE
)
364 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
365 pending_reclaims
[i_thr
].queue
= calloc(1, CAA_CACHE_LINE_SIZE
);
367 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
368 pending_reclaims
[i_thr
].queue
= calloc(reclaim_batch
,
369 sizeof(*pending_reclaims
[i_thr
].queue
));
370 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
371 pending_reclaims
[i_thr
].head
= pending_reclaims
[i_thr
].queue
;
375 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
376 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
377 &count_reader
[i_thr
]);
381 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
382 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
383 (void *)(long)i_thr
);
396 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
397 err
= pthread_join(tid_reader
[i_thr
], &tret
);
400 tot_reads
+= count_reader
[i_thr
];
402 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
403 err
= pthread_join(tid_writer
[i_thr
], &tret
);
406 tot_writes
+= tot_nr_writes
[i_thr
];
407 rcu_gc_clear_queue(i_thr
);
410 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
412 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
414 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
416 argv
[0], duration
, nr_readers
, rduration
, wduration
,
417 nr_writers
, wdelay
, tot_reads
, tot_writes
,
418 tot_reads
+ tot_writes
, reclaim_batch
);
425 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
426 free(pending_reclaims
[i_thr
].queue
);
427 free(pending_reclaims
);