tests/unit: Remove urcu-signal unit tests
[userspace-rcu.git] / tests / benchmark / test_urcu_lfq.c
CommitLineData
ce29b371
MJ
1// SPDX-FileCopyrightText: 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2// SPDX-FileCopyrightText: 2010 Paolo Bonzini <pbonzini@redhat.com>
3//
4// SPDX-License-Identifier: GPL-2.0-or-later
5
453629a9 6/*
453629a9 7 * Userspace RCU library - example RCU-based lock-free queue
453629a9
MD
8 */
9
453629a9
MD
10#include <stdio.h>
11#include <pthread.h>
12#include <stdlib.h>
13#include <stdint.h>
14#include <stdbool.h>
15#include <string.h>
16#include <sys/types.h>
17#include <sys/wait.h>
18#include <unistd.h>
19#include <stdio.h>
453629a9
MD
20#include <errno.h>
21
22#include <urcu/arch.h>
01477510 23#include <urcu/assert.h>
bd252a04 24#include <urcu/tls-compat.h>
94df6318 25#include "thread-id.h"
65fcc7e9 26
453629a9
MD
27/* hardcoded number of CPUs */
28#define NR_CPUS 16384
29
453629a9
MD
30#ifndef DYNAMIC_LINK_TEST
31#define _LGPL_SOURCE
32#endif
33#include <urcu.h>
e5a7d709 34#include <urcu/cds.h>
453629a9 35
453629a9
MD
36static unsigned long rduration;
37
38static unsigned long duration;
39
40/* read-side C.S. duration, in loops */
41static unsigned long wdelay;
42
ab0aacbe 43static inline void loop_sleep(unsigned long loops)
453629a9 44{
ab0aacbe 45 while (loops-- != 0)
06f22bdb 46 caa_cpu_relax();
453629a9
MD
47}
48
49static int verbose_mode;
50
51#define printf_verbose(fmt, args...) \
52 do { \
53 if (verbose_mode) \
54 printf(fmt, args); \
55 } while (0)
56
57static unsigned int cpu_affinities[NR_CPUS];
58static unsigned int next_aff = 0;
59static int use_affinity = 0;
60
61pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
62
453629a9
MD
63static void set_affinity(void)
64{
2388c075 65#ifdef HAVE_SCHED_SETAFFINITY
453629a9 66 cpu_set_t mask;
95bc7fb9
MD
67 int cpu, ret;
68#endif /* HAVE_SCHED_SETAFFINITY */
453629a9
MD
69
70 if (!use_affinity)
71 return;
72
2388c075 73#ifdef HAVE_SCHED_SETAFFINITY
453629a9
MD
74 ret = pthread_mutex_lock(&affinity_mutex);
75 if (ret) {
76 perror("Error in pthread mutex lock");
77 exit(-1);
78 }
79 cpu = cpu_affinities[next_aff++];
80 ret = pthread_mutex_unlock(&affinity_mutex);
81 if (ret) {
82 perror("Error in pthread mutex unlock");
83 exit(-1);
84 }
85
86 CPU_ZERO(&mask);
87 CPU_SET(cpu, &mask);
453629a9 88 sched_setaffinity(0, sizeof(mask), &mask);
453629a9
MD
89#endif /* HAVE_SCHED_SETAFFINITY */
90}
91
92/*
93 * returns 0 if test should end.
94 */
95static int test_duration_dequeue(void)
96{
9e4e7ad1 97 return test_duration_read();
453629a9
MD
98}
99
100static int test_duration_enqueue(void)
101{
9e4e7ad1 102 return test_duration_write();
453629a9
MD
103}
104
bd252a04
MD
105static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
106static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
453629a9 107
bd252a04
MD
108static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
109static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
453629a9
MD
110
111static unsigned int nr_enqueuers;
112static unsigned int nr_dequeuers;
113
94aec122
MD
114struct test {
115 struct cds_lfq_node_rcu list;
116 struct rcu_head rcu;
117};
118
16aa9ee8 119static struct cds_lfq_queue_rcu q;
453629a9 120
61c3fb60 121static
453629a9
MD
122void *thr_enqueuer(void *_count)
123{
124 unsigned long long *count = _count;
125
94df6318
MD
126 printf_verbose("thread_begin %s, tid %lu\n",
127 "enqueuer", urcu_get_thread_id());
453629a9
MD
128
129 set_affinity();
130
131 rcu_register_thread();
132
9e4e7ad1 133 wait_until_go();
453629a9
MD
134
135 for (;;) {
94aec122 136 struct test *node = malloc(sizeof(*node));
453629a9
MD
137 if (!node)
138 goto fail;
94aec122 139 cds_lfq_node_init_rcu(&node->list);
6e5f88cf 140 rcu_read_lock();
94aec122 141 cds_lfq_enqueue_rcu(&q, &node->list);
6e5f88cf 142 rcu_read_unlock();
bd252a04 143 URCU_TLS(nr_successful_enqueues)++;
453629a9 144
a0b7f7ea 145 if (caa_unlikely(wdelay))
453629a9
MD
146 loop_sleep(wdelay);
147fail:
bd252a04 148 URCU_TLS(nr_enqueues)++;
a0b7f7ea 149 if (caa_unlikely(!test_duration_enqueue()))
453629a9
MD
150 break;
151 }
152
153 rcu_unregister_thread();
154
bd252a04
MD
155 count[0] = URCU_TLS(nr_enqueues);
156 count[1] = URCU_TLS(nr_successful_enqueues);
94df6318
MD
157 printf_verbose("enqueuer thread_end, tid %lu, "
158 "enqueues %llu successful_enqueues %llu\n",
159 urcu_get_thread_id(),
160 URCU_TLS(nr_enqueues),
161 URCU_TLS(nr_successful_enqueues));
453629a9
MD
162 return ((void*)1);
163
164}
165
94aec122
MD
166static
167void free_node_cb(struct rcu_head *head)
168{
169 struct test *node =
170 caa_container_of(head, struct test, rcu);
171 free(node);
172}
173
61c3fb60 174static
453629a9
MD
175void *thr_dequeuer(void *_count)
176{
177 unsigned long long *count = _count;
178
94df6318
MD
179 printf_verbose("thread_begin %s, tid %lu\n",
180 "dequeuer", urcu_get_thread_id());
453629a9
MD
181
182 set_affinity();
183
453629a9
MD
184 rcu_register_thread();
185
9e4e7ad1 186 wait_until_go();
453629a9
MD
187
188 for (;;) {
94aec122 189 struct cds_lfq_node_rcu *qnode;
d9b52143 190
6e5f88cf 191 rcu_read_lock();
94aec122 192 qnode = cds_lfq_dequeue_rcu(&q);
6e5f88cf
MD
193 rcu_read_unlock();
194
747b5930
LJ
195 if (qnode) {
196 struct test *node;
197
198 node = caa_container_of(qnode, struct test, list);
94aec122 199 call_rcu(&node->rcu, free_node_cb);
bd252a04 200 URCU_TLS(nr_successful_dequeues)++;
453629a9
MD
201 }
202
bd252a04 203 URCU_TLS(nr_dequeues)++;
a0b7f7ea 204 if (caa_unlikely(!test_duration_dequeue()))
453629a9 205 break;
a0b7f7ea 206 if (caa_unlikely(rduration))
453629a9
MD
207 loop_sleep(rduration);
208 }
209
210 rcu_unregister_thread();
94df6318
MD
211 printf_verbose("dequeuer thread_end, tid %lu, "
212 "dequeues %llu, successful_dequeues %llu\n",
213 urcu_get_thread_id(),
214 URCU_TLS(nr_dequeues),
215 URCU_TLS(nr_successful_dequeues));
bd252a04
MD
216 count[0] = URCU_TLS(nr_dequeues);
217 count[1] = URCU_TLS(nr_successful_dequeues);
453629a9
MD
218 return ((void*)2);
219}
220
61c3fb60 221static
bd23a6c6 222void test_end(unsigned long long *nr_dequeues_l)
453629a9 223{
94aec122 224 struct cds_lfq_node_rcu *snode;
453629a9
MD
225
226 do {
bd23a6c6 227 snode = cds_lfq_dequeue_rcu(&q);
94aec122
MD
228 if (snode) {
229 struct test *node;
230
231 node = caa_container_of(snode, struct test, list);
e17d9985 232 free(node); /* no more concurrent access */
bd23a6c6 233 (*nr_dequeues_l)++;
453629a9 234 }
94aec122 235 } while (snode);
453629a9
MD
236}
237
61c3fb60 238static
70469b43 239void show_usage(char **argv)
453629a9 240{
06637138
MD
241 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
242 argv[0]);
243 printf("OPTIONS:\n");
244 printf(" [-d delay] (enqueuer period (in loops))\n");
245 printf(" [-c duration] (dequeuer period (in loops))\n");
246 printf(" [-v] (verbose output)\n");
247 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
453629a9
MD
248 printf("\n");
249}
250
251int main(int argc, char **argv)
252{
253 int err;
254 pthread_t *tid_enqueuer, *tid_dequeuer;
255 void *tret;
256 unsigned long long *count_enqueuer, *count_dequeuer;
257 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
258 unsigned long long tot_successful_enqueues = 0,
259 tot_successful_dequeues = 0;
260 unsigned long long end_dequeues = 0;
261 int i, a;
83e334d0 262 unsigned int i_thr;
453629a9
MD
263
264 if (argc < 4) {
70469b43 265 show_usage(argv);
453629a9
MD
266 return -1;
267 }
268
269 err = sscanf(argv[1], "%u", &nr_dequeuers);
270 if (err != 1) {
70469b43 271 show_usage(argv);
453629a9
MD
272 return -1;
273 }
274
275 err = sscanf(argv[2], "%u", &nr_enqueuers);
276 if (err != 1) {
70469b43 277 show_usage(argv);
453629a9
MD
278 return -1;
279 }
83e334d0 280
453629a9
MD
281 err = sscanf(argv[3], "%lu", &duration);
282 if (err != 1) {
70469b43 283 show_usage(argv);
453629a9
MD
284 return -1;
285 }
286
287 for (i = 4; i < argc; i++) {
288 if (argv[i][0] != '-')
289 continue;
290 switch (argv[i][1]) {
291 case 'a':
292 if (argc < i + 2) {
70469b43 293 show_usage(argv);
453629a9
MD
294 return -1;
295 }
296 a = atoi(argv[++i]);
297 cpu_affinities[next_aff++] = a;
298 use_affinity = 1;
299 printf_verbose("Adding CPU %d affinity\n", a);
300 break;
301 case 'c':
302 if (argc < i + 2) {
70469b43 303 show_usage(argv);
453629a9
MD
304 return -1;
305 }
306 rduration = atol(argv[++i]);
307 break;
308 case 'd':
309 if (argc < i + 2) {
70469b43 310 show_usage(argv);
453629a9
MD
311 return -1;
312 }
313 wdelay = atol(argv[++i]);
314 break;
315 case 'v':
316 verbose_mode = 1;
317 break;
318 }
319 }
320
321 printf_verbose("running test for %lu seconds, %u enqueuers, "
322 "%u dequeuers.\n",
323 duration, nr_enqueuers, nr_dequeuers);
324 printf_verbose("Writer delay : %lu loops.\n", rduration);
325 printf_verbose("Reader duration : %lu loops.\n", wdelay);
94df6318
MD
326 printf_verbose("thread %-6s, tid %lu\n",
327 "main", urcu_get_thread_id());
453629a9 328
9aa14175
MD
329 tid_enqueuer = calloc(nr_enqueuers, sizeof(*tid_enqueuer));
330 tid_dequeuer = calloc(nr_dequeuers, sizeof(*tid_dequeuer));
331 count_enqueuer = calloc(nr_enqueuers, 2 * sizeof(*count_enqueuer));
332 count_dequeuer = calloc(nr_dequeuers, 2 * sizeof(*count_dequeuer));
6e5f88cf 333 cds_lfq_init_rcu(&q, call_rcu);
94aec122 334 err = create_all_cpu_call_rcu_data(0);
8bcbd94a
MD
335 if (err) {
336 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
337 }
453629a9
MD
338
339 next_aff = 0;
340
83e334d0
MJ
341 for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) {
342 err = pthread_create(&tid_enqueuer[i_thr], NULL, thr_enqueuer,
343 &count_enqueuer[2 * i_thr]);
453629a9
MD
344 if (err != 0)
345 exit(1);
346 }
83e334d0
MJ
347 for (i_thr = 0; i_thr < nr_dequeuers; i_thr++) {
348 err = pthread_create(&tid_dequeuer[i_thr], NULL, thr_dequeuer,
349 &count_dequeuer[2 * i_thr]);
453629a9
MD
350 if (err != 0)
351 exit(1);
352 }
353
5481ddb3 354 cmm_smp_mb();
453629a9 355
9e4e7ad1 356 begin_test();
453629a9 357
83e334d0 358 for (i_thr = 0; i_thr < duration; i_thr++) {
453629a9 359 sleep(1);
4b665350
MD
360 if (verbose_mode) {
361 fwrite(".", sizeof(char), 1, stdout);
362 fflush(stdout);
363 }
453629a9
MD
364 }
365
9e4e7ad1 366 end_test();
453629a9 367
83e334d0
MJ
368 for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) {
369 err = pthread_join(tid_enqueuer[i_thr], &tret);
453629a9
MD
370 if (err != 0)
371 exit(1);
83e334d0
MJ
372 tot_enqueues += count_enqueuer[2 * i_thr];
373 tot_successful_enqueues += count_enqueuer[2 * i_thr + 1];
453629a9 374 }
83e334d0
MJ
375 for (i_thr = 0; i_thr < nr_dequeuers; i_thr++) {
376 err = pthread_join(tid_dequeuer[i_thr], &tret);
453629a9
MD
377 if (err != 0)
378 exit(1);
83e334d0
MJ
379 tot_dequeues += count_dequeuer[2 * i_thr];
380 tot_successful_dequeues += count_dequeuer[2 * i_thr + 1];
453629a9 381 }
83e334d0 382
bd23a6c6 383 test_end(&end_dequeues);
e17d9985 384 err = cds_lfq_destroy_rcu(&q);
01477510 385 urcu_posix_assert(!err);
453629a9
MD
386
387 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
388 tot_enqueues, tot_dequeues);
389 printf_verbose("total number of successful enqueues : %llu, "
390 "successful dequeues %llu\n",
391 tot_successful_enqueues, tot_successful_dequeues);
392 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
393 "nr_dequeuers %3u "
394 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
395 "successful enqueues %12llu successful dequeues %12llu "
396 "end_dequeues %llu nr_ops %12llu\n",
397 argv[0], duration, nr_enqueuers, wdelay,
398 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
399 tot_successful_enqueues,
400 tot_successful_dequeues, end_dequeues,
401 tot_enqueues + tot_dequeues);
402 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
403 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
404 "succ. dequeues + end dequeues %llu.\n",
405 tot_successful_enqueues,
406 tot_successful_dequeues + end_dequeues);
407
0938c541 408 free_all_cpu_call_rcu_data();
453629a9
MD
409 free(count_enqueuer);
410 free(count_dequeuer);
411 free(tid_enqueuer);
412 free(tid_dequeuer);
83e334d0 413
453629a9
MD
414 return 0;
415}
This page took 0.080234 seconds and 4 git commands to generate.