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