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