LCOV - code coverage report
Current view: top level - spdk/test/nvme/reset - reset.c (source / functions) Hit Total Coverage
Test: Combined Lines: 250 342 73.1 %
Date: 2024-07-12 15:00:37 Functions: 20 22 90.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 145 337 43.0 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2015 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : 
       8                 :            : #include "spdk/nvme.h"
       9                 :            : #include "spdk/env.h"
      10                 :            : #include "spdk/string.h"
      11                 :            : #include "spdk/pci_ids.h"
      12                 :            : 
      13                 :            : struct ctrlr_entry {
      14                 :            :         struct spdk_nvme_ctrlr          *ctrlr;
      15                 :            :         TAILQ_ENTRY(ctrlr_entry)        link;
      16                 :            :         char                            name[1024];
      17                 :            : };
      18                 :            : 
      19                 :            : struct ns_entry {
      20                 :            :         struct spdk_nvme_ns     *ns;
      21                 :            :         struct spdk_nvme_ctrlr  *ctrlr;
      22                 :            :         TAILQ_ENTRY(ns_entry)   link;
      23                 :            :         uint32_t                io_size_blocks;
      24                 :            :         uint64_t                size_in_ios;
      25                 :            :         char                    name[1024];
      26                 :            : };
      27                 :            : 
      28                 :            : struct ns_worker_ctx {
      29                 :            :         struct ns_entry                 *entry;
      30                 :            :         struct spdk_nvme_qpair          *qpair;
      31                 :            :         uint64_t                        io_completed;
      32                 :            :         uint64_t                        io_completed_error;
      33                 :            :         uint64_t                        io_submitted;
      34                 :            :         uint64_t                        current_queue_depth;
      35                 :            :         uint64_t                        offset_in_ios;
      36                 :            :         bool                            is_draining;
      37                 :            : 
      38                 :            :         TAILQ_ENTRY(ns_worker_ctx)      link;
      39                 :            : };
      40                 :            : 
      41                 :            : struct reset_task {
      42                 :            :         struct ns_worker_ctx    *ns_ctx;
      43                 :            :         void                    *buf;
      44                 :            : };
      45                 :            : 
      46                 :            : struct worker_thread {
      47                 :            :         TAILQ_HEAD(, ns_worker_ctx)     ns_ctx;
      48                 :            :         unsigned                        lcore;
      49                 :            : };
      50                 :            : 
      51                 :            : static struct spdk_mempool *task_pool;
      52                 :            : 
      53                 :            : static TAILQ_HEAD(, ctrlr_entry) g_controllers = TAILQ_HEAD_INITIALIZER(g_controllers);
      54                 :            : static TAILQ_HEAD(, ns_entry) g_namespaces = TAILQ_HEAD_INITIALIZER(g_namespaces);
      55                 :            : static int g_num_namespaces = 0;
      56                 :            : static struct worker_thread *g_worker = NULL;
      57                 :            : static bool g_qemu_ssd_found = false;
      58                 :            : 
      59                 :            : static uint64_t g_tsc_rate;
      60                 :            : 
      61                 :            : static int g_io_size_bytes;
      62                 :            : static int g_rw_percentage;
      63                 :            : static int g_is_random;
      64                 :            : static int g_queue_depth;
      65                 :            : static int g_time_in_sec;
      66                 :            : 
      67                 :            : #define  TASK_POOL_NUM 8192
      68                 :            : 
      69                 :            : static void
      70                 :          1 : register_ns(struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_ns *ns)
      71                 :            : {
      72                 :            :         struct ns_entry *entry;
      73                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
      74                 :            : 
      75         [ -  + ]:          1 :         if (!spdk_nvme_ns_is_active(ns)) {
      76         [ #  # ]:          0 :                 printf("Skipping inactive NS %u\n", spdk_nvme_ns_get_id(ns));
      77                 :          0 :                 return;
      78                 :            :         }
      79                 :            : 
      80                 :          1 :         entry = malloc(sizeof(struct ns_entry));
      81         [ -  + ]:          1 :         if (entry == NULL) {
      82                 :          0 :                 perror("ns_entry malloc");
      83                 :          0 :                 exit(1);
      84                 :            :         }
      85                 :            : 
      86                 :          1 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
      87                 :            : 
      88                 :          1 :         entry->ns = ns;
      89                 :          1 :         entry->ctrlr = ctrlr;
      90         [ -  + ]:          1 :         entry->size_in_ios = spdk_nvme_ns_get_size(ns) /
      91                 :            :                              g_io_size_bytes;
      92         [ -  + ]:          1 :         entry->io_size_blocks = g_io_size_bytes / spdk_nvme_ns_get_sector_size(ns);
      93                 :            : 
      94         [ -  + ]:          1 :         snprintf(entry->name, 44, "%-20.20s (%-20.20s)", cdata->mn, cdata->sn);
      95                 :            : 
      96                 :          1 :         g_num_namespaces++;
      97                 :          1 :         TAILQ_INSERT_TAIL(&g_namespaces, entry, link);
      98                 :            : }
      99                 :            : 
     100                 :            : static void
     101                 :          1 : register_ctrlr(struct spdk_nvme_ctrlr *ctrlr)
     102                 :            : {
     103                 :            :         int nsid;
     104                 :            :         struct spdk_nvme_ns *ns;
     105                 :          1 :         struct ctrlr_entry *entry = malloc(sizeof(struct ctrlr_entry));
     106                 :            : 
     107         [ -  + ]:          1 :         if (entry == NULL) {
     108                 :          0 :                 perror("ctrlr_entry malloc");
     109                 :          0 :                 exit(1);
     110                 :            :         }
     111                 :            : 
     112                 :          1 :         entry->ctrlr = ctrlr;
     113                 :          1 :         TAILQ_INSERT_TAIL(&g_controllers, entry, link);
     114                 :            : 
     115         [ +  + ]:          2 :         for (nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr); nsid != 0;
     116                 :          1 :              nsid = spdk_nvme_ctrlr_get_next_active_ns(ctrlr, nsid)) {
     117                 :          1 :                 ns = spdk_nvme_ctrlr_get_ns(ctrlr, nsid);
     118         [ -  + ]:          1 :                 if (ns == NULL) {
     119                 :          0 :                         continue;
     120                 :            :                 }
     121                 :          1 :                 register_ns(ctrlr, ns);
     122                 :            :         }
     123                 :          1 : }
     124                 :            : 
     125                 :            : static void io_complete(void *ctx, const struct spdk_nvme_cpl *completion);
     126                 :            : 
     127                 :            : static __thread unsigned int seed = 0;
     128                 :            : 
     129                 :            : static void
     130                 :    1829124 : submit_single_io(struct ns_worker_ctx *ns_ctx)
     131                 :            : {
     132                 :    1829124 :         struct reset_task       *task = NULL;
     133                 :            :         uint64_t                offset_in_ios;
     134                 :            :         int                     rc;
     135                 :    1829124 :         struct ns_entry         *entry = ns_ctx->entry;
     136                 :            : 
     137                 :    1829124 :         task = spdk_mempool_get(task_pool);
     138         [ -  + ]:    1829124 :         if (!task) {
     139   [ #  #  #  # ]:          0 :                 fprintf(stderr, "Failed to get task from task_pool\n");
     140                 :          0 :                 exit(1);
     141                 :            :         }
     142                 :            : 
     143                 :    1829124 :         task->buf = spdk_zmalloc(g_io_size_bytes, 0x200, NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
     144         [ -  + ]:    1829124 :         if (!task->buf) {
     145                 :          0 :                 spdk_free(task->buf);
     146   [ #  #  #  # ]:          0 :                 fprintf(stderr, "task->buf spdk_zmalloc failed\n");
     147                 :          0 :                 exit(1);
     148                 :            :         }
     149                 :            : 
     150                 :    1829124 :         task->ns_ctx = ns_ctx;
     151                 :            : 
     152         [ -  + ]:    1829124 :         if (g_is_random) {
     153         [ #  # ]:          0 :                 offset_in_ios = rand_r(&seed) % entry->size_in_ios;
     154                 :            :         } else {
     155                 :    1829124 :                 offset_in_ios = ns_ctx->offset_in_ios++;
     156         [ -  + ]:    1829124 :                 if (ns_ctx->offset_in_ios == entry->size_in_ios) {
     157                 :          0 :                         ns_ctx->offset_in_ios = 0;
     158                 :            :                 }
     159                 :            :         }
     160                 :            : 
     161         [ +  - ]:    1829124 :         if ((g_rw_percentage == 100) ||
     162   [ -  +  -  - ]:    1829124 :             (g_rw_percentage != 0 && ((rand_r(&seed) % 100) < g_rw_percentage))) {
     163                 :          0 :                 rc = spdk_nvme_ns_cmd_read(entry->ns, ns_ctx->qpair, task->buf,
     164                 :          0 :                                            offset_in_ios * entry->io_size_blocks,
     165                 :            :                                            entry->io_size_blocks, io_complete, task, 0);
     166                 :            :         } else {
     167                 :    3658248 :                 rc = spdk_nvme_ns_cmd_write(entry->ns, ns_ctx->qpair, task->buf,
     168                 :    1829124 :                                             offset_in_ios * entry->io_size_blocks,
     169                 :            :                                             entry->io_size_blocks, io_complete, task, 0);
     170                 :            :         }
     171                 :            : 
     172         [ -  + ]:    1829124 :         if (rc != 0) {
     173   [ #  #  #  # ]:          0 :                 fprintf(stderr, "starting I/O failed\n");
     174                 :            :         } else {
     175                 :    1829124 :                 ns_ctx->current_queue_depth++;
     176                 :    1829124 :                 ns_ctx->io_submitted++;
     177                 :            :         }
     178                 :    1829124 : }
     179                 :            : 
     180                 :            : static void
     181                 :    1829124 : task_complete(struct reset_task *task, const struct spdk_nvme_cpl *completion)
     182                 :            : {
     183                 :            :         struct ns_worker_ctx    *ns_ctx;
     184                 :            : 
     185                 :    1829124 :         ns_ctx = task->ns_ctx;
     186                 :    1829124 :         ns_ctx->current_queue_depth--;
     187                 :            : 
     188   [ +  +  -  + ]:    1829124 :         if (spdk_nvme_cpl_is_error(completion)) {
     189                 :        192 :                 ns_ctx->io_completed_error++;
     190                 :            :         } else {
     191                 :    1828932 :                 ns_ctx->io_completed++;
     192                 :            :         }
     193                 :            : 
     194                 :    1829124 :         spdk_free(task->buf);
     195                 :    1829124 :         spdk_mempool_put(task_pool, task);
     196                 :            : 
     197                 :            :         /*
     198                 :            :          * is_draining indicates when time has expired for the test run
     199                 :            :          * and we are just waiting for the previously submitted I/O
     200                 :            :          * to complete.  In this case, do not submit a new I/O to replace
     201                 :            :          * the one just completed.
     202                 :            :          */
     203   [ -  +  +  + ]:    1829124 :         if (!ns_ctx->is_draining) {
     204                 :    1828932 :                 submit_single_io(ns_ctx);
     205                 :            :         }
     206                 :    1829124 : }
     207                 :            : 
     208                 :            : static void
     209                 :    1829124 : io_complete(void *ctx, const struct spdk_nvme_cpl *completion)
     210                 :            : {
     211                 :    1829124 :         task_complete((struct reset_task *)ctx, completion);
     212                 :    1829124 : }
     213                 :            : 
     214                 :            : static void
     215                 :      14444 : check_io(struct ns_worker_ctx *ns_ctx)
     216                 :            : {
     217                 :      14444 :         spdk_nvme_qpair_process_completions(ns_ctx->qpair, 0);
     218                 :      14444 : }
     219                 :            : 
     220                 :            : static void
     221                 :          3 : submit_io(struct ns_worker_ctx *ns_ctx, int queue_depth)
     222                 :            : {
     223         [ +  + ]:        195 :         while (queue_depth-- > 0) {
     224                 :        192 :                 submit_single_io(ns_ctx);
     225                 :            :         }
     226                 :          3 : }
     227                 :            : 
     228                 :            : static void
     229                 :          3 : drain_io(struct ns_worker_ctx *ns_ctx)
     230                 :            : {
     231                 :          3 :         ns_ctx->is_draining = true;
     232         [ +  + ]:          6 :         while (ns_ctx->current_queue_depth > 0) {
     233                 :          3 :                 check_io(ns_ctx);
     234                 :            :         }
     235                 :          3 : }
     236                 :            : 
     237                 :            : static int
     238                 :          3 : work_fn(void *arg)
     239                 :            : {
     240                 :          3 :         uint64_t tsc_end = spdk_get_ticks() + g_time_in_sec * g_tsc_rate;
     241                 :          3 :         struct worker_thread *worker = (struct worker_thread *)arg;
     242                 :          3 :         struct ns_worker_ctx *ns_ctx = NULL;
     243                 :          3 :         bool did_reset = false;
     244                 :            : 
     245         [ -  + ]:          3 :         printf("Starting thread on core %u\n", worker->lcore);
     246                 :            : 
     247                 :            :         /* Submit initial I/O for each namespace. */
     248         [ +  + ]:          6 :         TAILQ_FOREACH(ns_ctx, &worker->ns_ctx, link) {
     249                 :          3 :                 ns_ctx->qpair = spdk_nvme_ctrlr_alloc_io_qpair(ns_ctx->entry->ctrlr, NULL, 0);
     250         [ -  + ]:          3 :                 if (ns_ctx->qpair == NULL) {
     251   [ #  #  #  # ]:          0 :                         fprintf(stderr, "spdk_nvme_ctrlr_alloc_io_qpair() failed on core %u\n", worker->lcore);
     252                 :          0 :                         return -1;
     253                 :            :                 }
     254                 :          3 :                 submit_io(ns_ctx, g_queue_depth);
     255                 :            :         }
     256                 :            : 
     257                 :            :         while (1) {
     258   [ +  +  -  +  :      14441 :                 if (!did_reset && ((tsc_end - spdk_get_ticks()) / g_tsc_rate) > (uint64_t)g_time_in_sec / 2) {
                   +  - ]
     259         [ +  + ]:          6 :                         TAILQ_FOREACH(ns_ctx, &worker->ns_ctx, link) {
     260         [ -  + ]:          3 :                                 if (spdk_nvme_ctrlr_reset(ns_ctx->entry->ctrlr) < 0) {
     261   [ #  #  #  # ]:          0 :                                         fprintf(stderr, "nvme reset failed.\n");
     262                 :          0 :                                         return -1;
     263                 :            :                                 }
     264                 :            :                         }
     265                 :          3 :                         did_reset = true;
     266                 :            :                 }
     267                 :            : 
     268                 :            :                 /*
     269                 :            :                  * Check for completed I/O for each controller. A new
     270                 :            :                  * I/O will be submitted in the io_complete callback
     271                 :            :                  * to replace each I/O that is completed.
     272                 :            :                  */
     273         [ +  + ]:      28882 :                 TAILQ_FOREACH(ns_ctx, &worker->ns_ctx, link) {
     274                 :      14441 :                         check_io(ns_ctx);
     275                 :            :                 }
     276                 :            : 
     277         [ +  + ]:      14441 :                 if (spdk_get_ticks() > tsc_end) {
     278                 :          3 :                         break;
     279                 :            :                 }
     280                 :            :         }
     281                 :            : 
     282         [ +  + ]:          6 :         TAILQ_FOREACH(ns_ctx, &worker->ns_ctx, link) {
     283                 :          3 :                 drain_io(ns_ctx);
     284                 :          3 :                 spdk_nvme_ctrlr_free_io_qpair(ns_ctx->qpair);
     285                 :            :         }
     286                 :            : 
     287                 :          3 :         return 0;
     288                 :            : }
     289                 :            : 
     290                 :            : static void
     291                 :          0 : usage(char *program_name)
     292                 :            : {
     293         [ #  # ]:          0 :         printf("%s options", program_name);
     294                 :          0 :         printf("\n");
     295         [ #  # ]:          0 :         printf("\t[-q io depth]\n");
     296         [ #  # ]:          0 :         printf("\t[-o io size in bytes]\n");
     297         [ #  # ]:          0 :         printf("\t[-w io pattern type, must be one of\n");
     298         [ #  # ]:          0 :         printf("\t\t(read, write, randread, randwrite, rw, randrw)]\n");
     299         [ #  # ]:          0 :         printf("\t[-M rwmixread (100 for reads, 0 for writes)]\n");
     300         [ #  # ]:          0 :         printf("\t[-t time in seconds(should be larger than 15 seconds)]\n");
     301         [ #  # ]:          0 :         printf("\t\t(default:0 - unlimited)\n");
     302                 :          0 : }
     303                 :            : 
     304                 :            : static int
     305                 :          3 : print_stats(void)
     306                 :            : {
     307                 :            :         uint64_t io_completed, io_submitted, io_completed_error;
     308                 :            :         uint64_t total_completed_io, total_submitted_io, total_completed_err_io;
     309                 :            :         struct worker_thread    *worker;
     310                 :            :         struct ns_worker_ctx    *ns_ctx;
     311                 :            : 
     312                 :          3 :         total_completed_io = 0;
     313                 :          3 :         total_submitted_io = 0;
     314                 :          3 :         total_completed_err_io = 0;
     315                 :            : 
     316                 :          3 :         worker = g_worker;
     317         [ +  + ]:          6 :         TAILQ_FOREACH(ns_ctx, &worker->ns_ctx, link) {
     318                 :          3 :                 io_completed = ns_ctx->io_completed;
     319                 :          3 :                 io_submitted = ns_ctx->io_submitted;
     320                 :          3 :                 io_completed_error = ns_ctx->io_completed_error;
     321                 :          3 :                 total_completed_io += io_completed;
     322                 :          3 :                 total_submitted_io += io_submitted;
     323                 :          3 :                 total_completed_err_io += io_completed_error;
     324                 :            :         }
     325                 :            : 
     326         [ -  + ]:          3 :         printf("========================================================\n");
     327         [ -  + ]:          3 :         printf("%16" PRIu64 " IO completed successfully\n", total_completed_io);
     328         [ -  + ]:          3 :         printf("%16" PRIu64 " IO completed with error\n", total_completed_err_io);
     329         [ -  + ]:          3 :         printf("--------------------------------------------------------\n");
     330         [ -  + ]:          3 :         printf("%16" PRIu64 " IO completed total\n", total_completed_io + total_completed_err_io);
     331         [ -  + ]:          3 :         printf("%16" PRIu64 " IO submitted\n", total_submitted_io);
     332                 :            : 
     333         [ -  + ]:          3 :         if (total_submitted_io != (total_completed_io + total_completed_err_io)) {
     334   [ #  #  #  # ]:          0 :                 fprintf(stderr, "Some IO are missing......\n");
     335                 :          0 :                 return -1;
     336                 :            :         }
     337                 :            : 
     338                 :          3 :         return 0;
     339                 :            : }
     340                 :            : 
     341                 :            : static int
     342                 :          6 : parse_args(int argc, char **argv)
     343                 :            : {
     344                 :            :         const char *workload_type;
     345                 :            :         int op;
     346                 :          6 :         bool mix_specified = false;
     347                 :            :         long int val;
     348                 :            : 
     349                 :            :         /* default value */
     350                 :          6 :         g_queue_depth = 0;
     351                 :          6 :         g_io_size_bytes = 0;
     352                 :          6 :         workload_type = NULL;
     353                 :          6 :         g_time_in_sec = 0;
     354                 :          6 :         g_rw_percentage = -1;
     355                 :            : 
     356   [ +  +  +  + ]:         30 :         while ((op = getopt(argc, argv, "o:q:t:w:M:")) != -1) {
     357         [ +  + ]:         24 :                 if (op == 'w') {
     358                 :          6 :                         workload_type = optarg;
     359         [ -  + ]:         18 :                 } else if (op == '?') {
     360                 :          0 :                         usage(argv[0]);
     361                 :          0 :                         return -EINVAL;
     362                 :            :                 } else {
     363                 :         18 :                         val = spdk_strtol(optarg, 10);
     364         [ -  + ]:         18 :                         if (val < 0) {
     365         [ #  # ]:          0 :                                 fprintf(stderr, "Converting a string to integer failed\n");
     366                 :          0 :                                 return val;
     367                 :            :                         }
     368   [ +  +  +  -  :         18 :                         switch (op) {
                      - ]
     369                 :          6 :                         case 'q':
     370                 :          6 :                                 g_queue_depth = val;
     371                 :          6 :                                 break;
     372                 :          6 :                         case 'o':
     373                 :          6 :                                 g_io_size_bytes = val;
     374                 :          6 :                                 break;
     375                 :          6 :                         case 't':
     376                 :          6 :                                 g_time_in_sec = val;
     377                 :          6 :                                 break;
     378                 :          0 :                         case 'M':
     379                 :          0 :                                 g_rw_percentage = val;
     380                 :          0 :                                 mix_specified = true;
     381                 :          0 :                                 break;
     382                 :          0 :                         default:
     383                 :          0 :                                 usage(argv[0]);
     384                 :          0 :                                 return -EINVAL;
     385                 :            :                         }
     386                 :            :                 }
     387                 :            :         }
     388                 :            : 
     389         [ -  + ]:          6 :         if (!g_queue_depth) {
     390                 :          0 :                 usage(argv[0]);
     391                 :          0 :                 return 1;
     392                 :            :         }
     393         [ -  + ]:          6 :         if (!g_io_size_bytes) {
     394                 :          0 :                 usage(argv[0]);
     395                 :          0 :                 return 1;
     396                 :            :         }
     397         [ -  + ]:          6 :         if (!workload_type) {
     398                 :          0 :                 usage(argv[0]);
     399                 :          0 :                 return 1;
     400                 :            :         }
     401         [ -  + ]:          6 :         if (!g_time_in_sec) {
     402                 :          0 :                 usage(argv[0]);
     403                 :          0 :                 return 1;
     404                 :            :         }
     405                 :            : 
     406   [ +  +  +  - ]:          6 :         if (strcmp(workload_type, "read") &&
     407   [ -  +  -  + ]:          6 :             strcmp(workload_type, "write") &&
     408   [ #  #  #  # ]:          0 :             strcmp(workload_type, "randread") &&
     409   [ #  #  #  # ]:          0 :             strcmp(workload_type, "randwrite") &&
     410   [ #  #  #  # ]:          0 :             strcmp(workload_type, "rw") &&
     411   [ #  #  #  # ]:          0 :             strcmp(workload_type, "randrw")) {
     412         [ #  # ]:          0 :                 fprintf(stderr,
     413                 :            :                         "io pattern type must be one of\n"
     414                 :            :                         "(read, write, randread, randwrite, rw, randrw)\n");
     415                 :          0 :                 return 1;
     416                 :            :         }
     417                 :            : 
     418   [ +  +  +  - ]:          6 :         if (!strcmp(workload_type, "read") ||
     419   [ -  +  -  + ]:          6 :             !strcmp(workload_type, "randread")) {
     420                 :          0 :                 g_rw_percentage = 100;
     421                 :            :         }
     422                 :            : 
     423   [ -  +  -  + ]:          6 :         if (!strcmp(workload_type, "write") ||
     424   [ #  #  #  # ]:          0 :             !strcmp(workload_type, "randwrite")) {
     425                 :          6 :                 g_rw_percentage = 0;
     426                 :            :         }
     427                 :            : 
     428   [ +  +  +  - ]:          6 :         if (!strcmp(workload_type, "read") ||
     429   [ +  +  +  - ]:          6 :             !strcmp(workload_type, "randread") ||
     430   [ -  +  -  + ]:          6 :             !strcmp(workload_type, "write") ||
     431   [ #  #  #  # ]:          0 :             !strcmp(workload_type, "randwrite")) {
     432         [ -  + ]:          6 :                 if (mix_specified) {
     433         [ #  # ]:          0 :                         fprintf(stderr, "Ignoring -M option... Please use -M option"
     434                 :            :                                 " only when using rw or randrw.\n");
     435                 :            :                 }
     436                 :            :         }
     437                 :            : 
     438   [ +  +  +  - ]:          6 :         if (!strcmp(workload_type, "rw") ||
     439   [ -  +  -  + ]:          6 :             !strcmp(workload_type, "randrw")) {
     440   [ #  #  #  # ]:          0 :                 if (g_rw_percentage < 0 || g_rw_percentage > 100) {
     441         [ #  # ]:          0 :                         fprintf(stderr,
     442                 :            :                                 "-M must be specified to value from 0 to 100 "
     443                 :            :                                 "for rw or randrw.\n");
     444                 :          0 :                         return 1;
     445                 :            :                 }
     446                 :            :         }
     447                 :            : 
     448   [ +  +  +  - ]:          6 :         if (!strcmp(workload_type, "read") ||
     449   [ -  +  -  + ]:          6 :             !strcmp(workload_type, "write") ||
     450   [ #  #  #  # ]:          0 :             !strcmp(workload_type, "rw")) {
     451                 :          6 :                 g_is_random = 0;
     452                 :            :         } else {
     453                 :          0 :                 g_is_random = 1;
     454                 :            :         }
     455                 :            : 
     456                 :          6 :         return 0;
     457                 :            : }
     458                 :            : 
     459                 :            : static int
     460                 :          1 : register_worker(void)
     461                 :            : {
     462                 :            :         struct worker_thread *worker;
     463                 :            : 
     464                 :          1 :         worker = malloc(sizeof(struct worker_thread));
     465         [ -  + ]:          1 :         if (worker == NULL) {
     466                 :          0 :                 perror("worker_thread malloc");
     467                 :          0 :                 return -1;
     468                 :            :         }
     469                 :            : 
     470         [ -  + ]:          1 :         memset(worker, 0, sizeof(struct worker_thread));
     471                 :          1 :         TAILQ_INIT(&worker->ns_ctx);
     472                 :          1 :         worker->lcore = spdk_env_get_current_core();
     473                 :            : 
     474                 :          1 :         g_worker = worker;
     475                 :            : 
     476                 :          1 :         return 0;
     477                 :            : }
     478                 :            : 
     479                 :            : 
     480                 :            : static bool
     481                 :          0 : probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
     482                 :            :          struct spdk_nvme_ctrlr_opts *opts)
     483                 :            : {
     484                 :          0 :         opts->disable_error_logging = true;
     485                 :          0 :         return true;
     486                 :            : }
     487                 :            : 
     488                 :            : static void
     489                 :         10 : attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
     490                 :            :           struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
     491                 :            : {
     492         [ +  - ]:         10 :         if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
     493                 :         10 :                 struct spdk_pci_device *dev = spdk_nvme_ctrlr_get_pci_device(ctrlr);
     494                 :            : 
     495                 :            :                 /* QEMU emulated SSDs can't handle this test, so we will skip them.  QEMU NVMe SSDs
     496                 :            :                  * report themselves as VID == (Intel|Red Hat).  So we need to check this specific
     497                 :            :                  * (0x5845|0x0010) device ID to know whether it's QEMU or not.
     498                 :            :                  */
     499   [ +  +  +  - ]:         11 :                 if ((spdk_pci_device_get_vendor_id(dev) == SPDK_PCI_VID_INTEL &&
     500         [ +  + ]:         11 :                      spdk_pci_device_get_device_id(dev) == 0x5845) ||
     501         [ +  - ]:         19 :                     (spdk_pci_device_get_vendor_id(dev) == SPDK_PCI_VID_REDHAT &&
     502                 :          9 :                      spdk_pci_device_get_device_id(dev) == 0x0010)) {
     503                 :          9 :                         g_qemu_ssd_found = true;
     504         [ -  + ]:          9 :                         printf("Skipping QEMU NVMe SSD at %s\n", trid->traddr);
     505                 :          9 :                         return;
     506                 :            :                 }
     507                 :            :         }
     508                 :            : 
     509                 :          1 :         register_ctrlr(ctrlr);
     510                 :            : }
     511                 :            : 
     512                 :            : static int
     513                 :          6 : register_controllers(void)
     514                 :            : {
     515         [ -  + ]:          6 :         printf("Initializing NVMe Controllers\n");
     516                 :            : 
     517         [ -  + ]:          6 :         if (spdk_nvme_probe(NULL, NULL, probe_cb, attach_cb, NULL) != 0) {
     518   [ #  #  #  # ]:          0 :                 fprintf(stderr, "spdk_nvme_probe() failed\n");
     519                 :          0 :                 return 1;
     520                 :            :         }
     521                 :            : 
     522                 :          6 :         return 0;
     523                 :            : }
     524                 :            : 
     525                 :            : static void
     526                 :          1 : unregister_controllers(void)
     527                 :            : {
     528                 :            :         struct ctrlr_entry *entry, *tmp;
     529                 :          1 :         struct spdk_nvme_detach_ctx *detach_ctx = NULL;
     530                 :            : 
     531         [ +  + ]:          2 :         TAILQ_FOREACH_SAFE(entry, &g_controllers, link, tmp) {
     532         [ -  + ]:          1 :                 TAILQ_REMOVE(&g_controllers, entry, link);
     533                 :          1 :                 spdk_nvme_detach_async(entry->ctrlr, &detach_ctx);
     534                 :          1 :                 free(entry);
     535                 :            :         }
     536                 :            : 
     537         [ -  + ]:          1 :         if (detach_ctx) {
     538                 :          0 :                 spdk_nvme_detach_poll(detach_ctx);
     539                 :            :         }
     540                 :          1 : }
     541                 :            : 
     542                 :            : static int
     543                 :          1 : associate_workers_with_ns(void)
     544                 :            : {
     545                 :          1 :         struct ns_entry         *entry = TAILQ_FIRST(&g_namespaces);
     546                 :          1 :         struct worker_thread    *worker = g_worker;
     547                 :            :         struct ns_worker_ctx    *ns_ctx;
     548                 :            :         int                     i, count;
     549                 :            : 
     550                 :          1 :         count = g_num_namespaces;
     551                 :            : 
     552         [ +  + ]:          2 :         for (i = 0; i < count; i++) {
     553         [ -  + ]:          1 :                 if (entry == NULL) {
     554                 :          0 :                         break;
     555                 :            :                 }
     556                 :          1 :                 ns_ctx = malloc(sizeof(struct ns_worker_ctx));
     557         [ -  + ]:          1 :                 if (!ns_ctx) {
     558                 :          0 :                         return -1;
     559                 :            :                 }
     560         [ -  + ]:          1 :                 memset(ns_ctx, 0, sizeof(*ns_ctx));
     561                 :            : 
     562         [ -  + ]:          1 :                 printf("Associating %s with lcore %d\n", entry->name, worker->lcore);
     563                 :          1 :                 ns_ctx->entry = entry;
     564                 :          1 :                 TAILQ_INSERT_TAIL(&worker->ns_ctx, ns_ctx, link);
     565                 :            : 
     566                 :          1 :                 entry = TAILQ_NEXT(entry, link);;
     567         [ +  - ]:          1 :                 if (entry == NULL) {
     568                 :          1 :                         entry = TAILQ_FIRST(&g_namespaces);
     569                 :            :                 }
     570                 :            :         }
     571                 :            : 
     572                 :          1 :         return 0;
     573                 :            : }
     574                 :            : 
     575                 :            : static void
     576                 :          1 : unregister_worker(void)
     577                 :            : {
     578                 :            :         struct ns_worker_ctx    *ns_ctx, *tmp;
     579                 :            : 
     580         [ -  + ]:          1 :         assert(g_worker != NULL);
     581                 :            : 
     582         [ +  + ]:          2 :         TAILQ_FOREACH_SAFE(ns_ctx, &g_worker->ns_ctx, link, tmp) {
     583         [ -  + ]:          1 :                 TAILQ_REMOVE(&g_worker->ns_ctx, ns_ctx, link);
     584                 :          1 :                 free(ns_ctx);
     585                 :            :         }
     586                 :            : 
     587                 :          1 :         free(g_worker);
     588                 :          1 :         g_worker = NULL;
     589                 :          1 : }
     590                 :            : 
     591                 :            : static int
     592                 :          3 : run_nvme_reset_cycle(void)
     593                 :            : {
     594                 :          3 :         struct worker_thread *worker = g_worker;
     595                 :            :         struct ns_worker_ctx *ns_ctx;
     596                 :            : 
     597         [ -  + ]:          3 :         if (work_fn(worker) != 0) {
     598                 :          0 :                 return -1;
     599                 :            :         }
     600                 :            : 
     601         [ -  + ]:          3 :         if (print_stats() != 0) {
     602                 :          0 :                 return -1;
     603                 :            :         }
     604                 :            : 
     605         [ +  + ]:          6 :         TAILQ_FOREACH(ns_ctx, &worker->ns_ctx, link) {
     606                 :          3 :                 ns_ctx->io_completed = 0;
     607                 :          3 :                 ns_ctx->io_completed_error = 0;
     608                 :          3 :                 ns_ctx->io_submitted = 0;
     609                 :          3 :                 ns_ctx->is_draining = false;
     610                 :            :         }
     611                 :            : 
     612                 :          3 :         return 0;
     613                 :            : }
     614                 :            : 
     615                 :            : static void
     616                 :          1 : free_tasks(void)
     617                 :            : {
     618         [ -  + ]:          1 :         if (spdk_mempool_count(task_pool) != TASK_POOL_NUM) {
     619   [ #  #  #  # ]:          0 :                 fprintf(stderr, "task_pool count is %zu but should be %d\n",
     620                 :            :                         spdk_mempool_count(task_pool), TASK_POOL_NUM);
     621                 :            :         }
     622                 :          1 :         spdk_mempool_free(task_pool);
     623                 :          1 : }
     624                 :            : 
     625                 :            : int
     626                 :          6 : main(int argc, char **argv)
     627                 :            : {
     628                 :            :         int                     rc;
     629                 :            :         int                     i;
     630                 :          3 :         struct spdk_env_opts    opts;
     631                 :            : 
     632                 :            : 
     633                 :          6 :         rc = parse_args(argc, argv);
     634         [ -  + ]:          6 :         if (rc != 0) {
     635                 :          0 :                 return rc;
     636                 :            :         }
     637                 :            : 
     638                 :          6 :         spdk_env_opts_init(&opts);
     639                 :          6 :         opts.name = "reset";
     640                 :          6 :         opts.core_mask = "0x1";
     641                 :          6 :         opts.shm_id = 0;
     642         [ -  + ]:          6 :         if (spdk_env_init(&opts) < 0) {
     643   [ #  #  #  # ]:          0 :                 fprintf(stderr, "Unable to initialize SPDK env\n");
     644                 :          0 :                 return 1;
     645                 :            :         }
     646                 :            : 
     647         [ -  + ]:          6 :         if (register_controllers() != 0) {
     648                 :          0 :                 return 1;
     649                 :            :         }
     650                 :            : 
     651         [ +  + ]:          6 :         if (TAILQ_EMPTY(&g_controllers)) {
     652         [ -  + ]:          5 :                 printf("No NVMe controller found, %s exiting\n", argv[0]);
     653         [ -  + ]:          5 :                 return g_qemu_ssd_found ? 0 : 1;
     654                 :            :         }
     655                 :            : 
     656                 :          1 :         task_pool = spdk_mempool_create("task_pool", TASK_POOL_NUM,
     657                 :            :                                         sizeof(struct reset_task),
     658                 :            :                                         64, SPDK_ENV_SOCKET_ID_ANY);
     659         [ -  + ]:          1 :         if (!task_pool) {
     660   [ #  #  #  # ]:          0 :                 fprintf(stderr, "Cannot create task pool\n");
     661                 :          0 :                 return 1;
     662                 :            :         }
     663                 :            : 
     664                 :          1 :         g_tsc_rate = spdk_get_ticks_hz();
     665                 :            : 
     666         [ -  + ]:          1 :         if (register_worker() != 0) {
     667                 :          0 :                 return 1;
     668                 :            :         }
     669                 :            : 
     670         [ -  + ]:          1 :         if (associate_workers_with_ns() != 0) {
     671                 :          0 :                 rc = 1;
     672                 :          0 :                 goto cleanup;
     673                 :            :         }
     674                 :            : 
     675         [ -  + ]:          1 :         printf("Initialization complete. Launching workers.\n");
     676                 :            : 
     677         [ +  + ]:          4 :         for (i = 2; i >= 0; i--) {
     678                 :          3 :                 rc = run_nvme_reset_cycle();
     679         [ -  + ]:          3 :                 if (rc != 0) {
     680                 :          0 :                         goto cleanup;
     681                 :            :                 }
     682                 :            :         }
     683                 :            : 
     684                 :          1 : cleanup:
     685                 :          1 :         unregister_controllers();
     686                 :          1 :         unregister_worker();
     687                 :          1 :         free_tasks();
     688                 :            : 
     689         [ -  + ]:          1 :         if (rc != 0) {
     690   [ #  #  #  # ]:          0 :                 fprintf(stderr, "%s: errors occurred\n", argv[0]);
     691                 :            :         }
     692                 :            : 
     693                 :          1 :         return rc;
     694                 :            : }

Generated by: LCOV version 1.14