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