Cleanup: cast pthread_self() return value to unsigned long
[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", (unsigned long) pthread_self(),
179 (unsigned long) gettid());
180
181 set_affinity();
182
183 while (!test_go)
184 {
185 }
186 cmm_smp_mb();
187
188 for (;;) {
189 struct cds_wfcq_node *node = malloc(sizeof(*node));
190 if (!node)
191 goto fail;
192 cds_wfcq_node_init(node);
193 cds_wfcq_enqueue(&head, &tail, node);
194 URCU_TLS(nr_successful_enqueues)++;
195
196 if (caa_unlikely(wdelay))
197 loop_sleep(wdelay);
198 fail:
199 URCU_TLS(nr_enqueues)++;
200 if (caa_unlikely(!test_duration_enqueue()))
201 break;
202 }
203
204 count[0] = URCU_TLS(nr_enqueues);
205 count[1] = URCU_TLS(nr_successful_enqueues);
206 printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
207 "enqueues %llu successful_enqueues %llu\n",
208 pthread_self(),
209 (unsigned long) gettid(),
210 URCU_TLS(nr_enqueues), URCU_TLS(nr_successful_enqueues));
211 return ((void*)1);
212
213 }
214
215 static void do_test_dequeue(enum test_sync sync)
216 {
217 struct cds_wfcq_node *node;
218
219 if (sync == TEST_SYNC_MUTEX)
220 node = cds_wfcq_dequeue_blocking(&head, &tail);
221 else
222 node = __cds_wfcq_dequeue_blocking(&head, &tail);
223
224 if (node) {
225 free(node);
226 URCU_TLS(nr_successful_dequeues)++;
227 }
228 URCU_TLS(nr_dequeues)++;
229 }
230
231 static void do_test_splice(enum test_sync sync)
232 {
233 struct cds_wfcq_head tmp_head;
234 struct cds_wfcq_tail tmp_tail;
235 struct cds_wfcq_node *node, *n;
236
237 cds_wfcq_init(&tmp_head, &tmp_tail);
238
239 if (sync == TEST_SYNC_MUTEX)
240 cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
241 &head, &tail);
242 else
243 __cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
244 &head, &tail);
245
246 __cds_wfcq_for_each_blocking_safe(&tmp_head, &tmp_tail, node, n) {
247 free(node);
248 URCU_TLS(nr_successful_dequeues)++;
249 URCU_TLS(nr_dequeues)++;
250 }
251 }
252
253 static void *thr_dequeuer(void *_count)
254 {
255 unsigned long long *count = _count;
256 unsigned int counter;
257
258 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
259 "dequeuer", (unsigned long) pthread_self(),
260 (unsigned long) gettid());
261
262 set_affinity();
263
264 while (!test_go)
265 {
266 }
267 cmm_smp_mb();
268
269 for (;;) {
270 if (test_dequeue && test_splice) {
271 if (counter & 1)
272 do_test_dequeue(test_sync);
273 else
274 do_test_splice(test_sync);
275 counter++;
276 } else {
277 if (test_dequeue)
278 do_test_dequeue(test_sync);
279 else
280 do_test_splice(test_sync);
281 }
282 if (caa_unlikely(!test_duration_dequeue()))
283 break;
284 if (caa_unlikely(rduration))
285 loop_sleep(rduration);
286 }
287
288 printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
289 "dequeues %llu, successful_dequeues %llu\n",
290 pthread_self(),
291 (unsigned long) gettid(),
292 URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues));
293 count[0] = URCU_TLS(nr_dequeues);
294 count[1] = URCU_TLS(nr_successful_dequeues);
295 return ((void*)2);
296 }
297
298 static void test_end(unsigned long long *nr_dequeues)
299 {
300 struct cds_wfcq_node *node;
301
302 do {
303 node = cds_wfcq_dequeue_blocking(&head, &tail);
304 if (node) {
305 free(node);
306 (*nr_dequeues)++;
307 }
308 } while (node);
309 }
310
311 static void show_usage(int argc, char **argv)
312 {
313 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]);
314 printf(" [-d delay] (enqueuer period (in loops))");
315 printf(" [-c duration] (dequeuer period (in loops))");
316 printf(" [-v] (verbose output)");
317 printf(" [-a cpu#] [-a cpu#]... (affinity)");
318 printf(" [-q] (test dequeue)");
319 printf(" [-s] (test splice, enabled by default)");
320 printf(" [-M] (use mutex external synchronization)");
321 printf(" [-0] (use no external synchronization)");
322 printf(" Note: default: mutex external synchronization used.");
323 printf("\n");
324 }
325
326 int main(int argc, char **argv)
327 {
328 int err;
329 pthread_t *tid_enqueuer, *tid_dequeuer;
330 void *tret;
331 unsigned long long *count_enqueuer, *count_dequeuer;
332 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
333 unsigned long long tot_successful_enqueues = 0,
334 tot_successful_dequeues = 0;
335 unsigned long long end_dequeues = 0;
336 int i, a;
337
338 if (argc < 4) {
339 show_usage(argc, argv);
340 return -1;
341 }
342
343 err = sscanf(argv[1], "%u", &nr_dequeuers);
344 if (err != 1) {
345 show_usage(argc, argv);
346 return -1;
347 }
348
349 err = sscanf(argv[2], "%u", &nr_enqueuers);
350 if (err != 1) {
351 show_usage(argc, argv);
352 return -1;
353 }
354
355 err = sscanf(argv[3], "%lu", &duration);
356 if (err != 1) {
357 show_usage(argc, argv);
358 return -1;
359 }
360
361 for (i = 4; i < argc; i++) {
362 if (argv[i][0] != '-')
363 continue;
364 switch (argv[i][1]) {
365 case 'a':
366 if (argc < i + 2) {
367 show_usage(argc, argv);
368 return -1;
369 }
370 a = atoi(argv[++i]);
371 cpu_affinities[next_aff++] = a;
372 use_affinity = 1;
373 printf_verbose("Adding CPU %d affinity\n", a);
374 break;
375 case 'c':
376 if (argc < i + 2) {
377 show_usage(argc, argv);
378 return -1;
379 }
380 rduration = atol(argv[++i]);
381 break;
382 case 'd':
383 if (argc < i + 2) {
384 show_usage(argc, argv);
385 return -1;
386 }
387 wdelay = atol(argv[++i]);
388 break;
389 case 'v':
390 verbose_mode = 1;
391 break;
392 case 'q':
393 test_dequeue = 1;
394 break;
395 case 's':
396 test_splice = 1;
397 break;
398 case 'M':
399 test_sync = TEST_SYNC_MUTEX;
400 break;
401 case '0':
402 test_sync = TEST_SYNC_NONE;
403 break;
404 }
405 }
406
407 /* activate splice test by default */
408 if (!test_dequeue && !test_splice)
409 test_splice = 1;
410
411 printf_verbose("running test for %lu seconds, %u enqueuers, "
412 "%u dequeuers.\n",
413 duration, nr_enqueuers, nr_dequeuers);
414 if (test_dequeue)
415 printf_verbose("dequeue test activated.\n");
416 else
417 printf_verbose("splice test activated.\n");
418 if (test_sync == TEST_SYNC_MUTEX)
419 printf_verbose("External sync: mutex.\n");
420 else
421 printf_verbose("External sync: none.\n");
422 printf_verbose("Writer delay : %lu loops.\n", rduration);
423 printf_verbose("Reader duration : %lu loops.\n", wdelay);
424 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
425 "main", (unsigned long) pthread_self(),
426 (unsigned long) gettid());
427
428 tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
429 tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
430 count_enqueuer = malloc(2 * sizeof(*count_enqueuer) * nr_enqueuers);
431 count_dequeuer = malloc(2 * sizeof(*count_dequeuer) * nr_dequeuers);
432 cds_wfcq_init(&head, &tail);
433
434 next_aff = 0;
435
436 for (i = 0; i < nr_enqueuers; i++) {
437 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
438 &count_enqueuer[2 * i]);
439 if (err != 0)
440 exit(1);
441 }
442 for (i = 0; i < nr_dequeuers; i++) {
443 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
444 &count_dequeuer[2 * i]);
445 if (err != 0)
446 exit(1);
447 }
448
449 cmm_smp_mb();
450
451 test_go = 1;
452
453 for (i = 0; i < duration; i++) {
454 sleep(1);
455 if (verbose_mode)
456 write (1, ".", 1);
457 }
458
459 test_stop = 1;
460
461 for (i = 0; i < nr_enqueuers; i++) {
462 err = pthread_join(tid_enqueuer[i], &tret);
463 if (err != 0)
464 exit(1);
465 tot_enqueues += count_enqueuer[2 * i];
466 tot_successful_enqueues += count_enqueuer[2 * i + 1];
467 }
468 for (i = 0; i < nr_dequeuers; i++) {
469 err = pthread_join(tid_dequeuer[i], &tret);
470 if (err != 0)
471 exit(1);
472 tot_dequeues += count_dequeuer[2 * i];
473 tot_successful_dequeues += count_dequeuer[2 * i + 1];
474 }
475
476 test_end(&end_dequeues);
477
478 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
479 tot_enqueues, tot_dequeues);
480 printf_verbose("total number of successful enqueues : %llu, "
481 "successful dequeues %llu\n",
482 tot_successful_enqueues, tot_successful_dequeues);
483 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
484 "nr_dequeuers %3u "
485 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
486 "successful enqueues %12llu successful dequeues %12llu "
487 "end_dequeues %llu nr_ops %12llu\n",
488 argv[0], duration, nr_enqueuers, wdelay,
489 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
490 tot_successful_enqueues,
491 tot_successful_dequeues, end_dequeues,
492 tot_enqueues + tot_dequeues);
493 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
494 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
495 "succ. dequeues + end dequeues %llu.\n",
496 tot_successful_enqueues,
497 tot_successful_dequeues + end_dequeues);
498
499 free(count_enqueuer);
500 free(count_dequeuer);
501 free(tid_enqueuer);
502 free(tid_dequeuer);
503 return 0;
504 }
This page took 0.039211 seconds and 5 git commands to generate.