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