LCOV - code coverage report
Current view: top level - spdk/lib/thread - thread.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1234 1547 79.8 %
Date: 2024-12-14 20:52:57 Functions: 145 168 86.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 4193 10466 40.1 %

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

Generated by: LCOV version 1.15