test wfcqueue: enforce external mutex if needed by default
[urcu.git] / tests / test_urcu_lfs.c
1 /*
2 * test_urcu_lfs.c
3 *
4 * Userspace RCU library - example lock-free stack
5 *
6 * Copyright 2010-2012 - 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/cds.h>
70
71 /*
72 * External synchronization used.
73 */
74 enum test_sync {
75 TEST_SYNC_NONE = 0,
76 TEST_SYNC_RCU,
77 };
78
79 static enum test_sync test_sync;
80
81 static volatile int test_go, test_stop;
82
83 static unsigned long rduration;
84
85 static unsigned long duration;
86
87 /* read-side C.S. duration, in loops */
88 static unsigned long wdelay;
89
90 static inline void loop_sleep(unsigned long loops)
91 {
92 while (loops-- != 0)
93 caa_cpu_relax();
94 }
95
96 static int verbose_mode;
97
98 static int test_pop, test_pop_all;
99
100 #define printf_verbose(fmt, args...) \
101 do { \
102 if (verbose_mode) \
103 printf(fmt, ## args); \
104 } while (0)
105
106 static unsigned int cpu_affinities[NR_CPUS];
107 static unsigned int next_aff = 0;
108 static int use_affinity = 0;
109
110 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
111
112 #ifndef HAVE_CPU_SET_T
113 typedef unsigned long cpu_set_t;
114 # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0)
115 # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0)
116 #endif
117
118 static void set_affinity(void)
119 {
120 #if HAVE_SCHED_SETAFFINITY
121 cpu_set_t mask;
122 int cpu, ret;
123 #endif /* HAVE_SCHED_SETAFFINITY */
124
125 if (!use_affinity)
126 return;
127
128 #if HAVE_SCHED_SETAFFINITY
129 ret = pthread_mutex_lock(&affinity_mutex);
130 if (ret) {
131 perror("Error in pthread mutex lock");
132 exit(-1);
133 }
134 cpu = cpu_affinities[next_aff++];
135 ret = pthread_mutex_unlock(&affinity_mutex);
136 if (ret) {
137 perror("Error in pthread mutex unlock");
138 exit(-1);
139 }
140
141 CPU_ZERO(&mask);
142 CPU_SET(cpu, &mask);
143 #if SCHED_SETAFFINITY_ARGS == 2
144 sched_setaffinity(0, &mask);
145 #else
146 sched_setaffinity(0, sizeof(mask), &mask);
147 #endif
148 #endif /* HAVE_SCHED_SETAFFINITY */
149 }
150
151 /*
152 * returns 0 if test should end.
153 */
154 static int test_duration_dequeue(void)
155 {
156 return !test_stop;
157 }
158
159 static int test_duration_enqueue(void)
160 {
161 return !test_stop;
162 }
163
164 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
165 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
166
167 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
168 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
169
170 static unsigned int nr_enqueuers;
171 static unsigned int nr_dequeuers;
172
173 struct test {
174 struct cds_lfs_node list;
175 struct rcu_head rcu;
176 };
177
178 static struct cds_lfs_stack s;
179
180 static void *thr_enqueuer(void *_count)
181 {
182 unsigned long long *count = _count;
183
184 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
185 "enqueuer", (unsigned long) pthread_self(),
186 (unsigned long) gettid());
187
188 set_affinity();
189
190 rcu_register_thread();
191
192 while (!test_go)
193 {
194 }
195 cmm_smp_mb();
196
197 for (;;) {
198 struct test *node = malloc(sizeof(*node));
199 if (!node)
200 goto fail;
201 cds_lfs_node_init(&node->list);
202 cds_lfs_push(&s, &node->list);
203 URCU_TLS(nr_successful_enqueues)++;
204
205 if (caa_unlikely(wdelay))
206 loop_sleep(wdelay);
207 fail:
208 URCU_TLS(nr_enqueues)++;
209 if (caa_unlikely(!test_duration_enqueue()))
210 break;
211 }
212
213 rcu_unregister_thread();
214
215 count[0] = URCU_TLS(nr_enqueues);
216 count[1] = URCU_TLS(nr_successful_enqueues);
217 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
218 "enqueues %llu successful_enqueues %llu\n",
219 pthread_self(),
220 (unsigned long) gettid(),
221 URCU_TLS(nr_enqueues), URCU_TLS(nr_successful_enqueues));
222 return ((void*)1);
223
224 }
225
226 static
227 void free_node_cb(struct rcu_head *head)
228 {
229 struct test *node =
230 caa_container_of(head, struct test, rcu);
231 free(node);
232 }
233
234 static
235 void do_test_pop(enum test_sync sync)
236 {
237 struct cds_lfs_node *snode;
238
239 if (sync == TEST_SYNC_RCU)
240 rcu_read_lock();
241 snode = __cds_lfs_pop(&s);
242 if (sync == TEST_SYNC_RCU)
243 rcu_read_unlock();
244 if (snode) {
245 struct test *node;
246
247 node = caa_container_of(snode,
248 struct test, list);
249 if (sync == TEST_SYNC_RCU)
250 call_rcu(&node->rcu, free_node_cb);
251 else
252 free(node);
253 URCU_TLS(nr_successful_dequeues)++;
254 }
255 URCU_TLS(nr_dequeues)++;
256 }
257
258 static
259 void do_test_pop_all(enum test_sync sync)
260 {
261 struct cds_lfs_node *snode;
262 struct cds_lfs_head *head;
263 struct cds_lfs_node *n;
264
265 head = __cds_lfs_pop_all(&s);
266 cds_lfs_for_each_safe(head, snode, n) {
267 struct test *node;
268
269 node = caa_container_of(snode, struct test, list);
270 if (sync == TEST_SYNC_RCU)
271 call_rcu(&node->rcu, free_node_cb);
272 else
273 free(node);
274 URCU_TLS(nr_successful_dequeues)++;
275 URCU_TLS(nr_dequeues)++;
276 }
277
278 }
279
280 static void *thr_dequeuer(void *_count)
281 {
282 unsigned long long *count = _count;
283
284 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
285 "dequeuer", (unsigned long) pthread_self(),
286 (unsigned long) gettid());
287
288 set_affinity();
289
290 rcu_register_thread();
291
292 while (!test_go)
293 {
294 }
295 cmm_smp_mb();
296
297 assert(test_pop || test_pop_all);
298
299 for (;;) {
300 unsigned int counter = 0;
301
302 if (test_pop && test_pop_all) {
303 /* both pop and pop all */
304 if (counter & 1)
305 do_test_pop(test_sync);
306 else
307 do_test_pop_all(test_sync);
308 counter++;
309 } else {
310 if (test_pop)
311 do_test_pop(test_sync);
312 else
313 do_test_pop_all(test_sync);
314 }
315
316 if (caa_unlikely(!test_duration_dequeue()))
317 break;
318 if (caa_unlikely(rduration))
319 loop_sleep(rduration);
320 }
321
322 rcu_unregister_thread();
323
324 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
325 "dequeues %llu, successful_dequeues %llu\n",
326 pthread_self(),
327 (unsigned long) gettid(),
328 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues));
329 count[0] = URCU_TLS(nr_dequeues);
330 count[1] = URCU_TLS(nr_successful_dequeues);
331 return ((void*)2);
332 }
333
334 static void test_end(struct cds_lfs_stack *s, unsigned long long *nr_dequeues)
335 {
336 struct cds_lfs_node *snode;
337
338 do {
339 snode = __cds_lfs_pop(s);
340 if (snode) {
341 struct test *node;
342
343 node = caa_container_of(snode, struct test, list);
344 free(node);
345 (*nr_dequeues)++;
346 }
347 } while (snode);
348 }
349
350 static void show_usage(int argc, char **argv)
351 {
352 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]);
353 printf(" [-d delay] (enqueuer period (in loops))");
354 printf(" [-c duration] (dequeuer period (in loops))");
355 printf(" [-v] (verbose output)");
356 printf(" [-a cpu#] [-a cpu#]... (affinity)");
357 printf(" [-p] (test pop)");
358 printf(" [-P] (test pop_all, enabled by default)");
359 printf(" [-R] (use RCU external synchronization)");
360 printf(" Note: default: no external synchronization used.");
361 printf("\n");
362 }
363
364 int main(int argc, char **argv)
365 {
366 int err;
367 pthread_t *tid_enqueuer, *tid_dequeuer;
368 void *tret;
369 unsigned long long *count_enqueuer, *count_dequeuer;
370 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
371 unsigned long long tot_successful_enqueues = 0,
372 tot_successful_dequeues = 0;
373 unsigned long long end_dequeues = 0;
374 int i, a;
375
376 if (argc < 4) {
377 show_usage(argc, argv);
378 return -1;
379 }
380
381 err = sscanf(argv[1], "%u", &nr_dequeuers);
382 if (err != 1) {
383 show_usage(argc, argv);
384 return -1;
385 }
386
387 err = sscanf(argv[2], "%u", &nr_enqueuers);
388 if (err != 1) {
389 show_usage(argc, argv);
390 return -1;
391 }
392
393 err = sscanf(argv[3], "%lu", &duration);
394 if (err != 1) {
395 show_usage(argc, argv);
396 return -1;
397 }
398
399 for (i = 4; i < argc; i++) {
400 if (argv[i][0] != '-')
401 continue;
402 switch (argv[i][1]) {
403 case 'a':
404 if (argc < i + 2) {
405 show_usage(argc, argv);
406 return -1;
407 }
408 a = atoi(argv[++i]);
409 cpu_affinities[next_aff++] = a;
410 use_affinity = 1;
411 printf_verbose("Adding CPU %d affinity\n", a);
412 break;
413 case 'c':
414 if (argc < i + 2) {
415 show_usage(argc, argv);
416 return -1;
417 }
418 rduration = atol(argv[++i]);
419 break;
420 case 'd':
421 if (argc < i + 2) {
422 show_usage(argc, argv);
423 return -1;
424 }
425 wdelay = atol(argv[++i]);
426 break;
427 case 'v':
428 verbose_mode = 1;
429 break;
430 case 'p':
431 test_pop = 1;
432 break;
433 case 'P':
434 test_pop_all = 1;
435 break;
436 case 'R':
437 test_sync = TEST_SYNC_RCU;
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 printf_verbose("running test for %lu seconds, %u enqueuers, "
447 "%u dequeuers.\n",
448 duration, nr_enqueuers, nr_dequeuers);
449 if (test_pop)
450 printf_verbose("pop test activated.\n");
451 if (test_pop_all)
452 printf_verbose("pop_all test activated.\n");
453 if (test_sync == TEST_SYNC_RCU)
454 printf_verbose("External sync: RCU.\n");
455 else
456 printf_verbose("External sync: none.\n");
457 printf_verbose("Writer delay : %lu loops.\n", rduration);
458 printf_verbose("Reader duration : %lu loops.\n", wdelay);
459 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
460 "main", (unsigned long) pthread_self(),
461 (unsigned long) gettid());
462
463 tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
464 tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
465 count_enqueuer = malloc(2 * sizeof(*count_enqueuer) * nr_enqueuers);
466 count_dequeuer = malloc(2 * sizeof(*count_dequeuer) * nr_dequeuers);
467 cds_lfs_init(&s);
468 err = create_all_cpu_call_rcu_data(0);
469 if (err) {
470 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
471 }
472
473 next_aff = 0;
474
475 for (i = 0; i < nr_enqueuers; i++) {
476 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
477 &count_enqueuer[2 * i]);
478 if (err != 0)
479 exit(1);
480 }
481 for (i = 0; i < nr_dequeuers; i++) {
482 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
483 &count_dequeuer[2 * i]);
484 if (err != 0)
485 exit(1);
486 }
487
488 cmm_smp_mb();
489
490 test_go = 1;
491
492 for (i = 0; i < duration; i++) {
493 sleep(1);
494 if (verbose_mode)
495 write (1, ".", 1);
496 }
497
498 test_stop = 1;
499
500 for (i = 0; i < nr_enqueuers; i++) {
501 err = pthread_join(tid_enqueuer[i], &tret);
502 if (err != 0)
503 exit(1);
504 tot_enqueues += count_enqueuer[2 * i];
505 tot_successful_enqueues += count_enqueuer[2 * i + 1];
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[2 * i];
512 tot_successful_dequeues += count_dequeuer[2 * i + 1];
513 }
514
515 test_end(&s, &end_dequeues);
516
517 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
518 tot_enqueues, tot_dequeues);
519 printf_verbose("total number of successful enqueues : %llu, "
520 "successful dequeues %llu\n",
521 tot_successful_enqueues, tot_successful_dequeues);
522 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
523 "nr_dequeuers %3u "
524 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
525 "successful enqueues %12llu successful dequeues %12llu "
526 "end_dequeues %llu nr_ops %12llu\n",
527 argv[0], duration, nr_enqueuers, wdelay,
528 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
529 tot_successful_enqueues,
530 tot_successful_dequeues, end_dequeues,
531 tot_enqueues + tot_dequeues);
532 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
533 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
534 "succ. dequeues + end dequeues %llu.\n",
535 tot_successful_enqueues,
536 tot_successful_dequeues + end_dequeues);
537
538 free_all_cpu_call_rcu_data();
539 free(count_enqueuer);
540 free(count_dequeuer);
541 free(tid_enqueuer);
542 free(tid_dequeuer);
543 return 0;
544 }
This page took 0.03917 seconds and 4 git commands to generate.