tests: use SPDX identifiers
[urcu.git] / tests / benchmark / test_urcu_qsbr_gc.c
CommitLineData
ce29b371
MJ
1// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2//
3// SPDX-License-Identifier: GPL-2.0-or-later
4
de10a585 5/*
de10a585 6 * Userspace RCU library - test program (with baatch reclamation)
de10a585
MD
7 */
8
de10a585
MD
9#include <stdio.h>
10#include <pthread.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/types.h>
14#include <sys/wait.h>
15#include <unistd.h>
16#include <stdio.h>
de10a585
MD
17#include <errno.h>
18
19#include <urcu/arch.h>
01477510 20#include <urcu/assert.h>
bd252a04 21#include <urcu/tls-compat.h>
94df6318 22#include "thread-id.h"
2650042a 23#include "../common/debug-yield.h"
de10a585
MD
24
25/* hardcoded number of CPUs */
26#define NR_CPUS 16384
27
de10a585
MD
28#define _LGPL_SOURCE
29#include <urcu-qsbr.h>
30
31struct test_array {
32 int a;
33};
34
35static volatile int test_go, test_stop;
36
37static unsigned long wdelay;
38
39static struct test_array *test_rcu_pointer;
40
41static unsigned long duration;
42
43/* read-side C.S. duration, in loops */
44static unsigned long rduration;
d266df35 45static long reclaim_batch = 1;
de10a585
MD
46
47struct reclaim_queue {
48 void **queue; /* Beginning of queue */
49 void **head; /* Insert position */
50};
51
52static struct reclaim_queue *pending_reclaims;
53
54
55/* write-side C.S. duration, in loops */
56static unsigned long wduration;
57
ab0aacbe 58static inline void loop_sleep(unsigned long loops)
de10a585 59{
ab0aacbe 60 while (loops-- != 0)
de10a585
MD
61 caa_cpu_relax();
62}
63
64static int verbose_mode;
65
66#define printf_verbose(fmt, args...) \
67 do { \
68 if (verbose_mode) \
69 printf(fmt, args); \
70 } while (0)
71
72static unsigned int cpu_affinities[NR_CPUS];
73static unsigned int next_aff = 0;
74static int use_affinity = 0;
75
76pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
77
de10a585
MD
78static void set_affinity(void)
79{
2388c075 80#ifdef HAVE_SCHED_SETAFFINITY
de10a585 81 cpu_set_t mask;
95bc7fb9
MD
82 int cpu, ret;
83#endif /* HAVE_SCHED_SETAFFINITY */
de10a585
MD
84
85 if (!use_affinity)
86 return;
87
2388c075 88#ifdef HAVE_SCHED_SETAFFINITY
de10a585
MD
89 ret = pthread_mutex_lock(&affinity_mutex);
90 if (ret) {
91 perror("Error in pthread mutex lock");
92 exit(-1);
93 }
94 cpu = cpu_affinities[next_aff++];
95 ret = pthread_mutex_unlock(&affinity_mutex);
96 if (ret) {
97 perror("Error in pthread mutex unlock");
98 exit(-1);
99 }
100
101 CPU_ZERO(&mask);
102 CPU_SET(cpu, &mask);
de10a585 103 sched_setaffinity(0, sizeof(mask), &mask);
de10a585
MD
104#endif /* HAVE_SCHED_SETAFFINITY */
105}
106
107/*
108 * returns 0 if test should end.
109 */
110static int test_duration_write(void)
111{
112 return !test_stop;
113}
114
115static int test_duration_read(void)
116{
117 return !test_stop;
118}
119
bd252a04
MD
120static DEFINE_URCU_TLS(unsigned long long, nr_writes);
121static DEFINE_URCU_TLS(unsigned long long, nr_reads);
de10a585
MD
122
123static unsigned int nr_readers;
124static unsigned int nr_writers;
125
126pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
127static
128unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
129
61c3fb60 130static
de10a585
MD
131void *thr_reader(void *_count)
132{
133 unsigned long long *count = _count;
134 struct test_array *local_ptr;
135
94df6318
MD
136 printf_verbose("thread_begin %s, tid %lu\n",
137 "reader", urcu_get_thread_id());
de10a585
MD
138
139 set_affinity();
140
141 rcu_register_thread();
142
143 while (!test_go)
144 {
145 }
146 cmm_smp_mb();
147
148 for (;;) {
149 _rcu_read_lock();
150 local_ptr = _rcu_dereference(test_rcu_pointer);
1de4df4b 151 rcu_debug_yield_read();
de10a585 152 if (local_ptr)
01477510 153 urcu_posix_assert(local_ptr->a == 8);
a0b7f7ea 154 if (caa_unlikely(rduration))
de10a585
MD
155 loop_sleep(rduration);
156 _rcu_read_unlock();
bd252a04 157 URCU_TLS(nr_reads)++;
de10a585 158 /* QS each 1024 reads */
bd252a04 159 if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
de10a585 160 _rcu_quiescent_state();
a0b7f7ea 161 if (caa_unlikely(!test_duration_read()))
de10a585
MD
162 break;
163 }
164
165 rcu_unregister_thread();
166
bd252a04 167 *count = URCU_TLS(nr_reads);
94df6318
MD
168 printf_verbose("thread_end %s, tid %lu\n",
169 "reader", urcu_get_thread_id());
de10a585
MD
170 return ((void*)1);
171
172}
173
174static void rcu_gc_clear_queue(unsigned long wtidx)
175{
176 void **p;
177
178 /* Wait for Q.S and empty queue */
179 synchronize_rcu();
180
181 for (p = pending_reclaims[wtidx].queue;
182 p < pending_reclaims[wtidx].head; p++) {
183 /* poison */
184 if (*p)
185 ((struct test_array *)*p)->a = 0;
186 free(*p);
187 }
188 pending_reclaims[wtidx].head = pending_reclaims[wtidx].queue;
189}
190
191/* Using per-thread queue */
192static void rcu_gc_reclaim(unsigned long wtidx, void *old)
193{
194 /* Queue pointer */
195 *pending_reclaims[wtidx].head = old;
196 pending_reclaims[wtidx].head++;
197
a0b7f7ea 198 if (caa_likely(pending_reclaims[wtidx].head - pending_reclaims[wtidx].queue
de10a585
MD
199 < reclaim_batch))
200 return;
201
202 rcu_gc_clear_queue(wtidx);
203}
204
61c3fb60 205static
de10a585
MD
206void *thr_writer(void *data)
207{
208 unsigned long wtidx = (unsigned long)data;
209#ifdef TEST_LOCAL_GC
210 struct test_array *old = NULL;
211#else
212 struct test_array *new, *old;
213#endif
214
94df6318
MD
215 printf_verbose("thread_begin %s, tid %lu\n",
216 "writer", urcu_get_thread_id());
de10a585
MD
217
218 set_affinity();
219
220 while (!test_go)
221 {
222 }
223 cmm_smp_mb();
224
225 for (;;) {
226#ifndef TEST_LOCAL_GC
227 new = malloc(sizeof(*new));
228 new->a = 8;
229 old = _rcu_xchg_pointer(&test_rcu_pointer, new);
230#endif
a0b7f7ea 231 if (caa_unlikely(wduration))
de10a585
MD
232 loop_sleep(wduration);
233 rcu_gc_reclaim(wtidx, old);
bd252a04 234 URCU_TLS(nr_writes)++;
a0b7f7ea 235 if (caa_unlikely(!test_duration_write()))
de10a585 236 break;
a0b7f7ea 237 if (caa_unlikely(wdelay))
de10a585
MD
238 loop_sleep(wdelay);
239 }
240
94df6318
MD
241 printf_verbose("thread_end %s, tid %lu\n",
242 "writer", urcu_get_thread_id());
bd252a04 243 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
de10a585
MD
244 return ((void*)2);
245}
246
61c3fb60 247static
70469b43 248void show_usage(char **argv)
de10a585 249{
06637138
MD
250 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
251 argv[0]);
252 printf("OPTIONS:\n");
06637138 253 printf(" [-r] [-w] (yield reader and/or writer)\n");
06637138
MD
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");
de10a585
MD
260 printf("\n");
261}
262
263int main(int argc, char **argv)
264{
265 int err;
266 pthread_t *tid_reader, *tid_writer;
267 void *tret;
268 unsigned long long *count_reader;
269 unsigned long long tot_reads = 0, tot_writes = 0;
270 int i, a;
83e334d0 271 unsigned int i_thr;
de10a585
MD
272
273 if (argc < 4) {
70469b43 274 show_usage(argv);
de10a585
MD
275 return -1;
276 }
277
278 err = sscanf(argv[1], "%u", &nr_readers);
279 if (err != 1) {
70469b43 280 show_usage(argv);
de10a585
MD
281 return -1;
282 }
283
284 err = sscanf(argv[2], "%u", &nr_writers);
285 if (err != 1) {
70469b43 286 show_usage(argv);
de10a585
MD
287 return -1;
288 }
83e334d0 289
de10a585
MD
290 err = sscanf(argv[3], "%lu", &duration);
291 if (err != 1) {
70469b43 292 show_usage(argv);
de10a585
MD
293 return -1;
294 }
295
296 for (i = 4; i < argc; i++) {
297 if (argv[i][0] != '-')
298 continue;
299 switch (argv[i][1]) {
de10a585 300 case 'r':
2650042a 301 rcu_debug_yield_enable(RCU_YIELD_READ);
de10a585
MD
302 break;
303 case 'w':
2650042a 304 rcu_debug_yield_enable(RCU_YIELD_WRITE);
de10a585 305 break;
de10a585
MD
306 case 'a':
307 if (argc < i + 2) {
70469b43 308 show_usage(argv);
de10a585
MD
309 return -1;
310 }
311 a = atoi(argv[++i]);
312 cpu_affinities[next_aff++] = a;
313 use_affinity = 1;
314 printf_verbose("Adding CPU %d affinity\n", a);
315 break;
316 case 'b':
317 if (argc < i + 2) {
70469b43 318 show_usage(argv);
de10a585
MD
319 return -1;
320 }
321 reclaim_batch = atol(argv[++i]);
322 break;
323 case 'c':
324 if (argc < i + 2) {
70469b43 325 show_usage(argv);
de10a585
MD
326 return -1;
327 }
328 rduration = atol(argv[++i]);
329 break;
330 case 'd':
331 if (argc < i + 2) {
70469b43 332 show_usage(argv);
de10a585
MD
333 return -1;
334 }
335 wdelay = atol(argv[++i]);
336 break;
337 case 'e':
338 if (argc < i + 2) {
70469b43 339 show_usage(argv);
de10a585
MD
340 return -1;
341 }
342 wduration = atol(argv[++i]);
343 break;
344 case 'v':
345 verbose_mode = 1;
346 break;
347 }
348 }
349
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);
94df6318
MD
354 printf_verbose("thread %-6s, tid %lu\n",
355 "main", urcu_get_thread_id());
de10a585 356
9aa14175
MD
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));
83e334d0 362 if (reclaim_batch * sizeof(*pending_reclaims[0].queue)
de10a585 363 < CAA_CACHE_LINE_SIZE)
83e334d0
MJ
364 for (i_thr = 0; i_thr < nr_writers; i_thr++)
365 pending_reclaims[i_thr].queue = calloc(1, CAA_CACHE_LINE_SIZE);
de10a585 366 else
83e334d0
MJ
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;
de10a585
MD
372
373 next_aff = 0;
374
83e334d0
MJ
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]);
de10a585
MD
378 if (err != 0)
379 exit(1);
380 }
83e334d0
MJ
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);
de10a585
MD
384 if (err != 0)
385 exit(1);
386 }
387
388 cmm_smp_mb();
389
390 test_go = 1;
391
392 sleep(duration);
393
394 test_stop = 1;
395
83e334d0
MJ
396 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
397 err = pthread_join(tid_reader[i_thr], &tret);
de10a585
MD
398 if (err != 0)
399 exit(1);
83e334d0 400 tot_reads += count_reader[i_thr];
de10a585 401 }
83e334d0
MJ
402 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
403 err = pthread_join(tid_writer[i_thr], &tret);
de10a585
MD
404 if (err != 0)
405 exit(1);
83e334d0
MJ
406 tot_writes += tot_nr_writes[i_thr];
407 rcu_gc_clear_queue(i_thr);
de10a585 408 }
83e334d0 409
de10a585
MD
410 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
411 tot_writes);
412 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
413 "nr_writers %3u "
414 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
d266df35 415 "batch %ld\n",
de10a585
MD
416 argv[0], duration, nr_readers, rduration, wduration,
417 nr_writers, wdelay, tot_reads, tot_writes,
418 tot_reads + tot_writes, reclaim_batch);
83e334d0 419
de10a585
MD
420 free(tid_reader);
421 free(tid_writer);
422 free(count_reader);
423 free(tot_nr_writes);
83e334d0
MJ
424
425 for (i_thr = 0; i_thr < nr_writers; i_thr++)
426 free(pending_reclaims[i_thr].queue);
de10a585
MD
427 free(pending_reclaims);
428
429 return 0;
430}
This page took 0.063586 seconds and 4 git commands to generate.