test_urcu_wfcq: add splice and nosync tests
[urcu.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 <sched.h>
38 #include <errno.h>
39
40 #include <urcu/arch.h>
41 #include <urcu/tls-compat.h>
42
43 #ifdef __linux__
44 #include <syscall.h>
45 #endif
46
47 /* hardcoded number of CPUs */
48 #define NR_CPUS 16384
49
50 #if defined(_syscall0)
51 _syscall0(pid_t, gettid)
52 #elif defined(__NR_gettid)
53 static inline pid_t gettid(void)
54 {
55 return syscall(__NR_gettid);
56 }
57 #else
58 #warning "use pid as tid"
59 static inline pid_t gettid(void)
60 {
61 return getpid();
62 }
63 #endif
64
65 #ifndef DYNAMIC_LINK_TEST
66 #define _LGPL_SOURCE
67 #endif
68 #include <urcu.h>
69 #include <urcu/wfcqueue.h>
70
71 enum test_sync {
72 TEST_SYNC_MUTEX = 0,
73 TEST_SYNC_NONE,
74 };
75
76 static enum test_sync test_sync;
77
78 static volatile int test_go, test_stop;
79
80 static unsigned long rduration;
81
82 static unsigned long duration;
83
84 /* read-side C.S. duration, in loops */
85 static unsigned long wdelay;
86
87 static inline void loop_sleep(unsigned long loops)
88 {
89 while (loops-- != 0)
90 caa_cpu_relax();
91 }
92
93 static int verbose_mode;
94
95 static int test_dequeue, test_splice;
96
97 #define printf_verbose(fmt, args...) \
98 do { \
99 if (verbose_mode) \
100 printf(fmt, ## args); \
101 } while (0)
102
103 static unsigned int cpu_affinities[NR_CPUS];
104 static unsigned int next_aff = 0;
105 static int use_affinity = 0;
106
107 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
108
109 #ifndef HAVE_CPU_SET_T
110 typedef unsigned long cpu_set_t;
111 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
112 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
113 #endif
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;
154 }
155
156 static int test_duration_enqueue(void)
157 {
158 return !test_stop;
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
167 static unsigned int nr_enqueuers;
168 static unsigned int nr_dequeuers;
169
170 static struct cds_wfcq_head __attribute__((aligned(CAA_CACHE_LINE_SIZE))) head;
171 static struct cds_wfcq_tail __attribute__((aligned(CAA_CACHE_LINE_SIZE))) tail;
172
173 static void *thr_enqueuer(void *_count)
174 {
175 unsigned long long *count = _count;
176
177 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
178 "enqueuer", pthread_self(), (unsigned long)gettid());
179
180 set_affinity();
181
182 while (!test_go)
183 {
184 }
185 cmm_smp_mb();
186
187 for (;;) {
188 struct cds_wfcq_node *node = malloc(sizeof(*node));
189 if (!node)
190 goto fail;
191 cds_wfcq_node_init(node);
192 cds_wfcq_enqueue(&head, &tail, node);
193 URCU_TLS(nr_successful_enqueues)++;
194
195 if (caa_unlikely(wdelay))
196 loop_sleep(wdelay);
197 fail:
198 URCU_TLS(nr_enqueues)++;
199 if (caa_unlikely(!test_duration_enqueue()))
200 break;
201 }
202
203 count[0] = URCU_TLS(nr_enqueues);
204 count[1] = URCU_TLS(nr_successful_enqueues);
205 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
206 "enqueues %llu successful_enqueues %llu\n",
207 pthread_self(), (unsigned long)gettid(),
208 URCU_TLS(nr_enqueues), URCU_TLS(nr_successful_enqueues));
209 return ((void*)1);
210
211 }
212
213 static void do_test_dequeue(enum test_sync sync)
214 {
215 struct cds_wfcq_node *node;
216
217 if (sync == TEST_SYNC_MUTEX)
218 node = cds_wfcq_dequeue_blocking(&head, &tail);
219 else
220 node = __cds_wfcq_dequeue_blocking(&head, &tail);
221
222 if (node) {
223 free(node);
224 URCU_TLS(nr_successful_dequeues)++;
225 }
226 URCU_TLS(nr_dequeues)++;
227 }
228
229 static void do_test_splice(enum test_sync sync)
230 {
231 struct cds_wfcq_head tmp_head;
232 struct cds_wfcq_tail tmp_tail;
233 struct cds_wfcq_node *node, *n;
234
235 cds_wfcq_init(&tmp_head, &tmp_tail);
236
237 if (sync == TEST_SYNC_MUTEX)
238 cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
239 &head, &tail);
240 else
241 __cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
242 &head, &tail);
243
244 __cds_wfcq_for_each_blocking_safe(&tmp_head, &tmp_tail, node, n) {
245 free(node);
246 URCU_TLS(nr_successful_dequeues)++;
247 URCU_TLS(nr_dequeues)++;
248 }
249 }
250
251 static void *thr_dequeuer(void *_count)
252 {
253 unsigned long long *count = _count;
254 unsigned int counter;
255
256 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
257 "dequeuer", pthread_self(), (unsigned long)gettid());
258
259 set_affinity();
260
261 while (!test_go)
262 {
263 }
264 cmm_smp_mb();
265
266 for (;;) {
267 if (test_dequeue && test_splice) {
268 if (counter & 1)
269 do_test_dequeue(test_sync);
270 else
271 do_test_splice(test_sync);
272 counter++;
273 } else {
274 if (test_dequeue)
275 do_test_dequeue(test_sync);
276 else
277 do_test_splice(test_sync);
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, thread id : %lx, tid %lu, "
286 "dequeues %llu, successful_dequeues %llu\n",
287 pthread_self(), (unsigned long)gettid(),
288 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues));
289 count[0] = URCU_TLS(nr_dequeues);
290 count[1] = URCU_TLS(nr_successful_dequeues);
291 return ((void*)2);
292 }
293
294 static void test_end(unsigned long long *nr_dequeues)
295 {
296 struct cds_wfcq_node *node;
297
298 do {
299 node = cds_wfcq_dequeue_blocking(&head, &tail);
300 if (node) {
301 free(node);
302 (*nr_dequeues)++;
303 }
304 } while (node);
305 }
306
307 static void show_usage(int argc, char **argv)
308 {
309 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]);
310 printf(" [-d delay] (enqueuer period (in loops))");
311 printf(" [-c duration] (dequeuer period (in loops))");
312 printf(" [-v] (verbose output)");
313 printf(" [-a cpu#] [-a cpu#]... (affinity)");
314 printf(" [-q] (test dequeue)");
315 printf(" [-s] (test splice, enabled by default)");
316 printf(" [-M] (use mutex external synchronization)");
317 printf(" [-0] (use no external synchronization)");
318 printf(" Note: default: mutex external synchronization used.");
319 printf("\n");
320 }
321
322 int main(int argc, char **argv)
323 {
324 int err;
325 pthread_t *tid_enqueuer, *tid_dequeuer;
326 void *tret;
327 unsigned long long *count_enqueuer, *count_dequeuer;
328 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
329 unsigned long long tot_successful_enqueues = 0,
330 tot_successful_dequeues = 0;
331 unsigned long long end_dequeues = 0;
332 int i, a;
333
334 if (argc < 4) {
335 show_usage(argc, argv);
336 return -1;
337 }
338
339 err = sscanf(argv[1], "%u", &nr_dequeuers);
340 if (err != 1) {
341 show_usage(argc, argv);
342 return -1;
343 }
344
345 err = sscanf(argv[2], "%u", &nr_enqueuers);
346 if (err != 1) {
347 show_usage(argc, argv);
348 return -1;
349 }
350
351 err = sscanf(argv[3], "%lu", &duration);
352 if (err != 1) {
353 show_usage(argc, argv);
354 return -1;
355 }
356
357 for (i = 4; i < argc; i++) {
358 if (argv[i][0] != '-')
359 continue;
360 switch (argv[i][1]) {
361 case 'a':
362 if (argc < i + 2) {
363 show_usage(argc, argv);
364 return -1;
365 }
366 a = atoi(argv[++i]);
367 cpu_affinities[next_aff++] = a;
368 use_affinity = 1;
369 printf_verbose("Adding CPU %d affinity\n", a);
370 break;
371 case 'c':
372 if (argc < i + 2) {
373 show_usage(argc, argv);
374 return -1;
375 }
376 rduration = atol(argv[++i]);
377 break;
378 case 'd':
379 if (argc < i + 2) {
380 show_usage(argc, argv);
381 return -1;
382 }
383 wdelay = atol(argv[++i]);
384 break;
385 case 'v':
386 verbose_mode = 1;
387 break;
388 case 'q':
389 test_dequeue = 1;
390 break;
391 case 's':
392 test_splice = 1;
393 break;
394 case 'M':
395 test_sync = TEST_SYNC_MUTEX;
396 break;
397 case '0':
398 test_sync = TEST_SYNC_NONE;
399 break;
400 }
401 }
402
403 /* activate splice test by default */
404 if (!test_dequeue && !test_splice)
405 test_splice = 1;
406
407 printf_verbose("running test for %lu seconds, %u enqueuers, "
408 "%u dequeuers.\n",
409 duration, nr_enqueuers, nr_dequeuers);
410 if (test_dequeue)
411 printf_verbose("dequeue test activated.\n");
412 else
413 printf_verbose("splice test activated.\n");
414 if (test_sync == TEST_SYNC_MUTEX)
415 printf_verbose("External sync: mutex.\n");
416 else
417 printf_verbose("External sync: none.\n");
418 printf_verbose("Writer delay : %lu loops.\n", rduration);
419 printf_verbose("Reader duration : %lu loops.\n", wdelay);
420 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
421 "main", pthread_self(), (unsigned long)gettid());
422
423 tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
424 tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
425 count_enqueuer = malloc(2 * sizeof(*count_enqueuer) * nr_enqueuers);
426 count_dequeuer = malloc(2 * sizeof(*count_dequeuer) * nr_dequeuers);
427 cds_wfcq_init(&head, &tail);
428
429 next_aff = 0;
430
431 for (i = 0; i < nr_enqueuers; i++) {
432 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
433 &count_enqueuer[2 * i]);
434 if (err != 0)
435 exit(1);
436 }
437 for (i = 0; i < nr_dequeuers; i++) {
438 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
439 &count_dequeuer[2 * i]);
440 if (err != 0)
441 exit(1);
442 }
443
444 cmm_smp_mb();
445
446 test_go = 1;
447
448 for (i = 0; i < duration; i++) {
449 sleep(1);
450 if (verbose_mode)
451 write (1, ".", 1);
452 }
453
454 test_stop = 1;
455
456 for (i = 0; i < nr_enqueuers; i++) {
457 err = pthread_join(tid_enqueuer[i], &tret);
458 if (err != 0)
459 exit(1);
460 tot_enqueues += count_enqueuer[2 * i];
461 tot_successful_enqueues += count_enqueuer[2 * i + 1];
462 }
463 for (i = 0; i < nr_dequeuers; i++) {
464 err = pthread_join(tid_dequeuer[i], &tret);
465 if (err != 0)
466 exit(1);
467 tot_dequeues += count_dequeuer[2 * i];
468 tot_successful_dequeues += count_dequeuer[2 * i + 1];
469 }
470
471 test_end(&end_dequeues);
472
473 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
474 tot_enqueues, tot_dequeues);
475 printf_verbose("total number of successful enqueues : %llu, "
476 "successful dequeues %llu\n",
477 tot_successful_enqueues, tot_successful_dequeues);
478 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
479 "nr_dequeuers %3u "
480 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
481 "successful enqueues %12llu successful dequeues %12llu "
482 "end_dequeues %llu nr_ops %12llu\n",
483 argv[0], duration, nr_enqueuers, wdelay,
484 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
485 tot_successful_enqueues,
486 tot_successful_dequeues, end_dequeues,
487 tot_enqueues + tot_dequeues);
488 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
489 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
490 "succ. dequeues + end dequeues %llu.\n",
491 tot_successful_enqueues,
492 tot_successful_dequeues + end_dequeues);
493
494 free(count_enqueuer);
495 free(count_dequeuer);
496 free(tid_enqueuer);
497 free(tid_dequeuer);
498 return 0;
499 }
This page took 0.039629 seconds and 5 git commands to generate.