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