Fix: tests: invoke destroy APIs for queues/stacks
[urcu.git] / tests / benchmark / test_urcu_wfcq.c
1 /*
2 * test_urcu_wfcq.c
3 *
4 * Userspace RCU library - example RCU-based lock-free concurrent queue
5 *
6 * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright February 2010 - Paolo Bonzini <pbonzini@redhat.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #define _GNU_SOURCE
25 #include "config.h"
26 #include <stdio.h>
27 #include <pthread.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <assert.h>
37 #include <errno.h>
38
39 #include <urcu/arch.h>
40 #include <urcu/tls-compat.h>
41 #include <urcu/uatomic.h>
42 #include "cpuset.h"
43 #include "thread-id.h"
44
45 /* hardcoded number of CPUs */
46 #define NR_CPUS 16384
47
48 #ifndef DYNAMIC_LINK_TEST
49 #define _LGPL_SOURCE
50 #endif
51 #include <urcu/wfcqueue.h>
52
53 enum test_sync {
54 TEST_SYNC_NONE = 0,
55 TEST_SYNC_MUTEX,
56 };
57
58 static enum test_sync test_sync;
59
60 static int test_force_sync;
61
62 static volatile int test_go, test_stop_enqueue, test_stop_dequeue;
63
64 static unsigned long rduration;
65
66 static unsigned long duration;
67
68 /* read-side C.S. duration, in loops */
69 static unsigned long wdelay;
70
71 static inline void loop_sleep(unsigned long loops)
72 {
73 while (loops-- != 0)
74 caa_cpu_relax();
75 }
76
77 static int verbose_mode;
78
79 static int test_dequeue, test_splice, test_wait_empty;
80 static int test_enqueue_stopped;
81
82 #define printf_verbose(fmt, args...) \
83 do { \
84 if (verbose_mode) \
85 printf(fmt, ## args); \
86 } while (0)
87
88 static unsigned int cpu_affinities[NR_CPUS];
89 static unsigned int next_aff = 0;
90 static int use_affinity = 0;
91
92 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
93
94 static void set_affinity(void)
95 {
96 #if HAVE_SCHED_SETAFFINITY
97 cpu_set_t mask;
98 int cpu, ret;
99 #endif /* HAVE_SCHED_SETAFFINITY */
100
101 if (!use_affinity)
102 return;
103
104 #if HAVE_SCHED_SETAFFINITY
105 ret = pthread_mutex_lock(&affinity_mutex);
106 if (ret) {
107 perror("Error in pthread mutex lock");
108 exit(-1);
109 }
110 cpu = cpu_affinities[next_aff++];
111 ret = pthread_mutex_unlock(&affinity_mutex);
112 if (ret) {
113 perror("Error in pthread mutex unlock");
114 exit(-1);
115 }
116
117 CPU_ZERO(&mask);
118 CPU_SET(cpu, &mask);
119 #if SCHED_SETAFFINITY_ARGS == 2
120 sched_setaffinity(0, &mask);
121 #else
122 sched_setaffinity(0, sizeof(mask), &mask);
123 #endif
124 #endif /* HAVE_SCHED_SETAFFINITY */
125 }
126
127 /*
128 * returns 0 if test should end.
129 */
130 static int test_duration_dequeue(void)
131 {
132 return !test_stop_dequeue;
133 }
134
135 static int test_duration_enqueue(void)
136 {
137 return !test_stop_enqueue;
138 }
139
140 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
141 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
142
143 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
144 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
145 static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues);
146 static DEFINE_URCU_TLS(unsigned long long, nr_splice);
147 static DEFINE_URCU_TLS(unsigned long long, nr_dequeue_last);
148
149 static unsigned int nr_enqueuers;
150 static unsigned int nr_dequeuers;
151
152 static struct cds_wfcq_head __attribute__((aligned(CAA_CACHE_LINE_SIZE))) head;
153 static struct cds_wfcq_tail __attribute__((aligned(CAA_CACHE_LINE_SIZE))) tail;
154
155 static void *thr_enqueuer(void *_count)
156 {
157 unsigned long long *count = _count;
158 bool was_nonempty;
159
160 printf_verbose("thread_begin %s, tid %lu\n",
161 "enqueuer", urcu_get_thread_id());
162
163 set_affinity();
164
165 while (!test_go)
166 {
167 }
168 cmm_smp_mb();
169
170 for (;;) {
171 struct cds_wfcq_node *node = malloc(sizeof(*node));
172 if (!node)
173 goto fail;
174 cds_wfcq_node_init(node);
175 was_nonempty = cds_wfcq_enqueue(&head, &tail, node);
176 URCU_TLS(nr_successful_enqueues)++;
177 if (!was_nonempty)
178 URCU_TLS(nr_empty_dest_enqueues)++;
179
180 if (caa_unlikely(wdelay))
181 loop_sleep(wdelay);
182 fail:
183 URCU_TLS(nr_enqueues)++;
184 if (caa_unlikely(!test_duration_enqueue()))
185 break;
186 }
187
188 uatomic_inc(&test_enqueue_stopped);
189 count[0] = URCU_TLS(nr_enqueues);
190 count[1] = URCU_TLS(nr_successful_enqueues);
191 count[2] = URCU_TLS(nr_empty_dest_enqueues);
192 printf_verbose("enqueuer thread_end, tid %lu, "
193 "enqueues %llu successful_enqueues %llu, "
194 "empty_dest_enqueues %llu\n",
195 urcu_get_thread_id(),
196 URCU_TLS(nr_enqueues),
197 URCU_TLS(nr_successful_enqueues),
198 URCU_TLS(nr_empty_dest_enqueues));
199 return ((void*)1);
200
201 }
202
203 static void do_test_dequeue(enum test_sync sync)
204 {
205 struct cds_wfcq_node *node;
206 int state;
207
208 if (sync == TEST_SYNC_MUTEX)
209 node = cds_wfcq_dequeue_with_state_blocking(&head, &tail,
210 &state);
211 else
212 node = __cds_wfcq_dequeue_with_state_blocking(&head, &tail,
213 &state);
214
215 if (state & CDS_WFCQ_STATE_LAST)
216 URCU_TLS(nr_dequeue_last)++;
217
218 if (node) {
219 free(node);
220 URCU_TLS(nr_successful_dequeues)++;
221 }
222 URCU_TLS(nr_dequeues)++;
223 }
224
225 static void do_test_splice(enum test_sync sync)
226 {
227 struct cds_wfcq_head tmp_head;
228 struct cds_wfcq_tail tmp_tail;
229 struct cds_wfcq_node *node, *n;
230 enum cds_wfcq_ret ret;
231
232 cds_wfcq_init(&tmp_head, &tmp_tail);
233
234 if (sync == TEST_SYNC_MUTEX)
235 ret = cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
236 &head, &tail);
237 else
238 ret = __cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
239 &head, &tail);
240
241 switch (ret) {
242 case CDS_WFCQ_RET_WOULDBLOCK:
243 assert(0); /* blocking call */
244 break;
245 case CDS_WFCQ_RET_DEST_EMPTY:
246 URCU_TLS(nr_splice)++;
247 URCU_TLS(nr_dequeue_last)++;
248 /* ok */
249 break;
250 case CDS_WFCQ_RET_DEST_NON_EMPTY:
251 assert(0); /* entirely unexpected */
252 break;
253 case CDS_WFCQ_RET_SRC_EMPTY:
254 /* ok, we could even skip iteration on dest if we wanted */
255 break;
256 }
257
258 __cds_wfcq_for_each_blocking_safe(&tmp_head, &tmp_tail, node, n) {
259 free(node);
260 URCU_TLS(nr_successful_dequeues)++;
261 URCU_TLS(nr_dequeues)++;
262 }
263 cds_wfcq_destroy(&tmp_head, &tmp_tail);
264 }
265
266 static void *thr_dequeuer(void *_count)
267 {
268 unsigned long long *count = _count;
269 unsigned int counter = 0;
270
271 printf_verbose("thread_begin %s, tid %lu\n",
272 "dequeuer", urcu_get_thread_id());
273
274 set_affinity();
275
276 while (!test_go)
277 {
278 }
279 cmm_smp_mb();
280
281 for (;;) {
282 if (test_dequeue && test_splice) {
283 if (counter & 1)
284 do_test_dequeue(test_sync);
285 else
286 do_test_splice(test_sync);
287 counter++;
288 } else {
289 if (test_dequeue)
290 do_test_dequeue(test_sync);
291 else
292 do_test_splice(test_sync);
293 }
294 if (caa_unlikely(!test_duration_dequeue()))
295 break;
296 if (caa_unlikely(rduration))
297 loop_sleep(rduration);
298 }
299
300 printf_verbose("dequeuer thread_end, tid %lu, "
301 "dequeues %llu, successful_dequeues %llu, "
302 "nr_splice %llu\n",
303 urcu_get_thread_id(),
304 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues),
305 URCU_TLS(nr_splice));
306 count[0] = URCU_TLS(nr_dequeues);
307 count[1] = URCU_TLS(nr_successful_dequeues);
308 count[2] = URCU_TLS(nr_splice);
309 count[3] = URCU_TLS(nr_dequeue_last);
310 return ((void*)2);
311 }
312
313 static void test_end(unsigned long long *nr_dequeues,
314 unsigned long long *nr_dequeue_last)
315 {
316 struct cds_wfcq_node *node;
317 int state;
318
319 do {
320 node = cds_wfcq_dequeue_with_state_blocking(&head, &tail,
321 &state);
322 if (node) {
323 if (state & CDS_WFCQ_STATE_LAST)
324 (*nr_dequeue_last)++;
325 free(node);
326 (*nr_dequeues)++;
327 }
328 } while (node);
329 }
330
331 static void show_usage(int argc, char **argv)
332 {
333 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
334 argv[0]);
335 printf("OPTIONS:\n");
336 printf(" [-d delay] (enqueuer period (in loops))\n");
337 printf(" [-c duration] (dequeuer period (in loops))\n");
338 printf(" [-v] (verbose output)\n");
339 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
340 printf(" [-q] (test dequeue)\n");
341 printf(" [-s] (test splice, enabled by default)\n");
342 printf(" [-M] (use mutex external synchronization)\n");
343 printf(" Note: default: no external synchronization used.\n");
344 printf(" [-f] (force user-provided synchronization)\n");
345 printf(" [-w] Wait for dequeuer to empty queue\n");
346 printf("\n");
347 }
348
349 int main(int argc, char **argv)
350 {
351 int err;
352 pthread_t *tid_enqueuer, *tid_dequeuer;
353 void *tret;
354 unsigned long long *count_enqueuer, *count_dequeuer;
355 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
356 unsigned long long tot_successful_enqueues = 0,
357 tot_successful_dequeues = 0,
358 tot_empty_dest_enqueues = 0,
359 tot_splice = 0, tot_dequeue_last = 0;
360 unsigned long long end_dequeues = 0;
361 int i, a, retval = 0;
362
363 if (argc < 4) {
364 show_usage(argc, argv);
365 return -1;
366 }
367
368 err = sscanf(argv[1], "%u", &nr_dequeuers);
369 if (err != 1) {
370 show_usage(argc, argv);
371 return -1;
372 }
373
374 err = sscanf(argv[2], "%u", &nr_enqueuers);
375 if (err != 1) {
376 show_usage(argc, argv);
377 return -1;
378 }
379
380 err = sscanf(argv[3], "%lu", &duration);
381 if (err != 1) {
382 show_usage(argc, argv);
383 return -1;
384 }
385
386 for (i = 4; i < argc; i++) {
387 if (argv[i][0] != '-')
388 continue;
389 switch (argv[i][1]) {
390 case 'a':
391 if (argc < i + 2) {
392 show_usage(argc, argv);
393 return -1;
394 }
395 a = atoi(argv[++i]);
396 cpu_affinities[next_aff++] = a;
397 use_affinity = 1;
398 printf_verbose("Adding CPU %d affinity\n", a);
399 break;
400 case 'c':
401 if (argc < i + 2) {
402 show_usage(argc, argv);
403 return -1;
404 }
405 rduration = atol(argv[++i]);
406 break;
407 case 'd':
408 if (argc < i + 2) {
409 show_usage(argc, argv);
410 return -1;
411 }
412 wdelay = atol(argv[++i]);
413 break;
414 case 'v':
415 verbose_mode = 1;
416 break;
417 case 'q':
418 test_dequeue = 1;
419 break;
420 case 's':
421 test_splice = 1;
422 break;
423 case 'M':
424 test_sync = TEST_SYNC_MUTEX;
425 break;
426 case 'w':
427 test_wait_empty = 1;
428 break;
429 case 'f':
430 test_force_sync = 1;
431 break;
432 }
433 }
434
435 /* activate splice test by default */
436 if (!test_dequeue && !test_splice)
437 test_splice = 1;
438
439 if (test_sync == TEST_SYNC_NONE && nr_dequeuers > 1 && test_dequeue) {
440 if (test_force_sync) {
441 fprintf(stderr, "[WARNING] Using dequeue concurrently "
442 "with other dequeue or splice without external "
443 "synchronization. Expect run-time failure.\n");
444 } else {
445 printf("Enforcing mutex synchronization\n");
446 test_sync = TEST_SYNC_MUTEX;
447 }
448 }
449
450 printf_verbose("running test for %lu seconds, %u enqueuers, "
451 "%u dequeuers.\n",
452 duration, nr_enqueuers, nr_dequeuers);
453 if (test_dequeue)
454 printf_verbose("dequeue test activated.\n");
455 else
456 printf_verbose("splice test activated.\n");
457 if (test_sync == TEST_SYNC_MUTEX)
458 printf_verbose("External sync: mutex.\n");
459 else
460 printf_verbose("External sync: none.\n");
461 if (test_wait_empty)
462 printf_verbose("Wait for dequeuers to empty queue.\n");
463 printf_verbose("Writer delay : %lu loops.\n", rduration);
464 printf_verbose("Reader duration : %lu loops.\n", wdelay);
465 printf_verbose("thread %-6s, tid %lu\n",
466 "main", urcu_get_thread_id());
467
468 tid_enqueuer = calloc(nr_enqueuers, sizeof(*tid_enqueuer));
469 tid_dequeuer = calloc(nr_dequeuers, sizeof(*tid_dequeuer));
470 count_enqueuer = calloc(nr_enqueuers, 3 * sizeof(*count_enqueuer));
471 count_dequeuer = calloc(nr_dequeuers, 4 * sizeof(*count_dequeuer));
472 cds_wfcq_init(&head, &tail);
473
474 next_aff = 0;
475
476 for (i = 0; i < nr_enqueuers; i++) {
477 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
478 &count_enqueuer[3 * i]);
479 if (err != 0)
480 exit(1);
481 }
482 for (i = 0; i < nr_dequeuers; i++) {
483 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
484 &count_dequeuer[4 * i]);
485 if (err != 0)
486 exit(1);
487 }
488
489 cmm_smp_mb();
490
491 test_go = 1;
492
493 for (i = 0; i < duration; i++) {
494 sleep(1);
495 if (verbose_mode) {
496 fwrite(".", sizeof(char), 1, stdout);
497 fflush(stdout);
498 }
499 }
500
501 test_stop_enqueue = 1;
502
503 if (test_wait_empty) {
504 while (nr_enqueuers != uatomic_read(&test_enqueue_stopped)) {
505 sleep(1);
506 }
507 while (!cds_wfcq_empty(&head, &tail)) {
508 sleep(1);
509 }
510 }
511
512 test_stop_dequeue = 1;
513
514 for (i = 0; i < nr_enqueuers; i++) {
515 err = pthread_join(tid_enqueuer[i], &tret);
516 if (err != 0)
517 exit(1);
518 tot_enqueues += count_enqueuer[3 * i];
519 tot_successful_enqueues += count_enqueuer[3 * i + 1];
520 tot_empty_dest_enqueues += count_enqueuer[3 * i + 2];
521 }
522 for (i = 0; i < nr_dequeuers; i++) {
523 err = pthread_join(tid_dequeuer[i], &tret);
524 if (err != 0)
525 exit(1);
526 tot_dequeues += count_dequeuer[4 * i];
527 tot_successful_dequeues += count_dequeuer[4 * i + 1];
528 tot_splice += count_dequeuer[4 * i + 2];
529 tot_dequeue_last += count_dequeuer[4 * i + 3];
530 }
531
532 test_end(&end_dequeues, &tot_dequeue_last);
533
534 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
535 tot_enqueues, tot_dequeues);
536 printf_verbose("total number of successful enqueues : %llu, "
537 "enqueues to empty dest : %llu, "
538 "successful dequeues %llu, "
539 "splice : %llu, dequeue_last : %llu\n",
540 tot_successful_enqueues,
541 tot_empty_dest_enqueues,
542 tot_successful_dequeues,
543 tot_splice, tot_dequeue_last);
544 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
545 "nr_dequeuers %3u "
546 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
547 "successful enqueues %12llu enqueues to empty dest %12llu "
548 "successful dequeues %12llu splice %12llu "
549 "dequeue_last %llu "
550 "end_dequeues %llu nr_ops %12llu\n",
551 argv[0], duration, nr_enqueuers, wdelay,
552 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
553 tot_successful_enqueues,
554 tot_empty_dest_enqueues,
555 tot_successful_dequeues, tot_splice, tot_dequeue_last,
556 end_dequeues,
557 tot_enqueues + tot_dequeues);
558
559 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) {
560 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
561 "succ. dequeues + end dequeues %llu.\n",
562 tot_successful_enqueues,
563 tot_successful_dequeues + end_dequeues);
564 retval = 1;
565 }
566
567 /*
568 * If only using splice to dequeue, the enqueuer should see
569 * exactly as many empty queues than the number of non-empty
570 * src splice.
571 */
572 if (tot_empty_dest_enqueues != tot_dequeue_last) {
573 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
574 "number of dequeue of last element (%llu)\n",
575 tot_empty_dest_enqueues,
576 tot_dequeue_last);
577 retval = 1;
578 }
579 cds_wfcq_destroy(&head, &tail);
580 free(count_enqueuer);
581 free(count_dequeuer);
582 free(tid_enqueuer);
583 free(tid_dequeuer);
584 return retval;
585 }
This page took 0.062424 seconds and 4 git commands to generate.