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