tests/unit: Remove urcu-signal unit tests
[userspace-rcu.git] / tests / benchmark / test_urcu_lfq.c
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
6 /*
7 * Userspace RCU library - example RCU-based lock-free queue
8 */
9
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>
20 #include <errno.h>
21
22 #include <urcu/arch.h>
23 #include <urcu/assert.h>
24 #include <urcu/tls-compat.h>
25 #include "thread-id.h"
26
27 /* hardcoded number of CPUs */
28 #define NR_CPUS 16384
29
30 #ifndef DYNAMIC_LINK_TEST
31 #define _LGPL_SOURCE
32 #endif
33 #include <urcu.h>
34 #include <urcu/cds.h>
35
36 static unsigned long rduration;
37
38 static unsigned long duration;
39
40 /* read-side C.S. duration, in loops */
41 static unsigned long wdelay;
42
43 static inline void loop_sleep(unsigned long loops)
44 {
45 while (loops-- != 0)
46 caa_cpu_relax();
47 }
48
49 static int verbose_mode;
50
51 #define printf_verbose(fmt, args...) \
52 do { \
53 if (verbose_mode) \
54 printf(fmt, args); \
55 } while (0)
56
57 static unsigned int cpu_affinities[NR_CPUS];
58 static unsigned int next_aff = 0;
59 static int use_affinity = 0;
60
61 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
62
63 static void set_affinity(void)
64 {
65 #ifdef HAVE_SCHED_SETAFFINITY
66 cpu_set_t mask;
67 int cpu, ret;
68 #endif /* HAVE_SCHED_SETAFFINITY */
69
70 if (!use_affinity)
71 return;
72
73 #ifdef HAVE_SCHED_SETAFFINITY
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);
88 sched_setaffinity(0, sizeof(mask), &mask);
89 #endif /* HAVE_SCHED_SETAFFINITY */
90 }
91
92 /*
93 * returns 0 if test should end.
94 */
95 static int test_duration_dequeue(void)
96 {
97 return test_duration_read();
98 }
99
100 static int test_duration_enqueue(void)
101 {
102 return test_duration_write();
103 }
104
105 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
106 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
107
108 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
109 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
110
111 static unsigned int nr_enqueuers;
112 static unsigned int nr_dequeuers;
113
114 struct test {
115 struct cds_lfq_node_rcu list;
116 struct rcu_head rcu;
117 };
118
119 static struct cds_lfq_queue_rcu q;
120
121 static
122 void *thr_enqueuer(void *_count)
123 {
124 unsigned long long *count = _count;
125
126 printf_verbose("thread_begin %s, tid %lu\n",
127 "enqueuer", urcu_get_thread_id());
128
129 set_affinity();
130
131 rcu_register_thread();
132
133 wait_until_go();
134
135 for (;;) {
136 struct test *node = malloc(sizeof(*node));
137 if (!node)
138 goto fail;
139 cds_lfq_node_init_rcu(&node->list);
140 rcu_read_lock();
141 cds_lfq_enqueue_rcu(&q, &node->list);
142 rcu_read_unlock();
143 URCU_TLS(nr_successful_enqueues)++;
144
145 if (caa_unlikely(wdelay))
146 loop_sleep(wdelay);
147 fail:
148 URCU_TLS(nr_enqueues)++;
149 if (caa_unlikely(!test_duration_enqueue()))
150 break;
151 }
152
153 rcu_unregister_thread();
154
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));
162 return ((void*)1);
163
164 }
165
166 static
167 void 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
174 static
175 void *thr_dequeuer(void *_count)
176 {
177 unsigned long long *count = _count;
178
179 printf_verbose("thread_begin %s, tid %lu\n",
180 "dequeuer", urcu_get_thread_id());
181
182 set_affinity();
183
184 rcu_register_thread();
185
186 wait_until_go();
187
188 for (;;) {
189 struct cds_lfq_node_rcu *qnode;
190
191 rcu_read_lock();
192 qnode = cds_lfq_dequeue_rcu(&q);
193 rcu_read_unlock();
194
195 if (qnode) {
196 struct test *node;
197
198 node = caa_container_of(qnode, struct test, list);
199 call_rcu(&node->rcu, free_node_cb);
200 URCU_TLS(nr_successful_dequeues)++;
201 }
202
203 URCU_TLS(nr_dequeues)++;
204 if (caa_unlikely(!test_duration_dequeue()))
205 break;
206 if (caa_unlikely(rduration))
207 loop_sleep(rduration);
208 }
209
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);
218 return ((void*)2);
219 }
220
221 static
222 void test_end(unsigned long long *nr_dequeues_l)
223 {
224 struct cds_lfq_node_rcu *snode;
225
226 do {
227 snode = cds_lfq_dequeue_rcu(&q);
228 if (snode) {
229 struct test *node;
230
231 node = caa_container_of(snode, struct test, list);
232 free(node); /* no more concurrent access */
233 (*nr_dequeues_l)++;
234 }
235 } while (snode);
236 }
237
238 static
239 void show_usage(char **argv)
240 {
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");
248 printf("\n");
249 }
250
251 int 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;
262 unsigned int i_thr;
263
264 if (argc < 4) {
265 show_usage(argv);
266 return -1;
267 }
268
269 err = sscanf(argv[1], "%u", &nr_dequeuers);
270 if (err != 1) {
271 show_usage(argv);
272 return -1;
273 }
274
275 err = sscanf(argv[2], "%u", &nr_enqueuers);
276 if (err != 1) {
277 show_usage(argv);
278 return -1;
279 }
280
281 err = sscanf(argv[3], "%lu", &duration);
282 if (err != 1) {
283 show_usage(argv);
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) {
293 show_usage(argv);
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) {
303 show_usage(argv);
304 return -1;
305 }
306 rduration = atol(argv[++i]);
307 break;
308 case 'd':
309 if (argc < i + 2) {
310 show_usage(argv);
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);
326 printf_verbose("thread %-6s, tid %lu\n",
327 "main", urcu_get_thread_id());
328
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);
335 if (err) {
336 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
337 }
338
339 next_aff = 0;
340
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]);
344 if (err != 0)
345 exit(1);
346 }
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]);
350 if (err != 0)
351 exit(1);
352 }
353
354 cmm_smp_mb();
355
356 begin_test();
357
358 for (i_thr = 0; i_thr < duration; i_thr++) {
359 sleep(1);
360 if (verbose_mode) {
361 fwrite(".", sizeof(char), 1, stdout);
362 fflush(stdout);
363 }
364 }
365
366 end_test();
367
368 for (i_thr = 0; i_thr < nr_enqueuers; i_thr++) {
369 err = pthread_join(tid_enqueuer[i_thr], &tret);
370 if (err != 0)
371 exit(1);
372 tot_enqueues += count_enqueuer[2 * i_thr];
373 tot_successful_enqueues += count_enqueuer[2 * i_thr + 1];
374 }
375 for (i_thr = 0; i_thr < nr_dequeuers; i_thr++) {
376 err = pthread_join(tid_dequeuer[i_thr], &tret);
377 if (err != 0)
378 exit(1);
379 tot_dequeues += count_dequeuer[2 * i_thr];
380 tot_successful_dequeues += count_dequeuer[2 * i_thr + 1];
381 }
382
383 test_end(&end_dequeues);
384 err = cds_lfq_destroy_rcu(&q);
385 urcu_posix_assert(!err);
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
408 free_all_cpu_call_rcu_data();
409 free(count_enqueuer);
410 free(count_dequeuer);
411 free(tid_enqueuer);
412 free(tid_dequeuer);
413
414 return 0;
415 }
This page took 0.038442 seconds and 4 git commands to generate.