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 batch 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 */
29 #ifndef DYNAMIC_LINK_TEST
38 static unsigned long wdelay
;
40 static struct test_array
*test_rcu_pointer
;
42 static long reclaim_batch
= 1;
44 struct reclaim_queue
{
45 void **queue
; /* Beginning of queue */
46 void **head
; /* Insert position */
49 static struct reclaim_queue
*pending_reclaims
;
51 static unsigned long duration
;
53 /* read-side C.S. duration, in loops */
54 static unsigned long rduration
;
56 /* write-side C.S. duration, in loops */
57 static unsigned long wduration
;
59 static inline void loop_sleep(unsigned long loops
)
65 static int verbose_mode
;
67 #define printf_verbose(fmt, args...) \
73 static unsigned int cpu_affinities
[NR_CPUS
];
74 static unsigned int next_aff
= 0;
75 static int use_affinity
= 0;
77 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
79 static void set_affinity(void)
81 #ifdef HAVE_SCHED_SETAFFINITY
84 #endif /* HAVE_SCHED_SETAFFINITY */
89 #ifdef HAVE_SCHED_SETAFFINITY
90 ret
= pthread_mutex_lock(&affinity_mutex
);
92 perror("Error in pthread mutex lock");
95 cpu
= cpu_affinities
[next_aff
++];
96 ret
= pthread_mutex_unlock(&affinity_mutex
);
98 perror("Error in pthread mutex unlock");
104 sched_setaffinity(0, sizeof(mask
), &mask
);
105 #endif /* HAVE_SCHED_SETAFFINITY */
108 static DEFINE_URCU_TLS(unsigned long long, nr_writes
);
109 static DEFINE_URCU_TLS(unsigned long long, nr_reads
);
112 unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE
))) *tot_nr_writes
;
114 static unsigned int nr_readers
;
115 static unsigned int nr_writers
;
117 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
120 void *thr_reader(void *_count
)
122 unsigned long long *count
= _count
;
123 struct test_array
*local_ptr
;
125 printf_verbose("thread_begin %s, tid %lu\n",
126 "reader", urcu_get_thread_id());
130 rcu_register_thread();
136 local_ptr
= rcu_dereference(test_rcu_pointer
);
137 rcu_debug_yield_read();
139 urcu_posix_assert(local_ptr
->a
== 8);
140 if (caa_unlikely(rduration
))
141 loop_sleep(rduration
);
143 URCU_TLS(nr_reads
)++;
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(" [-d delay] (writer period (us))\n");
235 printf(" [-c duration] (reader C.S. duration (in loops))\n");
236 printf(" [-e duration] (writer C.S. duration (in loops))\n");
237 printf(" [-v] (verbose output)\n");
238 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
242 int main(int argc
, char **argv
)
245 pthread_t
*tid_reader
, *tid_writer
;
247 unsigned long long *count_reader
;
248 unsigned long long tot_reads
= 0, tot_writes
= 0;
257 err
= sscanf(argv
[1], "%u", &nr_readers
);
263 err
= sscanf(argv
[2], "%u", &nr_writers
);
269 err
= sscanf(argv
[3], "%lu", &duration
);
275 for (i
= 4; i
< argc
; i
++) {
276 if (argv
[i
][0] != '-')
278 switch (argv
[i
][1]) {
280 rcu_debug_yield_enable(RCU_YIELD_READ
);
283 rcu_debug_yield_enable(RCU_YIELD_WRITE
);
291 cpu_affinities
[next_aff
++] = a
;
293 printf_verbose("Adding CPU %d affinity\n", a
);
300 reclaim_batch
= atol(argv
[++i
]);
307 rduration
= atol(argv
[++i
]);
314 wdelay
= atol(argv
[++i
]);
321 wduration
= atol(argv
[++i
]);
329 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
330 duration
, nr_readers
, nr_writers
);
331 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
332 printf_verbose("Reader duration : %lu loops.\n", rduration
);
333 printf_verbose("thread %-6s, tid %lu\n",
334 "main", urcu_get_thread_id());
336 tid_reader
= calloc(nr_readers
, sizeof(*tid_reader
));
337 tid_writer
= calloc(nr_writers
, sizeof(*tid_writer
));
338 count_reader
= calloc(nr_readers
, sizeof(*count_reader
));
339 tot_nr_writes
= calloc(nr_writers
, sizeof(*tot_nr_writes
));
340 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
);