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