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