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