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 <urcu/uatomic.h>
23 #include "thread-id.h"
24 #include "../common/debug-yield.h"
26 /* hardcoded number of CPUs */
30 #include <urcu-qsbr.h>
36 static unsigned long wdelay
;
38 static struct test_array
*test_rcu_pointer
;
40 static unsigned long duration
;
42 /* read-side C.S. duration, in loops */
43 static unsigned long rduration
;
44 static long reclaim_batch
= 1;
46 struct reclaim_queue
{
47 void **queue
; /* Beginning of queue */
48 void **head
; /* Insert position */
51 static struct reclaim_queue
*pending_reclaims
;
54 /* write-side C.S. duration, in loops */
55 static unsigned long wduration
;
57 static inline void loop_sleep(unsigned long loops
)
63 static int verbose_mode
;
65 #define printf_verbose(fmt, args...) \
71 static unsigned int cpu_affinities
[NR_CPUS
];
72 static unsigned int next_aff
= 0;
73 static int use_affinity
= 0;
75 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
77 static void set_affinity(void)
79 #ifdef HAVE_SCHED_SETAFFINITY
82 #endif /* HAVE_SCHED_SETAFFINITY */
87 #ifdef HAVE_SCHED_SETAFFINITY
88 ret
= pthread_mutex_lock(&affinity_mutex
);
90 perror("Error in pthread mutex lock");
93 cpu
= cpu_affinities
[next_aff
++];
94 ret
= pthread_mutex_unlock(&affinity_mutex
);
96 perror("Error in pthread mutex unlock");
102 sched_setaffinity(0, sizeof(mask
), &mask
);
103 #endif /* HAVE_SCHED_SETAFFINITY */
106 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
107 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
109 static unsigned int nr_readers
;
110 static unsigned int nr_writers
;
112 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
114 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_writes
;
117 void *thr_reader(void *_count
)
119 unsigned long long *count
= _count
;
120 struct test_array
*local_ptr
;
122 printf_verbose("thread_begin %s, tid %lu\n",
123 "reader", urcu_get_thread_id());
127 rcu_register_thread();
133 local_ptr
= _rcu_dereference(test_rcu_pointer
);
134 rcu_debug_yield_read();
136 urcu_posix_assert(local_ptr
->a
== 8);
137 if (caa_unlikely(rduration
))
138 loop_sleep(rduration
);
140 URCU_TLS(nr_reads
)++;
141 /* QS each 1024 reads */
142 if (caa_unlikely((URCU_TLS(nr_reads
) & ((1 << 10) - 1)) == 0))
143 _rcu_quiescent_state();
144 if (caa_unlikely(!test_duration_read()))
148 rcu_unregister_thread();
150 *count
= URCU_TLS(nr_reads
);
151 printf_verbose("thread_end %s, tid %lu\n",
152 "reader", urcu_get_thread_id());
157 static void rcu_gc_clear_queue(unsigned long wtidx
)
161 /* Wait for Q.S and empty queue */
164 for (p
= pending_reclaims
[wtidx
].queue
;
165 p
< pending_reclaims
[wtidx
].head
; p
++) {
168 ((struct test_array
*)*p
)->a
= 0;
171 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
174 /* Using per-thread queue */
175 static void rcu_gc_reclaim(unsigned long wtidx
, void *old
)
178 *pending_reclaims
[wtidx
].head
= old
;
179 pending_reclaims
[wtidx
].head
++;
181 if (caa_likely(pending_reclaims
[wtidx
].head
- pending_reclaims
[wtidx
].queue
185 rcu_gc_clear_queue(wtidx
);
189 void *thr_writer(void *data
)
191 unsigned long wtidx
= (unsigned long)data
;
193 struct test_array
*old
= NULL
;
195 struct test_array
*new, *old
;
198 printf_verbose("thread_begin %s, tid %lu\n",
199 "writer", urcu_get_thread_id());
206 #ifndef TEST_LOCAL_GC
207 new = malloc(sizeof(*new));
209 old
= _rcu_xchg_pointer(&test_rcu_pointer
, new);
211 if (caa_unlikely(wduration
))
212 loop_sleep(wduration
);
213 rcu_gc_reclaim(wtidx
, old
);
214 URCU_TLS(nr_writes
)++;
215 if (caa_unlikely(!test_duration_write()))
217 if (caa_unlikely(wdelay
))
221 printf_verbose("thread_end %s, tid %lu\n",
222 "writer", urcu_get_thread_id());
223 tot_nr_writes
[wtidx
] = URCU_TLS(nr_writes
);
228 void show_usage(char **argv
)
230 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
232 printf("OPTIONS:\n");
233 printf(" [-r] [-w] (yield reader and/or writer)\n");
234 printf(" [-b batch] (batch reclaim)\n");
235 printf(" [-d delay] (writer period (us))\n");
236 printf(" [-c duration] (reader C.S. duration (in loops))\n");
237 printf(" [-e duration] (writer C.S. duration (in loops))\n");
238 printf(" [-v] (verbose output)\n");
239 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
243 int main(int argc
, char **argv
)
246 pthread_t
*tid_reader
, *tid_writer
;
248 unsigned long long *count_reader
;
249 unsigned long long tot_reads
= 0, tot_writes
= 0;
258 err
= sscanf(argv
[1], "%u", &nr_readers
);
264 err
= sscanf(argv
[2], "%u", &nr_writers
);
270 err
= sscanf(argv
[3], "%lu", &duration
);
276 for (i
= 4; i
< argc
; i
++) {
277 if (argv
[i
][0] != '-')
279 switch (argv
[i
][1]) {
281 rcu_debug_yield_enable(RCU_YIELD_READ
);
284 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
292 cpu_affinities
[next_aff
++] = a
;
294 printf_verbose("Adding CPU %d affinity\n", a
);
301 reclaim_batch
= atol(argv
[++i
]);
308 rduration
= atol(argv
[++i
]);
315 wdelay
= atol(argv
[++i
]);
322 wduration
= atol(argv
[++i
]);
330 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
331 duration
, nr_readers
, nr_writers
);
332 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
333 printf_verbose("Reader duration : %lu loops.\n", rduration
);
334 printf_verbose("thread %-6s, tid %lu\n",
335 "main", urcu_get_thread_id());
337 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
338 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
339 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
340 tot_nr_writes
= calloc(nr_writers
, sizeof(*tot_nr_writes
));
341 pending_reclaims
= calloc(nr_writers
, sizeof(*pending_reclaims
));
342 if (reclaim_batch
* sizeof(*pending_reclaims
[0].queue
)
343 < CAA_CACHE_LINE_SIZE
)
344 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
345 pending_reclaims
[i_thr
].queue
= calloc(1, CAA_CACHE_LINE_SIZE
);
347 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
348 pending_reclaims
[i_thr
].queue
= calloc(reclaim_batch
,
349 sizeof(*pending_reclaims
[i_thr
].queue
));
350 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
351 pending_reclaims
[i_thr
].head
= pending_reclaims
[i_thr
].queue
;
355 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
356 err
= pthread_create(&tid_reader
[i_thr
], NULL
, thr_reader
,
357 &count_reader
[i_thr
]);
361 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
362 err
= pthread_create(&tid_writer
[i_thr
], NULL
, thr_writer
,
363 (void *)(long)i_thr
);
370 for (i_thr
= 0; i_thr
< nr_readers
; i_thr
++) {
371 err
= pthread_join(tid_reader
[i_thr
], &tret
);
374 tot_reads
+= count_reader
[i_thr
];
376 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++) {
377 err
= pthread_join(tid_writer
[i_thr
], &tret
);
380 tot_writes
+= tot_nr_writes
[i_thr
];
381 rcu_gc_clear_queue(i_thr
);
384 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
386 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
388 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
390 argv
[0], duration
, nr_readers
, rduration
, wduration
,
391 nr_writers
, wdelay
, tot_reads
, tot_writes
,
392 tot_reads
+ tot_writes
, reclaim_batch
);
399 for (i_thr
= 0; i_thr
< nr_writers
; i_thr
++)
400 free(pending_reclaims
[i_thr
].queue
);
401 free(pending_reclaims
);