Fix tests: use of uninitialized variables
[userspace-rcu.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
166 static unsigned int nr_enqueuers;
167 static unsigned int nr_dequeuers;
168
169 static struct cds_wfcq_head __attribute__((aligned(CAA_CACHE_LINE_SIZE))) head;
170 static struct cds_wfcq_tail __attribute__((aligned(CAA_CACHE_LINE_SIZE))) tail;
171
172 static void *thr_enqueuer(void *_count)
173 {
174 unsigned long long *count = _count;
175 bool was_nonempty;
176
177 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
178 "enqueuer", (unsigned long) pthread_self(),
179 (unsigned long) gettid());
180
181 set_affinity();
182
183 while (!test_go)
184 {
185 }
186 cmm_smp_mb();
187
188 for (;;) {
189 struct cds_wfcq_node *node = malloc(sizeof(*node));
190 if (!node)
191 goto fail;
192 cds_wfcq_node_init(node);
193 was_nonempty = cds_wfcq_enqueue(&head, &tail, node);
194 URCU_TLS(nr_successful_enqueues)++;
195 if (!was_nonempty)
196 URCU_TLS(nr_empty_dest_enqueues)++;
197
198 if (caa_unlikely(wdelay))
199 loop_sleep(wdelay);
200 fail:
201 URCU_TLS(nr_enqueues)++;
202 if (caa_unlikely(!test_duration_enqueue()))
203 break;
204 }
205
206 uatomic_inc(&test_enqueue_stopped);
207 count[0] = URCU_TLS(nr_enqueues);
208 count[1] = URCU_TLS(nr_successful_enqueues);
209 count[2] = URCU_TLS(nr_empty_dest_enqueues);
210 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
211 "enqueues %llu successful_enqueues %llu, "
212 "empty_dest_enqueues %llu\n",
213 pthread_self(),
214 (unsigned long) gettid(),
215 URCU_TLS(nr_enqueues),
216 URCU_TLS(nr_successful_enqueues),
217 URCU_TLS(nr_empty_dest_enqueues));
218 return ((void*)1);
219
220 }
221
222 static void do_test_dequeue(enum test_sync sync)
223 {
224 struct cds_wfcq_node *node;
225
226 if (sync == TEST_SYNC_MUTEX)
227 node = cds_wfcq_dequeue_blocking(&head, &tail);
228 else
229 node = __cds_wfcq_dequeue_blocking(&head, &tail);
230
231 if (node) {
232 free(node);
233 URCU_TLS(nr_successful_dequeues)++;
234 }
235 URCU_TLS(nr_dequeues)++;
236 }
237
238 static void do_test_splice(enum test_sync sync)
239 {
240 struct cds_wfcq_head tmp_head;
241 struct cds_wfcq_tail tmp_tail;
242 struct cds_wfcq_node *node, *n;
243 enum cds_wfcq_ret ret;
244
245 cds_wfcq_init(&tmp_head, &tmp_tail);
246
247 if (sync == TEST_SYNC_MUTEX)
248 ret = cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
249 &head, &tail);
250 else
251 ret = __cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
252 &head, &tail);
253
254 switch (ret) {
255 case CDS_WFCQ_RET_WOULDBLOCK:
256 assert(0); /* blocking call */
257 break;
258 case CDS_WFCQ_RET_DEST_EMPTY:
259 URCU_TLS(nr_splice)++;
260 /* ok */
261 break;
262 case CDS_WFCQ_RET_DEST_NON_EMPTY:
263 assert(0); /* entirely unexpected */
264 break;
265 case CDS_WFCQ_RET_SRC_EMPTY:
266 /* ok, we could even skip iteration on dest if we wanted */
267 break;
268 }
269
270 __cds_wfcq_for_each_blocking_safe(&tmp_head, &tmp_tail, node, n) {
271 free(node);
272 URCU_TLS(nr_successful_dequeues)++;
273 URCU_TLS(nr_dequeues)++;
274 }
275 }
276
277 static void *thr_dequeuer(void *_count)
278 {
279 unsigned long long *count = _count;
280 unsigned int counter;
281
282 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
283 "dequeuer", (unsigned long) pthread_self(),
284 (unsigned long) gettid());
285
286 set_affinity();
287
288 while (!test_go)
289 {
290 }
291 cmm_smp_mb();
292
293 for (;;) {
294 if (test_dequeue && test_splice) {
295 if (counter & 1)
296 do_test_dequeue(test_sync);
297 else
298 do_test_splice(test_sync);
299 counter++;
300 } else {
301 if (test_dequeue)
302 do_test_dequeue(test_sync);
303 else
304 do_test_splice(test_sync);
305 }
306 if (caa_unlikely(!test_duration_dequeue()))
307 break;
308 if (caa_unlikely(rduration))
309 loop_sleep(rduration);
310 }
311
312 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
313 "dequeues %llu, successful_dequeues %llu, "
314 "nr_splice %llu\n",
315 pthread_self(),
316 (unsigned long) gettid(),
317 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues),
318 URCU_TLS(nr_splice));
319 count[0] = URCU_TLS(nr_dequeues);
320 count[1] = URCU_TLS(nr_successful_dequeues);
321 count[2] = URCU_TLS(nr_splice);
322 return ((void*)2);
323 }
324
325 static void test_end(unsigned long long *nr_dequeues)
326 {
327 struct cds_wfcq_node *node;
328
329 do {
330 node = cds_wfcq_dequeue_blocking(&head, &tail);
331 if (node) {
332 free(node);
333 (*nr_dequeues)++;
334 }
335 } while (node);
336 }
337
338 static void show_usage(int argc, char **argv)
339 {
340 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]);
341 printf(" [-d delay] (enqueuer period (in loops))");
342 printf(" [-c duration] (dequeuer period (in loops))");
343 printf(" [-v] (verbose output)");
344 printf(" [-a cpu#] [-a cpu#]... (affinity)");
345 printf(" [-q] (test dequeue)");
346 printf(" [-s] (test splice, enabled by default)");
347 printf(" [-M] (use mutex external synchronization)");
348 printf(" Note: default: no external synchronization used.");
349 printf(" [-f] (force user-provided synchronization)");
350 printf(" [-w] Wait for dequeuer to empty queue");
351 printf("\n");
352 }
353
354 int main(int argc, char **argv)
355 {
356 int err;
357 pthread_t *tid_enqueuer, *tid_dequeuer;
358 void *tret;
359 unsigned long long *count_enqueuer, *count_dequeuer;
360 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
361 unsigned long long tot_successful_enqueues = 0,
362 tot_successful_dequeues = 0,
363 tot_empty_dest_enqueues = 0,
364 tot_splice = 0;
365 unsigned long long end_dequeues = 0;
366 int i, a, retval = 0;
367
368 if (argc < 4) {
369 show_usage(argc, argv);
370 return -1;
371 }
372
373 err = sscanf(argv[1], "%u", &nr_dequeuers);
374 if (err != 1) {
375 show_usage(argc, argv);
376 return -1;
377 }
378
379 err = sscanf(argv[2], "%u", &nr_enqueuers);
380 if (err != 1) {
381 show_usage(argc, argv);
382 return -1;
383 }
384
385 err = sscanf(argv[3], "%lu", &duration);
386 if (err != 1) {
387 show_usage(argc, argv);
388 return -1;
389 }
390
391 for (i = 4; i < argc; i++) {
392 if (argv[i][0] != '-')
393 continue;
394 switch (argv[i][1]) {
395 case 'a':
396 if (argc < i + 2) {
397 show_usage(argc, argv);
398 return -1;
399 }
400 a = atoi(argv[++i]);
401 cpu_affinities[next_aff++] = a;
402 use_affinity = 1;
403 printf_verbose("Adding CPU %d affinity\n", a);
404 break;
405 case 'c':
406 if (argc < i + 2) {
407 show_usage(argc, argv);
408 return -1;
409 }
410 rduration = atol(argv[++i]);
411 break;
412 case 'd':
413 if (argc < i + 2) {
414 show_usage(argc, argv);
415 return -1;
416 }
417 wdelay = atol(argv[++i]);
418 break;
419 case 'v':
420 verbose_mode = 1;
421 break;
422 case 'q':
423 test_dequeue = 1;
424 break;
425 case 's':
426 test_splice = 1;
427 break;
428 case 'M':
429 test_sync = TEST_SYNC_MUTEX;
430 break;
431 case 'w':
432 test_wait_empty = 1;
433 break;
434 case 'f':
435 test_force_sync = 1;
436 break;
437 }
438 }
439
440 /* activate splice test by default */
441 if (!test_dequeue && !test_splice)
442 test_splice = 1;
443
444 if (test_sync == TEST_SYNC_NONE && nr_dequeuers > 1 && test_dequeue) {
445 if (test_force_sync) {
446 fprintf(stderr, "[WARNING] Using dequeue concurrently "
447 "with other dequeue or splice without external "
448 "synchronization. Expect run-time failure.\n");
449 } else {
450 printf("Enforcing mutex synchronization\n");
451 test_sync = TEST_SYNC_MUTEX;
452 }
453 }
454
455 printf_verbose("running test for %lu seconds, %u enqueuers, "
456 "%u dequeuers.\n",
457 duration, nr_enqueuers, nr_dequeuers);
458 if (test_dequeue)
459 printf_verbose("dequeue test activated.\n");
460 else
461 printf_verbose("splice test activated.\n");
462 if (test_sync == TEST_SYNC_MUTEX)
463 printf_verbose("External sync: mutex.\n");
464 else
465 printf_verbose("External sync: none.\n");
466 if (test_wait_empty)
467 printf_verbose("Wait for dequeuers to empty queue.\n");
468 printf_verbose("Writer delay : %lu loops.\n", rduration);
469 printf_verbose("Reader duration : %lu loops.\n", wdelay);
470 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
471 "main", (unsigned long) pthread_self(),
472 (unsigned long) gettid());
473
474 tid_enqueuer = calloc(nr_enqueuers, sizeof(*tid_enqueuer));
475 tid_dequeuer = calloc(nr_dequeuers, sizeof(*tid_dequeuer));
476 count_enqueuer = calloc(nr_enqueuers, 3 * sizeof(*count_enqueuer));
477 count_dequeuer = calloc(nr_dequeuers, 3 * sizeof(*count_dequeuer));
478
479 cds_wfcq_init(&head, &tail);
480
481 next_aff = 0;
482
483 for (i = 0; i < nr_enqueuers; i++) {
484 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
485 &count_enqueuer[3 * i]);
486 if (err != 0)
487 exit(1);
488 }
489 for (i = 0; i < nr_dequeuers; i++) {
490 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
491 &count_dequeuer[3 * i]);
492 if (err != 0)
493 exit(1);
494 }
495
496 cmm_smp_mb();
497
498 test_go = 1;
499
500 for (i = 0; i < duration; i++) {
501 sleep(1);
502 if (verbose_mode)
503 write (1, ".", 1);
504 }
505
506 test_stop_enqueue = 1;
507
508 if (test_wait_empty) {
509 while (nr_enqueuers != uatomic_read(&test_enqueue_stopped)) {
510 sleep(1);
511 }
512 while (!cds_wfcq_empty(&head, &tail)) {
513 sleep(1);
514 }
515 }
516
517 test_stop_dequeue = 1;
518
519 for (i = 0; i < nr_enqueuers; i++) {
520 err = pthread_join(tid_enqueuer[i], &tret);
521 if (err != 0)
522 exit(1);
523 tot_enqueues += count_enqueuer[3 * i];
524 tot_successful_enqueues += count_enqueuer[3 * i + 1];
525 tot_empty_dest_enqueues += count_enqueuer[3 * i + 2];
526 }
527 for (i = 0; i < nr_dequeuers; i++) {
528 err = pthread_join(tid_dequeuer[i], &tret);
529 if (err != 0)
530 exit(1);
531 tot_dequeues += count_dequeuer[3 * i];
532 tot_successful_dequeues += count_dequeuer[3 * i + 1];
533 tot_splice += count_dequeuer[3 * i + 2];
534 }
535
536 test_end(&end_dequeues);
537
538 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
539 tot_enqueues, tot_dequeues);
540 printf_verbose("total number of successful enqueues : %llu, "
541 "enqueues to empty dest : %llu, "
542 "successful dequeues %llu, "
543 "splice : %llu\n",
544 tot_successful_enqueues,
545 tot_empty_dest_enqueues,
546 tot_successful_dequeues,
547 tot_splice);
548 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
549 "nr_dequeuers %3u "
550 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
551 "successful enqueues %12llu enqueues to empty dest %12llu "
552 "successful dequeues %12llu splice %12llu "
553 "end_dequeues %llu nr_ops %12llu\n",
554 argv[0], duration, nr_enqueuers, wdelay,
555 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
556 tot_successful_enqueues,
557 tot_empty_dest_enqueues,
558 tot_successful_dequeues, tot_splice, end_dequeues,
559 tot_enqueues + tot_dequeues);
560
561 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) {
562 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
563 "succ. dequeues + end dequeues %llu.\n",
564 tot_successful_enqueues,
565 tot_successful_dequeues + end_dequeues);
566 retval = 1;
567 }
568
569 /*
570 * If only using splice to dequeue, the enqueuer should see
571 * exactly as many empty queues than the number of non-empty
572 * src splice.
573 */
574 if (test_wait_empty && test_splice && !test_dequeue
575 && tot_empty_dest_enqueues != tot_splice) {
576 printf("WARNING! Discrepancy between empty enqueue (%llu) and "
577 "number of non-empty splice (%llu)\n",
578 tot_empty_dest_enqueues,
579 tot_splice);
580 retval = 1;
581 }
582 free(count_enqueuer);
583 free(count_dequeuer);
584 free(tid_enqueuer);
585 free(tid_dequeuer);
586 return retval;
587 }
This page took 0.040887 seconds and 4 git commands to generate.