LCOV - code coverage report
Current view: top level - spdk/lib/thread - thread.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1086 1303 83.3 %
Date: 2024-07-11 19:06:16 Functions: 139 160 86.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 772 1126 68.6 %

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

Generated by: LCOV version 1.14