cb1cc23ed91aa575ea9667a4594ec9269dc3b1b6
[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) <OPTIONS>\n",
355 argv[0]);
356 printf("OPTIONS:\n");
357 printf(" [-d delay] (enqueuer period (in loops))\n");
358 printf(" [-c duration] (dequeuer period (in loops))\n");
359 printf(" [-v] (verbose output)\n");
360 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
361 printf(" [-q] (test dequeue)\n");
362 printf(" [-s] (test splice, enabled by default)\n");
363 printf(" [-M] (use mutex external synchronization)\n");
364 printf(" Note: default: no external synchronization used.\n");
365 printf(" [-f] (force user-provided synchronization)\n");
366 printf(" [-w] Wait for dequeuer to empty queue\n");
367 printf("\n");
368 }
369
370 int main(int argc, char **argv)
371 {
372 int err;
373 pthread_t *tid_enqueuer, *tid_dequeuer;
374 void *tret;
375 unsigned long long *count_enqueuer, *count_dequeuer;
376 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
377 unsigned long long tot_successful_enqueues = 0,
378 tot_successful_dequeues = 0,
379 tot_empty_dest_enqueues = 0,
380 tot_splice = 0, tot_dequeue_last = 0;
381 unsigned long long end_dequeues = 0;
382 int i, a, retval = 0;
383
384 if (argc < 4) {
385 show_usage(argc, argv);
386 return -1;
387 }
388
389 err = sscanf(argv[1], "%u", &nr_dequeuers);
390 if (err != 1) {
391 show_usage(argc, argv);
392 return -1;
393 }
394
395 err = sscanf(argv[2], "%u", &nr_enqueuers);
396 if (err != 1) {
397 show_usage(argc, argv);
398 return -1;
399 }
400
401 err = sscanf(argv[3], "%lu", &duration);
402 if (err != 1) {
403 show_usage(argc, argv);
404 return -1;
405 }
406
407 for (i = 4; i < argc; i++) {
408 if (argv[i][0] != '-')
409 continue;
410 switch (argv[i][1]) {
411 case 'a':
412 if (argc < i + 2) {
413 show_usage(argc, argv);
414 return -1;
415 }
416 a = atoi(argv[++i]);
417 cpu_affinities[next_aff++] = a;
418 use_affinity = 1;
419 printf_verbose("Adding CPU %d affinity\n", a);
420 break;
421 case 'c':
422 if (argc < i + 2) {
423 show_usage(argc, argv);
424 return -1;
425 }
426 rduration = atol(argv[++i]);
427 break;
428 case 'd':
429 if (argc < i + 2) {
430 show_usage(argc, argv);
431 return -1;
432 }
433 wdelay = atol(argv[++i]);
434 break;
435 case 'v':
436 verbose_mode = 1;
437 break;
438 case 'q':
439 test_dequeue = 1;
440 break;
441 case 's':
442 test_splice = 1;
443 break;
444 case 'M':
445 test_sync = TEST_SYNC_MUTEX;
446 break;
447 case 'w':
448 test_wait_empty = 1;
449 break;
450 case 'f':
451 test_force_sync = 1;
452 break;
453 }
454 }
455
456 /* activate splice test by default */
457 if (!test_dequeue && !test_splice)
458 test_splice = 1;
459
460 if (test_sync == TEST_SYNC_NONE && nr_dequeuers > 1 && test_dequeue) {
461 if (test_force_sync) {
462 fprintf(stderr, "[WARNING] Using dequeue concurrently "
463 "with other dequeue or splice without external "
464 "synchronization. Expect run-time failure.\n");
465 } else {
466 printf("Enforcing mutex synchronization\n");
467 test_sync = TEST_SYNC_MUTEX;
468 }
469 }
470
471 printf_verbose("running test for %lu seconds, %u enqueuers, "
472 "%u dequeuers.\n",
473 duration, nr_enqueuers, nr_dequeuers);
474 if (test_dequeue)
475 printf_verbose("dequeue test activated.\n");
476 else
477 printf_verbose("splice test activated.\n");
478 if (test_sync == TEST_SYNC_MUTEX)
479 printf_verbose("External sync: mutex.\n");
480 else
481 printf_verbose("External sync: none.\n");
482 if (test_wait_empty)
483 printf_verbose("Wait for dequeuers to empty queue.\n");
484 printf_verbose("Writer delay : %lu loops.\n", rduration);
485 printf_verbose("Reader duration : %lu loops.\n", wdelay);
486 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
487 "main", (unsigned long) pthread_self(),
488 (unsigned long) gettid());
489
490 tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
491 tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
492 count_enqueuer = malloc(3 * sizeof(*count_enqueuer) * nr_enqueuers);
493 count_dequeuer = malloc(4 * sizeof(*count_dequeuer) * nr_dequeuers);
494 cds_wfcq_init(&head, &tail);
495
496 next_aff = 0;
497
498 for (i = 0; i < nr_enqueuers; i++) {
499 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
500 &count_enqueuer[3 * i]);
501 if (err != 0)
502 exit(1);
503 }
504 for (i = 0; i < nr_dequeuers; i++) {
505 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
506 &count_dequeuer[4 * i]);
507 if (err != 0)
508 exit(1);
509 }
510
511 cmm_smp_mb();
512
513 test_go = 1;
514
515 for (i = 0; i < duration; i++) {
516 sleep(1);
517 if (verbose_mode)
518 write (1, ".", 1);
519 }
520
521 test_stop_enqueue = 1;
522
523 if (test_wait_empty) {
524 while (nr_enqueuers != uatomic_read(&test_enqueue_stopped)) {
525 sleep(1);
526 }
527 while (!cds_wfcq_empty(&head, &tail)) {
528 sleep(1);
529 }
530 }
531
532 test_stop_dequeue = 1;
533
534 for (i = 0; i < nr_enqueuers; i++) {
535 err = pthread_join(tid_enqueuer[i], &tret);
536 if (err != 0)
537 exit(1);
538 tot_enqueues += count_enqueuer[3 * i];
539 tot_successful_enqueues += count_enqueuer[3 * i + 1];
540 tot_empty_dest_enqueues += count_enqueuer[3 * i + 2];
541 }
542 for (i = 0; i < nr_dequeuers; i++) {
543 err = pthread_join(tid_dequeuer[i], &tret);
544 if (err != 0)
545 exit(1);
546 tot_dequeues += count_dequeuer[4 * i];
547 tot_successful_dequeues += count_dequeuer[4 * i + 1];
548 tot_splice += count_dequeuer[4 * i + 2];
549 tot_dequeue_last += count_dequeuer[4 * i + 3];
550 }
551
552 test_end(&end_dequeues, &tot_dequeue_last);
553
554 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
555 tot_enqueues, tot_dequeues);
556 printf_verbose("total number of successful enqueues : %llu, "
557 "enqueues to empty dest : %llu, "
558 "successful dequeues %llu, "
559 "splice : %llu, dequeue_last : %llu\n",
560 tot_successful_enqueues,
561 tot_empty_dest_enqueues,
562 tot_successful_dequeues,
563 tot_splice, tot_dequeue_last);
564 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
565 "nr_dequeuers %3u "
566 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
567 "successful enqueues %12llu enqueues to empty dest %12llu "
568 "successful dequeues %12llu splice %12llu "
569 "dequeue_last %llu "
570 "end_dequeues %llu nr_ops %12llu\n",
571 argv[0], duration, nr_enqueuers, wdelay,
572 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
573 tot_successful_enqueues,
574 tot_empty_dest_enqueues,
575 tot_successful_dequeues, tot_splice, tot_dequeue_last,
576 end_dequeues,
577 tot_enqueues + tot_dequeues);
578
579 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) {
580 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
581 "succ. dequeues + end dequeues %llu.\n",
582 tot_successful_enqueues,
583 tot_successful_dequeues + end_dequeues);
584 retval = 1;
585 }
586
587 /*
588 * If only using splice to dequeue, the enqueuer should see
589 * exactly as many empty queues than the number of non-empty
590 * src splice.
591 */
592 if (tot_empty_dest_enqueues != tot_dequeue_last) {
593 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
594 "number of dequeue of last element (%llu)\n",
595 tot_empty_dest_enqueues,
596 tot_dequeue_last);
597 retval = 1;
598 }
599 free(count_enqueuer);
600 free(count_dequeuer);
601 free(tid_enqueuer);
602 free(tid_dequeuer);
603 return retval;
604 }
This page took 0.044038 seconds and 3 git commands to generate.