| 1 | /* |
| 2 | * test_urcu_lfq.c |
| 3 | * |
| 4 | * Userspace RCU library - example RCU-based lock-free 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/cds.h> |
| 70 | #include <urcu-defer.h> |
| 71 | |
| 72 | static volatile int test_go, test_stop; |
| 73 | |
| 74 | static unsigned long rduration; |
| 75 | |
| 76 | static unsigned long duration; |
| 77 | |
| 78 | /* read-side C.S. duration, in loops */ |
| 79 | static unsigned long wdelay; |
| 80 | |
| 81 | static inline void loop_sleep(unsigned long l) |
| 82 | { |
| 83 | while(l-- != 0) |
| 84 | caa_cpu_relax(); |
| 85 | } |
| 86 | |
| 87 | static int verbose_mode; |
| 88 | |
| 89 | #define printf_verbose(fmt, args...) \ |
| 90 | do { \ |
| 91 | if (verbose_mode) \ |
| 92 | printf(fmt, args); \ |
| 93 | } while (0) |
| 94 | |
| 95 | static unsigned int cpu_affinities[NR_CPUS]; |
| 96 | static unsigned int next_aff = 0; |
| 97 | static int use_affinity = 0; |
| 98 | |
| 99 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 100 | |
| 101 | #ifndef HAVE_CPU_SET_T |
| 102 | typedef unsigned long cpu_set_t; |
| 103 | # define CPU_ZERO(cpuset) do { *(cpuset) = 0; } while(0) |
| 104 | # define CPU_SET(cpu, cpuset) do { *(cpuset) |= (1UL << (cpu)); } while(0) |
| 105 | #endif |
| 106 | |
| 107 | static void set_affinity(void) |
| 108 | { |
| 109 | cpu_set_t mask; |
| 110 | int cpu; |
| 111 | int ret; |
| 112 | |
| 113 | if (!use_affinity) |
| 114 | return; |
| 115 | |
| 116 | #if HAVE_SCHED_SETAFFINITY |
| 117 | ret = pthread_mutex_lock(&affinity_mutex); |
| 118 | if (ret) { |
| 119 | perror("Error in pthread mutex lock"); |
| 120 | exit(-1); |
| 121 | } |
| 122 | cpu = cpu_affinities[next_aff++]; |
| 123 | ret = pthread_mutex_unlock(&affinity_mutex); |
| 124 | if (ret) { |
| 125 | perror("Error in pthread mutex unlock"); |
| 126 | exit(-1); |
| 127 | } |
| 128 | |
| 129 | CPU_ZERO(&mask); |
| 130 | CPU_SET(cpu, &mask); |
| 131 | #if SCHED_SETAFFINITY_ARGS == 2 |
| 132 | sched_setaffinity(0, &mask); |
| 133 | #else |
| 134 | sched_setaffinity(0, sizeof(mask), &mask); |
| 135 | #endif |
| 136 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * returns 0 if test should end. |
| 141 | */ |
| 142 | static int test_duration_dequeue(void) |
| 143 | { |
| 144 | return !test_stop; |
| 145 | } |
| 146 | |
| 147 | static int test_duration_enqueue(void) |
| 148 | { |
| 149 | return !test_stop; |
| 150 | } |
| 151 | |
| 152 | static DEFINE_URCU_TLS(unsigned long long, nr_dequeues); |
| 153 | static DEFINE_URCU_TLS(unsigned long long, nr_enqueues); |
| 154 | |
| 155 | static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues); |
| 156 | static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues); |
| 157 | |
| 158 | static unsigned int nr_enqueuers; |
| 159 | static unsigned int nr_dequeuers; |
| 160 | |
| 161 | struct test { |
| 162 | struct cds_lfq_node_rcu list; |
| 163 | struct rcu_head rcu; |
| 164 | }; |
| 165 | |
| 166 | static struct cds_lfq_queue_rcu q; |
| 167 | |
| 168 | void *thr_enqueuer(void *_count) |
| 169 | { |
| 170 | unsigned long long *count = _count; |
| 171 | |
| 172 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", |
| 173 | "enqueuer", pthread_self(), (unsigned long)gettid()); |
| 174 | |
| 175 | set_affinity(); |
| 176 | |
| 177 | rcu_register_thread(); |
| 178 | |
| 179 | while (!test_go) |
| 180 | { |
| 181 | } |
| 182 | cmm_smp_mb(); |
| 183 | |
| 184 | for (;;) { |
| 185 | struct test *node = malloc(sizeof(*node)); |
| 186 | if (!node) |
| 187 | goto fail; |
| 188 | cds_lfq_node_init_rcu(&node->list); |
| 189 | rcu_read_lock(); |
| 190 | cds_lfq_enqueue_rcu(&q, &node->list); |
| 191 | rcu_read_unlock(); |
| 192 | URCU_TLS(nr_successful_enqueues)++; |
| 193 | |
| 194 | if (caa_unlikely(wdelay)) |
| 195 | loop_sleep(wdelay); |
| 196 | fail: |
| 197 | URCU_TLS(nr_enqueues)++; |
| 198 | if (caa_unlikely(!test_duration_enqueue())) |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | rcu_unregister_thread(); |
| 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(), (unsigned long)gettid(), |
| 209 | URCU_TLS(nr_enqueues), URCU_TLS(nr_successful_enqueues)); |
| 210 | return ((void*)1); |
| 211 | |
| 212 | } |
| 213 | |
| 214 | static |
| 215 | void free_node_cb(struct rcu_head *head) |
| 216 | { |
| 217 | struct test *node = |
| 218 | caa_container_of(head, struct test, rcu); |
| 219 | free(node); |
| 220 | } |
| 221 | |
| 222 | void *thr_dequeuer(void *_count) |
| 223 | { |
| 224 | unsigned long long *count = _count; |
| 225 | int ret; |
| 226 | |
| 227 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", |
| 228 | "dequeuer", pthread_self(), (unsigned long)gettid()); |
| 229 | |
| 230 | set_affinity(); |
| 231 | |
| 232 | ret = rcu_defer_register_thread(); |
| 233 | if (ret) { |
| 234 | printf("Error in rcu_defer_register_thread\n"); |
| 235 | exit(-1); |
| 236 | } |
| 237 | rcu_register_thread(); |
| 238 | |
| 239 | while (!test_go) |
| 240 | { |
| 241 | } |
| 242 | cmm_smp_mb(); |
| 243 | |
| 244 | for (;;) { |
| 245 | struct cds_lfq_node_rcu *qnode; |
| 246 | struct test *node; |
| 247 | |
| 248 | rcu_read_lock(); |
| 249 | qnode = cds_lfq_dequeue_rcu(&q); |
| 250 | node = caa_container_of(qnode, struct test, list); |
| 251 | rcu_read_unlock(); |
| 252 | |
| 253 | if (node) { |
| 254 | call_rcu(&node->rcu, free_node_cb); |
| 255 | URCU_TLS(nr_successful_dequeues)++; |
| 256 | } |
| 257 | |
| 258 | URCU_TLS(nr_dequeues)++; |
| 259 | if (caa_unlikely(!test_duration_dequeue())) |
| 260 | break; |
| 261 | if (caa_unlikely(rduration)) |
| 262 | loop_sleep(rduration); |
| 263 | } |
| 264 | |
| 265 | rcu_unregister_thread(); |
| 266 | rcu_defer_unregister_thread(); |
| 267 | printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, " |
| 268 | "dequeues %llu, successful_dequeues %llu\n", |
| 269 | pthread_self(), (unsigned long)gettid(), |
| 270 | URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues)); |
| 271 | count[0] = URCU_TLS(nr_dequeues); |
| 272 | count[1] = URCU_TLS(nr_successful_dequeues); |
| 273 | return ((void*)2); |
| 274 | } |
| 275 | |
| 276 | void test_end(struct cds_lfq_queue_rcu *q, unsigned long long *nr_dequeues) |
| 277 | { |
| 278 | struct cds_lfq_node_rcu *snode; |
| 279 | |
| 280 | do { |
| 281 | snode = cds_lfq_dequeue_rcu(q); |
| 282 | if (snode) { |
| 283 | struct test *node; |
| 284 | |
| 285 | node = caa_container_of(snode, struct test, list); |
| 286 | free(node); /* no more concurrent access */ |
| 287 | (*nr_dequeues)++; |
| 288 | } |
| 289 | } while (snode); |
| 290 | } |
| 291 | |
| 292 | void show_usage(int argc, char **argv) |
| 293 | { |
| 294 | printf("Usage : %s nr_dequeuers nr_enqueuers duration (s)", argv[0]); |
| 295 | printf(" [-d delay] (enqueuer period (in loops))"); |
| 296 | printf(" [-c duration] (dequeuer period (in loops))"); |
| 297 | printf(" [-v] (verbose output)"); |
| 298 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); |
| 299 | printf("\n"); |
| 300 | } |
| 301 | |
| 302 | int main(int argc, char **argv) |
| 303 | { |
| 304 | int err; |
| 305 | pthread_t *tid_enqueuer, *tid_dequeuer; |
| 306 | void *tret; |
| 307 | unsigned long long *count_enqueuer, *count_dequeuer; |
| 308 | unsigned long long tot_enqueues = 0, tot_dequeues = 0; |
| 309 | unsigned long long tot_successful_enqueues = 0, |
| 310 | tot_successful_dequeues = 0; |
| 311 | unsigned long long end_dequeues = 0; |
| 312 | int i, a; |
| 313 | |
| 314 | if (argc < 4) { |
| 315 | show_usage(argc, argv); |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | err = sscanf(argv[1], "%u", &nr_dequeuers); |
| 320 | if (err != 1) { |
| 321 | show_usage(argc, argv); |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | err = sscanf(argv[2], "%u", &nr_enqueuers); |
| 326 | if (err != 1) { |
| 327 | show_usage(argc, argv); |
| 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | err = sscanf(argv[3], "%lu", &duration); |
| 332 | if (err != 1) { |
| 333 | show_usage(argc, argv); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | for (i = 4; i < argc; i++) { |
| 338 | if (argv[i][0] != '-') |
| 339 | continue; |
| 340 | switch (argv[i][1]) { |
| 341 | case 'a': |
| 342 | if (argc < i + 2) { |
| 343 | show_usage(argc, argv); |
| 344 | return -1; |
| 345 | } |
| 346 | a = atoi(argv[++i]); |
| 347 | cpu_affinities[next_aff++] = a; |
| 348 | use_affinity = 1; |
| 349 | printf_verbose("Adding CPU %d affinity\n", a); |
| 350 | break; |
| 351 | case 'c': |
| 352 | if (argc < i + 2) { |
| 353 | show_usage(argc, argv); |
| 354 | return -1; |
| 355 | } |
| 356 | rduration = atol(argv[++i]); |
| 357 | break; |
| 358 | case 'd': |
| 359 | if (argc < i + 2) { |
| 360 | show_usage(argc, argv); |
| 361 | return -1; |
| 362 | } |
| 363 | wdelay = atol(argv[++i]); |
| 364 | break; |
| 365 | case 'v': |
| 366 | verbose_mode = 1; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | printf_verbose("running test for %lu seconds, %u enqueuers, " |
| 372 | "%u dequeuers.\n", |
| 373 | duration, nr_enqueuers, nr_dequeuers); |
| 374 | printf_verbose("Writer delay : %lu loops.\n", rduration); |
| 375 | printf_verbose("Reader duration : %lu loops.\n", wdelay); |
| 376 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", |
| 377 | "main", pthread_self(), (unsigned long)gettid()); |
| 378 | |
| 379 | tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers); |
| 380 | tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers); |
| 381 | count_enqueuer = malloc(2 * sizeof(*count_enqueuer) * nr_enqueuers); |
| 382 | count_dequeuer = malloc(2 * sizeof(*count_dequeuer) * nr_dequeuers); |
| 383 | cds_lfq_init_rcu(&q, call_rcu); |
| 384 | err = create_all_cpu_call_rcu_data(0); |
| 385 | if (err) { |
| 386 | printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n"); |
| 387 | } |
| 388 | |
| 389 | next_aff = 0; |
| 390 | |
| 391 | for (i = 0; i < nr_enqueuers; i++) { |
| 392 | err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer, |
| 393 | &count_enqueuer[2 * i]); |
| 394 | if (err != 0) |
| 395 | exit(1); |
| 396 | } |
| 397 | for (i = 0; i < nr_dequeuers; i++) { |
| 398 | err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer, |
| 399 | &count_dequeuer[2 * i]); |
| 400 | if (err != 0) |
| 401 | exit(1); |
| 402 | } |
| 403 | |
| 404 | cmm_smp_mb(); |
| 405 | |
| 406 | test_go = 1; |
| 407 | |
| 408 | for (i = 0; i < duration; i++) { |
| 409 | sleep(1); |
| 410 | if (verbose_mode) |
| 411 | write (1, ".", 1); |
| 412 | } |
| 413 | |
| 414 | test_stop = 1; |
| 415 | |
| 416 | for (i = 0; i < nr_enqueuers; i++) { |
| 417 | err = pthread_join(tid_enqueuer[i], &tret); |
| 418 | if (err != 0) |
| 419 | exit(1); |
| 420 | tot_enqueues += count_enqueuer[2 * i]; |
| 421 | tot_successful_enqueues += count_enqueuer[2 * i + 1]; |
| 422 | } |
| 423 | for (i = 0; i < nr_dequeuers; i++) { |
| 424 | err = pthread_join(tid_dequeuer[i], &tret); |
| 425 | if (err != 0) |
| 426 | exit(1); |
| 427 | tot_dequeues += count_dequeuer[2 * i]; |
| 428 | tot_successful_dequeues += count_dequeuer[2 * i + 1]; |
| 429 | } |
| 430 | |
| 431 | test_end(&q, &end_dequeues); |
| 432 | err = cds_lfq_destroy_rcu(&q); |
| 433 | assert(!err); |
| 434 | |
| 435 | printf_verbose("total number of enqueues : %llu, dequeues %llu\n", |
| 436 | tot_enqueues, tot_dequeues); |
| 437 | printf_verbose("total number of successful enqueues : %llu, " |
| 438 | "successful dequeues %llu\n", |
| 439 | tot_successful_enqueues, tot_successful_dequeues); |
| 440 | printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu " |
| 441 | "nr_dequeuers %3u " |
| 442 | "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu " |
| 443 | "successful enqueues %12llu successful dequeues %12llu " |
| 444 | "end_dequeues %llu nr_ops %12llu\n", |
| 445 | argv[0], duration, nr_enqueuers, wdelay, |
| 446 | nr_dequeuers, rduration, tot_enqueues, tot_dequeues, |
| 447 | tot_successful_enqueues, |
| 448 | tot_successful_dequeues, end_dequeues, |
| 449 | tot_enqueues + tot_dequeues); |
| 450 | if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) |
| 451 | printf("WARNING! Discrepancy between nr succ. enqueues %llu vs " |
| 452 | "succ. dequeues + end dequeues %llu.\n", |
| 453 | tot_successful_enqueues, |
| 454 | tot_successful_dequeues + end_dequeues); |
| 455 | |
| 456 | free_all_cpu_call_rcu_data(); |
| 457 | free(count_enqueuer); |
| 458 | free(count_dequeuer); |
| 459 | free(tid_enqueuer); |
| 460 | free(tid_dequeuer); |
| 461 | return 0; |
| 462 | } |