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