LCOV - code coverage report
Current view: top level - spdk/lib/event - reactor.c (source / functions) Hit Total Coverage
Test: Combined Lines: 730 868 84.1 %
Date: 2024-11-20 16:53:05 Functions: 62 64 96.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1073 2045 52.5 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2016 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : #include "spdk/likely.h"
       8                 :            : 
       9                 :            : #include "event_internal.h"
      10                 :            : 
      11                 :            : #include "spdk_internal/event.h"
      12                 :            : #include "spdk_internal/usdt.h"
      13                 :            : 
      14                 :            : #include "spdk/log.h"
      15                 :            : #include "spdk/thread.h"
      16                 :            : #include "spdk/env.h"
      17                 :            : #include "spdk/util.h"
      18                 :            : #include "spdk/scheduler.h"
      19                 :            : #include "spdk/string.h"
      20                 :            : #include "spdk/fd_group.h"
      21                 :            : #include "spdk/trace.h"
      22                 :            : #include "spdk_internal/trace_defs.h"
      23                 :            : 
      24                 :            : #ifdef __linux__
      25                 :            : #include <sys/prctl.h>
      26                 :            : #include <sys/eventfd.h>
      27                 :            : #endif
      28                 :            : 
      29                 :            : #ifdef __FreeBSD__
      30                 :            : #include <pthread_np.h>
      31                 :            : #endif
      32                 :            : 
      33                 :            : #define SPDK_EVENT_BATCH_SIZE           8
      34                 :            : 
      35                 :            : static struct spdk_reactor *g_reactors;
      36                 :            : static uint32_t g_reactor_count;
      37                 :            : static struct spdk_cpuset g_reactor_core_mask;
      38                 :            : static enum spdk_reactor_state  g_reactor_state = SPDK_REACTOR_STATE_UNINITIALIZED;
      39                 :            : 
      40                 :            : static bool g_framework_context_switch_monitor_enabled = true;
      41                 :            : 
      42                 :            : static struct spdk_mempool *g_spdk_event_mempool = NULL;
      43                 :            : 
      44                 :            : TAILQ_HEAD(, spdk_scheduler) g_scheduler_list
      45                 :            :         = TAILQ_HEAD_INITIALIZER(g_scheduler_list);
      46                 :            : 
      47                 :            : static struct spdk_scheduler *g_scheduler = NULL;
      48                 :            : static struct spdk_reactor *g_scheduling_reactor;
      49                 :            : bool g_scheduling_in_progress = false;
      50                 :            : static uint64_t g_scheduler_period_in_tsc = 0;
      51                 :            : static uint64_t g_scheduler_period_in_us;
      52                 :            : static uint32_t g_scheduler_core_number;
      53                 :            : static struct spdk_scheduler_core_info *g_core_infos = NULL;
      54                 :            : static struct spdk_cpuset g_scheduler_isolated_core_mask;
      55                 :            : 
      56                 :            : TAILQ_HEAD(, spdk_governor) g_governor_list
      57                 :            :         = TAILQ_HEAD_INITIALIZER(g_governor_list);
      58                 :            : 
      59                 :            : static struct spdk_governor *g_governor = NULL;
      60                 :            : 
      61                 :            : static int reactor_interrupt_init(struct spdk_reactor *reactor);
      62                 :            : static void reactor_interrupt_fini(struct spdk_reactor *reactor);
      63                 :            : 
      64                 :            : static pthread_mutex_t g_stopping_reactors_mtx = PTHREAD_MUTEX_INITIALIZER;
      65                 :            : static bool g_stopping_reactors = false;
      66                 :            : 
      67                 :            : static struct spdk_scheduler *
      68                 :       7633 : _scheduler_find(const char *name)
      69                 :            : {
      70                 :            :         struct spdk_scheduler *tmp;
      71                 :            : 
      72   [ +  +  -  +  :      14318 :         TAILQ_FOREACH(tmp, &g_scheduler_list, link) {
             -  +  -  + ]
      73   [ +  +  +  +  :       7669 :                 if (strcmp(name, tmp->name) == 0) {
          +  +  +  -  +  
                      + ]
      74                 :        984 :                         return tmp;
      75                 :            :                 }
      76                 :        341 :         }
      77                 :            : 
      78                 :       6649 :         return NULL;
      79                 :        345 : }
      80                 :            : 
      81                 :            : int
      82                 :       1948 : spdk_scheduler_set(const char *name)
      83                 :            : {
      84                 :            :         struct spdk_scheduler *scheduler;
      85                 :       1948 :         int rc = 0;
      86                 :            : 
      87                 :            :         /* NULL scheduler was specifically requested */
      88         [ +  + ]:       1948 :         if (name == NULL) {
      89         [ +  + ]:        964 :                 if (g_scheduler) {
      90   [ +  -  +  -  :        964 :                         g_scheduler->deinit();
             -  +  +  - ]
      91                 :         70 :                 }
      92                 :        964 :                 g_scheduler = NULL;
      93                 :        964 :                 return 0;
      94                 :            :         }
      95                 :            : 
      96                 :        984 :         scheduler = _scheduler_find(name);
      97         [ +  + ]:        984 :         if (scheduler == NULL) {
      98                 :          0 :                 SPDK_ERRLOG("Requested scheduler is missing\n");
      99                 :          0 :                 return -EINVAL;
     100                 :            :         }
     101                 :            : 
     102         [ +  + ]:        984 :         if (g_scheduler == scheduler) {
     103                 :         10 :                 return 0;
     104                 :            :         }
     105                 :            : 
     106         [ +  + ]:        974 :         if (g_scheduler) {
     107   [ #  #  #  #  :          6 :                 g_scheduler->deinit();
             #  #  #  # ]
     108                 :          0 :         }
     109                 :            : 
     110   [ +  -  +  -  :        974 :         rc = scheduler->init();
             -  +  +  - ]
     111         [ +  + ]:        974 :         if (rc == 0) {
     112                 :        974 :                 g_scheduler = scheduler;
     113                 :         71 :         } else {
     114                 :            :                 /* Could not switch to the new scheduler, so keep the old
     115                 :            :                  * one. We need to check if it wasn't NULL, and ->init() it again.
     116                 :            :                  */
     117         [ #  # ]:          0 :                 if (g_scheduler) {
     118   [ #  #  #  # ]:          0 :                         SPDK_ERRLOG("Could not ->init() '%s' scheduler, reverting to '%s'\n",
     119                 :            :                                     name, g_scheduler->name);
     120   [ #  #  #  #  :          0 :                         g_scheduler->init();
             #  #  #  # ]
     121                 :          0 :                 } else {
     122                 :          0 :                         SPDK_ERRLOG("Could not ->init() '%s' scheduler.\n", name);
     123                 :            :                 }
     124                 :            :         }
     125                 :            : 
     126                 :        974 :         return rc;
     127                 :        142 : }
     128                 :            : 
     129                 :            : struct spdk_scheduler *
     130                 :       1373 : spdk_scheduler_get(void)
     131                 :            : {
     132                 :       1373 :         return g_scheduler;
     133                 :            : }
     134                 :            : 
     135                 :            : uint64_t
     136                 :        205 : spdk_scheduler_get_period(void)
     137                 :            : {
     138                 :        205 :         return g_scheduler_period_in_us;
     139                 :            : }
     140                 :            : 
     141                 :            : void
     142                 :       1982 : spdk_scheduler_set_period(uint64_t period)
     143                 :            : {
     144                 :       1982 :         g_scheduler_period_in_us = period;
     145         [ +  - ]:       1982 :         g_scheduler_period_in_tsc = period * spdk_get_ticks_hz() / SPDK_SEC_TO_USEC;
     146                 :       1982 : }
     147                 :            : 
     148                 :            : void
     149                 :       6649 : spdk_scheduler_register(struct spdk_scheduler *scheduler)
     150                 :            : {
     151   [ +  +  +  -  :       6649 :         if (_scheduler_find(scheduler->name)) {
                   +  - ]
     152   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG("scheduler named '%s' already registered.\n", scheduler->name);
     153         [ #  # ]:          0 :                 assert(false);
     154                 :            :                 return;
     155                 :            :         }
     156                 :            : 
     157   [ +  -  +  -  :       6649 :         TAILQ_INSERT_TAIL(&g_scheduler_list, scheduler, link);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     158                 :        273 : }
     159                 :            : 
     160                 :            : uint32_t
     161                 :       1531 : spdk_scheduler_get_scheduling_lcore(void)
     162                 :            : {
     163   [ +  -  +  - ]:       1531 :         return g_scheduling_reactor->lcore;
     164                 :            : }
     165                 :            : 
     166                 :            : bool
     167                 :          1 : spdk_scheduler_set_scheduling_lcore(uint32_t core)
     168                 :            : {
     169                 :          1 :         struct spdk_reactor *reactor = spdk_reactor_get(core);
     170         [ -  + ]:          1 :         if (reactor == NULL) {
     171                 :          0 :                 SPDK_ERRLOG("Failed to set scheduling reactor. Reactor(lcore:%d) does not exist", core);
     172                 :          0 :                 return false;
     173                 :            :         }
     174                 :            : 
     175                 :          1 :         g_scheduling_reactor = reactor;
     176                 :          1 :         return true;
     177                 :          0 : }
     178                 :            : 
     179                 :            : bool
     180                 :         13 : scheduler_set_isolated_core_mask(struct spdk_cpuset isolated_core_mask)
     181                 :            : {
     182                 :          9 :         struct spdk_cpuset tmp_mask;
     183                 :            : 
     184                 :         13 :         spdk_cpuset_copy(&tmp_mask, spdk_app_get_core_mask());
     185                 :         13 :         spdk_cpuset_or(&tmp_mask, &isolated_core_mask);
     186         [ +  + ]:         13 :         if (spdk_cpuset_equal(&tmp_mask, spdk_app_get_core_mask()) == false) {
     187                 :          8 :                 SPDK_ERRLOG("Isolated core mask is not included in app core mask.\n");
     188                 :          8 :                 return false;
     189                 :            :         }
     190                 :          5 :         spdk_cpuset_copy(&g_scheduler_isolated_core_mask, &isolated_core_mask);
     191                 :          5 :         return true;
     192                 :          3 : }
     193                 :            : 
     194                 :            : const char *
     195                 :         10 : scheduler_get_isolated_core_mask(void)
     196                 :            : {
     197                 :         10 :         return spdk_cpuset_fmt(&g_scheduler_isolated_core_mask);
     198                 :            : }
     199                 :            : 
     200                 :            : static bool
     201                 :       1272 : scheduler_is_isolated_core(uint32_t core)
     202                 :            : {
     203                 :       1272 :         return spdk_cpuset_get_cpu(&g_scheduler_isolated_core_mask, core);
     204                 :            : }
     205                 :            : 
     206                 :            : static void
     207                 :       3527 : reactor_construct(struct spdk_reactor *reactor, uint32_t lcore)
     208                 :            : {
     209   [ +  -  +  - ]:       3527 :         reactor->lcore = lcore;
     210   [ +  -  +  - ]:       3527 :         reactor->flags.is_valid = true;
     211                 :            : 
     212   [ +  -  +  -  :       3527 :         TAILQ_INIT(&reactor->threads);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     213   [ +  -  +  - ]:       3527 :         reactor->thread_count = 0;
     214         [ +  - ]:       3527 :         spdk_cpuset_zero(&reactor->notify_cpuset);
     215                 :            : 
     216   [ +  -  +  - ]:       3527 :         reactor->events = spdk_ring_create(SPDK_RING_TYPE_MP_SC, 65536, SPDK_ENV_NUMA_ID_ANY);
     217   [ +  +  +  -  :       3527 :         if (reactor->events == NULL) {
                   +  - ]
     218                 :          0 :                 SPDK_ERRLOG("Failed to allocate events ring\n");
     219         [ #  # ]:          0 :                 assert(false);
     220                 :            :         }
     221                 :            : 
     222                 :            :         /* Always initialize interrupt facilities for reactor */
     223         [ +  + ]:       3527 :         if (reactor_interrupt_init(reactor) != 0) {
     224                 :            :                 /* Reactor interrupt facilities are necessary if setting app to interrupt mode. */
     225         [ -  + ]:         94 :                 if (spdk_interrupt_mode_is_enabled()) {
     226                 :          0 :                         SPDK_ERRLOG("Failed to prepare intr facilities\n");
     227         [ #  # ]:          0 :                         assert(false);
     228                 :            :                 }
     229                 :         94 :                 return;
     230                 :            :         }
     231                 :            : 
     232                 :            :         /* If application runs with full interrupt ability,
     233                 :            :          * all reactors are going to run in interrupt mode.
     234                 :            :          */
     235         [ +  + ]:       3433 :         if (spdk_interrupt_mode_is_enabled()) {
     236                 :            :                 uint32_t i;
     237                 :            : 
     238         [ +  + ]:        494 :                 SPDK_ENV_FOREACH_CORE(i) {
     239         [ #  # ]:        377 :                         spdk_cpuset_set_cpu(&reactor->notify_cpuset, i, true);
     240                 :          0 :                 }
     241   [ #  #  #  # ]:        117 :                 reactor->in_interrupt = true;
     242                 :          0 :         }
     243                 :        177 : }
     244                 :            : 
     245                 :            : struct spdk_reactor *
     246                 :   92684995 : spdk_reactor_get(uint32_t lcore)
     247                 :            : {
     248                 :            :         struct spdk_reactor *reactor;
     249                 :            : 
     250         [ +  + ]:   92684995 :         if (g_reactors == NULL) {
     251                 :          0 :                 SPDK_WARNLOG("Called spdk_reactor_get() while the g_reactors array was NULL!\n");
     252                 :          0 :                 return NULL;
     253                 :            :         }
     254                 :            : 
     255         [ -  + ]:   92684995 :         if (lcore >= g_reactor_count) {
     256                 :          0 :                 return NULL;
     257                 :            :         }
     258                 :            : 
     259         [ +  - ]:   92684995 :         reactor = &g_reactors[lcore];
     260                 :            : 
     261   [ +  +  +  -  :   92684995 :         if (reactor->flags.is_valid == false) {
                   -  + ]
     262                 :          0 :                 return NULL;
     263                 :            :         }
     264                 :            : 
     265                 :   92684995 :         return reactor;
     266                 :   15066357 : }
     267                 :            : 
     268                 :            : static int reactor_thread_op(struct spdk_thread *thread, enum spdk_thread_op op);
     269                 :            : static bool reactor_thread_op_supported(enum spdk_thread_op op);
     270                 :            : 
     271                 :            : /* Power of 2 minus 1 is optimal for memory consumption */
     272                 :            : #define EVENT_MSG_MEMPOOL_SHIFT 14 /* 2^14 = 16384 */
     273                 :            : #define EVENT_MSG_MEMPOOL_SIZE ((1 << EVENT_MSG_MEMPOOL_SHIFT) - 1)
     274                 :            : 
     275                 :            : int
     276                 :       2332 : spdk_reactors_init(size_t msg_mempool_size)
     277                 :            : {
     278                 :            :         struct spdk_reactor *reactor;
     279                 :            :         int rc;
     280                 :            :         uint32_t i, current_core;
     281                 :        928 :         char mempool_name[32];
     282                 :            : 
     283         [ -  + ]:       2332 :         snprintf(mempool_name, sizeof(mempool_name), "evtpool_%d", getpid());
     284                 :       2397 :         g_spdk_event_mempool = spdk_mempool_create(mempool_name,
     285   [ -  +  +  - ]:         65 :                                EVENT_MSG_MEMPOOL_SIZE,
     286                 :            :                                sizeof(struct spdk_event),
     287                 :            :                                SPDK_MEMPOOL_DEFAULT_CACHE_SIZE,
     288                 :            :                                SPDK_ENV_NUMA_ID_ANY);
     289                 :            : 
     290         [ +  + ]:       2332 :         if (g_spdk_event_mempool == NULL) {
     291                 :          0 :                 SPDK_ERRLOG("spdk_event_mempool creation failed\n");
     292                 :          0 :                 return -1;
     293                 :            :         }
     294                 :            : 
     295                 :            :         /* struct spdk_reactor must be aligned on 64 byte boundary */
     296                 :       2332 :         g_reactor_count = spdk_env_get_last_core() + 1;
     297         [ +  + ]:       2332 :         rc = posix_memalign((void **)&g_reactors, 64,
     298                 :        120 :                             g_reactor_count * sizeof(struct spdk_reactor));
     299         [ -  + ]:       2332 :         if (rc != 0) {
     300                 :          0 :                 SPDK_ERRLOG("Could not allocate array size=%u for g_reactors\n",
     301                 :            :                             g_reactor_count);
     302                 :          0 :                 spdk_mempool_free(g_spdk_event_mempool);
     303                 :          0 :                 return -1;
     304                 :            :         }
     305                 :            : 
     306                 :       2332 :         g_core_infos = calloc(g_reactor_count, sizeof(*g_core_infos));
     307         [ +  + ]:       2332 :         if (g_core_infos == NULL) {
     308                 :          0 :                 SPDK_ERRLOG("Could not allocate memory for g_core_infos\n");
     309                 :          0 :                 spdk_mempool_free(g_spdk_event_mempool);
     310                 :          0 :                 free(g_reactors);
     311                 :          0 :                 return -ENOMEM;
     312                 :            :         }
     313                 :            : 
     314         [ +  + ]:       2332 :         memset(g_reactors, 0, (g_reactor_count) * sizeof(struct spdk_reactor));
     315                 :            : 
     316                 :       2332 :         rc = spdk_thread_lib_init_ext(reactor_thread_op, reactor_thread_op_supported,
     317                 :        120 :                                       sizeof(struct spdk_lw_thread), msg_mempool_size);
     318         [ -  + ]:       2332 :         if (rc != 0) {
     319                 :          0 :                 SPDK_ERRLOG("Initialize spdk thread lib failed\n");
     320                 :          0 :                 spdk_mempool_free(g_spdk_event_mempool);
     321                 :          0 :                 free(g_reactors);
     322                 :          0 :                 free(g_core_infos);
     323                 :          0 :                 return rc;
     324                 :            :         }
     325                 :            : 
     326         [ +  + ]:       5855 :         SPDK_ENV_FOREACH_CORE(i) {
     327         [ +  - ]:       3523 :                 reactor_construct(&g_reactors[i], i);
     328                 :        176 :         }
     329                 :            : 
     330                 :       2332 :         current_core = spdk_env_get_current_core();
     331                 :       2332 :         reactor = spdk_reactor_get(current_core);
     332   [ +  +  #  # ]:       2332 :         assert(reactor != NULL);
     333                 :       2332 :         g_scheduling_reactor = reactor;
     334                 :            : 
     335                 :       2332 :         g_reactor_state = SPDK_REACTOR_STATE_INITIALIZED;
     336                 :            : 
     337                 :       2332 :         return 0;
     338                 :        120 : }
     339                 :            : 
     340                 :            : void
     341                 :       2394 : spdk_reactors_fini(void)
     342                 :            : {
     343                 :            :         uint32_t i;
     344                 :            :         struct spdk_reactor *reactor;
     345                 :            : 
     346         [ +  + ]:       2394 :         if (g_reactor_state == SPDK_REACTOR_STATE_UNINITIALIZED) {
     347                 :         62 :                 return;
     348                 :            :         }
     349                 :            : 
     350                 :       2332 :         spdk_thread_lib_fini();
     351                 :            : 
     352         [ +  + ]:       5855 :         SPDK_ENV_FOREACH_CORE(i) {
     353                 :       3523 :                 reactor = spdk_reactor_get(i);
     354   [ +  +  #  # ]:       3523 :                 assert(reactor != NULL);
     355   [ +  +  +  -  :       3523 :                 assert(reactor->thread_count == 0);
             +  -  #  # ]
     356   [ +  +  +  -  :       3523 :                 if (reactor->events != NULL) {
                   -  + ]
     357   [ +  -  +  - ]:       3523 :                         spdk_ring_free(reactor->events);
     358                 :        176 :                 }
     359                 :            : 
     360                 :       3523 :                 reactor_interrupt_fini(reactor);
     361                 :            : 
     362         [ +  + ]:       3523 :                 if (g_core_infos != NULL) {
     363   [ +  -  +  -  :       3523 :                         free(g_core_infos[i].thread_infos);
                   +  - ]
     364                 :        176 :                 }
     365                 :        176 :         }
     366                 :            : 
     367                 :       2332 :         spdk_mempool_free(g_spdk_event_mempool);
     368                 :            : 
     369                 :       2332 :         free(g_reactors);
     370                 :       2332 :         g_reactors = NULL;
     371                 :       2332 :         free(g_core_infos);
     372                 :       2332 :         g_core_infos = NULL;
     373                 :        124 : }
     374                 :            : 
     375                 :            : static void _reactor_set_interrupt_mode(void *arg1, void *arg2);
     376                 :            : 
     377                 :            : static void
     378                 :        876 : _reactor_set_notify_cpuset(void *arg1, void *arg2)
     379                 :            : {
     380                 :        876 :         struct spdk_reactor *target = arg1;
     381                 :        876 :         struct spdk_reactor *reactor = spdk_reactor_get(spdk_env_get_current_core());
     382                 :            : 
     383   [ +  +  #  # ]:        876 :         assert(reactor != NULL);
     384   [ +  +  +  -  :        876 :         spdk_cpuset_set_cpu(&reactor->notify_cpuset, target->lcore, target->new_in_interrupt);
          +  -  +  -  +  
                -  +  - ]
     385                 :        876 : }
     386                 :            : 
     387                 :            : static void
     388                 :      28260 : _event_call(uint32_t lcore, spdk_event_fn fn, void *arg1, void *arg2)
     389                 :            : {
     390                 :            :         struct spdk_event *ev;
     391                 :            : 
     392                 :      28260 :         ev = spdk_event_allocate(lcore, fn, arg1, arg2);
     393   [ +  +  #  # ]:      28260 :         assert(ev);
     394                 :      28260 :         spdk_event_call(ev);
     395                 :      28260 : }
     396                 :            : 
     397                 :            : static void
     398                 :        184 : _reactor_set_notify_cpuset_cpl(void *arg1, void *arg2)
     399                 :            : {
     400                 :        184 :         struct spdk_reactor *target = arg1;
     401                 :            : 
     402   [ +  +  +  +  :        184 :         if (target->new_in_interrupt == false) {
             +  -  +  + ]
     403   [ -  +  -  + ]:         64 :                 target->set_interrupt_mode_in_progress = false;
     404   [ -  +  -  + ]:         67 :                 _event_call(spdk_scheduler_get_scheduling_lcore(), target->set_interrupt_mode_cb_fn,
     405   [ -  +  -  + ]:          3 :                             target->set_interrupt_mode_cb_arg, NULL);
     406                 :          3 :         } else {
     407   [ +  -  +  - ]:        120 :                 _event_call(target->lcore, _reactor_set_interrupt_mode, target, NULL);
     408                 :            :         }
     409                 :        184 : }
     410                 :            : 
     411                 :            : static void
     412                 :        138 : _reactor_set_thread_interrupt_mode(void *ctx)
     413                 :            : {
     414                 :        138 :         struct spdk_reactor *reactor = ctx;
     415                 :            : 
     416   [ -  +  #  #  :        138 :         spdk_thread_set_interrupt_mode(reactor->in_interrupt);
                   #  # ]
     417                 :        138 : }
     418                 :            : 
     419                 :            : static void
     420                 :        184 : _reactor_set_interrupt_mode(void *arg1, void *arg2)
     421                 :            : {
     422                 :        184 :         struct spdk_reactor *target = arg1;
     423                 :            :         struct spdk_thread *thread;
     424                 :            :         struct spdk_fd_group *grp;
     425                 :            :         struct spdk_lw_thread *lw_thread, *tmp;
     426                 :            : 
     427   [ +  +  #  # ]:        184 :         assert(target == spdk_reactor_get(spdk_env_get_current_core()));
     428   [ +  +  #  # ]:        184 :         assert(target != NULL);
     429   [ +  +  +  +  :        184 :         assert(target->in_interrupt != target->new_in_interrupt);
          +  +  +  -  +  
          -  +  -  +  -  
                   #  # ]
     430   [ +  +  +  +  :        184 :         SPDK_DEBUGLOG(reactor, "Do reactor set on core %u from %s to state %s\n",
          +  -  -  -  -  
          -  -  -  #  #  
          #  #  #  #  #  
                #  #  # ]
     431                 :            :                       target->lcore, target->in_interrupt ? "intr" : "poll", target->new_in_interrupt ? "intr" : "poll");
     432                 :            : 
     433   [ +  +  +  -  :        184 :         target->in_interrupt = target->new_in_interrupt;
          +  -  +  -  +  
                      - ]
     434                 :            : 
     435         [ +  + ]:        184 :         if (spdk_interrupt_mode_is_enabled()) {
     436                 :            :                 /* Align spdk_thread with reactor to interrupt mode or poll mode */
     437   [ +  +  #  #  :         40 :                 TAILQ_FOREACH_SAFE(lw_thread, &target->threads, link, tmp) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     438                 :          8 :                         thread = spdk_thread_get_from_ctx(lw_thread);
     439   [ +  +  +  +  :          8 :                         if (target->in_interrupt) {
             #  #  #  # ]
     440                 :          4 :                                 grp = spdk_thread_get_interrupt_fd_group(thread);
     441   [ #  #  #  # ]:          4 :                                 spdk_fd_group_nest(target->fgrp, grp);
     442                 :          0 :                         } else {
     443                 :          4 :                                 grp = spdk_thread_get_interrupt_fd_group(thread);
     444   [ #  #  #  # ]:          4 :                                 spdk_fd_group_unnest(target->fgrp, grp);
     445                 :            :                         }
     446                 :            : 
     447                 :          8 :                         spdk_thread_send_msg(thread, _reactor_set_thread_interrupt_mode, target);
     448                 :          0 :                 }
     449                 :          0 :         }
     450                 :            : 
     451   [ +  +  +  +  :        184 :         if (target->new_in_interrupt == false) {
             +  -  +  + ]
     452                 :            :                 /* Reactor is no longer in interrupt mode. Refresh the tsc_last to accurately
     453                 :            :                  * track reactor stats. */
     454   [ +  -  +  - ]:         64 :                 target->tsc_last = spdk_get_ticks();
     455                 :         64 :                 spdk_for_each_reactor(_reactor_set_notify_cpuset, target, NULL, _reactor_set_notify_cpuset_cpl);
     456                 :          3 :         } else {
     457                 :        120 :                 uint64_t notify = 1;
     458                 :        120 :                 int rc = 0;
     459                 :            : 
     460                 :            :                 /* Always trigger spdk_event and resched event in case of race condition */
     461   [ +  -  +  - ]:        120 :                 rc = write(target->events_fd, &notify, sizeof(notify));
     462         [ +  + ]:        120 :                 if (rc < 0) {
     463         [ #  # ]:          0 :                         SPDK_ERRLOG("failed to notify event queue: %s.\n", spdk_strerror(errno));
     464                 :          0 :                 }
     465   [ +  -  +  - ]:        120 :                 rc = write(target->resched_fd, &notify, sizeof(notify));
     466         [ +  + ]:        120 :                 if (rc < 0) {
     467         [ #  # ]:          0 :                         SPDK_ERRLOG("failed to notify reschedule: %s.\n", spdk_strerror(errno));
     468                 :          0 :                 }
     469                 :            : 
     470   [ +  -  +  - ]:        120 :                 target->set_interrupt_mode_in_progress = false;
     471   [ +  -  +  - ]:        123 :                 _event_call(spdk_scheduler_get_scheduling_lcore(), target->set_interrupt_mode_cb_fn,
     472   [ +  -  +  - ]:          3 :                             target->set_interrupt_mode_cb_arg, NULL);
     473                 :            :         }
     474                 :        184 : }
     475                 :            : 
     476                 :            : int
     477                 :        185 : spdk_reactor_set_interrupt_mode(uint32_t lcore, bool new_in_interrupt,
     478                 :            :                                 spdk_reactor_set_interrupt_mode_cb cb_fn, void *cb_arg)
     479                 :            : {
     480                 :            :         struct spdk_reactor *target;
     481                 :            : 
     482                 :        185 :         target = spdk_reactor_get(lcore);
     483         [ +  + ]:        185 :         if (target == NULL) {
     484                 :          0 :                 return -EINVAL;
     485                 :            :         }
     486                 :            : 
     487                 :            :         /* Eventfd has to be supported in order to use interrupt functionality. */
     488   [ +  +  +  -  :        185 :         if (target->fgrp == NULL) {
                   +  - ]
     489                 :          0 :                 return -ENOTSUP;
     490                 :            :         }
     491                 :            : 
     492   [ +  +  +  -  :        185 :         if (spdk_env_get_current_core() != g_scheduling_reactor->lcore) {
                   -  + ]
     493                 :          0 :                 SPDK_ERRLOG("It is only permitted within scheduling reactor.\n");
     494                 :          0 :                 return -EPERM;
     495                 :            :         }
     496                 :            : 
     497   [ +  +  +  +  :        185 :         if (target->in_interrupt == new_in_interrupt) {
          +  -  +  -  -  
                      + ]
     498   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL);
     499                 :          0 :                 return 0;
     500                 :            :         }
     501                 :            : 
     502   [ +  +  +  +  :        185 :         if (target->set_interrupt_mode_in_progress) {
             +  -  -  + ]
     503                 :          0 :                 SPDK_NOTICELOG("Reactor(%u) is already in progress to set interrupt mode\n", lcore);
     504                 :          0 :                 return -EBUSY;
     505                 :            :         }
     506   [ +  -  +  - ]:        185 :         target->set_interrupt_mode_in_progress = true;
     507                 :            : 
     508   [ +  -  +  -  :        185 :         target->new_in_interrupt = new_in_interrupt;
                   +  - ]
     509   [ +  -  +  - ]:        185 :         target->set_interrupt_mode_cb_fn = cb_fn;
     510   [ +  -  +  - ]:        185 :         target->set_interrupt_mode_cb_arg = cb_arg;
     511                 :            : 
     512   [ +  +  +  +  :        185 :         SPDK_DEBUGLOG(reactor, "Starting reactor event from %d to %d\n",
                   +  - ]
     513                 :            :                       spdk_env_get_current_core(), lcore);
     514                 :            : 
     515   [ +  +  +  + ]:        185 :         if (new_in_interrupt == false) {
     516                 :            :                 /* For potential race cases, when setting the reactor to poll mode,
     517                 :            :                  * first change the mode of the reactor and then clear the corresponding
     518                 :            :                  * bit of the notify_cpuset of each reactor.
     519                 :            :                  */
     520                 :         64 :                 _event_call(lcore, _reactor_set_interrupt_mode, target, NULL);
     521                 :          3 :         } else {
     522                 :            :                 /* For race cases, when setting the reactor to interrupt mode, first set the
     523                 :            :                  * corresponding bit of the notify_cpuset of each reactor and then change the mode.
     524                 :            :                  */
     525                 :        121 :                 spdk_for_each_reactor(_reactor_set_notify_cpuset, target, NULL, _reactor_set_notify_cpuset_cpl);
     526                 :            :         }
     527                 :            : 
     528                 :        185 :         return 0;
     529                 :          6 : }
     530                 :            : 
     531                 :            : struct spdk_event *
     532                 :   31457350 : spdk_event_allocate(uint32_t lcore, spdk_event_fn fn, void *arg1, void *arg2)
     533                 :            : {
     534                 :   31457350 :         struct spdk_event *event = NULL;
     535                 :   31457350 :         struct spdk_reactor *reactor = spdk_reactor_get(lcore);
     536                 :            : 
     537         [ +  + ]:   31457350 :         if (!reactor) {
     538         [ #  # ]:          0 :                 assert(false);
     539                 :            :                 return NULL;
     540                 :            :         }
     541                 :            : 
     542                 :   31457350 :         event = spdk_mempool_get(g_spdk_event_mempool);
     543         [ +  + ]:   31457350 :         if (event == NULL) {
     544         [ #  # ]:          0 :                 assert(false);
     545                 :            :                 return NULL;
     546                 :            :         }
     547                 :            : 
     548   [ +  -  +  - ]:   31457350 :         event->lcore = lcore;
     549   [ +  -  +  - ]:   31457350 :         event->fn = fn;
     550   [ +  -  +  - ]:   31457350 :         event->arg1 = arg1;
     551   [ +  -  +  - ]:   31457350 :         event->arg2 = arg2;
     552                 :            : 
     553                 :   31457350 :         return event;
     554                 :            : }
     555                 :            : 
     556                 :            : void
     557                 :   31563758 : spdk_event_call(struct spdk_event *event)
     558                 :            : {
     559                 :            :         int rc;
     560                 :            :         struct spdk_reactor *reactor;
     561                 :   31563758 :         struct spdk_reactor *local_reactor = NULL;
     562                 :   31563758 :         uint32_t current_core = spdk_env_get_current_core();
     563                 :            : 
     564   [ +  -  +  - ]:   31563758 :         reactor = spdk_reactor_get(event->lcore);
     565                 :            : 
     566   [ +  +  #  # ]:   31563758 :         assert(reactor != NULL);
     567   [ +  +  +  -  :   31563758 :         assert(reactor->events != NULL);
             +  -  #  # ]
     568                 :            : 
     569   [ +  -  +  - ]:   31563758 :         rc = spdk_ring_enqueue(reactor->events, (void **)&event, 1, NULL);
     570         [ +  + ]:   31563758 :         if (rc != 1) {
     571         [ #  # ]:          0 :                 assert(false);
     572                 :            :         }
     573                 :            : 
     574         [ +  + ]:   31563758 :         if (current_core != SPDK_ENV_LCORE_ID_ANY) {
     575                 :   31558479 :                 local_reactor = spdk_reactor_get(current_core);
     576                 :    5695264 :         }
     577                 :            : 
     578                 :            :         /* If spdk_event_call isn't called on a reactor, always send a notification.
     579                 :            :          * If it is called on a reactor, send a notification if the destination reactor
     580                 :            :          * is indicated in interrupt mode state.
     581                 :            :          */
     582   [ +  +  +  + ]:   32483738 :         if (spdk_unlikely(local_reactor == NULL) ||
     583   [ +  +  +  -  :   31515634 :             spdk_unlikely(spdk_cpuset_get_cpu(&local_reactor->notify_cpuset, event->lcore))) {
                   +  - ]
     584                 :     168704 :                 uint64_t notify = 1;
     585                 :            : 
     586   [ +  -  +  - ]:     168704 :                 rc = write(reactor->events_fd, &notify, sizeof(notify));
     587         [ +  + ]:     154710 :                 if (rc < 0) {
     588         [ #  # ]:          0 :                         SPDK_ERRLOG("failed to notify event queue: %s.\n", spdk_strerror(errno));
     589                 :          0 :                 }
     590                 :       4386 :         }
     591                 :   31472686 : }
     592                 :            : 
     593                 :            : static inline int
     594                 : 7166364076 : event_queue_run_batch(void *arg)
     595                 :            : {
     596                 : 7166364076 :         struct spdk_reactor *reactor = arg;
     597                 :            :         size_t count, i;
     598                 : 2191795112 :         void *events[SPDK_EVENT_BATCH_SIZE];
     599                 :            : 
     600                 :            : #ifdef DEBUG
     601                 :            :         /*
     602                 :            :          * spdk_ring_dequeue() fills events and returns how many entries it wrote,
     603                 :            :          * so we will never actually read uninitialized data from events, but just to be sure
     604                 :            :          * (and to silence a static analyzer false positive), initialize the array to NULL pointers.
     605                 :            :          */
     606         [ +  - ]: 7166364076 :         memset(events, 0, sizeof(events));
     607                 :            : #endif
     608                 :            : 
     609                 :            :         /* Operate event notification if this reactor currently runs in interrupt state */
     610   [ +  +  +  +  : 7166364076 :         if (spdk_unlikely(reactor->in_interrupt)) {
             +  -  +  + ]
     611                 :      73928 :                 uint64_t notify = 1;
     612                 :            :                 int rc;
     613                 :            : 
     614                 :            :                 /* There may be race between event_acknowledge and another producer's event_notify,
     615                 :            :                  * so event_acknowledge should be applied ahead. And then check for self's event_notify.
     616                 :            :                  * This can avoid event notification missing.
     617                 :            :                  */
     618   [ +  -  +  - ]:      73928 :                 rc = read(reactor->events_fd, &notify, sizeof(notify));
     619         [ +  + ]:      73928 :                 if (rc < 0) {
     620         [ #  # ]:          0 :                         SPDK_ERRLOG("failed to acknowledge event queue: %s.\n", spdk_strerror(errno));
     621   [ #  #  #  # ]:          0 :                         return -errno;
     622                 :            :                 }
     623                 :            : 
     624   [ +  -  +  - ]:      73928 :                 count = spdk_ring_dequeue(reactor->events, events, SPDK_EVENT_BATCH_SIZE);
     625                 :            : 
     626   [ +  +  +  -  :      73928 :                 if (spdk_ring_count(reactor->events) != 0) {
                   +  - ]
     627                 :            :                         /* Trigger new notification if there are still events in event-queue waiting for processing. */
     628   [ #  #  #  # ]:          0 :                         rc = write(reactor->events_fd, &notify, sizeof(notify));
     629         [ #  # ]:          0 :                         if (rc < 0) {
     630         [ #  # ]:          0 :                                 SPDK_ERRLOG("failed to notify event queue: %s.\n", spdk_strerror(errno));
     631   [ #  #  #  # ]:          0 :                                 return -errno;
     632                 :            :                         }
     633                 :          0 :                 }
     634                 :       4372 :         } else {
     635   [ +  -  +  - ]: 7166290148 :                 count = spdk_ring_dequeue(reactor->events, events, SPDK_EVENT_BATCH_SIZE);
     636                 :            :         }
     637                 :            : 
     638         [ +  + ]: 7166364076 :         if (count == 0) {
     639                 : 7147258002 :                 return 0;
     640                 :            :         }
     641                 :            : 
     642         [ +  + ]:   50594944 :         for (i = 0; i < count; i++) {
     643   [ +  -  +  -  :   31488870 :                 struct spdk_event *event = events[i];
                   +  - ]
     644                 :            : 
     645   [ +  +  #  # ]:   31488870 :                 assert(event != NULL);
     646   [ +  +  #  # ]:   31488870 :                 assert(spdk_get_thread() == NULL);
     647                 :    2215000 :                 SPDK_DTRACE_PROBE3(event_exec, event->fn,
     648                 :            :                                    event->arg1, event->arg2);
     649   [ +  -  +  -  :   31488870 :                 event->fn(event->arg1, event->arg2);
          +  +  +  -  +  
          -  +  -  +  -  
                   +  - ]
     650                 :    5625676 :         }
     651                 :            : 
     652                 :   19106074 :         spdk_mempool_put_bulk(g_spdk_event_mempool, events, count);
     653                 :            : 
     654                 :   19106074 :         return (int)count;
     655                 :  260596366 : }
     656                 :            : 
     657                 :            : /* 1s */
     658                 :            : #define CONTEXT_SWITCH_MONITOR_PERIOD 1000000
     659                 :            : 
     660                 :            : static int
     661                 :      26089 : get_rusage(struct spdk_reactor *reactor)
     662                 :            : {
     663                 :       9007 :         struct rusage           rusage;
     664                 :            : 
     665         [ -  + ]:      26089 :         if (getrusage(RUSAGE_THREAD, &rusage) != 0) {
     666                 :          0 :                 return -1;
     667                 :            :         }
     668                 :            : 
     669   [ +  +  +  +  :      26089 :         if (rusage.ru_nvcsw != reactor->rusage.ru_nvcsw || rusage.ru_nivcsw != reactor->rusage.ru_nivcsw) {
          +  -  +  -  +  
          -  +  -  +  +  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     670   [ +  +  +  +  :      24866 :                 SPDK_INFOLOG(reactor,
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     671                 :            :                              "Reactor %d: %ld voluntary context switches and %ld involuntary context switches in the last second.\n",
     672                 :            :                              reactor->lcore, rusage.ru_nvcsw - reactor->rusage.ru_nvcsw,
     673                 :            :                              rusage.ru_nivcsw - reactor->rusage.ru_nivcsw);
     674                 :        451 :         }
     675         [ +  - ]:      26089 :         reactor->rusage = rusage;
     676                 :            : 
     677                 :      26089 :         return -1;
     678                 :        451 : }
     679                 :            : 
     680                 :            : void
     681                 :          0 : spdk_framework_enable_context_switch_monitor(bool enable)
     682                 :            : {
     683                 :            :         /* This global is being read by multiple threads, so this isn't
     684                 :            :          * strictly thread safe. However, we're toggling between true and
     685                 :            :          * false here, and if a thread sees the value update later than it
     686                 :            :          * should, it's no big deal. */
     687         [ #  # ]:          0 :         g_framework_context_switch_monitor_enabled = enable;
     688                 :          0 : }
     689                 :            : 
     690                 :            : bool
     691                 :          0 : spdk_framework_context_switch_monitor_enabled(void)
     692                 :            : {
     693         [ #  # ]:          0 :         return g_framework_context_switch_monitor_enabled;
     694                 :            : }
     695                 :            : 
     696                 :            : static void
     697                 :       3438 : _set_thread_name(const char *thread_name)
     698                 :            : {
     699                 :            : #if defined(__linux__)
     700                 :       3366 :         prctl(PR_SET_NAME, thread_name, 0, 0, 0);
     701                 :            : #elif defined(__FreeBSD__)
     702                 :         72 :         pthread_set_name_np(pthread_self(), thread_name);
     703                 :            : #else
     704                 :            :         pthread_setname_np(pthread_self(), thread_name);
     705                 :            : #endif
     706                 :       3438 : }
     707                 :            : 
     708                 :            : static void
     709                 :       1514 : _init_thread_stats(struct spdk_reactor *reactor, struct spdk_lw_thread *lw_thread)
     710                 :            : {
     711                 :       1514 :         struct spdk_thread *thread = spdk_thread_get_from_ctx(lw_thread);
     712                 :            :         struct spdk_thread_stats prev_total_stats;
     713                 :            : 
     714                 :            :         /* Read total_stats before updating it to calculate stats during the last scheduling period. */
     715         [ +  - ]:       1514 :         prev_total_stats = lw_thread->total_stats;
     716                 :            : 
     717                 :       1514 :         spdk_set_thread(thread);
     718         [ +  - ]:       1514 :         spdk_thread_get_stats(&lw_thread->total_stats);
     719                 :       1514 :         spdk_set_thread(NULL);
     720                 :            : 
     721   [ +  -  +  -  :       1514 :         lw_thread->current_stats.busy_tsc = lw_thread->total_stats.busy_tsc - prev_total_stats.busy_tsc;
          +  -  +  -  +  
                -  +  - ]
     722   [ +  -  +  -  :       1514 :         lw_thread->current_stats.idle_tsc = lw_thread->total_stats.idle_tsc - prev_total_stats.idle_tsc;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     723                 :       1514 : }
     724                 :            : 
     725                 :            : static void
     726                 :        126 : _threads_reschedule_thread(struct spdk_scheduler_thread_info *thread_info)
     727                 :            : {
     728                 :            :         struct spdk_lw_thread *lw_thread;
     729                 :            :         struct spdk_thread *thread;
     730                 :            : 
     731   [ +  -  +  - ]:        126 :         thread = spdk_thread_get_by_id(thread_info->thread_id);
     732         [ +  + ]:        126 :         if (thread == NULL) {
     733                 :            :                 /* Thread no longer exists. */
     734                 :         18 :                 return;
     735                 :            :         }
     736                 :        108 :         lw_thread = spdk_thread_get_ctx(thread);
     737   [ +  +  #  # ]:        108 :         assert(lw_thread != NULL);
     738                 :            : 
     739   [ +  -  +  -  :        108 :         lw_thread->lcore = thread_info->lcore;
             +  -  +  - ]
     740   [ +  -  +  - ]:        108 :         lw_thread->resched = true;
     741                 :          9 : }
     742                 :            : 
     743                 :            : static void
     744                 :        200 : _threads_reschedule(struct spdk_scheduler_core_info *cores_info)
     745                 :            : {
     746                 :            :         struct spdk_scheduler_core_info *core;
     747                 :            :         struct spdk_scheduler_thread_info *thread_info;
     748                 :            :         uint32_t i, j;
     749                 :            : 
     750         [ +  + ]:       1460 :         SPDK_ENV_FOREACH_CORE(i) {
     751         [ -  + ]:       1260 :                 core = &cores_info[i];
     752   [ +  +  +  -  :       2770 :                 for (j = 0; j < core->threads_count; j++) {
                   +  + ]
     753   [ -  +  -  +  :       1510 :                         thread_info = &core->thread_infos[j];
                   -  + ]
     754   [ +  +  -  +  :       1510 :                         if (thread_info->lcore != i) {
                   +  + ]
     755   [ +  +  +  +  :        126 :                                 if (core->isolated || cores_info[thread_info->lcore].isolated) {
          -  +  +  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                +  -  + ]
     756                 :          0 :                                         SPDK_ERRLOG("A thread cannot be moved from an isolated core or \
     757                 :            :                                                                 moved to an isolated core. Skip rescheduling thread\n");
     758                 :          0 :                                         continue;
     759                 :            :                                 }
     760                 :        126 :                                 _threads_reschedule_thread(thread_info);
     761                 :          9 :                         }
     762                 :         33 :                 }
     763   [ -  +  -  + ]:       1260 :                 core->threads_count = 0;
     764   [ -  +  -  + ]:       1260 :                 free(core->thread_infos);
     765   [ -  +  -  + ]:       1260 :                 core->thread_infos = NULL;
     766                 :         21 :         }
     767                 :        200 : }
     768                 :            : 
     769                 :            : static void
     770                 :        200 : _reactors_scheduler_fini(void)
     771                 :            : {
     772                 :            :         /* Reschedule based on the balancing output */
     773                 :        200 :         _threads_reschedule(g_core_infos);
     774                 :            : 
     775                 :        200 :         g_scheduling_in_progress = false;
     776                 :        200 : }
     777                 :            : 
     778                 :            : static void
     779                 :        353 : _reactors_scheduler_update_core_mode(void *ctx1, void *ctx2)
     780                 :            : {
     781                 :            :         struct spdk_reactor *reactor;
     782                 :            :         uint32_t i;
     783                 :        353 :         int rc = 0;
     784                 :            : 
     785         [ +  + ]:       1462 :         for (i = g_scheduler_core_number; i < SPDK_ENV_LCORE_ID_ANY; i = spdk_env_get_next_core(i)) {
     786                 :       1262 :                 reactor = spdk_reactor_get(i);
     787   [ +  +  #  # ]:       1262 :                 assert(reactor != NULL);
     788   [ +  +  +  +  :       1262 :                 if (reactor->in_interrupt != g_core_infos[i].interrupt_mode) {
          +  +  +  -  +  
          -  +  -  +  -  
                   +  + ]
     789                 :            :                         /* Switch next found reactor to new state */
     790   [ +  +  +  -  :        153 :                         rc = spdk_reactor_set_interrupt_mode(i, g_core_infos[i].interrupt_mode,
             +  -  +  - ]
     791                 :            :                                                              _reactors_scheduler_update_core_mode, NULL);
     792         [ +  + ]:        153 :                         if (rc == 0) {
     793                 :            :                                 /* Set core to start with after callback completes */
     794                 :        153 :                                 g_scheduler_core_number = spdk_env_get_next_core(i);
     795                 :        153 :                                 return;
     796                 :            :                         }
     797                 :          0 :                 }
     798                 :         15 :         }
     799                 :        200 :         _reactors_scheduler_fini();
     800                 :         12 : }
     801                 :            : 
     802                 :            : static void
     803                 :          2 : _reactors_scheduler_cancel(void *arg1, void *arg2)
     804                 :            : {
     805                 :            :         struct spdk_scheduler_core_info *core;
     806                 :            :         uint32_t i;
     807                 :            : 
     808         [ +  + ]:         10 :         SPDK_ENV_FOREACH_CORE(i) {
     809         [ #  # ]:          8 :                 core = &g_core_infos[i];
     810   [ #  #  #  # ]:          8 :                 core->threads_count = 0;
     811   [ #  #  #  # ]:          8 :                 free(core->thread_infos);
     812   [ #  #  #  # ]:          8 :                 core->thread_infos = NULL;
     813                 :          0 :         }
     814                 :            : 
     815                 :          2 :         g_scheduling_in_progress = false;
     816                 :          2 : }
     817                 :            : 
     818                 :            : static void
     819                 :        203 : _reactors_scheduler_balance(void *arg1, void *arg2)
     820                 :            : {
     821                 :        203 :         struct spdk_scheduler *scheduler = spdk_scheduler_get();
     822                 :            : 
     823   [ +  -  +  + ]:        203 :         if (g_reactor_state != SPDK_REACTOR_STATE_RUNNING || scheduler == NULL) {
     824                 :          2 :                 _reactors_scheduler_cancel(NULL, NULL);
     825                 :          2 :                 return;
     826                 :            :         }
     827                 :            : 
     828   [ +  -  +  -  :        201 :         scheduler->balance(g_core_infos, g_reactor_count);
             -  +  +  - ]
     829                 :            : 
     830                 :        201 :         g_scheduler_core_number = spdk_env_get_first_core();
     831                 :        201 :         _reactors_scheduler_update_core_mode(NULL, NULL);
     832                 :          6 : }
     833                 :            : 
     834                 :            : /* Phase 1 of thread scheduling is to gather metrics on the existing threads */
     835                 :            : static void
     836                 :       1272 : _reactors_scheduler_gather_metrics(void *arg1, void *arg2)
     837                 :            : {
     838                 :            :         struct spdk_scheduler_core_info *core_info;
     839                 :            :         struct spdk_lw_thread *lw_thread;
     840                 :            :         struct spdk_thread *thread;
     841                 :            :         struct spdk_reactor *reactor;
     842                 :            :         uint32_t next_core;
     843                 :       1272 :         uint32_t i = 0;
     844                 :            : 
     845                 :       1272 :         reactor = spdk_reactor_get(spdk_env_get_current_core());
     846   [ +  +  #  # ]:       1272 :         assert(reactor != NULL);
     847   [ +  -  +  -  :       1272 :         core_info = &g_core_infos[reactor->lcore];
                   +  - ]
     848   [ +  -  +  -  :       1272 :         core_info->lcore = reactor->lcore;
             +  -  +  - ]
     849   [ +  -  +  -  :       1272 :         core_info->current_idle_tsc = reactor->idle_tsc - core_info->total_idle_tsc;
          +  -  +  -  +  
                -  +  - ]
     850   [ +  -  +  -  :       1272 :         core_info->total_idle_tsc = reactor->idle_tsc;
             +  -  +  - ]
     851   [ +  -  +  -  :       1272 :         core_info->current_busy_tsc = reactor->busy_tsc - core_info->total_busy_tsc;
          +  -  +  -  +  
                -  +  - ]
     852   [ +  -  +  -  :       1272 :         core_info->total_busy_tsc = reactor->busy_tsc;
             +  -  +  - ]
     853   [ +  +  +  -  :       1272 :         core_info->interrupt_mode = reactor->in_interrupt;
          +  -  +  -  +  
                      - ]
     854   [ +  -  +  - ]:       1272 :         core_info->threads_count = 0;
     855   [ +  -  +  -  :       1272 :         core_info->isolated = scheduler_is_isolated_core(reactor->lcore);
             +  -  +  - ]
     856                 :            : 
     857   [ +  +  +  +  :       1272 :         SPDK_DEBUGLOG(reactor, "Gathering metrics on %u\n", reactor->lcore);
          +  -  #  #  #  
                      # ]
     858                 :            : 
     859   [ +  +  +  +  :       1272 :         spdk_trace_record(TRACE_SCHEDULER_CORE_STATS, reactor->trace_id, 0, 0,
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     860                 :            :                           core_info->current_busy_tsc,
     861                 :            :                           core_info->current_idle_tsc);
     862                 :            : 
     863   [ +  +  +  -  :       1272 :         if (reactor->thread_count > 0) {
                   +  + ]
     864   [ +  -  +  -  :        377 :                 core_info->thread_infos = calloc(reactor->thread_count, sizeof(*core_info->thread_infos));
             +  -  +  - ]
     865   [ +  +  +  -  :        377 :                 if (core_info->thread_infos == NULL) {
                   +  - ]
     866   [ #  #  #  # ]:          0 :                         SPDK_ERRLOG("Failed to allocate memory when gathering metrics on %u\n", reactor->lcore);
     867                 :            : 
     868                 :            :                         /* Cancel this round of schedule work */
     869                 :          0 :                         _event_call(spdk_scheduler_get_scheduling_lcore(), _reactors_scheduler_cancel, NULL, NULL);
     870                 :          0 :                         return;
     871                 :            :                 }
     872                 :            : 
     873   [ +  +  +  -  :       1891 :                 TAILQ_FOREACH(lw_thread, &reactor->threads, link) {
          +  -  +  +  +  
             -  +  -  +  
                      - ]
     874                 :       1514 :                         _init_thread_stats(reactor, lw_thread);
     875                 :            : 
     876   [ -  +  -  +  :       1514 :                         core_info->thread_infos[i].lcore = lw_thread->lcore;
          -  +  -  +  -  
             +  -  +  -  
                      + ]
     877                 :       1514 :                         thread = spdk_thread_get_from_ctx(lw_thread);
     878   [ +  +  #  # ]:       1514 :                         assert(thread != NULL);
     879   [ +  -  +  -  :       1514 :                         core_info->thread_infos[i].thread_id = spdk_thread_get_id(thread);
          +  -  +  -  +  
                      - ]
     880   [ +  -  +  -  :       1514 :                         core_info->thread_infos[i].total_stats = lw_thread->total_stats;
          +  -  +  -  +  
                      - ]
     881   [ +  -  +  -  :       1514 :                         core_info->thread_infos[i].current_stats = lw_thread->current_stats;
          +  -  +  -  +  
                      - ]
     882         [ +  - ]:       1514 :                         core_info->threads_count++;
     883   [ +  +  +  -  :       1514 :                         assert(core_info->threads_count <= reactor->thread_count);
          +  -  +  -  +  
                -  #  # ]
     884                 :            : 
     885   [ +  +  +  +  :       1514 :                         spdk_trace_record(TRACE_SCHEDULER_THREAD_STATS, spdk_thread_get_trace_id(thread), 0, 0,
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     886                 :            :                                           lw_thread->current_stats.busy_tsc,
     887                 :            :                                           lw_thread->current_stats.idle_tsc);
     888                 :            : 
     889                 :       1514 :                         i++;
     890                 :         33 :                 }
     891                 :         11 :         }
     892                 :            : 
     893   [ +  -  +  - ]:       1272 :         next_core = spdk_env_get_next_core(reactor->lcore);
     894         [ +  + ]:       1272 :         if (next_core == UINT32_MAX) {
     895                 :        203 :                 next_core = spdk_env_get_first_core();
     896                 :          6 :         }
     897                 :            : 
     898                 :            :         /* If we've looped back around to the scheduler thread, move to the next phase */
     899         [ +  + ]:       1272 :         if (next_core == spdk_scheduler_get_scheduling_lcore()) {
     900                 :            :                 /* Phase 2 of scheduling is rebalancing - deciding which threads to move where */
     901                 :        203 :                 _event_call(next_core, _reactors_scheduler_balance, NULL, NULL);
     902                 :        203 :                 return;
     903                 :            :         }
     904                 :            : 
     905                 :       1069 :         _event_call(next_core, _reactors_scheduler_gather_metrics, NULL, NULL);
     906                 :         21 : }
     907                 :            : 
     908                 :            : static int _reactor_schedule_thread(struct spdk_thread *thread);
     909                 :            : static uint64_t g_rusage_period;
     910                 :            : 
     911                 :            : static void
     912                 :       6529 : _reactor_remove_lw_thread(struct spdk_reactor *reactor, struct spdk_lw_thread *lw_thread)
     913                 :            : {
     914                 :       6529 :         struct spdk_thread      *thread = spdk_thread_get_from_ctx(lw_thread);
     915                 :            :         struct spdk_fd_group    *grp;
     916                 :            : 
     917   [ +  +  +  -  :       6529 :         TAILQ_REMOVE(&reactor->threads, lw_thread, link);
          +  -  +  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     918   [ +  +  +  -  :       6529 :         assert(reactor->thread_count > 0);
             +  -  #  # ]
     919         [ +  - ]:       6529 :         reactor->thread_count--;
     920                 :            : 
     921                 :            :         /* Operate thread intr if running with full interrupt ability */
     922         [ +  + ]:       6529 :         if (spdk_interrupt_mode_is_enabled()) {
     923   [ +  +  +  -  :        130 :                 if (reactor->in_interrupt) {
             #  #  #  # ]
     924                 :        130 :                         grp = spdk_thread_get_interrupt_fd_group(thread);
     925   [ #  #  #  # ]:        130 :                         spdk_fd_group_unnest(reactor->fgrp, grp);
     926                 :          0 :                 }
     927                 :          0 :         }
     928                 :       6529 : }
     929                 :            : 
     930                 :            : static bool
     931                 : 7858416639 : reactor_post_process_lw_thread(struct spdk_reactor *reactor, struct spdk_lw_thread *lw_thread)
     932                 :            : {
     933                 : 7858416639 :         struct spdk_thread *thread = spdk_thread_get_from_ctx(lw_thread);
     934                 :            : 
     935   [ +  +  +  + ]: 7858416639 :         if (spdk_unlikely(spdk_thread_is_exited(thread) &&
     936                 :            :                           spdk_thread_is_idle(thread))) {
     937                 :       3200 :                 _reactor_remove_lw_thread(reactor, lw_thread);
     938                 :       3200 :                 spdk_thread_destroy(thread);
     939                 :       3200 :                 return true;
     940                 :            :         }
     941                 :            : 
     942   [ +  +  +  +  : 7858413439 :         if (spdk_unlikely(lw_thread->resched && !spdk_thread_is_bound(thread))) {
          +  +  +  +  +  
                      + ]
     943   [ -  +  -  + ]:        112 :                 lw_thread->resched = false;
     944                 :        112 :                 _reactor_remove_lw_thread(reactor, lw_thread);
     945                 :        112 :                 _reactor_schedule_thread(thread);
     946                 :        112 :                 return true;
     947                 :            :         }
     948                 :            : 
     949                 : 7858413327 :         return false;
     950                 :  356994584 : }
     951                 :            : 
     952                 :            : static void
     953                 :  140987504 : reactor_interrupt_run(struct spdk_reactor *reactor)
     954                 :            : {
     955                 :  140987504 :         int block_timeout = -1; /* _EPOLL_WAIT_FOREVER */
     956                 :            : 
     957   [ +  -  +  - ]:  140987504 :         spdk_fd_group_wait(reactor->fgrp, block_timeout);
     958                 :  140987504 : }
     959                 :            : 
     960                 :            : static void
     961                 : 7167999186 : _reactor_run(struct spdk_reactor *reactor)
     962                 :            : {
     963                 :            :         struct spdk_thread      *thread;
     964                 :            :         struct spdk_lw_thread   *lw_thread, *tmp;
     965                 :            :         uint64_t                now;
     966                 :            :         int                     rc;
     967                 :            : 
     968                 : 7167999186 :         event_queue_run_batch(reactor);
     969                 :            : 
     970                 :            :         /* If no threads are present on the reactor,
     971                 :            :          * tsc_last gets outdated. Update it to track
     972                 :            :          * thread execution time correctly. */
     973   [ +  +  +  -  : 7167999186 :         if (spdk_unlikely(TAILQ_EMPTY(&reactor->threads))) {
             +  -  +  + ]
     974                 : 2759219849 :                 now = spdk_get_ticks();
     975   [ +  -  +  -  : 2759219849 :                 reactor->idle_tsc += now - reactor->tsc_last;
             +  -  +  - ]
     976   [ +  -  +  - ]: 2759219849 :                 reactor->tsc_last = now;
     977                 : 2759219849 :                 return;
     978                 :            :         }
     979                 :            : 
     980   [ +  +  +  +  :12267635256 :         TAILQ_FOREACH_SAFE(lw_thread, &reactor->threads, link, tmp) {
          +  -  +  +  +  
          -  +  -  +  -  
                   +  + ]
     981                 : 7858855925 :                 thread = spdk_thread_get_from_ctx(lw_thread);
     982   [ +  -  +  - ]: 7858855925 :                 rc = spdk_thread_poll(thread, 0, reactor->tsc_last);
     983                 :            : 
     984                 : 7858855925 :                 now = spdk_thread_get_last_tsc(thread);
     985         [ +  + ]: 7858855925 :                 if (rc == 0) {
     986   [ -  +  -  +  : 7754034838 :                         reactor->idle_tsc += now - reactor->tsc_last;
             -  +  -  + ]
     987         [ +  + ]:  461093563 :                 } else if (rc > 0) {
     988   [ +  -  +  -  :  104821084 :                         reactor->busy_tsc += now - reactor->tsc_last;
             +  -  +  - ]
     989                 :    1161513 :                 }
     990   [ +  -  +  - ]: 7858855919 :                 reactor->tsc_last = now;
     991                 :            : 
     992                 : 7858855919 :                 reactor_post_process_lw_thread(reactor, lw_thread);
     993                 :  357433986 :         }
     994                 :  262301323 : }
     995                 :            : 
     996                 :            : static int
     997                 :       3433 : reactor_run(void *arg)
     998                 :            : {
     999                 :       3433 :         struct spdk_reactor     *reactor = arg;
    1000                 :            :         struct spdk_thread      *thread;
    1001                 :            :         struct spdk_lw_thread   *lw_thread, *tmp;
    1002                 :       1216 :         char                    thread_name[32];
    1003                 :       3433 :         uint64_t                last_sched = 0;
    1004                 :            : 
    1005   [ +  -  +  - ]:       3433 :         SPDK_NOTICELOG("Reactor started on core %u\n", reactor->lcore);
    1006                 :            : 
    1007                 :            :         /* Rename the POSIX thread because the reactor is tied to the POSIX
    1008                 :            :          * thread in the SPDK event library.
    1009                 :            :          */
    1010   [ +  -  +  - ]:       3433 :         snprintf(thread_name, sizeof(thread_name), "reactor_%u", reactor->lcore);
    1011                 :       3433 :         _set_thread_name(thread_name);
    1012                 :            : 
    1013   [ +  -  +  - ]:       3433 :         reactor->trace_id = spdk_trace_register_owner(OWNER_TYPE_REACTOR, thread_name);
    1014                 :            : 
    1015   [ +  -  +  - ]:       3433 :         reactor->tsc_last = spdk_get_ticks();
    1016                 :            : 
    1017                 :  258788487 :         while (1) {
    1018                 :            :                 /* Execute interrupt process fn if this reactor currently runs in interrupt state */
    1019   [ +  +  +  +  : 7305469327 :                 if (spdk_unlikely(reactor->in_interrupt)) {
             +  -  +  + ]
    1020                 :  140987463 :                         reactor_interrupt_run(reactor);
    1021                 :       4372 :                 } else {
    1022                 : 7164481864 :                         _reactor_run(reactor);
    1023                 :            :                 }
    1024                 :            : 
    1025   [ +  +  +  + ]: 7305469327 :                 if (g_framework_context_switch_monitor_enabled) {
    1026   [ +  +  +  -  : 7305167218 :                         if ((reactor->last_rusage + g_rusage_period) < reactor->tsc_last) {
          +  -  +  -  +  
                      + ]
    1027                 :      26089 :                                 get_rusage(reactor);
    1028   [ +  -  +  -  :      26089 :                                 reactor->last_rusage = reactor->tsc_last;
             +  -  +  - ]
    1029                 :        451 :                         }
    1030                 :  258486378 :                 }
    1031                 :            : 
    1032   [ +  +  +  +  : 7305469327 :                 if (spdk_unlikely(g_scheduler_period_in_tsc > 0 &&
          +  +  +  +  +  
             +  +  +  +  
                      + ]
    1033                 :            :                                   (reactor->tsc_last - last_sched) > g_scheduler_period_in_tsc &&
    1034                 :            :                                   reactor == g_scheduling_reactor &&
    1035                 :            :                                   !g_scheduling_in_progress)) {
    1036   [ +  -  +  - ]:        182 :                         last_sched = reactor->tsc_last;
    1037                 :        182 :                         g_scheduling_in_progress = true;
    1038   [ +  +  +  +  :        182 :                         spdk_trace_record(TRACE_SCHEDULER_PERIOD_START, 0, 0, 0);
          +  -  +  -  +  
          -  +  -  +  -  
                   -  + ]
    1039                 :        182 :                         _reactors_scheduler_gather_metrics(NULL, NULL);
    1040                 :          3 :                 }
    1041                 :            : 
    1042         [ +  + ]: 7305469327 :                 if (g_reactor_state != SPDK_REACTOR_STATE_RUNNING) {
    1043                 :       3433 :                         break;
    1044                 :            :                 }
    1045                 :            :         }
    1046                 :            : 
    1047   [ +  +  +  -  :       6651 :         TAILQ_FOREACH(lw_thread, &reactor->threads, link) {
          +  -  +  +  +  
             -  +  -  +  
                      - ]
    1048                 :       3218 :                 thread = spdk_thread_get_from_ctx(lw_thread);
    1049                 :            :                 /* All threads should have already had spdk_thread_exit() called on them, except
    1050                 :            :                  * for the app thread.
    1051                 :            :                  */
    1052         [ +  + ]:       3218 :                 if (spdk_thread_is_running(thread)) {
    1053         [ +  + ]:       2288 :                         if (!spdk_thread_is_app_thread(thread)) {
    1054                 :          0 :                                 SPDK_ERRLOG("spdk_thread_exit() was not called on thread '%s'\n",
    1055                 :            :                                             spdk_thread_get_name(thread));
    1056                 :          0 :                                 SPDK_ERRLOG("This will result in a non-zero exit code in a future release.\n");
    1057                 :          0 :                         }
    1058                 :       2288 :                         spdk_set_thread(thread);
    1059                 :       2288 :                         spdk_thread_exit(thread);
    1060                 :        110 :                 }
    1061                 :        190 :         }
    1062                 :            : 
    1063   [ +  +  +  -  :       8335 :         while (!TAILQ_EMPTY(&reactor->threads)) {
             +  -  +  + ]
    1064   [ +  +  +  +  :      10407 :                 TAILQ_FOREACH_SAFE(lw_thread, &reactor->threads, link, tmp) {
          +  -  +  +  +  
          -  +  -  +  -  
                   +  + ]
    1065                 :       5505 :                         thread = spdk_thread_get_from_ctx(lw_thread);
    1066                 :       5505 :                         spdk_set_thread(thread);
    1067         [ +  + ]:       5505 :                         if (spdk_thread_is_exited(thread)) {
    1068                 :       3217 :                                 _reactor_remove_lw_thread(reactor, lw_thread);
    1069                 :       3217 :                                 spdk_thread_destroy(thread);
    1070                 :        189 :                         } else {
    1071   [ +  +  +  +  :       2288 :                                 if (spdk_unlikely(reactor->in_interrupt)) {
             +  -  -  + ]
    1072                 :         41 :                                         reactor_interrupt_run(reactor);
    1073                 :          0 :                                 } else {
    1074                 :       2247 :                                         spdk_thread_poll(thread, 0, 0);
    1075                 :            :                                 }
    1076                 :            :                         }
    1077                 :        299 :                 }
    1078                 :            :         }
    1079                 :            : 
    1080                 :       3433 :         return 0;
    1081                 :            : }
    1082                 :            : 
    1083                 :            : int
    1084                 :          8 : spdk_app_parse_core_mask(const char *mask, struct spdk_cpuset *cpumask)
    1085                 :            : {
    1086                 :            :         int ret;
    1087                 :            :         const struct spdk_cpuset *validmask;
    1088                 :            : 
    1089                 :          8 :         ret = spdk_cpuset_parse(cpumask, mask);
    1090         [ -  + ]:          8 :         if (ret < 0) {
    1091                 :          0 :                 return ret;
    1092                 :            :         }
    1093                 :            : 
    1094                 :          8 :         validmask = spdk_app_get_core_mask();
    1095                 :          8 :         spdk_cpuset_and(cpumask, validmask);
    1096                 :            : 
    1097                 :          8 :         return 0;
    1098                 :          0 : }
    1099                 :            : 
    1100                 :            : const struct spdk_cpuset *
    1101                 :        397 : spdk_app_get_core_mask(void)
    1102                 :            : {
    1103                 :        397 :         return &g_reactor_core_mask;
    1104                 :            : }
    1105                 :            : 
    1106                 :            : void
    1107                 :       2288 : spdk_reactors_start(void)
    1108                 :            : {
    1109                 :            :         struct spdk_reactor *reactor;
    1110                 :            :         uint32_t i, current_core;
    1111                 :            :         int rc;
    1112                 :            : 
    1113         [ +  - ]:       2288 :         g_rusage_period = (CONTEXT_SWITCH_MONITOR_PERIOD * spdk_get_ticks_hz()) / SPDK_SEC_TO_USEC;
    1114                 :       2288 :         g_reactor_state = SPDK_REACTOR_STATE_RUNNING;
    1115                 :            :         /* Reinitialize to false, in case the app framework is restarting in the same process. */
    1116                 :       2288 :         g_stopping_reactors = false;
    1117                 :            : 
    1118                 :       2288 :         current_core = spdk_env_get_current_core();
    1119         [ +  + ]:       5692 :         SPDK_ENV_FOREACH_CORE(i) {
    1120         [ +  + ]:       3404 :                 if (i != current_core) {
    1121                 :       1116 :                         reactor = spdk_reactor_get(i);
    1122         [ +  + ]:       1116 :                         if (reactor == NULL) {
    1123                 :          0 :                                 continue;
    1124                 :            :                         }
    1125                 :            : 
    1126   [ +  -  +  - ]:       1116 :                         rc = spdk_env_thread_launch_pinned(reactor->lcore, reactor_run, reactor);
    1127         [ +  + ]:       1116 :                         if (rc < 0) {
    1128   [ #  #  #  # ]:          0 :                                 SPDK_ERRLOG("Unable to start reactor thread on core %u\n", reactor->lcore);
    1129         [ #  # ]:          0 :                                 assert(false);
    1130                 :            :                                 return;
    1131                 :            :                         }
    1132                 :         38 :                 }
    1133                 :       3404 :                 spdk_cpuset_set_cpu(&g_reactor_core_mask, i, true);
    1134                 :        148 :         }
    1135                 :            : 
    1136                 :            :         /* Start the main reactor */
    1137                 :       2288 :         reactor = spdk_reactor_get(current_core);
    1138   [ +  +  #  # ]:       2288 :         assert(reactor != NULL);
    1139                 :       2288 :         reactor_run(reactor);
    1140                 :            : 
    1141                 :       2288 :         spdk_env_thread_wait_all();
    1142                 :            : 
    1143                 :       2288 :         g_reactor_state = SPDK_REACTOR_STATE_SHUTDOWN;
    1144                 :        110 : }
    1145                 :            : 
    1146                 :            : static void
    1147                 :       2288 : _reactors_stop(void *arg1, void *arg2)
    1148                 :            : {
    1149                 :            :         uint32_t i;
    1150                 :            :         int rc;
    1151                 :            :         struct spdk_reactor *reactor;
    1152                 :            :         struct spdk_reactor *local_reactor;
    1153                 :       2288 :         uint64_t notify = 1;
    1154                 :            : 
    1155                 :       2288 :         g_reactor_state = SPDK_REACTOR_STATE_EXITING;
    1156                 :       2288 :         local_reactor = spdk_reactor_get(spdk_env_get_current_core());
    1157                 :            : 
    1158         [ +  + ]:       5692 :         SPDK_ENV_FOREACH_CORE(i) {
    1159                 :            :                 /* If spdk_event_call isn't called  on a reactor, always send a notification.
    1160                 :            :                  * If it is called on a reactor, send a notification if the destination reactor
    1161                 :            :                  * is indicated in interrupt mode state.
    1162                 :            :                  */
    1163   [ +  -  +  +  :       3404 :                 if (local_reactor == NULL || spdk_cpuset_get_cpu(&local_reactor->notify_cpuset, i)) {
                   -  + ]
    1164                 :        173 :                         reactor = spdk_reactor_get(i);
    1165   [ -  +  #  # ]:        173 :                         assert(reactor != NULL);
    1166   [ #  #  #  # ]:        173 :                         rc = write(reactor->events_fd, &notify, sizeof(notify));
    1167         [ -  + ]:        173 :                         if (rc < 0) {
    1168         [ #  # ]:          0 :                                 SPDK_ERRLOG("failed to notify event queue for reactor(%u): %s.\n", i, spdk_strerror(errno));
    1169                 :          0 :                                 continue;
    1170                 :            :                         }
    1171                 :          0 :                 }
    1172                 :        148 :         }
    1173                 :       2288 : }
    1174                 :            : 
    1175                 :            : static void
    1176                 :       3404 : nop(void *arg1, void *arg2)
    1177                 :            : {
    1178                 :       3404 : }
    1179                 :            : 
    1180                 :            : void
    1181                 :       2288 : spdk_reactors_stop(void *arg1)
    1182                 :            : {
    1183                 :       2288 :         spdk_for_each_reactor(nop, NULL, NULL, _reactors_stop);
    1184                 :       2288 : }
    1185                 :            : 
    1186                 :            : static pthread_mutex_t g_scheduler_mtx = PTHREAD_MUTEX_INITIALIZER;
    1187                 :            : static uint32_t g_next_core = UINT32_MAX;
    1188                 :            : 
    1189                 :            : static void
    1190                 :       6540 : _schedule_thread(void *arg1, void *arg2)
    1191                 :            : {
    1192                 :       6540 :         struct spdk_lw_thread *lw_thread = arg1;
    1193                 :            :         struct spdk_thread *thread;
    1194                 :            :         struct spdk_reactor *reactor;
    1195                 :            :         uint32_t current_core;
    1196                 :            :         struct spdk_fd_group *grp;
    1197                 :            : 
    1198                 :       6540 :         current_core = spdk_env_get_current_core();
    1199                 :       6540 :         reactor = spdk_reactor_get(current_core);
    1200   [ +  +  #  # ]:       6540 :         assert(reactor != NULL);
    1201                 :            : 
    1202                 :            :         /* Update total_stats to reflect state of thread
    1203                 :            :         * at the end of the move. */
    1204                 :       6540 :         thread = spdk_thread_get_from_ctx(lw_thread);
    1205                 :       6540 :         spdk_set_thread(thread);
    1206         [ +  - ]:       6540 :         spdk_thread_get_stats(&lw_thread->total_stats);
    1207                 :       6540 :         spdk_set_thread(NULL);
    1208                 :            : 
    1209   [ +  +  +  -  :       6540 :         if (lw_thread->initial_lcore == SPDK_ENV_LCORE_ID_ANY) {
                   +  + ]
    1210   [ +  -  +  - ]:       6428 :                 lw_thread->initial_lcore = current_core;
    1211                 :        394 :         }
    1212   [ +  -  +  - ]:       6540 :         lw_thread->lcore = current_core;
    1213                 :            : 
    1214   [ +  -  +  -  :       6540 :         TAILQ_INSERT_TAIL(&reactor->threads, lw_thread, link);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1215         [ +  - ]:       6540 :         reactor->thread_count++;
    1216                 :            : 
    1217                 :            :         /* Operate thread intr if running with full interrupt ability */
    1218         [ +  + ]:       6540 :         if (spdk_interrupt_mode_is_enabled()) {
    1219                 :            :                 int rc;
    1220                 :            : 
    1221   [ +  +  +  -  :        130 :                 if (reactor->in_interrupt) {
             #  #  #  # ]
    1222                 :        130 :                         grp = spdk_thread_get_interrupt_fd_group(thread);
    1223   [ #  #  #  # ]:        130 :                         rc = spdk_fd_group_nest(reactor->fgrp, grp);
    1224         [ -  + ]:        130 :                         if (rc < 0) {
    1225         [ #  # ]:          0 :                                 SPDK_ERRLOG("Failed to schedule spdk_thread: %s.\n", spdk_strerror(-rc));
    1226                 :          0 :                         }
    1227                 :          0 :                 }
    1228                 :            : 
    1229                 :            :                 /* Align spdk_thread with reactor to interrupt mode or poll mode */
    1230                 :        130 :                 spdk_thread_send_msg(thread, _reactor_set_thread_interrupt_mode, reactor);
    1231                 :          0 :         }
    1232                 :       6540 : }
    1233                 :            : 
    1234                 :            : static int
    1235                 :       6541 : _reactor_schedule_thread(struct spdk_thread *thread)
    1236                 :            : {
    1237                 :            :         uint32_t core, initial_core;
    1238                 :            :         struct spdk_lw_thread *lw_thread;
    1239                 :       6541 :         struct spdk_event *evt = NULL;
    1240                 :            :         struct spdk_cpuset *cpumask;
    1241                 :            :         uint32_t i;
    1242                 :       6541 :         struct spdk_reactor *local_reactor = NULL;
    1243                 :       6541 :         uint32_t current_lcore = spdk_env_get_current_core();
    1244                 :       2474 :         struct spdk_cpuset polling_cpumask;
    1245                 :       2474 :         struct spdk_cpuset valid_cpumask;
    1246                 :            : 
    1247                 :       6541 :         cpumask = spdk_thread_get_cpumask(thread);
    1248                 :            : 
    1249                 :       6541 :         lw_thread = spdk_thread_get_ctx(thread);
    1250   [ +  +  #  # ]:       6541 :         assert(lw_thread != NULL);
    1251   [ +  -  +  - ]:       6541 :         core = lw_thread->lcore;
    1252   [ +  -  +  - ]:       6541 :         initial_core = lw_thread->initial_lcore;
    1253         [ +  + ]:       6541 :         memset(lw_thread, 0, sizeof(*lw_thread));
    1254   [ +  -  +  - ]:       6541 :         lw_thread->initial_lcore = initial_core;
    1255                 :            : 
    1256         [ +  + ]:       6541 :         if (current_lcore != SPDK_ENV_LCORE_ID_ANY) {
    1257                 :       6534 :                 local_reactor = spdk_reactor_get(current_lcore);
    1258   [ +  +  #  # ]:       6534 :                 assert(local_reactor);
    1259                 :        394 :         }
    1260                 :            : 
    1261                 :            :         /* When interrupt ability of spdk_thread is not enabled and the current
    1262                 :            :          * reactor runs on DPDK thread, skip reactors which are in interrupt mode.
    1263                 :            :          */
    1264   [ +  +  +  + ]:       6541 :         if (!spdk_interrupt_mode_is_enabled() && local_reactor != NULL) {
    1265                 :            :                 /* Get the cpumask of all reactors in polling */
    1266                 :       6404 :                 spdk_cpuset_zero(&polling_cpumask);
    1267         [ +  + ]:      19204 :                 SPDK_ENV_FOREACH_CORE(i) {
    1268                 :      12800 :                         spdk_cpuset_set_cpu(&polling_cpumask, i, true);
    1269                 :        662 :                 }
    1270         [ +  - ]:       6404 :                 spdk_cpuset_xor(&polling_cpumask, &local_reactor->notify_cpuset);
    1271                 :            : 
    1272         [ +  + ]:       6404 :                 if (core == SPDK_ENV_LCORE_ID_ANY) {
    1273                 :            :                         /* Get the cpumask of all valid reactors which are suggested and also in polling */
    1274                 :       6304 :                         spdk_cpuset_copy(&valid_cpumask, &polling_cpumask);
    1275                 :       6304 :                         spdk_cpuset_and(&valid_cpumask, spdk_thread_get_cpumask(thread));
    1276                 :            : 
    1277                 :            :                         /* If there are any valid reactors, spdk_thread should be scheduled
    1278                 :            :                          * into one of the valid reactors.
    1279                 :            :                          * If there is no valid reactors, spdk_thread should be scheduled
    1280                 :            :                          * into one of the polling reactors.
    1281                 :            :                          */
    1282         [ +  + ]:       6304 :                         if (spdk_cpuset_count(&valid_cpumask) != 0) {
    1283                 :       6154 :                                 cpumask = &valid_cpumask;
    1284                 :        382 :                         } else {
    1285                 :        150 :                                 cpumask = &polling_cpumask;
    1286                 :            :                         }
    1287         [ +  + ]:        488 :                 } else if (!spdk_cpuset_get_cpu(&polling_cpumask, core)) {
    1288                 :            :                         /* If specified reactor is not in polling, spdk_thread should be scheduled
    1289                 :            :                          * into one of the polling reactors.
    1290                 :            :                          */
    1291                 :          0 :                         core = SPDK_ENV_LCORE_ID_ANY;
    1292                 :          0 :                         cpumask = &polling_cpumask;
    1293                 :          0 :                 }
    1294                 :        394 :         }
    1295                 :            : 
    1296         [ +  - ]:       6541 :         pthread_mutex_lock(&g_scheduler_mtx);
    1297         [ +  + ]:       6541 :         if (core == SPDK_ENV_LCORE_ID_ANY) {
    1298         [ +  + ]:       7980 :                 for (i = 0; i < spdk_env_get_core_count(); i++) {
    1299         [ +  + ]:       7980 :                         if (g_next_core >= g_reactor_count) {
    1300                 :       5125 :                                 g_next_core = spdk_env_get_first_core();
    1301                 :        319 :                         }
    1302                 :       7980 :                         core = g_next_core;
    1303                 :       7980 :                         g_next_core = spdk_env_get_next_core(g_next_core);
    1304                 :            : 
    1305         [ +  + ]:       7980 :                         if (spdk_cpuset_get_cpu(cpumask, core)) {
    1306                 :       6441 :                                 break;
    1307                 :            :                         }
    1308                 :         66 :                 }
    1309                 :        395 :         }
    1310                 :            : 
    1311                 :       6541 :         evt = spdk_event_allocate(core, _schedule_thread, lw_thread, NULL);
    1312                 :            : 
    1313         [ +  + ]:       6541 :         if (current_lcore != core) {
    1314   [ +  +  +  +  :       1402 :                 spdk_trace_record(TRACE_SCHEDULER_MOVE_THREAD, spdk_thread_get_trace_id(thread), 0, 0,
          +  -  +  -  +  
          -  +  -  +  -  
                   -  + ]
    1315                 :            :                                   current_lcore, core);
    1316                 :         83 :         }
    1317                 :            : 
    1318         [ +  - ]:       6541 :         pthread_mutex_unlock(&g_scheduler_mtx);
    1319                 :            : 
    1320   [ +  +  #  # ]:       6541 :         assert(evt != NULL);
    1321         [ +  + ]:       6541 :         if (evt == NULL) {
    1322                 :          0 :                 SPDK_ERRLOG("Unable to schedule thread on requested core mask.\n");
    1323                 :          0 :                 return -1;
    1324                 :            :         }
    1325                 :            : 
    1326   [ +  -  +  - ]:       6541 :         lw_thread->tsc_start = spdk_get_ticks();
    1327                 :            : 
    1328                 :       6541 :         spdk_event_call(evt);
    1329                 :            : 
    1330                 :       6541 :         return 0;
    1331                 :        401 : }
    1332                 :            : 
    1333                 :            : static void
    1334                 :        138 : _reactor_request_thread_reschedule(struct spdk_thread *thread)
    1335                 :            : {
    1336                 :            :         struct spdk_lw_thread *lw_thread;
    1337                 :            :         struct spdk_reactor *reactor;
    1338                 :            :         uint32_t current_core;
    1339                 :            : 
    1340   [ +  +  #  # ]:        138 :         assert(thread == spdk_get_thread());
    1341                 :            : 
    1342                 :        138 :         lw_thread = spdk_thread_get_ctx(thread);
    1343                 :            : 
    1344   [ +  +  #  # ]:        138 :         assert(lw_thread != NULL);
    1345   [ #  #  #  # ]:        138 :         lw_thread->resched = true;
    1346   [ #  #  #  # ]:        138 :         lw_thread->lcore = SPDK_ENV_LCORE_ID_ANY;
    1347                 :            : 
    1348                 :        138 :         current_core = spdk_env_get_current_core();
    1349                 :        138 :         reactor = spdk_reactor_get(current_core);
    1350   [ -  +  #  # ]:        138 :         assert(reactor != NULL);
    1351                 :            : 
    1352                 :            :         /* Send a notification if the destination reactor is indicated in intr mode state */
    1353   [ +  +  #  #  :        138 :         if (spdk_unlikely(spdk_cpuset_get_cpu(&reactor->notify_cpuset, reactor->lcore))) {
             #  #  #  # ]
    1354                 :        130 :                 uint64_t notify = 1;
    1355                 :            : 
    1356   [ -  +  #  #  :        130 :                 if (write(reactor->resched_fd, &notify, sizeof(notify)) < 0) {
                   #  # ]
    1357         [ #  # ]:          0 :                         SPDK_ERRLOG("failed to notify reschedule: %s.\n", spdk_strerror(errno));
    1358                 :          0 :                 }
    1359                 :          0 :         }
    1360                 :        138 : }
    1361                 :            : 
    1362                 :            : static int
    1363                 :       6567 : reactor_thread_op(struct spdk_thread *thread, enum spdk_thread_op op)
    1364                 :            : {
    1365                 :            :         struct spdk_lw_thread *lw_thread;
    1366                 :            : 
    1367      [ +  +  + ]:       6567 :         switch (op) {
    1368                 :       6035 :         case SPDK_THREAD_OP_NEW:
    1369                 :       6429 :                 lw_thread = spdk_thread_get_ctx(thread);
    1370   [ -  +  -  + ]:       6429 :                 lw_thread->lcore = SPDK_ENV_LCORE_ID_ANY;
    1371   [ -  +  -  + ]:       6429 :                 lw_thread->initial_lcore = SPDK_ENV_LCORE_ID_ANY;
    1372                 :       6429 :                 return _reactor_schedule_thread(thread);
    1373                 :        136 :         case SPDK_THREAD_OP_RESCHED:
    1374                 :        138 :                 _reactor_request_thread_reschedule(thread);
    1375                 :        138 :                 return 0;
    1376                 :          0 :         default:
    1377                 :          0 :                 return -ENOTSUP;
    1378                 :            :         }
    1379                 :        396 : }
    1380                 :            : 
    1381                 :            : static bool
    1382                 :       6445 : reactor_thread_op_supported(enum spdk_thread_op op)
    1383                 :            : {
    1384         [ +  + ]:       6445 :         switch (op) {
    1385                 :       6049 :         case SPDK_THREAD_OP_NEW:
    1386                 :            :         case SPDK_THREAD_OP_RESCHED:
    1387                 :       6445 :                 return true;
    1388                 :          0 :         default:
    1389                 :          0 :                 return false;
    1390                 :            :         }
    1391                 :        396 : }
    1392                 :            : 
    1393                 :            : struct call_reactor {
    1394                 :            :         uint32_t cur_core;
    1395                 :            :         spdk_event_fn fn;
    1396                 :            :         void *arg1;
    1397                 :            :         void *arg2;
    1398                 :            : 
    1399                 :            :         uint32_t orig_core;
    1400                 :            :         spdk_event_fn cpl;
    1401                 :            : };
    1402                 :            : 
    1403                 :            : static void
    1404                 :     100964 : on_reactor(void *arg1, void *arg2)
    1405                 :            : {
    1406                 :     100964 :         struct call_reactor *cr = arg1;
    1407                 :            :         struct spdk_event *evt;
    1408                 :            : 
    1409   [ +  -  +  -  :     100964 :         cr->fn(cr->arg1, cr->arg2);
          -  +  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1410                 :            : 
    1411   [ +  -  +  -  :     100964 :         cr->cur_core = spdk_env_get_next_core(cr->cur_core);
             +  -  +  - ]
    1412                 :            : 
    1413   [ +  +  +  -  :     100964 :         if (cr->cur_core >= g_reactor_count) {
                   +  + ]
    1414   [ +  +  +  +  :      26620 :                 SPDK_DEBUGLOG(reactor, "Completed reactor iteration\n");
                   +  - ]
    1415                 :            : 
    1416   [ +  -  +  -  :      26620 :                 evt = spdk_event_allocate(cr->orig_core, cr->cpl, cr->arg1, cr->arg2);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1417                 :      26620 :                 free(cr);
    1418                 :       1574 :         } else {
    1419   [ +  +  +  +  :      74344 :                 SPDK_DEBUGLOG(reactor, "Continuing reactor iteration to %d\n",
          +  -  #  #  #  
                      # ]
    1420                 :            :                               cr->cur_core);
    1421                 :            : 
    1422   [ +  -  +  - ]:      74344 :                 evt = spdk_event_allocate(cr->cur_core, on_reactor, arg1, NULL);
    1423                 :            :         }
    1424   [ +  +  #  # ]:     100964 :         assert(evt != NULL);
    1425                 :     100964 :         spdk_event_call(evt);
    1426                 :     100964 : }
    1427                 :            : 
    1428                 :            : void
    1429                 :      26642 : spdk_for_each_reactor(spdk_event_fn fn, void *arg1, void *arg2, spdk_event_fn cpl)
    1430                 :            : {
    1431                 :            :         struct call_reactor *cr;
    1432                 :            : 
    1433                 :            :         /* When the application framework is shutting down, we will send one
    1434                 :            :          * final for_each_reactor operation with completion callback _reactors_stop,
    1435                 :            :          * to flush any existing for_each_reactor operations to avoid any memory
    1436                 :            :          * leaks. We use a mutex here to protect a boolean flag that will ensure
    1437                 :            :          * we don't start any more operations once we've started shutting down.
    1438                 :            :          */
    1439         [ +  + ]:      26642 :         pthread_mutex_lock(&g_stopping_reactors_mtx);
    1440   [ +  +  +  + ]:      26642 :         if (g_stopping_reactors) {
    1441         [ +  + ]:         22 :                 pthread_mutex_unlock(&g_stopping_reactors_mtx);
    1442                 :         22 :                 return;
    1443         [ +  + ]:      26620 :         } else if (cpl == _reactors_stop) {
    1444                 :       2288 :                 g_stopping_reactors = true;
    1445                 :        110 :         }
    1446         [ +  + ]:      26620 :         pthread_mutex_unlock(&g_stopping_reactors_mtx);
    1447                 :            : 
    1448                 :      26620 :         cr = calloc(1, sizeof(*cr));
    1449         [ +  + ]:      26620 :         if (!cr) {
    1450                 :          0 :                 SPDK_ERRLOG("Unable to perform reactor iteration\n");
    1451   [ #  #  #  # ]:          0 :                 cpl(arg1, arg2);
    1452                 :          0 :                 return;
    1453                 :            :         }
    1454                 :            : 
    1455   [ +  -  +  - ]:      26620 :         cr->fn = fn;
    1456   [ +  -  +  - ]:      26620 :         cr->arg1 = arg1;
    1457   [ +  -  +  - ]:      26620 :         cr->arg2 = arg2;
    1458   [ +  -  +  - ]:      26620 :         cr->cpl = cpl;
    1459   [ +  -  +  - ]:      26620 :         cr->orig_core = spdk_env_get_current_core();
    1460   [ +  -  +  - ]:      26620 :         cr->cur_core = spdk_env_get_first_core();
    1461                 :            : 
    1462   [ +  +  +  +  :      26620 :         SPDK_DEBUGLOG(reactor, "Starting reactor iteration from %d\n", cr->orig_core);
          +  -  #  #  #  
                      # ]
    1463                 :            : 
    1464   [ +  -  +  - ]:      26620 :         _event_call(cr->cur_core, on_reactor, cr, NULL);
    1465                 :       1575 : }
    1466                 :            : 
    1467                 :            : #ifdef __linux__
    1468                 :            : static int
    1469                 :        206 : reactor_schedule_thread_event(void *arg)
    1470                 :            : {
    1471                 :        206 :         struct spdk_reactor *reactor = arg;
    1472                 :            :         struct spdk_lw_thread *lw_thread, *tmp;
    1473                 :        206 :         uint32_t count = 0;
    1474                 :        206 :         uint64_t notify = 1;
    1475                 :            : 
    1476   [ +  +  +  +  :        206 :         assert(reactor->in_interrupt);
          +  -  +  -  #  
                      # ]
    1477                 :            : 
    1478   [ +  +  +  -  :        206 :         if (read(reactor->resched_fd, &notify, sizeof(notify)) < 0) {
                   +  - ]
    1479         [ #  # ]:          0 :                 SPDK_ERRLOG("failed to acknowledge reschedule: %s.\n", spdk_strerror(errno));
    1480   [ #  #  #  # ]:          0 :                 return -errno;
    1481                 :            :         }
    1482                 :            : 
    1483   [ +  +  +  -  :        328 :         TAILQ_FOREACH_SAFE(lw_thread, &reactor->threads, link, tmp) {
          +  -  +  -  #  
          #  #  #  #  #  
                   -  + ]
    1484                 :        122 :                 count += reactor_post_process_lw_thread(reactor, lw_thread) ? 1 : 0;
    1485                 :          0 :         }
    1486                 :            : 
    1487                 :        206 :         return count;
    1488                 :          3 : }
    1489                 :            : 
    1490                 :            : static int
    1491                 :       3433 : reactor_interrupt_init(struct spdk_reactor *reactor)
    1492                 :            : {
    1493                 :            :         int rc;
    1494                 :            : 
    1495         [ +  - ]:       3433 :         rc = spdk_fd_group_create(&reactor->fgrp);
    1496         [ -  + ]:       3433 :         if (rc != 0) {
    1497                 :          0 :                 return rc;
    1498                 :            :         }
    1499                 :            : 
    1500   [ +  -  +  - ]:       3433 :         reactor->resched_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
    1501   [ +  +  +  -  :       3433 :         if (reactor->resched_fd < 0) {
                   -  + ]
    1502                 :          0 :                 rc = -EBADF;
    1503                 :          0 :                 goto err;
    1504                 :            :         }
    1505                 :            : 
    1506   [ +  -  +  -  :       3433 :         rc = SPDK_FD_GROUP_ADD(reactor->fgrp, reactor->resched_fd, reactor_schedule_thread_event,
             +  -  +  - ]
    1507                 :            :                                reactor);
    1508         [ -  + ]:       3433 :         if (rc) {
    1509   [ #  #  #  # ]:          0 :                 close(reactor->resched_fd);
    1510                 :          0 :                 goto err;
    1511                 :            :         }
    1512                 :            : 
    1513   [ +  -  +  - ]:       3433 :         reactor->events_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
    1514   [ +  +  +  -  :       3433 :         if (reactor->events_fd < 0) {
                   +  - ]
    1515   [ #  #  #  #  :          0 :                 spdk_fd_group_remove(reactor->fgrp, reactor->resched_fd);
             #  #  #  # ]
    1516   [ #  #  #  # ]:          0 :                 close(reactor->resched_fd);
    1517                 :            : 
    1518                 :          0 :                 rc = -EBADF;
    1519                 :          0 :                 goto err;
    1520                 :            :         }
    1521                 :            : 
    1522   [ +  -  +  -  :       3433 :         rc = SPDK_FD_GROUP_ADD(reactor->fgrp, reactor->events_fd,
             +  -  +  - ]
    1523                 :            :                                event_queue_run_batch, reactor);
    1524         [ -  + ]:       3433 :         if (rc) {
    1525   [ #  #  #  #  :          0 :                 spdk_fd_group_remove(reactor->fgrp, reactor->resched_fd);
             #  #  #  # ]
    1526   [ #  #  #  # ]:          0 :                 close(reactor->resched_fd);
    1527   [ #  #  #  # ]:          0 :                 close(reactor->events_fd);
    1528                 :          0 :                 goto err;
    1529                 :            :         }
    1530                 :            : 
    1531                 :       3433 :         return 0;
    1532                 :            : 
    1533                 :          0 : err:
    1534   [ #  #  #  # ]:          0 :         spdk_fd_group_destroy(reactor->fgrp);
    1535   [ #  #  #  # ]:          0 :         reactor->fgrp = NULL;
    1536                 :          0 :         return rc;
    1537                 :         83 : }
    1538                 :            : #else
    1539                 :            : static int
    1540                 :         94 : reactor_interrupt_init(struct spdk_reactor *reactor)
    1541                 :            : {
    1542                 :         94 :         return -ENOTSUP;
    1543                 :            : }
    1544                 :            : #endif
    1545                 :            : 
    1546                 :            : static void
    1547                 :       3527 : reactor_interrupt_fini(struct spdk_reactor *reactor)
    1548                 :            : {
    1549   [ +  -  +  - ]:       3527 :         struct spdk_fd_group *fgrp = reactor->fgrp;
    1550                 :            : 
    1551         [ +  + ]:       3527 :         if (!fgrp) {
    1552                 :         94 :                 return;
    1553                 :            :         }
    1554                 :            : 
    1555   [ -  +  -  + ]:       3433 :         spdk_fd_group_remove(fgrp, reactor->events_fd);
    1556   [ -  +  -  + ]:       3433 :         spdk_fd_group_remove(fgrp, reactor->resched_fd);
    1557                 :            : 
    1558   [ -  +  -  + ]:       3433 :         close(reactor->events_fd);
    1559   [ -  +  -  + ]:       3433 :         close(reactor->resched_fd);
    1560                 :            : 
    1561                 :       3433 :         spdk_fd_group_destroy(fgrp);
    1562   [ -  +  -  + ]:       3433 :         reactor->fgrp = NULL;
    1563                 :        177 : }
    1564                 :            : 
    1565                 :            : static struct spdk_governor *
    1566                 :       2136 : _governor_find(const char *name)
    1567                 :            : {
    1568                 :            :         struct spdk_governor *governor, *tmp;
    1569                 :            : 
    1570   [ +  +  +  +  :       2136 :         TAILQ_FOREACH_SAFE(governor, &g_governor_list, link, tmp) {
          +  -  +  -  +  
                      + ]
    1571   [ +  +  +  +  :         33 :                 if (strcmp(name, governor->name) == 0) {
          +  -  +  -  -  
                      + ]
    1572                 :         33 :                         return governor;
    1573                 :            :                 }
    1574                 :          0 :         }
    1575                 :            : 
    1576                 :       2103 :         return NULL;
    1577                 :         60 : }
    1578                 :            : 
    1579                 :            : int
    1580                 :         67 : spdk_governor_set(const char *name)
    1581                 :            : {
    1582                 :            :         struct spdk_governor *governor;
    1583                 :         67 :         int rc = 0;
    1584                 :            : 
    1585                 :            :         /* NULL governor was specifically requested */
    1586         [ +  + ]:         67 :         if (name == NULL) {
    1587         [ +  + ]:         30 :                 if (g_governor) {
    1588   [ #  #  #  #  :          9 :                         g_governor->deinit();
             #  #  #  # ]
    1589                 :          0 :                 }
    1590                 :         30 :                 g_governor = NULL;
    1591                 :         30 :                 return 0;
    1592                 :            :         }
    1593                 :            : 
    1594                 :         37 :         governor = _governor_find(name);
    1595         [ +  + ]:         37 :         if (governor == NULL) {
    1596                 :          4 :                 return -EINVAL;
    1597                 :            :         }
    1598                 :            : 
    1599         [ +  + ]:         33 :         if (g_governor == governor) {
    1600                 :          0 :                 return 0;
    1601                 :            :         }
    1602                 :            : 
    1603   [ +  -  +  -  :         33 :         rc = governor->init();
             -  +  +  - ]
    1604         [ +  + ]:         33 :         if (rc == 0) {
    1605         [ -  + ]:         12 :                 if (g_governor) {
    1606   [ #  #  #  #  :          0 :                         g_governor->deinit();
             #  #  #  # ]
    1607                 :          0 :                 }
    1608                 :         12 :                 g_governor = governor;
    1609                 :          0 :         }
    1610                 :            : 
    1611                 :         33 :         return rc;
    1612                 :          3 : }
    1613                 :            : 
    1614                 :            : struct spdk_governor *
    1615                 :       1389 : spdk_governor_get(void)
    1616                 :            : {
    1617                 :       1389 :         return g_governor;
    1618                 :            : }
    1619                 :            : 
    1620                 :            : void
    1621                 :       2099 : spdk_governor_register(struct spdk_governor *governor)
    1622                 :            : {
    1623   [ +  +  +  -  :       2099 :         if (_governor_find(governor->name)) {
                   +  - ]
    1624   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG("governor named '%s' already registered.\n", governor->name);
    1625         [ #  # ]:          0 :                 assert(false);
    1626                 :            :                 return;
    1627                 :            :         }
    1628                 :            : 
    1629   [ +  -  +  -  :       2099 :         TAILQ_INSERT_TAIL(&g_governor_list, governor, link);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1630                 :         58 : }
    1631                 :            : 
    1632                 :       2411 : SPDK_LOG_REGISTER_COMPONENT(reactor)
    1633                 :            : 
    1634                 :            : static void
    1635                 :       2289 : scheduler_trace(void)
    1636                 :            : {
    1637                 :       2289 :         struct spdk_trace_tpoint_opts opts[] = {
    1638                 :            :                 {
    1639                 :            :                         "SCHEDULER_PERIOD_START", TRACE_SCHEDULER_PERIOD_START,
    1640                 :            :                         OWNER_TYPE_NONE, OBJECT_NONE, 0,
    1641                 :            :                         {
    1642                 :            : 
    1643                 :            :                         }
    1644                 :            :                 },
    1645                 :            :                 {
    1646                 :            :                         "SCHEDULER_CORE_STATS", TRACE_SCHEDULER_CORE_STATS,
    1647                 :            :                         OWNER_TYPE_REACTOR, OBJECT_NONE, 0,
    1648                 :            :                         {
    1649                 :            :                                 { "busy", SPDK_TRACE_ARG_TYPE_INT, 8},
    1650                 :            :                                 { "idle", SPDK_TRACE_ARG_TYPE_INT, 8}
    1651                 :            :                         }
    1652                 :            :                 },
    1653                 :            :                 {
    1654                 :            :                         "SCHEDULER_THREAD_STATS", TRACE_SCHEDULER_THREAD_STATS,
    1655                 :            :                         OWNER_TYPE_THREAD, OBJECT_NONE, 0,
    1656                 :            :                         {
    1657                 :            :                                 { "busy", SPDK_TRACE_ARG_TYPE_INT, 8},
    1658                 :            :                                 { "idle", SPDK_TRACE_ARG_TYPE_INT, 8}
    1659                 :            :                         }
    1660                 :            :                 },
    1661                 :            :                 {
    1662                 :            :                         "SCHEDULER_MOVE_THREAD", TRACE_SCHEDULER_MOVE_THREAD,
    1663                 :            :                         OWNER_TYPE_THREAD, OBJECT_NONE, 0,
    1664                 :            :                         {
    1665                 :            :                                 { "src", SPDK_TRACE_ARG_TYPE_INT, 8 },
    1666                 :            :                                 { "dst", SPDK_TRACE_ARG_TYPE_INT, 8 }
    1667                 :            :                         }
    1668                 :            :                 }
    1669                 :            :         };
    1670                 :            : 
    1671                 :       2289 :         spdk_trace_register_owner_type(OWNER_TYPE_REACTOR, 'r');
    1672                 :       2289 :         spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
    1673                 :            : 
    1674                 :       2289 : }
    1675                 :            : 
    1676                 :       2411 : SPDK_TRACE_REGISTER_FN(scheduler_trace, "scheduler", TRACE_GROUP_SCHEDULER)

Generated by: LCOV version 1.15