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