1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk/env.h" 37 #include "spdk/likely.h" 38 #include "spdk/queue.h" 39 #include "spdk/string.h" 40 #include "spdk/thread.h" 41 #include "spdk/trace.h" 42 #include "spdk/util.h" 43 #include "spdk/fd_group.h" 44 45 #include "spdk/log.h" 46 #include "spdk_internal/thread.h" 47 #include "spdk_internal/usdt.h" 48 #include "thread_internal.h" 49 50 #include "spdk_internal/trace_defs.h" 51 52 #ifdef __linux__ 53 #include <sys/timerfd.h> 54 #include <sys/eventfd.h> 55 #endif 56 57 #define SPDK_MSG_BATCH_SIZE 8 58 #define SPDK_MAX_DEVICE_NAME_LEN 256 59 #define SPDK_THREAD_EXIT_TIMEOUT_SEC 5 60 #define SPDK_MAX_POLLER_NAME_LEN 256 61 #define SPDK_MAX_THREAD_NAME_LEN 256 62 63 enum spdk_poller_state { 64 /* The poller is registered with a thread but not currently executing its fn. */ 65 SPDK_POLLER_STATE_WAITING, 66 67 /* The poller is currently running its fn. */ 68 SPDK_POLLER_STATE_RUNNING, 69 70 /* The poller was unregistered during the execution of its fn. */ 71 SPDK_POLLER_STATE_UNREGISTERED, 72 73 /* The poller is in the process of being paused. It will be paused 74 * during the next time it's supposed to be executed. 75 */ 76 SPDK_POLLER_STATE_PAUSING, 77 78 /* The poller is registered but currently paused. It's on the 79 * paused_pollers list. 80 */ 81 SPDK_POLLER_STATE_PAUSED, 82 }; 83 84 struct spdk_poller { 85 TAILQ_ENTRY(spdk_poller) tailq; 86 RB_ENTRY(spdk_poller) node; 87 88 /* Current state of the poller; should only be accessed from the poller's thread. */ 89 enum spdk_poller_state state; 90 91 uint64_t period_ticks; 92 uint64_t next_run_tick; 93 uint64_t run_count; 94 uint64_t busy_count; 95 uint64_t id; 96 spdk_poller_fn fn; 97 void *arg; 98 struct spdk_thread *thread; 99 /* Native interruptfd for period or busy poller */ 100 int interruptfd; 101 spdk_poller_set_interrupt_mode_cb set_intr_cb_fn; 102 void *set_intr_cb_arg; 103 104 char name[SPDK_MAX_POLLER_NAME_LEN + 1]; 105 }; 106 107 enum spdk_thread_state { 108 /* The thread is processing poller and message by spdk_thread_poll(). */ 109 SPDK_THREAD_STATE_RUNNING, 110 111 /* The thread is in the process of termination. It reaps unregistering 112 * poller are releasing I/O channel. 113 */ 114 SPDK_THREAD_STATE_EXITING, 115 116 /* The thread is exited. It is ready to call spdk_thread_destroy(). */ 117 SPDK_THREAD_STATE_EXITED, 118 }; 119 120 struct spdk_thread { 121 uint64_t tsc_last; 122 struct spdk_thread_stats stats; 123 /* 124 * Contains pollers actively running on this thread. Pollers 125 * are run round-robin. The thread takes one poller from the head 126 * of the ring, executes it, then puts it back at the tail of 127 * the ring. 128 */ 129 TAILQ_HEAD(active_pollers_head, spdk_poller) active_pollers; 130 /** 131 * Contains pollers running on this thread with a periodic timer. 132 */ 133 RB_HEAD(timed_pollers_tree, spdk_poller) timed_pollers; 134 struct spdk_poller *first_timed_poller; 135 /* 136 * Contains paused pollers. Pollers on this queue are waiting until 137 * they are resumed (in which case they're put onto the active/timer 138 * queues) or unregistered. 139 */ 140 TAILQ_HEAD(paused_pollers_head, spdk_poller) paused_pollers; 141 struct spdk_ring *messages; 142 int msg_fd; 143 SLIST_HEAD(, spdk_msg) msg_cache; 144 size_t msg_cache_count; 145 spdk_msg_fn critical_msg; 146 uint64_t id; 147 uint64_t next_poller_id; 148 enum spdk_thread_state state; 149 int pending_unregister_count; 150 151 RB_HEAD(io_channel_tree, spdk_io_channel) io_channels; 152 TAILQ_ENTRY(spdk_thread) tailq; 153 154 char name[SPDK_MAX_THREAD_NAME_LEN + 1]; 155 struct spdk_cpuset cpumask; 156 uint64_t exit_timeout_tsc; 157 158 /* Indicates whether this spdk_thread currently runs in interrupt. */ 159 bool in_interrupt; 160 bool poller_unregistered; 161 struct spdk_fd_group *fgrp; 162 163 /* User context allocated at the end */ 164 uint8_t ctx[0]; 165 }; 166 167 static pthread_mutex_t g_devlist_mutex = PTHREAD_MUTEX_INITIALIZER; 168 169 static spdk_new_thread_fn g_new_thread_fn = NULL; 170 static spdk_thread_op_fn g_thread_op_fn = NULL; 171 static spdk_thread_op_supported_fn g_thread_op_supported_fn; 172 static size_t g_ctx_sz = 0; 173 /* Monotonic increasing ID is set to each created thread beginning at 1. Once the 174 * ID exceeds UINT64_MAX, further thread creation is not allowed and restarting 175 * SPDK application is required. 176 */ 177 static uint64_t g_thread_id = 1; 178 179 struct io_device { 180 void *io_device; 181 char name[SPDK_MAX_DEVICE_NAME_LEN + 1]; 182 spdk_io_channel_create_cb create_cb; 183 spdk_io_channel_destroy_cb destroy_cb; 184 spdk_io_device_unregister_cb unregister_cb; 185 struct spdk_thread *unregister_thread; 186 uint32_t ctx_size; 187 uint32_t for_each_count; 188 RB_ENTRY(io_device) node; 189 190 uint32_t refcnt; 191 192 bool unregistered; 193 }; 194 195 static RB_HEAD(io_device_tree, io_device) g_io_devices = RB_INITIALIZER(g_io_devices); 196 197 static int 198 io_device_cmp(struct io_device *dev1, struct io_device *dev2) 199 { 200 return (dev1->io_device < dev2->io_device ? -1 : dev1->io_device > dev2->io_device); 201 } 202 203 RB_GENERATE_STATIC(io_device_tree, io_device, node, io_device_cmp); 204 205 static int 206 io_channel_cmp(struct spdk_io_channel *ch1, struct spdk_io_channel *ch2) 207 { 208 return (ch1->dev < ch2->dev ? -1 : ch1->dev > ch2->dev); 209 } 210 211 RB_GENERATE_STATIC(io_channel_tree, spdk_io_channel, node, io_channel_cmp); 212 213 struct spdk_msg { 214 spdk_msg_fn fn; 215 void *arg; 216 217 SLIST_ENTRY(spdk_msg) link; 218 }; 219 220 #define SPDK_MSG_MEMPOOL_CACHE_SIZE 1024 221 static struct spdk_mempool *g_spdk_msg_mempool = NULL; 222 223 static TAILQ_HEAD(, spdk_thread) g_threads = TAILQ_HEAD_INITIALIZER(g_threads); 224 static uint32_t g_thread_count = 0; 225 226 static __thread struct spdk_thread *tls_thread = NULL; 227 228 SPDK_TRACE_REGISTER_FN(thread_trace, "thread", TRACE_GROUP_THREAD) 229 { 230 spdk_trace_register_description("THREAD_IOCH_GET", 231 TRACE_THREAD_IOCH_GET, 232 OWNER_NONE, OBJECT_NONE, 0, 233 SPDK_TRACE_ARG_TYPE_INT, "refcnt"); 234 spdk_trace_register_description("THREAD_IOCH_PUT", 235 TRACE_THREAD_IOCH_PUT, 236 OWNER_NONE, OBJECT_NONE, 0, 237 SPDK_TRACE_ARG_TYPE_INT, "refcnt"); 238 } 239 240 /* 241 * If this compare function returns zero when two next_run_ticks are equal, 242 * the macro RB_INSERT() returns a pointer to the element with the same 243 * next_run_tick. 244 * 245 * Fortunately, the macro RB_REMOVE() takes not a key but a pointer to the element 246 * to remove as a parameter. 247 * 248 * Hence we allow RB_INSERT() to insert elements with the same keys on the right 249 * side by returning 1 when two next_run_ticks are equal. 250 */ 251 static inline int 252 timed_poller_compare(struct spdk_poller *poller1, struct spdk_poller *poller2) 253 { 254 if (poller1->next_run_tick < poller2->next_run_tick) { 255 return -1; 256 } else { 257 return 1; 258 } 259 } 260 261 RB_GENERATE_STATIC(timed_pollers_tree, spdk_poller, node, timed_poller_compare); 262 263 static inline struct spdk_thread * 264 _get_thread(void) 265 { 266 return tls_thread; 267 } 268 269 static int 270 _thread_lib_init(size_t ctx_sz, size_t msg_mempool_sz) 271 { 272 char mempool_name[SPDK_MAX_MEMZONE_NAME_LEN]; 273 274 g_ctx_sz = ctx_sz; 275 276 snprintf(mempool_name, sizeof(mempool_name), "msgpool_%d", getpid()); 277 g_spdk_msg_mempool = spdk_mempool_create(mempool_name, msg_mempool_sz, 278 sizeof(struct spdk_msg), 279 0, /* No cache. We do our own. */ 280 SPDK_ENV_SOCKET_ID_ANY); 281 282 SPDK_DEBUGLOG(thread, "spdk_msg_mempool was created with size: %zu\n", 283 msg_mempool_sz); 284 285 if (!g_spdk_msg_mempool) { 286 SPDK_ERRLOG("spdk_msg_mempool creation failed\n"); 287 return -1; 288 } 289 290 return 0; 291 } 292 293 int 294 spdk_thread_lib_init(spdk_new_thread_fn new_thread_fn, size_t ctx_sz) 295 { 296 assert(g_new_thread_fn == NULL); 297 assert(g_thread_op_fn == NULL); 298 299 if (new_thread_fn == NULL) { 300 SPDK_INFOLOG(thread, "new_thread_fn was not specified at spdk_thread_lib_init\n"); 301 } else { 302 g_new_thread_fn = new_thread_fn; 303 } 304 305 return _thread_lib_init(ctx_sz, SPDK_DEFAULT_MSG_MEMPOOL_SIZE); 306 } 307 308 int 309 spdk_thread_lib_init_ext(spdk_thread_op_fn thread_op_fn, 310 spdk_thread_op_supported_fn thread_op_supported_fn, 311 size_t ctx_sz, size_t msg_mempool_sz) 312 { 313 assert(g_new_thread_fn == NULL); 314 assert(g_thread_op_fn == NULL); 315 assert(g_thread_op_supported_fn == NULL); 316 317 if ((thread_op_fn != NULL) != (thread_op_supported_fn != NULL)) { 318 SPDK_ERRLOG("Both must be defined or undefined together.\n"); 319 return -EINVAL; 320 } 321 322 if (thread_op_fn == NULL && thread_op_supported_fn == NULL) { 323 SPDK_INFOLOG(thread, "thread_op_fn and thread_op_supported_fn were not specified\n"); 324 } else { 325 g_thread_op_fn = thread_op_fn; 326 g_thread_op_supported_fn = thread_op_supported_fn; 327 } 328 329 return _thread_lib_init(ctx_sz, msg_mempool_sz); 330 } 331 332 void 333 spdk_thread_lib_fini(void) 334 { 335 struct io_device *dev; 336 337 RB_FOREACH(dev, io_device_tree, &g_io_devices) { 338 SPDK_ERRLOG("io_device %s not unregistered\n", dev->name); 339 } 340 341 if (g_spdk_msg_mempool) { 342 spdk_mempool_free(g_spdk_msg_mempool); 343 g_spdk_msg_mempool = NULL; 344 } 345 346 g_new_thread_fn = NULL; 347 g_thread_op_fn = NULL; 348 g_thread_op_supported_fn = NULL; 349 g_ctx_sz = 0; 350 } 351 352 static void thread_interrupt_destroy(struct spdk_thread *thread); 353 static int thread_interrupt_create(struct spdk_thread *thread); 354 355 static void 356 _free_thread(struct spdk_thread *thread) 357 { 358 struct spdk_io_channel *ch; 359 struct spdk_msg *msg; 360 struct spdk_poller *poller, *ptmp; 361 362 RB_FOREACH(ch, io_channel_tree, &thread->io_channels) { 363 SPDK_ERRLOG("thread %s still has channel for io_device %s\n", 364 thread->name, ch->dev->name); 365 } 366 367 TAILQ_FOREACH_SAFE(poller, &thread->active_pollers, tailq, ptmp) { 368 if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) { 369 SPDK_WARNLOG("active_poller %s still registered at thread exit\n", 370 poller->name); 371 } 372 TAILQ_REMOVE(&thread->active_pollers, poller, tailq); 373 free(poller); 374 } 375 376 RB_FOREACH_SAFE(poller, timed_pollers_tree, &thread->timed_pollers, ptmp) { 377 if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) { 378 SPDK_WARNLOG("timed_poller %s still registered at thread exit\n", 379 poller->name); 380 } 381 RB_REMOVE(timed_pollers_tree, &thread->timed_pollers, poller); 382 free(poller); 383 } 384 385 TAILQ_FOREACH_SAFE(poller, &thread->paused_pollers, tailq, ptmp) { 386 SPDK_WARNLOG("paused_poller %s still registered at thread exit\n", poller->name); 387 TAILQ_REMOVE(&thread->paused_pollers, poller, tailq); 388 free(poller); 389 } 390 391 pthread_mutex_lock(&g_devlist_mutex); 392 assert(g_thread_count > 0); 393 g_thread_count--; 394 TAILQ_REMOVE(&g_threads, thread, tailq); 395 pthread_mutex_unlock(&g_devlist_mutex); 396 397 msg = SLIST_FIRST(&thread->msg_cache); 398 while (msg != NULL) { 399 SLIST_REMOVE_HEAD(&thread->msg_cache, link); 400 401 assert(thread->msg_cache_count > 0); 402 thread->msg_cache_count--; 403 spdk_mempool_put(g_spdk_msg_mempool, msg); 404 405 msg = SLIST_FIRST(&thread->msg_cache); 406 } 407 408 assert(thread->msg_cache_count == 0); 409 410 if (spdk_interrupt_mode_is_enabled()) { 411 thread_interrupt_destroy(thread); 412 } 413 414 spdk_ring_free(thread->messages); 415 free(thread); 416 } 417 418 struct spdk_thread * 419 spdk_thread_create(const char *name, struct spdk_cpuset *cpumask) 420 { 421 struct spdk_thread *thread; 422 struct spdk_msg *msgs[SPDK_MSG_MEMPOOL_CACHE_SIZE]; 423 int rc = 0, i; 424 425 thread = calloc(1, sizeof(*thread) + g_ctx_sz); 426 if (!thread) { 427 SPDK_ERRLOG("Unable to allocate memory for thread\n"); 428 return NULL; 429 } 430 431 if (cpumask) { 432 spdk_cpuset_copy(&thread->cpumask, cpumask); 433 } else { 434 spdk_cpuset_negate(&thread->cpumask); 435 } 436 437 RB_INIT(&thread->io_channels); 438 TAILQ_INIT(&thread->active_pollers); 439 RB_INIT(&thread->timed_pollers); 440 TAILQ_INIT(&thread->paused_pollers); 441 SLIST_INIT(&thread->msg_cache); 442 thread->msg_cache_count = 0; 443 444 thread->tsc_last = spdk_get_ticks(); 445 446 /* Monotonic increasing ID is set to each created poller beginning at 1. Once the 447 * ID exceeds UINT64_MAX a warning message is logged 448 */ 449 thread->next_poller_id = 1; 450 451 thread->messages = spdk_ring_create(SPDK_RING_TYPE_MP_SC, 65536, SPDK_ENV_SOCKET_ID_ANY); 452 if (!thread->messages) { 453 SPDK_ERRLOG("Unable to allocate memory for message ring\n"); 454 free(thread); 455 return NULL; 456 } 457 458 /* Fill the local message pool cache. */ 459 rc = spdk_mempool_get_bulk(g_spdk_msg_mempool, (void **)msgs, SPDK_MSG_MEMPOOL_CACHE_SIZE); 460 if (rc == 0) { 461 /* If we can't populate the cache it's ok. The cache will get filled 462 * up organically as messages are passed to the thread. */ 463 for (i = 0; i < SPDK_MSG_MEMPOOL_CACHE_SIZE; i++) { 464 SLIST_INSERT_HEAD(&thread->msg_cache, msgs[i], link); 465 thread->msg_cache_count++; 466 } 467 } 468 469 if (name) { 470 snprintf(thread->name, sizeof(thread->name), "%s", name); 471 } else { 472 snprintf(thread->name, sizeof(thread->name), "%p", thread); 473 } 474 475 pthread_mutex_lock(&g_devlist_mutex); 476 if (g_thread_id == 0) { 477 SPDK_ERRLOG("Thread ID rolled over. Further thread creation is not allowed.\n"); 478 pthread_mutex_unlock(&g_devlist_mutex); 479 _free_thread(thread); 480 return NULL; 481 } 482 thread->id = g_thread_id++; 483 TAILQ_INSERT_TAIL(&g_threads, thread, tailq); 484 g_thread_count++; 485 pthread_mutex_unlock(&g_devlist_mutex); 486 487 SPDK_DEBUGLOG(thread, "Allocating new thread (%" PRIu64 ", %s)\n", 488 thread->id, thread->name); 489 490 if (spdk_interrupt_mode_is_enabled()) { 491 thread->in_interrupt = true; 492 rc = thread_interrupt_create(thread); 493 if (rc != 0) { 494 _free_thread(thread); 495 return NULL; 496 } 497 } 498 499 if (g_new_thread_fn) { 500 rc = g_new_thread_fn(thread); 501 } else if (g_thread_op_supported_fn && g_thread_op_supported_fn(SPDK_THREAD_OP_NEW)) { 502 rc = g_thread_op_fn(thread, SPDK_THREAD_OP_NEW); 503 } 504 505 if (rc != 0) { 506 _free_thread(thread); 507 return NULL; 508 } 509 510 thread->state = SPDK_THREAD_STATE_RUNNING; 511 512 return thread; 513 } 514 515 void 516 spdk_set_thread(struct spdk_thread *thread) 517 { 518 tls_thread = thread; 519 } 520 521 static void 522 thread_exit(struct spdk_thread *thread, uint64_t now) 523 { 524 struct spdk_poller *poller; 525 struct spdk_io_channel *ch; 526 527 if (now >= thread->exit_timeout_tsc) { 528 SPDK_ERRLOG("thread %s got timeout, and move it to the exited state forcefully\n", 529 thread->name); 530 goto exited; 531 } 532 533 TAILQ_FOREACH(poller, &thread->active_pollers, tailq) { 534 if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) { 535 SPDK_INFOLOG(thread, 536 "thread %s still has active poller %s\n", 537 thread->name, poller->name); 538 return; 539 } 540 } 541 542 RB_FOREACH(poller, timed_pollers_tree, &thread->timed_pollers) { 543 if (poller->state != SPDK_POLLER_STATE_UNREGISTERED) { 544 SPDK_INFOLOG(thread, 545 "thread %s still has active timed poller %s\n", 546 thread->name, poller->name); 547 return; 548 } 549 } 550 551 TAILQ_FOREACH(poller, &thread->paused_pollers, tailq) { 552 SPDK_INFOLOG(thread, 553 "thread %s still has paused poller %s\n", 554 thread->name, poller->name); 555 return; 556 } 557 558 RB_FOREACH(ch, io_channel_tree, &thread->io_channels) { 559 SPDK_INFOLOG(thread, 560 "thread %s still has channel for io_device %s\n", 561 thread->name, ch->dev->name); 562 return; 563 } 564 565 if (thread->pending_unregister_count > 0) { 566 SPDK_INFOLOG(thread, 567 "thread %s is still unregistering io_devices\n", 568 thread->name); 569 return; 570 } 571 572 exited: 573 thread->state = SPDK_THREAD_STATE_EXITED; 574 } 575 576 int 577 spdk_thread_exit(struct spdk_thread *thread) 578 { 579 SPDK_DEBUGLOG(thread, "Exit thread %s\n", thread->name); 580 581 assert(tls_thread == thread); 582 583 if (thread->state >= SPDK_THREAD_STATE_EXITING) { 584 SPDK_INFOLOG(thread, 585 "thread %s is already exiting\n", 586 thread->name); 587 return 0; 588 } 589 590 thread->exit_timeout_tsc = spdk_get_ticks() + (spdk_get_ticks_hz() * 591 SPDK_THREAD_EXIT_TIMEOUT_SEC); 592 thread->state = SPDK_THREAD_STATE_EXITING; 593 return 0; 594 } 595 596 bool 597 spdk_thread_is_exited(struct spdk_thread *thread) 598 { 599 return thread->state == SPDK_THREAD_STATE_EXITED; 600 } 601 602 void 603 spdk_thread_destroy(struct spdk_thread *thread) 604 { 605 SPDK_DEBUGLOG(thread, "Destroy thread %s\n", thread->name); 606 607 assert(thread->state == SPDK_THREAD_STATE_EXITED); 608 609 if (tls_thread == thread) { 610 tls_thread = NULL; 611 } 612 613 _free_thread(thread); 614 } 615 616 void * 617 spdk_thread_get_ctx(struct spdk_thread *thread) 618 { 619 if (g_ctx_sz > 0) { 620 return thread->ctx; 621 } 622 623 return NULL; 624 } 625 626 struct spdk_cpuset * 627 spdk_thread_get_cpumask(struct spdk_thread *thread) 628 { 629 return &thread->cpumask; 630 } 631 632 int 633 spdk_thread_set_cpumask(struct spdk_cpuset *cpumask) 634 { 635 struct spdk_thread *thread; 636 637 if (!g_thread_op_supported_fn || !g_thread_op_supported_fn(SPDK_THREAD_OP_RESCHED)) { 638 SPDK_ERRLOG("Framework does not support reschedule operation.\n"); 639 assert(false); 640 return -ENOTSUP; 641 } 642 643 thread = spdk_get_thread(); 644 if (!thread) { 645 SPDK_ERRLOG("Called from non-SPDK thread\n"); 646 assert(false); 647 return -EINVAL; 648 } 649 650 spdk_cpuset_copy(&thread->cpumask, cpumask); 651 652 /* Invoke framework's reschedule operation. If this function is called multiple times 653 * in a single spdk_thread_poll() context, the last cpumask will be used in the 654 * reschedule operation. 655 */ 656 g_thread_op_fn(thread, SPDK_THREAD_OP_RESCHED); 657 658 return 0; 659 } 660 661 struct spdk_thread * 662 spdk_thread_get_from_ctx(void *ctx) 663 { 664 if (ctx == NULL) { 665 assert(false); 666 return NULL; 667 } 668 669 assert(g_ctx_sz > 0); 670 671 return SPDK_CONTAINEROF(ctx, struct spdk_thread, ctx); 672 } 673 674 static inline uint32_t 675 msg_queue_run_batch(struct spdk_thread *thread, uint32_t max_msgs) 676 { 677 unsigned count, i; 678 void *messages[SPDK_MSG_BATCH_SIZE]; 679 uint64_t notify = 1; 680 int rc; 681 682 #ifdef DEBUG 683 /* 684 * spdk_ring_dequeue() fills messages and returns how many entries it wrote, 685 * so we will never actually read uninitialized data from events, but just to be sure 686 * (and to silence a static analyzer false positive), initialize the array to NULL pointers. 687 */ 688 memset(messages, 0, sizeof(messages)); 689 #endif 690 691 if (max_msgs > 0) { 692 max_msgs = spdk_min(max_msgs, SPDK_MSG_BATCH_SIZE); 693 } else { 694 max_msgs = SPDK_MSG_BATCH_SIZE; 695 } 696 697 count = spdk_ring_dequeue(thread->messages, messages, max_msgs); 698 if (spdk_unlikely(thread->in_interrupt) && 699 spdk_ring_count(thread->messages) != 0) { 700 rc = write(thread->msg_fd, ¬ify, sizeof(notify)); 701 if (rc < 0) { 702 SPDK_ERRLOG("failed to notify msg_queue: %s.\n", spdk_strerror(errno)); 703 } 704 } 705 if (count == 0) { 706 return 0; 707 } 708 709 for (i = 0; i < count; i++) { 710 struct spdk_msg *msg = messages[i]; 711 712 assert(msg != NULL); 713 714 SPDK_DTRACE_PROBE2(msg_exec, msg->fn, msg->arg); 715 716 msg->fn(msg->arg); 717 718 if (thread->msg_cache_count < SPDK_MSG_MEMPOOL_CACHE_SIZE) { 719 /* Insert the messages at the head. We want to re-use the hot 720 * ones. */ 721 SLIST_INSERT_HEAD(&thread->msg_cache, msg, link); 722 thread->msg_cache_count++; 723 } else { 724 spdk_mempool_put(g_spdk_msg_mempool, msg); 725 } 726 } 727 728 return count; 729 } 730 731 static void 732 poller_insert_timer(struct spdk_thread *thread, struct spdk_poller *poller, uint64_t now) 733 { 734 struct spdk_poller *tmp __attribute__((unused)); 735 736 poller->next_run_tick = now + poller->period_ticks; 737 738 /* 739 * Insert poller in the thread's timed_pollers tree by next scheduled run time 740 * as its key. 741 */ 742 tmp = RB_INSERT(timed_pollers_tree, &thread->timed_pollers, poller); 743 assert(tmp == NULL); 744 745 /* Update the cache only if it is empty or the inserted poller is earlier than it. 746 * RB_MIN() is not necessary here because all pollers, which has exactly the same 747 * next_run_tick as the existing poller, are inserted on the right side. 748 */ 749 if (thread->first_timed_poller == NULL || 750 poller->next_run_tick < thread->first_timed_poller->next_run_tick) { 751 thread->first_timed_poller = poller; 752 } 753 } 754 755 static inline void 756 poller_remove_timer(struct spdk_thread *thread, struct spdk_poller *poller) 757 { 758 struct spdk_poller *tmp __attribute__((unused)); 759 760 tmp = RB_REMOVE(timed_pollers_tree, &thread->timed_pollers, poller); 761 assert(tmp != NULL); 762 763 /* This function is not used in any case that is performance critical. 764 * Update the cache simply by RB_MIN() if it needs to be changed. 765 */ 766 if (thread->first_timed_poller == poller) { 767 thread->first_timed_poller = RB_MIN(timed_pollers_tree, &thread->timed_pollers); 768 } 769 } 770 771 static void 772 thread_insert_poller(struct spdk_thread *thread, struct spdk_poller *poller) 773 { 774 if (poller->period_ticks) { 775 poller_insert_timer(thread, poller, spdk_get_ticks()); 776 } else { 777 TAILQ_INSERT_TAIL(&thread->active_pollers, poller, tailq); 778 } 779 } 780 781 static inline void 782 thread_update_stats(struct spdk_thread *thread, uint64_t end, 783 uint64_t start, int rc) 784 { 785 if (rc == 0) { 786 /* Poller status idle */ 787 thread->stats.idle_tsc += end - start; 788 } else if (rc > 0) { 789 /* Poller status busy */ 790 thread->stats.busy_tsc += end - start; 791 } 792 /* Store end time to use it as start time of the next spdk_thread_poll(). */ 793 thread->tsc_last = end; 794 } 795 796 static inline int 797 thread_execute_poller(struct spdk_thread *thread, struct spdk_poller *poller) 798 { 799 int rc; 800 801 switch (poller->state) { 802 case SPDK_POLLER_STATE_UNREGISTERED: 803 TAILQ_REMOVE(&thread->active_pollers, poller, tailq); 804 free(poller); 805 return 0; 806 case SPDK_POLLER_STATE_PAUSING: 807 TAILQ_REMOVE(&thread->active_pollers, poller, tailq); 808 TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq); 809 poller->state = SPDK_POLLER_STATE_PAUSED; 810 return 0; 811 case SPDK_POLLER_STATE_WAITING: 812 break; 813 default: 814 assert(false); 815 break; 816 } 817 818 poller->state = SPDK_POLLER_STATE_RUNNING; 819 rc = poller->fn(poller->arg); 820 821 poller->run_count++; 822 if (rc > 0) { 823 poller->busy_count++; 824 } 825 826 #ifdef DEBUG 827 if (rc == -1) { 828 SPDK_DEBUGLOG(thread, "Poller %s returned -1\n", poller->name); 829 } 830 #endif 831 832 switch (poller->state) { 833 case SPDK_POLLER_STATE_UNREGISTERED: 834 TAILQ_REMOVE(&thread->active_pollers, poller, tailq); 835 free(poller); 836 break; 837 case SPDK_POLLER_STATE_PAUSING: 838 TAILQ_REMOVE(&thread->active_pollers, poller, tailq); 839 TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq); 840 poller->state = SPDK_POLLER_STATE_PAUSED; 841 break; 842 case SPDK_POLLER_STATE_PAUSED: 843 case SPDK_POLLER_STATE_WAITING: 844 break; 845 case SPDK_POLLER_STATE_RUNNING: 846 poller->state = SPDK_POLLER_STATE_WAITING; 847 break; 848 default: 849 assert(false); 850 break; 851 } 852 853 return rc; 854 } 855 856 static inline int 857 thread_execute_timed_poller(struct spdk_thread *thread, struct spdk_poller *poller, 858 uint64_t now) 859 { 860 int rc; 861 862 switch (poller->state) { 863 case SPDK_POLLER_STATE_UNREGISTERED: 864 free(poller); 865 return 0; 866 case SPDK_POLLER_STATE_PAUSING: 867 TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq); 868 poller->state = SPDK_POLLER_STATE_PAUSED; 869 return 0; 870 case SPDK_POLLER_STATE_WAITING: 871 break; 872 default: 873 assert(false); 874 break; 875 } 876 877 poller->state = SPDK_POLLER_STATE_RUNNING; 878 rc = poller->fn(poller->arg); 879 880 poller->run_count++; 881 if (rc > 0) { 882 poller->busy_count++; 883 } 884 885 #ifdef DEBUG 886 if (rc == -1) { 887 SPDK_DEBUGLOG(thread, "Timed poller %s returned -1\n", poller->name); 888 } 889 #endif 890 891 switch (poller->state) { 892 case SPDK_POLLER_STATE_UNREGISTERED: 893 free(poller); 894 break; 895 case SPDK_POLLER_STATE_PAUSING: 896 TAILQ_INSERT_TAIL(&thread->paused_pollers, poller, tailq); 897 poller->state = SPDK_POLLER_STATE_PAUSED; 898 break; 899 case SPDK_POLLER_STATE_PAUSED: 900 break; 901 case SPDK_POLLER_STATE_RUNNING: 902 poller->state = SPDK_POLLER_STATE_WAITING; 903 /* fallthrough */ 904 case SPDK_POLLER_STATE_WAITING: 905 poller_insert_timer(thread, poller, now); 906 break; 907 default: 908 assert(false); 909 break; 910 } 911 912 return rc; 913 } 914 915 static int 916 thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now) 917 { 918 uint32_t msg_count; 919 struct spdk_poller *poller, *tmp; 920 spdk_msg_fn critical_msg; 921 int rc = 0; 922 923 thread->tsc_last = now; 924 925 critical_msg = thread->critical_msg; 926 if (spdk_unlikely(critical_msg != NULL)) { 927 critical_msg(NULL); 928 thread->critical_msg = NULL; 929 rc = 1; 930 } 931 932 msg_count = msg_queue_run_batch(thread, max_msgs); 933 if (msg_count) { 934 rc = 1; 935 } 936 937 TAILQ_FOREACH_REVERSE_SAFE(poller, &thread->active_pollers, 938 active_pollers_head, tailq, tmp) { 939 int poller_rc; 940 941 poller_rc = thread_execute_poller(thread, poller); 942 if (poller_rc > rc) { 943 rc = poller_rc; 944 } 945 } 946 947 poller = thread->first_timed_poller; 948 while (poller != NULL) { 949 int timer_rc = 0; 950 951 if (now < poller->next_run_tick) { 952 break; 953 } 954 955 tmp = RB_NEXT(timed_pollers_tree, &thread->timed_pollers, poller); 956 RB_REMOVE(timed_pollers_tree, &thread->timed_pollers, poller); 957 958 /* Update the cache to the next timed poller in the list 959 * only if the current poller is still the closest, otherwise, 960 * do nothing because the cache has been already updated. 961 */ 962 if (thread->first_timed_poller == poller) { 963 thread->first_timed_poller = tmp; 964 } 965 966 timer_rc = thread_execute_timed_poller(thread, poller, now); 967 if (timer_rc > rc) { 968 rc = timer_rc; 969 } 970 971 poller = tmp; 972 } 973 974 return rc; 975 } 976 977 int 978 spdk_thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now) 979 { 980 struct spdk_thread *orig_thread; 981 int rc; 982 uint64_t notify = 1; 983 984 orig_thread = _get_thread(); 985 tls_thread = thread; 986 987 if (now == 0) { 988 now = spdk_get_ticks(); 989 } 990 991 if (spdk_likely(!thread->in_interrupt)) { 992 rc = thread_poll(thread, max_msgs, now); 993 if (spdk_unlikely(thread->in_interrupt)) { 994 /* The thread transitioned to interrupt mode during the above poll. 995 * Poll it one more time in case that during the transition time 996 * there is msg received without notification. 997 */ 998 rc = thread_poll(thread, max_msgs, now); 999 } 1000 } else { 1001 /* Non-block wait on thread's fd_group */ 1002 rc = spdk_fd_group_wait(thread->fgrp, 0); 1003 if (spdk_unlikely(!thread->in_interrupt)) { 1004 /* The thread transitioned to poll mode in a msg during the above processing. 1005 * Clear msg_fd since thread messages will be polled directly in poll mode. 1006 */ 1007 rc = read(thread->msg_fd, ¬ify, sizeof(notify)); 1008 if (rc < 0 && errno != EAGAIN) { 1009 SPDK_ERRLOG("failed to acknowledge msg queue: %s.\n", spdk_strerror(errno)); 1010 } 1011 } 1012 1013 /* Reap unregistered pollers out of poller execution in intr mode */ 1014 if (spdk_unlikely(thread->poller_unregistered)) { 1015 struct spdk_poller *poller, *tmp; 1016 1017 TAILQ_FOREACH_REVERSE_SAFE(poller, &thread->active_pollers, 1018 active_pollers_head, tailq, tmp) { 1019 if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { 1020 TAILQ_REMOVE(&thread->active_pollers, poller, tailq); 1021 free(poller); 1022 } 1023 } 1024 1025 RB_FOREACH_SAFE(poller, timed_pollers_tree, &thread->timed_pollers, tmp) { 1026 if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { 1027 poller_remove_timer(thread, poller); 1028 free(poller); 1029 } 1030 } 1031 1032 thread->poller_unregistered = false; 1033 } 1034 } 1035 1036 1037 if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITING)) { 1038 thread_exit(thread, now); 1039 } 1040 1041 thread_update_stats(thread, spdk_get_ticks(), now, rc); 1042 1043 tls_thread = orig_thread; 1044 1045 return rc; 1046 } 1047 1048 uint64_t 1049 spdk_thread_next_poller_expiration(struct spdk_thread *thread) 1050 { 1051 struct spdk_poller *poller; 1052 1053 poller = thread->first_timed_poller; 1054 if (poller) { 1055 return poller->next_run_tick; 1056 } 1057 1058 return 0; 1059 } 1060 1061 int 1062 spdk_thread_has_active_pollers(struct spdk_thread *thread) 1063 { 1064 return !TAILQ_EMPTY(&thread->active_pollers); 1065 } 1066 1067 static bool 1068 thread_has_unpaused_pollers(struct spdk_thread *thread) 1069 { 1070 if (TAILQ_EMPTY(&thread->active_pollers) && 1071 RB_EMPTY(&thread->timed_pollers)) { 1072 return false; 1073 } 1074 1075 return true; 1076 } 1077 1078 bool 1079 spdk_thread_has_pollers(struct spdk_thread *thread) 1080 { 1081 if (!thread_has_unpaused_pollers(thread) && 1082 TAILQ_EMPTY(&thread->paused_pollers)) { 1083 return false; 1084 } 1085 1086 return true; 1087 } 1088 1089 bool 1090 spdk_thread_is_idle(struct spdk_thread *thread) 1091 { 1092 if (spdk_ring_count(thread->messages) || 1093 thread_has_unpaused_pollers(thread) || 1094 thread->critical_msg != NULL) { 1095 return false; 1096 } 1097 1098 return true; 1099 } 1100 1101 uint32_t 1102 spdk_thread_get_count(void) 1103 { 1104 /* 1105 * Return cached value of the current thread count. We could acquire the 1106 * lock and iterate through the TAILQ of threads to count them, but that 1107 * count could still be invalidated after we release the lock. 1108 */ 1109 return g_thread_count; 1110 } 1111 1112 struct spdk_thread * 1113 spdk_get_thread(void) 1114 { 1115 return _get_thread(); 1116 } 1117 1118 const char * 1119 spdk_thread_get_name(const struct spdk_thread *thread) 1120 { 1121 return thread->name; 1122 } 1123 1124 uint64_t 1125 spdk_thread_get_id(const struct spdk_thread *thread) 1126 { 1127 return thread->id; 1128 } 1129 1130 struct spdk_thread * 1131 spdk_thread_get_by_id(uint64_t id) 1132 { 1133 struct spdk_thread *thread; 1134 1135 if (id == 0 || id >= g_thread_id) { 1136 SPDK_ERRLOG("invalid thread id: %" PRIu64 ".\n", id); 1137 return NULL; 1138 } 1139 pthread_mutex_lock(&g_devlist_mutex); 1140 TAILQ_FOREACH(thread, &g_threads, tailq) { 1141 if (thread->id == id) { 1142 break; 1143 } 1144 } 1145 pthread_mutex_unlock(&g_devlist_mutex); 1146 return thread; 1147 } 1148 1149 int 1150 spdk_thread_get_stats(struct spdk_thread_stats *stats) 1151 { 1152 struct spdk_thread *thread; 1153 1154 thread = _get_thread(); 1155 if (!thread) { 1156 SPDK_ERRLOG("No thread allocated\n"); 1157 return -EINVAL; 1158 } 1159 1160 if (stats == NULL) { 1161 return -EINVAL; 1162 } 1163 1164 *stats = thread->stats; 1165 1166 return 0; 1167 } 1168 1169 uint64_t 1170 spdk_thread_get_last_tsc(struct spdk_thread *thread) 1171 { 1172 if (thread == NULL) { 1173 thread = _get_thread(); 1174 } 1175 1176 return thread->tsc_last; 1177 } 1178 1179 static inline int 1180 thread_send_msg_notification(const struct spdk_thread *target_thread) 1181 { 1182 uint64_t notify = 1; 1183 int rc; 1184 1185 /* Not necessary to do notification if interrupt facility is not enabled */ 1186 if (spdk_likely(!spdk_interrupt_mode_is_enabled())) { 1187 return 0; 1188 } 1189 1190 /* When each spdk_thread can switch between poll and interrupt mode dynamically, 1191 * after sending thread msg, it is necessary to check whether target thread runs in 1192 * interrupt mode and then decide whether do event notification. 1193 */ 1194 if (spdk_unlikely(target_thread->in_interrupt)) { 1195 rc = write(target_thread->msg_fd, ¬ify, sizeof(notify)); 1196 if (rc < 0) { 1197 SPDK_ERRLOG("failed to notify msg_queue: %s.\n", spdk_strerror(errno)); 1198 return -EIO; 1199 } 1200 } 1201 1202 return 0; 1203 } 1204 1205 int 1206 spdk_thread_send_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void *ctx) 1207 { 1208 struct spdk_thread *local_thread; 1209 struct spdk_msg *msg; 1210 int rc; 1211 1212 assert(thread != NULL); 1213 1214 if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITED)) { 1215 SPDK_ERRLOG("Thread %s is marked as exited.\n", thread->name); 1216 return -EIO; 1217 } 1218 1219 local_thread = _get_thread(); 1220 1221 msg = NULL; 1222 if (local_thread != NULL) { 1223 if (local_thread->msg_cache_count > 0) { 1224 msg = SLIST_FIRST(&local_thread->msg_cache); 1225 assert(msg != NULL); 1226 SLIST_REMOVE_HEAD(&local_thread->msg_cache, link); 1227 local_thread->msg_cache_count--; 1228 } 1229 } 1230 1231 if (msg == NULL) { 1232 msg = spdk_mempool_get(g_spdk_msg_mempool); 1233 if (!msg) { 1234 SPDK_ERRLOG("msg could not be allocated\n"); 1235 return -ENOMEM; 1236 } 1237 } 1238 1239 msg->fn = fn; 1240 msg->arg = ctx; 1241 1242 rc = spdk_ring_enqueue(thread->messages, (void **)&msg, 1, NULL); 1243 if (rc != 1) { 1244 SPDK_ERRLOG("msg could not be enqueued\n"); 1245 spdk_mempool_put(g_spdk_msg_mempool, msg); 1246 return -EIO; 1247 } 1248 1249 return thread_send_msg_notification(thread); 1250 } 1251 1252 int 1253 spdk_thread_send_critical_msg(struct spdk_thread *thread, spdk_msg_fn fn) 1254 { 1255 spdk_msg_fn expected = NULL; 1256 1257 if (!__atomic_compare_exchange_n(&thread->critical_msg, &expected, fn, false, __ATOMIC_SEQ_CST, 1258 __ATOMIC_SEQ_CST)) { 1259 return -EIO; 1260 } 1261 1262 return thread_send_msg_notification(thread); 1263 } 1264 1265 #ifdef __linux__ 1266 static int 1267 interrupt_timerfd_process(void *arg) 1268 { 1269 struct spdk_poller *poller = arg; 1270 uint64_t exp; 1271 int rc; 1272 1273 /* clear the level of interval timer */ 1274 rc = read(poller->interruptfd, &exp, sizeof(exp)); 1275 if (rc < 0) { 1276 if (rc == -EAGAIN) { 1277 return 0; 1278 } 1279 1280 return rc; 1281 } 1282 1283 SPDK_DTRACE_PROBE2(timerfd_exec, poller->fn, poller->arg); 1284 1285 return poller->fn(poller->arg); 1286 } 1287 1288 static int 1289 period_poller_interrupt_init(struct spdk_poller *poller) 1290 { 1291 struct spdk_fd_group *fgrp = poller->thread->fgrp; 1292 int timerfd; 1293 int rc; 1294 1295 SPDK_DEBUGLOG(thread, "timerfd init for periodic poller %s\n", poller->name); 1296 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 1297 if (timerfd < 0) { 1298 return -errno; 1299 } 1300 1301 rc = SPDK_FD_GROUP_ADD(fgrp, timerfd, interrupt_timerfd_process, poller); 1302 if (rc < 0) { 1303 close(timerfd); 1304 return rc; 1305 } 1306 1307 poller->interruptfd = timerfd; 1308 return 0; 1309 } 1310 1311 static void 1312 period_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode) 1313 { 1314 int timerfd = poller->interruptfd; 1315 uint64_t now_tick = spdk_get_ticks(); 1316 uint64_t ticks = spdk_get_ticks_hz(); 1317 int ret; 1318 struct itimerspec new_tv = {}; 1319 struct itimerspec old_tv = {}; 1320 1321 assert(poller->period_ticks != 0); 1322 assert(timerfd >= 0); 1323 1324 SPDK_DEBUGLOG(thread, "timerfd set poller %s into %s mode\n", poller->name, 1325 interrupt_mode ? "interrupt" : "poll"); 1326 1327 if (interrupt_mode) { 1328 /* Set repeated timer expiration */ 1329 new_tv.it_interval.tv_sec = poller->period_ticks / ticks; 1330 new_tv.it_interval.tv_nsec = poller->period_ticks % ticks * SPDK_SEC_TO_NSEC / ticks; 1331 1332 /* Update next timer expiration */ 1333 if (poller->next_run_tick == 0) { 1334 poller->next_run_tick = now_tick + poller->period_ticks; 1335 } else if (poller->next_run_tick < now_tick) { 1336 poller->next_run_tick = now_tick; 1337 } 1338 1339 new_tv.it_value.tv_sec = (poller->next_run_tick - now_tick) / ticks; 1340 new_tv.it_value.tv_nsec = (poller->next_run_tick - now_tick) % ticks * SPDK_SEC_TO_NSEC / ticks; 1341 1342 ret = timerfd_settime(timerfd, 0, &new_tv, NULL); 1343 if (ret < 0) { 1344 SPDK_ERRLOG("Failed to arm timerfd: error(%d)\n", errno); 1345 assert(false); 1346 } 1347 } else { 1348 /* Disarm the timer */ 1349 ret = timerfd_settime(timerfd, 0, &new_tv, &old_tv); 1350 if (ret < 0) { 1351 /* timerfd_settime's failure indicates that the timerfd is in error */ 1352 SPDK_ERRLOG("Failed to disarm timerfd: error(%d)\n", errno); 1353 assert(false); 1354 } 1355 1356 /* In order to reuse poller_insert_timer, fix now_tick, so next_run_tick would be 1357 * now_tick + ticks * old_tv.it_value.tv_sec + (ticks * old_tv.it_value.tv_nsec) / SPDK_SEC_TO_NSEC 1358 */ 1359 now_tick = now_tick - poller->period_ticks + ticks * old_tv.it_value.tv_sec + \ 1360 (ticks * old_tv.it_value.tv_nsec) / SPDK_SEC_TO_NSEC; 1361 poller_remove_timer(poller->thread, poller); 1362 poller_insert_timer(poller->thread, poller, now_tick); 1363 } 1364 } 1365 1366 static void 1367 poller_interrupt_fini(struct spdk_poller *poller) 1368 { 1369 SPDK_DEBUGLOG(thread, "interrupt fini for poller %s\n", poller->name); 1370 assert(poller->interruptfd >= 0); 1371 spdk_fd_group_remove(poller->thread->fgrp, poller->interruptfd); 1372 close(poller->interruptfd); 1373 poller->interruptfd = -1; 1374 } 1375 1376 static int 1377 busy_poller_interrupt_init(struct spdk_poller *poller) 1378 { 1379 int busy_efd; 1380 int rc; 1381 1382 SPDK_DEBUGLOG(thread, "busy_efd init for busy poller %s\n", poller->name); 1383 busy_efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); 1384 if (busy_efd < 0) { 1385 SPDK_ERRLOG("Failed to create eventfd for Poller(%s).\n", poller->name); 1386 return -errno; 1387 } 1388 1389 rc = spdk_fd_group_add(poller->thread->fgrp, busy_efd, 1390 poller->fn, poller->arg, poller->name); 1391 if (rc < 0) { 1392 close(busy_efd); 1393 return rc; 1394 } 1395 1396 poller->interruptfd = busy_efd; 1397 return 0; 1398 } 1399 1400 static void 1401 busy_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode) 1402 { 1403 int busy_efd = poller->interruptfd; 1404 uint64_t notify = 1; 1405 int rc __attribute__((unused)); 1406 1407 assert(busy_efd >= 0); 1408 1409 if (interrupt_mode) { 1410 /* Write without read on eventfd will get it repeatedly triggered. */ 1411 if (write(busy_efd, ¬ify, sizeof(notify)) < 0) { 1412 SPDK_ERRLOG("Failed to set busy wait for Poller(%s).\n", poller->name); 1413 } 1414 } else { 1415 /* Read on eventfd will clear its level triggering. */ 1416 rc = read(busy_efd, ¬ify, sizeof(notify)); 1417 } 1418 } 1419 1420 #else 1421 1422 static int 1423 period_poller_interrupt_init(struct spdk_poller *poller) 1424 { 1425 return -ENOTSUP; 1426 } 1427 1428 static void 1429 period_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode) 1430 { 1431 } 1432 1433 static void 1434 poller_interrupt_fini(struct spdk_poller *poller) 1435 { 1436 } 1437 1438 static int 1439 busy_poller_interrupt_init(struct spdk_poller *poller) 1440 { 1441 return -ENOTSUP; 1442 } 1443 1444 static void 1445 busy_poller_set_interrupt_mode(struct spdk_poller *poller, void *cb_arg, bool interrupt_mode) 1446 { 1447 } 1448 1449 #endif 1450 1451 void 1452 spdk_poller_register_interrupt(struct spdk_poller *poller, 1453 spdk_poller_set_interrupt_mode_cb cb_fn, 1454 void *cb_arg) 1455 { 1456 assert(poller != NULL); 1457 assert(cb_fn != NULL); 1458 assert(spdk_get_thread() == poller->thread); 1459 1460 if (!spdk_interrupt_mode_is_enabled()) { 1461 return; 1462 } 1463 1464 /* when a poller is created we don't know if the user is ever going to 1465 * enable interrupts on it by calling this function, so the poller 1466 * registration function has to immediately create a interruptfd. 1467 * When this function does get called by user, we have to then destroy 1468 * that interruptfd. 1469 */ 1470 if (poller->set_intr_cb_fn && poller->interruptfd >= 0) { 1471 poller_interrupt_fini(poller); 1472 } 1473 1474 poller->set_intr_cb_fn = cb_fn; 1475 poller->set_intr_cb_arg = cb_arg; 1476 1477 /* Set poller into interrupt mode if thread is in interrupt. */ 1478 if (poller->thread->in_interrupt) { 1479 poller->set_intr_cb_fn(poller, poller->set_intr_cb_arg, true); 1480 } 1481 } 1482 1483 static uint64_t 1484 convert_us_to_ticks(uint64_t us) 1485 { 1486 uint64_t quotient, remainder, ticks; 1487 1488 if (us) { 1489 quotient = us / SPDK_SEC_TO_USEC; 1490 remainder = us % SPDK_SEC_TO_USEC; 1491 ticks = spdk_get_ticks_hz(); 1492 1493 return ticks * quotient + (ticks * remainder) / SPDK_SEC_TO_USEC; 1494 } else { 1495 return 0; 1496 } 1497 } 1498 1499 static struct spdk_poller * 1500 poller_register(spdk_poller_fn fn, 1501 void *arg, 1502 uint64_t period_microseconds, 1503 const char *name) 1504 { 1505 struct spdk_thread *thread; 1506 struct spdk_poller *poller; 1507 1508 thread = spdk_get_thread(); 1509 if (!thread) { 1510 assert(false); 1511 return NULL; 1512 } 1513 1514 if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITED)) { 1515 SPDK_ERRLOG("thread %s is marked as exited\n", thread->name); 1516 return NULL; 1517 } 1518 1519 poller = calloc(1, sizeof(*poller)); 1520 if (poller == NULL) { 1521 SPDK_ERRLOG("Poller memory allocation failed\n"); 1522 return NULL; 1523 } 1524 1525 if (name) { 1526 snprintf(poller->name, sizeof(poller->name), "%s", name); 1527 } else { 1528 snprintf(poller->name, sizeof(poller->name), "%p", fn); 1529 } 1530 1531 poller->state = SPDK_POLLER_STATE_WAITING; 1532 poller->fn = fn; 1533 poller->arg = arg; 1534 poller->thread = thread; 1535 poller->interruptfd = -1; 1536 if (thread->next_poller_id == 0) { 1537 SPDK_WARNLOG("Poller ID rolled over. Poller ID is duplicated.\n"); 1538 thread->next_poller_id = 1; 1539 } 1540 poller->id = thread->next_poller_id++; 1541 1542 poller->period_ticks = convert_us_to_ticks(period_microseconds); 1543 1544 if (spdk_interrupt_mode_is_enabled()) { 1545 int rc; 1546 1547 if (period_microseconds) { 1548 rc = period_poller_interrupt_init(poller); 1549 if (rc < 0) { 1550 SPDK_ERRLOG("Failed to register interruptfd for periodic poller: %s\n", spdk_strerror(-rc)); 1551 free(poller); 1552 return NULL; 1553 } 1554 1555 spdk_poller_register_interrupt(poller, period_poller_set_interrupt_mode, NULL); 1556 } else { 1557 /* If the poller doesn't have a period, create interruptfd that's always 1558 * busy automatically when running in interrupt mode. 1559 */ 1560 rc = busy_poller_interrupt_init(poller); 1561 if (rc > 0) { 1562 SPDK_ERRLOG("Failed to register interruptfd for busy poller: %s\n", spdk_strerror(-rc)); 1563 free(poller); 1564 return NULL; 1565 } 1566 1567 spdk_poller_register_interrupt(poller, busy_poller_set_interrupt_mode, NULL); 1568 } 1569 } 1570 1571 thread_insert_poller(thread, poller); 1572 1573 return poller; 1574 } 1575 1576 struct spdk_poller * 1577 spdk_poller_register(spdk_poller_fn fn, 1578 void *arg, 1579 uint64_t period_microseconds) 1580 { 1581 return poller_register(fn, arg, period_microseconds, NULL); 1582 } 1583 1584 struct spdk_poller * 1585 spdk_poller_register_named(spdk_poller_fn fn, 1586 void *arg, 1587 uint64_t period_microseconds, 1588 const char *name) 1589 { 1590 return poller_register(fn, arg, period_microseconds, name); 1591 } 1592 1593 static void 1594 wrong_thread(const char *func, const char *name, struct spdk_thread *thread, 1595 struct spdk_thread *curthread) 1596 { 1597 if (thread == NULL) { 1598 SPDK_ERRLOG("%s(%s) called with NULL thread\n", func, name); 1599 abort(); 1600 } 1601 SPDK_ERRLOG("%s(%s) called from wrong thread %s:%" PRIu64 " (should be " 1602 "%s:%" PRIu64 ")\n", func, name, curthread->name, curthread->id, 1603 thread->name, thread->id); 1604 assert(false); 1605 } 1606 1607 void 1608 spdk_poller_unregister(struct spdk_poller **ppoller) 1609 { 1610 struct spdk_thread *thread; 1611 struct spdk_poller *poller; 1612 1613 poller = *ppoller; 1614 if (poller == NULL) { 1615 return; 1616 } 1617 1618 *ppoller = NULL; 1619 1620 thread = spdk_get_thread(); 1621 if (!thread) { 1622 assert(false); 1623 return; 1624 } 1625 1626 if (poller->thread != thread) { 1627 wrong_thread(__func__, poller->name, poller->thread, thread); 1628 return; 1629 } 1630 1631 if (spdk_interrupt_mode_is_enabled()) { 1632 /* Release the interrupt resource for period or busy poller */ 1633 if (poller->interruptfd >= 0) { 1634 poller_interrupt_fini(poller); 1635 } 1636 1637 /* Mark there is poller unregistered. Then unregistered pollers will 1638 * get reaped by spdk_thread_poll also in intr mode. 1639 */ 1640 thread->poller_unregistered = true; 1641 } 1642 1643 /* If the poller was paused, put it on the active_pollers list so that 1644 * its unregistration can be processed by spdk_thread_poll(). 1645 */ 1646 if (poller->state == SPDK_POLLER_STATE_PAUSED) { 1647 TAILQ_REMOVE(&thread->paused_pollers, poller, tailq); 1648 TAILQ_INSERT_TAIL(&thread->active_pollers, poller, tailq); 1649 poller->period_ticks = 0; 1650 } 1651 1652 /* Simply set the state to unregistered. The poller will get cleaned up 1653 * in a subsequent call to spdk_thread_poll(). 1654 */ 1655 poller->state = SPDK_POLLER_STATE_UNREGISTERED; 1656 } 1657 1658 void 1659 spdk_poller_pause(struct spdk_poller *poller) 1660 { 1661 struct spdk_thread *thread; 1662 1663 thread = spdk_get_thread(); 1664 if (!thread) { 1665 assert(false); 1666 return; 1667 } 1668 1669 if (poller->thread != thread) { 1670 wrong_thread(__func__, poller->name, poller->thread, thread); 1671 return; 1672 } 1673 1674 /* We just set its state to SPDK_POLLER_STATE_PAUSING and let 1675 * spdk_thread_poll() move it. It allows a poller to be paused from 1676 * another one's context without breaking the TAILQ_FOREACH_REVERSE_SAFE 1677 * iteration, or from within itself without breaking the logic to always 1678 * remove the closest timed poller in the TAILQ_FOREACH_SAFE iteration. 1679 */ 1680 switch (poller->state) { 1681 case SPDK_POLLER_STATE_PAUSED: 1682 case SPDK_POLLER_STATE_PAUSING: 1683 break; 1684 case SPDK_POLLER_STATE_RUNNING: 1685 case SPDK_POLLER_STATE_WAITING: 1686 poller->state = SPDK_POLLER_STATE_PAUSING; 1687 break; 1688 default: 1689 assert(false); 1690 break; 1691 } 1692 } 1693 1694 void 1695 spdk_poller_resume(struct spdk_poller *poller) 1696 { 1697 struct spdk_thread *thread; 1698 1699 thread = spdk_get_thread(); 1700 if (!thread) { 1701 assert(false); 1702 return; 1703 } 1704 1705 if (poller->thread != thread) { 1706 wrong_thread(__func__, poller->name, poller->thread, thread); 1707 return; 1708 } 1709 1710 /* If a poller is paused it has to be removed from the paused pollers 1711 * list and put on the active list or timer tree depending on its 1712 * period_ticks. If a poller is still in the process of being paused, 1713 * we just need to flip its state back to waiting, as it's already on 1714 * the appropriate list or tree. 1715 */ 1716 switch (poller->state) { 1717 case SPDK_POLLER_STATE_PAUSED: 1718 TAILQ_REMOVE(&thread->paused_pollers, poller, tailq); 1719 thread_insert_poller(thread, poller); 1720 /* fallthrough */ 1721 case SPDK_POLLER_STATE_PAUSING: 1722 poller->state = SPDK_POLLER_STATE_WAITING; 1723 break; 1724 case SPDK_POLLER_STATE_RUNNING: 1725 case SPDK_POLLER_STATE_WAITING: 1726 break; 1727 default: 1728 assert(false); 1729 break; 1730 } 1731 } 1732 1733 const char * 1734 spdk_poller_get_name(struct spdk_poller *poller) 1735 { 1736 return poller->name; 1737 } 1738 1739 uint64_t 1740 spdk_poller_get_id(struct spdk_poller *poller) 1741 { 1742 return poller->id; 1743 } 1744 1745 const char * 1746 spdk_poller_get_state_str(struct spdk_poller *poller) 1747 { 1748 switch (poller->state) { 1749 case SPDK_POLLER_STATE_WAITING: 1750 return "waiting"; 1751 case SPDK_POLLER_STATE_RUNNING: 1752 return "running"; 1753 case SPDK_POLLER_STATE_UNREGISTERED: 1754 return "unregistered"; 1755 case SPDK_POLLER_STATE_PAUSING: 1756 return "pausing"; 1757 case SPDK_POLLER_STATE_PAUSED: 1758 return "paused"; 1759 default: 1760 return NULL; 1761 } 1762 } 1763 1764 uint64_t 1765 spdk_poller_get_period_ticks(struct spdk_poller *poller) 1766 { 1767 return poller->period_ticks; 1768 } 1769 1770 void 1771 spdk_poller_get_stats(struct spdk_poller *poller, struct spdk_poller_stats *stats) 1772 { 1773 stats->run_count = poller->run_count; 1774 stats->busy_count = poller->busy_count; 1775 } 1776 1777 struct spdk_poller * 1778 spdk_thread_get_first_active_poller(struct spdk_thread *thread) 1779 { 1780 return TAILQ_FIRST(&thread->active_pollers); 1781 } 1782 1783 struct spdk_poller * 1784 spdk_thread_get_next_active_poller(struct spdk_poller *prev) 1785 { 1786 return TAILQ_NEXT(prev, tailq); 1787 } 1788 1789 struct spdk_poller * 1790 spdk_thread_get_first_timed_poller(struct spdk_thread *thread) 1791 { 1792 return RB_MIN(timed_pollers_tree, &thread->timed_pollers); 1793 } 1794 1795 struct spdk_poller * 1796 spdk_thread_get_next_timed_poller(struct spdk_poller *prev) 1797 { 1798 return RB_NEXT(timed_pollers_tree, &thread->timed_pollers, prev); 1799 } 1800 1801 struct spdk_poller * 1802 spdk_thread_get_first_paused_poller(struct spdk_thread *thread) 1803 { 1804 return TAILQ_FIRST(&thread->paused_pollers); 1805 } 1806 1807 struct spdk_poller * 1808 spdk_thread_get_next_paused_poller(struct spdk_poller *prev) 1809 { 1810 return TAILQ_NEXT(prev, tailq); 1811 } 1812 1813 struct spdk_io_channel * 1814 spdk_thread_get_first_io_channel(struct spdk_thread *thread) 1815 { 1816 return RB_MIN(io_channel_tree, &thread->io_channels); 1817 } 1818 1819 struct spdk_io_channel * 1820 spdk_thread_get_next_io_channel(struct spdk_io_channel *prev) 1821 { 1822 return RB_NEXT(io_channel_tree, &thread->io_channels, prev); 1823 } 1824 1825 struct call_thread { 1826 struct spdk_thread *cur_thread; 1827 spdk_msg_fn fn; 1828 void *ctx; 1829 1830 struct spdk_thread *orig_thread; 1831 spdk_msg_fn cpl; 1832 }; 1833 1834 static void 1835 _on_thread(void *ctx) 1836 { 1837 struct call_thread *ct = ctx; 1838 int rc __attribute__((unused)); 1839 1840 ct->fn(ct->ctx); 1841 1842 pthread_mutex_lock(&g_devlist_mutex); 1843 ct->cur_thread = TAILQ_NEXT(ct->cur_thread, tailq); 1844 pthread_mutex_unlock(&g_devlist_mutex); 1845 1846 if (!ct->cur_thread) { 1847 SPDK_DEBUGLOG(thread, "Completed thread iteration\n"); 1848 1849 rc = spdk_thread_send_msg(ct->orig_thread, ct->cpl, ct->ctx); 1850 free(ctx); 1851 } else { 1852 SPDK_DEBUGLOG(thread, "Continuing thread iteration to %s\n", 1853 ct->cur_thread->name); 1854 1855 rc = spdk_thread_send_msg(ct->cur_thread, _on_thread, ctx); 1856 } 1857 assert(rc == 0); 1858 } 1859 1860 void 1861 spdk_for_each_thread(spdk_msg_fn fn, void *ctx, spdk_msg_fn cpl) 1862 { 1863 struct call_thread *ct; 1864 struct spdk_thread *thread; 1865 int rc __attribute__((unused)); 1866 1867 ct = calloc(1, sizeof(*ct)); 1868 if (!ct) { 1869 SPDK_ERRLOG("Unable to perform thread iteration\n"); 1870 cpl(ctx); 1871 return; 1872 } 1873 1874 ct->fn = fn; 1875 ct->ctx = ctx; 1876 ct->cpl = cpl; 1877 1878 thread = _get_thread(); 1879 if (!thread) { 1880 SPDK_ERRLOG("No thread allocated\n"); 1881 free(ct); 1882 cpl(ctx); 1883 return; 1884 } 1885 ct->orig_thread = thread; 1886 1887 pthread_mutex_lock(&g_devlist_mutex); 1888 ct->cur_thread = TAILQ_FIRST(&g_threads); 1889 pthread_mutex_unlock(&g_devlist_mutex); 1890 1891 SPDK_DEBUGLOG(thread, "Starting thread iteration from %s\n", 1892 ct->orig_thread->name); 1893 1894 rc = spdk_thread_send_msg(ct->cur_thread, _on_thread, ct); 1895 assert(rc == 0); 1896 } 1897 1898 static inline void 1899 poller_set_interrupt_mode(struct spdk_poller *poller, bool interrupt_mode) 1900 { 1901 if (poller->state == SPDK_POLLER_STATE_UNREGISTERED) { 1902 return; 1903 } 1904 1905 if (!poller->set_intr_cb_fn) { 1906 SPDK_ERRLOG("Poller(%s) doesn't support set interrupt mode.\n", poller->name); 1907 assert(false); 1908 return; 1909 } 1910 1911 poller->set_intr_cb_fn(poller, poller->set_intr_cb_arg, interrupt_mode); 1912 } 1913 1914 void 1915 spdk_thread_set_interrupt_mode(bool enable_interrupt) 1916 { 1917 struct spdk_thread *thread = _get_thread(); 1918 struct spdk_poller *poller, *tmp; 1919 1920 assert(thread); 1921 assert(spdk_interrupt_mode_is_enabled()); 1922 1923 SPDK_NOTICELOG("Set spdk_thread (%s) to %s mode from %s mode.\n", 1924 thread->name, enable_interrupt ? "intr" : "poll", 1925 thread->in_interrupt ? "intr" : "poll"); 1926 1927 if (thread->in_interrupt == enable_interrupt) { 1928 return; 1929 } 1930 1931 /* Set pollers to expected mode */ 1932 RB_FOREACH_SAFE(poller, timed_pollers_tree, &thread->timed_pollers, tmp) { 1933 poller_set_interrupt_mode(poller, enable_interrupt); 1934 } 1935 TAILQ_FOREACH_SAFE(poller, &thread->active_pollers, tailq, tmp) { 1936 poller_set_interrupt_mode(poller, enable_interrupt); 1937 } 1938 /* All paused pollers will go to work in interrupt mode */ 1939 TAILQ_FOREACH_SAFE(poller, &thread->paused_pollers, tailq, tmp) { 1940 poller_set_interrupt_mode(poller, enable_interrupt); 1941 } 1942 1943 thread->in_interrupt = enable_interrupt; 1944 return; 1945 } 1946 1947 static struct io_device * 1948 io_device_get(void *io_device) 1949 { 1950 struct io_device find = {}; 1951 1952 find.io_device = io_device; 1953 return RB_FIND(io_device_tree, &g_io_devices, &find); 1954 } 1955 1956 void 1957 spdk_io_device_register(void *io_device, spdk_io_channel_create_cb create_cb, 1958 spdk_io_channel_destroy_cb destroy_cb, uint32_t ctx_size, 1959 const char *name) 1960 { 1961 struct io_device *dev, *tmp; 1962 struct spdk_thread *thread; 1963 1964 assert(io_device != NULL); 1965 assert(create_cb != NULL); 1966 assert(destroy_cb != NULL); 1967 1968 thread = spdk_get_thread(); 1969 if (!thread) { 1970 SPDK_ERRLOG("called from non-SPDK thread\n"); 1971 assert(false); 1972 return; 1973 } 1974 1975 dev = calloc(1, sizeof(struct io_device)); 1976 if (dev == NULL) { 1977 SPDK_ERRLOG("could not allocate io_device\n"); 1978 return; 1979 } 1980 1981 dev->io_device = io_device; 1982 if (name) { 1983 snprintf(dev->name, sizeof(dev->name), "%s", name); 1984 } else { 1985 snprintf(dev->name, sizeof(dev->name), "%p", dev); 1986 } 1987 dev->create_cb = create_cb; 1988 dev->destroy_cb = destroy_cb; 1989 dev->unregister_cb = NULL; 1990 dev->ctx_size = ctx_size; 1991 dev->for_each_count = 0; 1992 dev->unregistered = false; 1993 dev->refcnt = 0; 1994 1995 SPDK_DEBUGLOG(thread, "Registering io_device %s (%p) on thread %s\n", 1996 dev->name, dev->io_device, thread->name); 1997 1998 pthread_mutex_lock(&g_devlist_mutex); 1999 tmp = RB_INSERT(io_device_tree, &g_io_devices, dev); 2000 if (tmp != NULL) { 2001 SPDK_ERRLOG("io_device %p already registered (old:%s new:%s)\n", 2002 io_device, tmp->name, dev->name); 2003 free(dev); 2004 } 2005 2006 pthread_mutex_unlock(&g_devlist_mutex); 2007 } 2008 2009 static void 2010 _finish_unregister(void *arg) 2011 { 2012 struct io_device *dev = arg; 2013 struct spdk_thread *thread; 2014 2015 thread = spdk_get_thread(); 2016 assert(thread == dev->unregister_thread); 2017 2018 SPDK_DEBUGLOG(thread, "Finishing unregistration of io_device %s (%p) on thread %s\n", 2019 dev->name, dev->io_device, thread->name); 2020 2021 assert(thread->pending_unregister_count > 0); 2022 thread->pending_unregister_count--; 2023 2024 dev->unregister_cb(dev->io_device); 2025 free(dev); 2026 } 2027 2028 static void 2029 io_device_free(struct io_device *dev) 2030 { 2031 int rc __attribute__((unused)); 2032 2033 if (dev->unregister_cb == NULL) { 2034 free(dev); 2035 } else { 2036 assert(dev->unregister_thread != NULL); 2037 SPDK_DEBUGLOG(thread, "io_device %s (%p) needs to unregister from thread %s\n", 2038 dev->name, dev->io_device, dev->unregister_thread->name); 2039 rc = spdk_thread_send_msg(dev->unregister_thread, _finish_unregister, dev); 2040 assert(rc == 0); 2041 } 2042 } 2043 2044 void 2045 spdk_io_device_unregister(void *io_device, spdk_io_device_unregister_cb unregister_cb) 2046 { 2047 struct io_device *dev; 2048 uint32_t refcnt; 2049 struct spdk_thread *thread; 2050 2051 thread = spdk_get_thread(); 2052 if (!thread) { 2053 SPDK_ERRLOG("called from non-SPDK thread\n"); 2054 assert(false); 2055 return; 2056 } 2057 2058 pthread_mutex_lock(&g_devlist_mutex); 2059 dev = io_device_get(io_device); 2060 if (!dev) { 2061 SPDK_ERRLOG("io_device %p not found\n", io_device); 2062 assert(false); 2063 pthread_mutex_unlock(&g_devlist_mutex); 2064 return; 2065 } 2066 2067 if (dev->for_each_count > 0) { 2068 SPDK_ERRLOG("io_device %s (%p) has %u for_each calls outstanding\n", 2069 dev->name, io_device, dev->for_each_count); 2070 pthread_mutex_unlock(&g_devlist_mutex); 2071 return; 2072 } 2073 2074 dev->unregister_cb = unregister_cb; 2075 dev->unregistered = true; 2076 RB_REMOVE(io_device_tree, &g_io_devices, dev); 2077 refcnt = dev->refcnt; 2078 dev->unregister_thread = thread; 2079 pthread_mutex_unlock(&g_devlist_mutex); 2080 2081 SPDK_DEBUGLOG(thread, "Unregistering io_device %s (%p) from thread %s\n", 2082 dev->name, dev->io_device, thread->name); 2083 2084 if (unregister_cb) { 2085 thread->pending_unregister_count++; 2086 } 2087 2088 if (refcnt > 0) { 2089 /* defer deletion */ 2090 return; 2091 } 2092 2093 io_device_free(dev); 2094 } 2095 2096 const char * 2097 spdk_io_device_get_name(struct io_device *dev) 2098 { 2099 return dev->name; 2100 } 2101 2102 static struct spdk_io_channel * 2103 thread_get_io_channel(struct spdk_thread *thread, struct io_device *dev) 2104 { 2105 struct spdk_io_channel find = {}; 2106 2107 find.dev = dev; 2108 return RB_FIND(io_channel_tree, &thread->io_channels, &find); 2109 } 2110 2111 struct spdk_io_channel * 2112 spdk_get_io_channel(void *io_device) 2113 { 2114 struct spdk_io_channel *ch; 2115 struct spdk_thread *thread; 2116 struct io_device *dev; 2117 int rc; 2118 2119 pthread_mutex_lock(&g_devlist_mutex); 2120 dev = io_device_get(io_device); 2121 if (dev == NULL) { 2122 SPDK_ERRLOG("could not find io_device %p\n", io_device); 2123 pthread_mutex_unlock(&g_devlist_mutex); 2124 return NULL; 2125 } 2126 2127 thread = _get_thread(); 2128 if (!thread) { 2129 SPDK_ERRLOG("No thread allocated\n"); 2130 pthread_mutex_unlock(&g_devlist_mutex); 2131 return NULL; 2132 } 2133 2134 if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITED)) { 2135 SPDK_ERRLOG("Thread %s is marked as exited\n", thread->name); 2136 pthread_mutex_unlock(&g_devlist_mutex); 2137 return NULL; 2138 } 2139 2140 ch = thread_get_io_channel(thread, dev); 2141 if (ch != NULL) { 2142 ch->ref++; 2143 2144 SPDK_DEBUGLOG(thread, "Get io_channel %p for io_device %s (%p) on thread %s refcnt %u\n", 2145 ch, dev->name, dev->io_device, thread->name, ch->ref); 2146 2147 /* 2148 * An I/O channel already exists for this device on this 2149 * thread, so return it. 2150 */ 2151 pthread_mutex_unlock(&g_devlist_mutex); 2152 spdk_trace_record(TRACE_THREAD_IOCH_GET, 0, 0, 2153 (uint64_t)spdk_io_channel_get_ctx(ch), ch->ref); 2154 return ch; 2155 } 2156 2157 ch = calloc(1, sizeof(*ch) + dev->ctx_size); 2158 if (ch == NULL) { 2159 SPDK_ERRLOG("could not calloc spdk_io_channel\n"); 2160 pthread_mutex_unlock(&g_devlist_mutex); 2161 return NULL; 2162 } 2163 2164 ch->dev = dev; 2165 ch->destroy_cb = dev->destroy_cb; 2166 ch->thread = thread; 2167 ch->ref = 1; 2168 ch->destroy_ref = 0; 2169 RB_INSERT(io_channel_tree, &thread->io_channels, ch); 2170 2171 SPDK_DEBUGLOG(thread, "Get io_channel %p for io_device %s (%p) on thread %s refcnt %u\n", 2172 ch, dev->name, dev->io_device, thread->name, ch->ref); 2173 2174 dev->refcnt++; 2175 2176 pthread_mutex_unlock(&g_devlist_mutex); 2177 2178 rc = dev->create_cb(io_device, (uint8_t *)ch + sizeof(*ch)); 2179 if (rc != 0) { 2180 pthread_mutex_lock(&g_devlist_mutex); 2181 RB_REMOVE(io_channel_tree, &ch->thread->io_channels, ch); 2182 dev->refcnt--; 2183 free(ch); 2184 pthread_mutex_unlock(&g_devlist_mutex); 2185 return NULL; 2186 } 2187 2188 spdk_trace_record(TRACE_THREAD_IOCH_GET, 0, 0, (uint64_t)spdk_io_channel_get_ctx(ch), 1); 2189 return ch; 2190 } 2191 2192 static void 2193 put_io_channel(void *arg) 2194 { 2195 struct spdk_io_channel *ch = arg; 2196 bool do_remove_dev = true; 2197 struct spdk_thread *thread; 2198 2199 thread = spdk_get_thread(); 2200 if (!thread) { 2201 SPDK_ERRLOG("called from non-SPDK thread\n"); 2202 assert(false); 2203 return; 2204 } 2205 2206 SPDK_DEBUGLOG(thread, 2207 "Releasing io_channel %p for io_device %s (%p) on thread %s\n", 2208 ch, ch->dev->name, ch->dev->io_device, thread->name); 2209 2210 assert(ch->thread == thread); 2211 2212 ch->destroy_ref--; 2213 2214 if (ch->ref > 0 || ch->destroy_ref > 0) { 2215 /* 2216 * Another reference to the associated io_device was requested 2217 * after this message was sent but before it had a chance to 2218 * execute. 2219 */ 2220 return; 2221 } 2222 2223 pthread_mutex_lock(&g_devlist_mutex); 2224 RB_REMOVE(io_channel_tree, &ch->thread->io_channels, ch); 2225 pthread_mutex_unlock(&g_devlist_mutex); 2226 2227 /* Don't hold the devlist mutex while the destroy_cb is called. */ 2228 ch->destroy_cb(ch->dev->io_device, spdk_io_channel_get_ctx(ch)); 2229 2230 pthread_mutex_lock(&g_devlist_mutex); 2231 ch->dev->refcnt--; 2232 2233 if (!ch->dev->unregistered) { 2234 do_remove_dev = false; 2235 } 2236 2237 if (ch->dev->refcnt > 0) { 2238 do_remove_dev = false; 2239 } 2240 2241 pthread_mutex_unlock(&g_devlist_mutex); 2242 2243 if (do_remove_dev) { 2244 io_device_free(ch->dev); 2245 } 2246 free(ch); 2247 } 2248 2249 void 2250 spdk_put_io_channel(struct spdk_io_channel *ch) 2251 { 2252 struct spdk_thread *thread; 2253 int rc __attribute__((unused)); 2254 2255 spdk_trace_record(TRACE_THREAD_IOCH_PUT, 0, 0, 2256 (uint64_t)spdk_io_channel_get_ctx(ch), ch->ref); 2257 2258 thread = spdk_get_thread(); 2259 if (!thread) { 2260 SPDK_ERRLOG("called from non-SPDK thread\n"); 2261 assert(false); 2262 return; 2263 } 2264 2265 if (ch->thread != thread) { 2266 wrong_thread(__func__, "ch", ch->thread, thread); 2267 return; 2268 } 2269 2270 SPDK_DEBUGLOG(thread, 2271 "Putting io_channel %p for io_device %s (%p) on thread %s refcnt %u\n", 2272 ch, ch->dev->name, ch->dev->io_device, thread->name, ch->ref); 2273 2274 ch->ref--; 2275 2276 if (ch->ref == 0) { 2277 ch->destroy_ref++; 2278 rc = spdk_thread_send_msg(thread, put_io_channel, ch); 2279 assert(rc == 0); 2280 } 2281 } 2282 2283 struct spdk_io_channel * 2284 spdk_io_channel_from_ctx(void *ctx) 2285 { 2286 return (struct spdk_io_channel *)((uint8_t *)ctx - sizeof(struct spdk_io_channel)); 2287 } 2288 2289 struct spdk_thread * 2290 spdk_io_channel_get_thread(struct spdk_io_channel *ch) 2291 { 2292 return ch->thread; 2293 } 2294 2295 void * 2296 spdk_io_channel_get_io_device(struct spdk_io_channel *ch) 2297 { 2298 return ch->dev->io_device; 2299 } 2300 2301 const char * 2302 spdk_io_channel_get_io_device_name(struct spdk_io_channel *ch) 2303 { 2304 return spdk_io_device_get_name(ch->dev); 2305 } 2306 2307 int 2308 spdk_io_channel_get_ref_count(struct spdk_io_channel *ch) 2309 { 2310 return ch->ref; 2311 } 2312 2313 struct spdk_io_channel_iter { 2314 void *io_device; 2315 struct io_device *dev; 2316 spdk_channel_msg fn; 2317 int status; 2318 void *ctx; 2319 struct spdk_io_channel *ch; 2320 2321 struct spdk_thread *cur_thread; 2322 2323 struct spdk_thread *orig_thread; 2324 spdk_channel_for_each_cpl cpl; 2325 }; 2326 2327 void * 2328 spdk_io_channel_iter_get_io_device(struct spdk_io_channel_iter *i) 2329 { 2330 return i->io_device; 2331 } 2332 2333 struct spdk_io_channel * 2334 spdk_io_channel_iter_get_channel(struct spdk_io_channel_iter *i) 2335 { 2336 return i->ch; 2337 } 2338 2339 void * 2340 spdk_io_channel_iter_get_ctx(struct spdk_io_channel_iter *i) 2341 { 2342 return i->ctx; 2343 } 2344 2345 static void 2346 _call_completion(void *ctx) 2347 { 2348 struct spdk_io_channel_iter *i = ctx; 2349 2350 if (i->cpl != NULL) { 2351 i->cpl(i, i->status); 2352 } 2353 free(i); 2354 } 2355 2356 static void 2357 _call_channel(void *ctx) 2358 { 2359 struct spdk_io_channel_iter *i = ctx; 2360 struct spdk_io_channel *ch; 2361 2362 /* 2363 * It is possible that the channel was deleted before this 2364 * message had a chance to execute. If so, skip calling 2365 * the fn() on this thread. 2366 */ 2367 pthread_mutex_lock(&g_devlist_mutex); 2368 ch = thread_get_io_channel(i->cur_thread, i->dev); 2369 pthread_mutex_unlock(&g_devlist_mutex); 2370 2371 if (ch) { 2372 i->fn(i); 2373 } else { 2374 spdk_for_each_channel_continue(i, 0); 2375 } 2376 } 2377 2378 void 2379 spdk_for_each_channel(void *io_device, spdk_channel_msg fn, void *ctx, 2380 spdk_channel_for_each_cpl cpl) 2381 { 2382 struct spdk_thread *thread; 2383 struct spdk_io_channel *ch; 2384 struct spdk_io_channel_iter *i; 2385 int rc __attribute__((unused)); 2386 2387 i = calloc(1, sizeof(*i)); 2388 if (!i) { 2389 SPDK_ERRLOG("Unable to allocate iterator\n"); 2390 return; 2391 } 2392 2393 i->io_device = io_device; 2394 i->fn = fn; 2395 i->ctx = ctx; 2396 i->cpl = cpl; 2397 i->orig_thread = _get_thread(); 2398 2399 pthread_mutex_lock(&g_devlist_mutex); 2400 i->dev = io_device_get(io_device); 2401 if (i->dev == NULL) { 2402 SPDK_ERRLOG("could not find io_device %p\n", io_device); 2403 assert(false); 2404 goto end; 2405 } 2406 2407 TAILQ_FOREACH(thread, &g_threads, tailq) { 2408 ch = thread_get_io_channel(thread, i->dev); 2409 if (ch != NULL) { 2410 ch->dev->for_each_count++; 2411 i->cur_thread = thread; 2412 i->ch = ch; 2413 pthread_mutex_unlock(&g_devlist_mutex); 2414 rc = spdk_thread_send_msg(thread, _call_channel, i); 2415 assert(rc == 0); 2416 return; 2417 } 2418 } 2419 2420 end: 2421 pthread_mutex_unlock(&g_devlist_mutex); 2422 2423 rc = spdk_thread_send_msg(i->orig_thread, _call_completion, i); 2424 assert(rc == 0); 2425 } 2426 2427 void 2428 spdk_for_each_channel_continue(struct spdk_io_channel_iter *i, int status) 2429 { 2430 struct spdk_thread *thread; 2431 struct spdk_io_channel *ch; 2432 int rc __attribute__((unused)); 2433 2434 assert(i->cur_thread == spdk_get_thread()); 2435 2436 i->status = status; 2437 2438 pthread_mutex_lock(&g_devlist_mutex); 2439 if (status) { 2440 goto end; 2441 } 2442 thread = TAILQ_NEXT(i->cur_thread, tailq); 2443 while (thread) { 2444 ch = thread_get_io_channel(thread, i->dev); 2445 if (ch != NULL) { 2446 i->cur_thread = thread; 2447 i->ch = ch; 2448 pthread_mutex_unlock(&g_devlist_mutex); 2449 rc = spdk_thread_send_msg(thread, _call_channel, i); 2450 assert(rc == 0); 2451 return; 2452 } 2453 thread = TAILQ_NEXT(thread, tailq); 2454 } 2455 2456 end: 2457 i->dev->for_each_count--; 2458 i->ch = NULL; 2459 pthread_mutex_unlock(&g_devlist_mutex); 2460 2461 rc = spdk_thread_send_msg(i->orig_thread, _call_completion, i); 2462 assert(rc == 0); 2463 } 2464 2465 struct spdk_interrupt { 2466 int efd; 2467 struct spdk_thread *thread; 2468 char name[SPDK_MAX_POLLER_NAME_LEN + 1]; 2469 }; 2470 2471 static void 2472 thread_interrupt_destroy(struct spdk_thread *thread) 2473 { 2474 struct spdk_fd_group *fgrp = thread->fgrp; 2475 2476 SPDK_INFOLOG(thread, "destroy fgrp for thread (%s)\n", thread->name); 2477 2478 if (thread->msg_fd < 0) { 2479 return; 2480 } 2481 2482 spdk_fd_group_remove(fgrp, thread->msg_fd); 2483 close(thread->msg_fd); 2484 thread->msg_fd = -1; 2485 2486 spdk_fd_group_destroy(fgrp); 2487 thread->fgrp = NULL; 2488 } 2489 2490 #ifdef __linux__ 2491 static int 2492 thread_interrupt_msg_process(void *arg) 2493 { 2494 struct spdk_thread *thread = arg; 2495 uint32_t msg_count; 2496 spdk_msg_fn critical_msg; 2497 int rc = 0; 2498 uint64_t notify = 1; 2499 2500 assert(spdk_interrupt_mode_is_enabled()); 2501 2502 /* There may be race between msg_acknowledge and another producer's msg_notify, 2503 * so msg_acknowledge should be applied ahead. And then check for self's msg_notify. 2504 * This can avoid msg notification missing. 2505 */ 2506 rc = read(thread->msg_fd, ¬ify, sizeof(notify)); 2507 if (rc < 0 && errno != EAGAIN) { 2508 SPDK_ERRLOG("failed to acknowledge msg event: %s.\n", spdk_strerror(errno)); 2509 } 2510 2511 critical_msg = thread->critical_msg; 2512 if (spdk_unlikely(critical_msg != NULL)) { 2513 critical_msg(NULL); 2514 thread->critical_msg = NULL; 2515 rc = 1; 2516 } 2517 2518 msg_count = msg_queue_run_batch(thread, 0); 2519 if (msg_count) { 2520 rc = 1; 2521 } 2522 2523 return rc; 2524 } 2525 2526 static int 2527 thread_interrupt_create(struct spdk_thread *thread) 2528 { 2529 int rc; 2530 2531 SPDK_INFOLOG(thread, "Create fgrp for thread (%s)\n", thread->name); 2532 2533 rc = spdk_fd_group_create(&thread->fgrp); 2534 if (rc) { 2535 return rc; 2536 } 2537 2538 thread->msg_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); 2539 if (thread->msg_fd < 0) { 2540 rc = -errno; 2541 spdk_fd_group_destroy(thread->fgrp); 2542 thread->fgrp = NULL; 2543 2544 return rc; 2545 } 2546 2547 return SPDK_FD_GROUP_ADD(thread->fgrp, thread->msg_fd, 2548 thread_interrupt_msg_process, thread); 2549 } 2550 #else 2551 static int 2552 thread_interrupt_create(struct spdk_thread *thread) 2553 { 2554 return -ENOTSUP; 2555 } 2556 #endif 2557 2558 struct spdk_interrupt * 2559 spdk_interrupt_register(int efd, spdk_interrupt_fn fn, 2560 void *arg, const char *name) 2561 { 2562 struct spdk_thread *thread; 2563 struct spdk_interrupt *intr; 2564 int ret; 2565 2566 thread = spdk_get_thread(); 2567 if (!thread) { 2568 assert(false); 2569 return NULL; 2570 } 2571 2572 if (spdk_unlikely(thread->state != SPDK_THREAD_STATE_RUNNING)) { 2573 SPDK_ERRLOG("thread %s is marked as exited\n", thread->name); 2574 return NULL; 2575 } 2576 2577 ret = spdk_fd_group_add(thread->fgrp, efd, fn, arg, name); 2578 2579 if (ret != 0) { 2580 SPDK_ERRLOG("thread %s: failed to add fd %d: %s\n", 2581 thread->name, efd, spdk_strerror(-ret)); 2582 return NULL; 2583 } 2584 2585 intr = calloc(1, sizeof(*intr)); 2586 if (intr == NULL) { 2587 SPDK_ERRLOG("Interrupt handler allocation failed\n"); 2588 return NULL; 2589 } 2590 2591 if (name) { 2592 snprintf(intr->name, sizeof(intr->name), "%s", name); 2593 } else { 2594 snprintf(intr->name, sizeof(intr->name), "%p", fn); 2595 } 2596 2597 intr->efd = efd; 2598 intr->thread = thread; 2599 2600 return intr; 2601 } 2602 2603 void 2604 spdk_interrupt_unregister(struct spdk_interrupt **pintr) 2605 { 2606 struct spdk_thread *thread; 2607 struct spdk_interrupt *intr; 2608 2609 intr = *pintr; 2610 if (intr == NULL) { 2611 return; 2612 } 2613 2614 *pintr = NULL; 2615 2616 thread = spdk_get_thread(); 2617 if (!thread) { 2618 assert(false); 2619 return; 2620 } 2621 2622 if (intr->thread != thread) { 2623 wrong_thread(__func__, intr->name, intr->thread, thread); 2624 return; 2625 } 2626 2627 spdk_fd_group_remove(thread->fgrp, intr->efd); 2628 free(intr); 2629 } 2630 2631 int 2632 spdk_interrupt_set_event_types(struct spdk_interrupt *intr, 2633 enum spdk_interrupt_event_types event_types) 2634 { 2635 struct spdk_thread *thread; 2636 2637 thread = spdk_get_thread(); 2638 if (!thread) { 2639 assert(false); 2640 return -EINVAL; 2641 } 2642 2643 if (intr->thread != thread) { 2644 wrong_thread(__func__, intr->name, intr->thread, thread); 2645 return -EINVAL; 2646 } 2647 2648 return spdk_fd_group_event_modify(thread->fgrp, intr->efd, event_types); 2649 } 2650 2651 int 2652 spdk_thread_get_interrupt_fd(struct spdk_thread *thread) 2653 { 2654 return spdk_fd_group_get_fd(thread->fgrp); 2655 } 2656 2657 static bool g_interrupt_mode = false; 2658 2659 int 2660 spdk_interrupt_mode_enable(void) 2661 { 2662 /* It must be called once prior to initializing the threading library. 2663 * g_spdk_msg_mempool will be valid if thread library is initialized. 2664 */ 2665 if (g_spdk_msg_mempool) { 2666 SPDK_ERRLOG("Failed due to threading library is already initialized.\n"); 2667 return -1; 2668 } 2669 2670 #ifdef __linux__ 2671 SPDK_NOTICELOG("Set SPDK running in interrupt mode.\n"); 2672 g_interrupt_mode = true; 2673 return 0; 2674 #else 2675 SPDK_ERRLOG("SPDK interrupt mode supports only Linux platform now.\n"); 2676 g_interrupt_mode = false; 2677 return -ENOTSUP; 2678 #endif 2679 } 2680 2681 bool 2682 spdk_interrupt_mode_is_enabled(void) 2683 { 2684 return g_interrupt_mode; 2685 } 2686 2687 SPDK_LOG_REGISTER_COMPONENT(thread) 2688