1 // SPDX-FileCopyrightText: 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 // SPDX-FileCopyrightText: 2010 Paolo Bonzini <pbonzini@redhat.com>
4 // SPDX-License-Identifier: GPL-2.0-or-later
7 * Userspace RCU library - example RCU-based lock-free queue
16 #include <sys/types.h>
22 #include <urcu/arch.h>
23 #include <urcu/assert.h>
24 #include <urcu/tls-compat.h>
25 #include "thread-id.h"
27 /* hardcoded number of CPUs */
30 #ifndef DYNAMIC_LINK_TEST
36 static unsigned long rduration
;
38 static unsigned long duration
;
40 /* read-side C.S. duration, in loops */
41 static unsigned long wdelay
;
43 static inline void loop_sleep(unsigned long loops
)
49 static int verbose_mode
;
51 #define printf_verbose(fmt, args...) \
57 static unsigned int cpu_affinities
[NR_CPUS
];
58 static unsigned int next_aff
= 0;
59 static int use_affinity
= 0;
61 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
63 static void set_affinity(void)
65 #ifdef HAVE_SCHED_SETAFFINITY
68 #endif /* HAVE_SCHED_SETAFFINITY */
73 #ifdef HAVE_SCHED_SETAFFINITY
74 ret
= pthread_mutex_lock(&affinity_mutex
);
76 perror("Error in pthread mutex lock");
79 cpu
= cpu_affinities
[next_aff
++];
80 ret
= pthread_mutex_unlock(&affinity_mutex
);
82 perror("Error in pthread mutex unlock");
88 sched_setaffinity(0, sizeof(mask
), &mask
);
89 #endif /* HAVE_SCHED_SETAFFINITY */
93 * returns 0 if test should end.
95 static int test_duration_dequeue(void)
97 return test_duration_read();
100 static int test_duration_enqueue(void)
102 return test_duration_write();
105 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues
);
106 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues
);
108 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues
);
109 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues
);
111 static unsigned int nr_enqueuers
;
112 static unsigned int nr_dequeuers
;
115 struct cds_lfq_node_rcu list
;
119 static struct cds_lfq_queue_rcu q
;
122 void *thr_enqueuer(void *_count
)
124 unsigned long long *count
= _count
;
126 printf_verbose("thread_begin %s, tid %lu\n",
127 "enqueuer", urcu_get_thread_id());
131 rcu_register_thread();
136 struct test
*node
= malloc(sizeof(*node
));
139 cds_lfq_node_init_rcu(&node
->list
);
141 cds_lfq_enqueue_rcu(&q
, &node
->list
);
143 URCU_TLS(nr_successful_enqueues
)++;
145 if (caa_unlikely(wdelay
))
148 URCU_TLS(nr_enqueues
)++;
149 if (caa_unlikely(!test_duration_enqueue()))
153 rcu_unregister_thread();
155 count
[0] = URCU_TLS(nr_enqueues
);
156 count
[1] = URCU_TLS(nr_successful_enqueues
);
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
));
167 void free_node_cb(struct rcu_head
*head
)
170 caa_container_of(head
, struct test
, rcu
);
175 void *thr_dequeuer(void *_count
)
177 unsigned long long *count
= _count
;
179 printf_verbose("thread_begin %s, tid %lu\n",
180 "dequeuer", urcu_get_thread_id());
184 rcu_register_thread();
189 struct cds_lfq_node_rcu
*qnode
;
192 qnode
= cds_lfq_dequeue_rcu(&q
);
198 node
= caa_container_of(qnode
, struct test
, list
);
199 call_rcu(&node
->rcu
, free_node_cb
);
200 URCU_TLS(nr_successful_dequeues
)++;
203 URCU_TLS(nr_dequeues
)++;
204 if (caa_unlikely(!test_duration_dequeue()))
206 if (caa_unlikely(rduration
))
207 loop_sleep(rduration
);
210 rcu_unregister_thread();
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
));
216 count
[0] = URCU_TLS(nr_dequeues
);
217 count
[1] = URCU_TLS(nr_successful_dequeues
);
222 void test_end(unsigned long long *nr_dequeues_l
)
224 struct cds_lfq_node_rcu
*snode
;
227 snode
= cds_lfq_dequeue_rcu(&q
);
231 node
= caa_container_of(snode
, struct test
, list
);
232 free(node
); /* no more concurrent access */
239 void show_usage(char **argv
)
241 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
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");
251 int main(int argc
, char **argv
)
254 pthread_t
*tid_enqueuer
, *tid_dequeuer
;
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;
269 err
= sscanf(argv
[1], "%u", &nr_dequeuers
);
275 err
= sscanf(argv
[2], "%u", &nr_enqueuers
);
281 err
= sscanf(argv
[3], "%lu", &duration
);
287 for (i
= 4; i
< argc
; i
++) {
288 if (argv
[i
][0] != '-')
290 switch (argv
[i
][1]) {
297 cpu_affinities
[next_aff
++] = a
;
299 printf_verbose("Adding CPU %d affinity\n", a
);
306 rduration
= atol(argv
[++i
]);
313 wdelay
= atol(argv
[++i
]);
321 printf_verbose("running test for %lu seconds, %u enqueuers, "
323 duration
, nr_enqueuers
, nr_dequeuers
);
324 printf_verbose("Writer delay : %lu loops.\n", rduration
);
325 printf_verbose("Reader duration : %lu loops.\n", wdelay
);
326 printf_verbose("thread %-6s, tid %lu\n",
327 "main", urcu_get_thread_id());
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
));
333 cds_lfq_init_rcu(&q
, call_rcu
);
334 err
= create_all_cpu_call_rcu_data(0);
336 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
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
]);
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
]);
358 for (i_thr
= 0; i_thr
< duration
; i_thr
++) {
361 fwrite(".", sizeof(char), 1, stdout
);
368 for (i_thr
= 0; i_thr
< nr_enqueuers
; i_thr
++) {
369 err
= pthread_join(tid_enqueuer
[i_thr
], &tret
);
372 tot_enqueues
+= count_enqueuer
[2 * i_thr
];
373 tot_successful_enqueues
+= count_enqueuer
[2 * i_thr
+ 1];
375 for (i_thr
= 0; i_thr
< nr_dequeuers
; i_thr
++) {
376 err
= pthread_join(tid_dequeuer
[i_thr
], &tret
);
379 tot_dequeues
+= count_dequeuer
[2 * i_thr
];
380 tot_successful_dequeues
+= count_dequeuer
[2 * i_thr
+ 1];
383 test_end(&end_dequeues
);
384 err
= cds_lfq_destroy_rcu(&q
);
385 urcu_posix_assert(!err
);
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 "
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
);
408 free_all_cpu_call_rcu_data();
409 free(count_enqueuer
);
410 free(count_dequeuer
);