LCOV - code coverage report
Current view: top level - spdk/test/unit/lib/ftl/ftl_io.c - ftl_io_ut.c (source / functions) Hit Total Coverage
Test: Combined Lines: 101 154 65.6 %
Date: 2024-12-11 14:41:19 Functions: 8 85 9.4 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 8 78 10.3 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2019 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : 
       8                 :            : #include "spdk_internal/cunit.h"
       9                 :            : #include "common/lib/ut_multithread.c"
      10                 :            : 
      11                 :            : #include "ftl/ftl_io.c"
      12                 :            : #include "ftl/utils/ftl_conf.c"
      13                 :            : 
      14         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_io_get_append_location, uint64_t, (struct spdk_bdev_io *bdev_io), 0);
      15                 :          0 : DEFINE_STUB_V(spdk_bdev_close, (struct spdk_bdev_desc *desc));
      16         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_desc_get_bdev, struct spdk_bdev *, (struct spdk_bdev_desc *desc), NULL);
      17         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_optimal_open_zones, uint32_t, (const struct spdk_bdev *b), 1);
      18         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_by_name, struct spdk_bdev *, (const char *bdev_name), NULL);
      19   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_bdev_is_md_separate, bool, (const struct spdk_bdev *bdev), false);
      20   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_bdev_is_zoned, bool, (const struct spdk_bdev *bdev), false);
      21         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_zone_appendv, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
      22                 :            :                 struct iovec *iov, int iovcnt, uint64_t zone_id, uint64_t num_blocks,
      23                 :            :                 spdk_bdev_io_completion_cb cb, void *cb_arg), 0);
      24         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_zone_size, uint64_t, (const struct spdk_bdev *b), 1024);
      25                 :          0 : DEFINE_STUB_V(spdk_bdev_free_io, (struct spdk_bdev_io *bdev_io));
      26         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_buf_align, size_t, (const struct spdk_bdev *bdev), 64);
      27         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_dif_type, enum spdk_dif_type,
      28                 :            :             (const struct spdk_bdev *bdev), 0);
      29         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_name, const char *, (const struct spdk_bdev *bdev), "test");
      30         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_write_unit_size, uint32_t,
      31                 :            :             (const struct spdk_bdev *bdev), 0);
      32   [ #  #  #  # ]:          0 : DEFINE_STUB(spdk_bdev_io_type_supported, bool, (struct spdk_bdev *bdev,
      33                 :            :                 enum spdk_bdev_io_type io_type), true);
      34         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_open_ext, int,
      35                 :            :             (const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
      36                 :            :              void *event_ctx, struct spdk_bdev_desc **desc), 0);
      37         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_read_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
      38                 :            :                 void *buf, uint64_t offset_blocks, uint64_t num_blocks,
      39                 :            :                 spdk_bdev_io_completion_cb cb, void *cb_arg), 0);
      40         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_write_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
      41                 :            :                 void *buf, uint64_t offset_blocks, uint64_t num_blocks, spdk_bdev_io_completion_cb cb,
      42                 :            :                 void *cb_arg), 0);
      43         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_write_blocks_with_md, int, (struct spdk_bdev_desc *desc,
      44                 :            :                 struct spdk_io_channel *ch, void *buf, void *md, uint64_t offset_blocks,
      45                 :            :                 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg), 0);
      46         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_writev_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
      47                 :            :                 struct iovec *iov, int iovcnt, uint64_t offset_blocks, uint64_t num_blocks,
      48                 :            :                 spdk_bdev_io_completion_cb cb, void *cb_arg), 0);
      49         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_num_blocks, uint64_t, (const struct spdk_bdev *bdev), 1024);
      50         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_md_size, uint32_t, (const struct spdk_bdev *bdev), 0);
      51         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_get_block_size, uint32_t, (const struct spdk_bdev *bdev), 4096);
      52                 :          0 : DEFINE_STUB_V(spdk_bdev_module_release_bdev, (struct spdk_bdev *bdev));
      53         [ #  # ]:          0 : DEFINE_STUB(spdk_bdev_write_zeroes_blocks, int,
      54                 :            :             (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
      55                 :            :              uint64_t offset_blocks, uint64_t num_blocks,
      56                 :            :              spdk_bdev_io_completion_cb cb, void *cb_arg), 0);
      57                 :          0 : DEFINE_STUB_V(ftl_reloc, (struct ftl_reloc *reloc));
      58                 :          0 : DEFINE_STUB_V(ftl_reloc_free, (struct ftl_reloc *reloc));
      59                 :          0 : DEFINE_STUB_V(ftl_reloc_halt, (struct ftl_reloc *reloc));
      60         [ #  # ]:          0 : DEFINE_STUB(ftl_reloc_init, struct ftl_reloc *, (struct spdk_ftl_dev *dev), NULL);
      61   [ #  #  #  # ]:          0 : DEFINE_STUB(ftl_reloc_is_halted, bool, (const struct ftl_reloc *reloc), false);
      62                 :          0 : DEFINE_STUB_V(ftl_reloc_resume, (struct ftl_reloc *reloc));
      63                 :          0 : DEFINE_STUB_V(ftl_l2p_unpin, (struct spdk_ftl_dev *dev, uint64_t lba, uint64_t count));
      64         [ #  # ]:          0 : DEFINE_STUB(ftl_p2l_ckpt_acquire, struct ftl_p2l_ckpt *, (struct spdk_ftl_dev *dev), NULL);
      65                 :          0 : DEFINE_STUB_V(ftl_p2l_ckpt_release, (struct spdk_ftl_dev *dev, struct ftl_p2l_ckpt *ckpt));
      66         [ #  # ]:          0 : DEFINE_STUB(ftl_l2p_get, ftl_addr, (struct spdk_ftl_dev *dev, uint64_t lba), 0);
      67                 :          0 : DEFINE_STUB_V(ftl_mempool_put, (struct ftl_mempool *mpool, void *element));
      68                 :          0 : DEFINE_STUB_V(ftl_property_dump_bool, (struct spdk_ftl_dev *dev,
      69                 :            :                                        const struct ftl_property *property,
      70                 :            :                                        struct spdk_json_write_ctx *w));
      71         [ #  # ]:          0 : DEFINE_STUB(ftl_property_decode_bool, int, (struct spdk_ftl_dev *dev, struct ftl_property *property,
      72                 :            :                 const char *value, size_t value_size, void *output, size_t output_size), 0);
      73                 :          0 : DEFINE_STUB_V(ftl_property_set_generic, (struct spdk_ftl_dev *dev, struct ftl_mngt_process *mngt,
      74                 :            :                 const struct ftl_property *property, void *new_value, size_t new_value_size));
      75                 :          0 : DEFINE_STUB_V(ftl_property_register, (struct spdk_ftl_dev *dev,
      76                 :            :                                       const char *name, void *value, size_t size,
      77                 :            :                                       const char *unit, const char *desc,
      78                 :            :                                       ftl_property_dump_fn dump,
      79                 :            :                                       ftl_property_decode_fn decode,
      80                 :            :                                       ftl_property_set_fn set,
      81                 :            :                                       bool verbose_mode));
      82                 :            : 
      83                 :            : #if defined(DEBUG)
      84                 :          0 : DEFINE_STUB_V(ftl_trace_submission, (struct spdk_ftl_dev *dev, const struct ftl_io *io,
      85                 :            :                                      ftl_addr addr, size_t addr_cnt));
      86                 :          0 : DEFINE_STUB_V(ftl_trace_lba_io_init, (struct spdk_ftl_dev *dev, const struct ftl_io *io));
      87                 :          0 : DEFINE_STUB_V(ftl_trace_limits, (struct spdk_ftl_dev *dev, int limit, size_t num_free));
      88         [ #  # ]:          0 : DEFINE_STUB(ftl_trace_alloc_id, uint64_t, (struct spdk_ftl_dev *dev), 0);
      89                 :          0 : DEFINE_STUB_V(ftl_trace_completion, (struct spdk_ftl_dev *dev, const struct ftl_io *io,
      90                 :            :                                      enum ftl_trace_completion type));
      91                 :          0 : DEFINE_STUB_V(ftl_trace_write_band, (struct spdk_ftl_dev *dev, const struct ftl_band *band));
      92                 :            : #endif
      93                 :            : 
      94                 :            : #if defined(FTL_DUMP_STATS)
      95                 :            : DEFINE_STUB_V(ftl_dev_dump_stats, (const struct spdk_ftl_dev *dev));
      96                 :            : #endif
      97                 :            : 
      98                 :            : struct ftl_io_channel_ctx {
      99                 :            :         struct ftl_io_channel *ioch;
     100                 :            : };
     101                 :            : 
     102                 :            : struct ftl_io_channel *
     103                 :         27 : ftl_io_channel_get_ctx(struct spdk_io_channel *ioch)
     104                 :            : {
     105                 :         27 :         struct ftl_io_channel_ctx *ctx = spdk_io_channel_get_ctx(ioch);
     106                 :            : 
     107                 :         27 :         return ctx->ioch;
     108                 :            : }
     109                 :            : 
     110                 :            : struct spdk_io_channel *
     111                 :          0 : spdk_bdev_get_io_channel(struct spdk_bdev_desc *bdev_desc)
     112                 :            : {
     113                 :          0 :         return spdk_get_io_channel(bdev_desc);
     114                 :            : }
     115                 :            : 
     116                 :            : static int
     117                 :          0 : channel_create_cb(void *io_device, void *ctx)
     118                 :            : {
     119                 :          0 :         return 0;
     120                 :            : }
     121                 :            : 
     122                 :            : static void
     123                 :          0 : channel_destroy_cb(void *io_device, void *ctx)
     124                 :            : {
     125                 :          0 : }
     126                 :            : 
     127                 :            : static struct spdk_ftl_dev *
     128                 :          6 : setup_device(uint32_t num_threads, uint32_t xfer_size)
     129                 :            : {
     130                 :            :         struct spdk_ftl_dev *dev;
     131                 :            :         struct ftl_io_channel *ioch;
     132                 :            :         struct ftl_io_channel_ctx *ctx;
     133                 :            : 
     134                 :          6 :         allocate_threads(num_threads);
     135                 :          6 :         set_thread(0);
     136                 :            : 
     137                 :          6 :         dev = calloc(1, sizeof(*dev));
     138         [ -  + ]:          6 :         SPDK_CU_ASSERT_FATAL(dev != NULL);
     139                 :            : 
     140                 :          6 :         dev->core_thread = spdk_get_thread();
     141                 :          6 :         dev->ioch = calloc(1, SPDK_IO_CHANNEL_STRUCT_SIZE + sizeof(struct ftl_io_channel_ctx));
     142         [ -  + ]:          6 :         SPDK_CU_ASSERT_FATAL(dev->ioch != NULL);
     143                 :            : 
     144                 :          6 :         ctx = spdk_io_channel_get_ctx(dev->ioch);
     145                 :          6 :         ctx->ioch = calloc(1, sizeof(*ctx->ioch));
     146                 :            : 
     147                 :          6 :         ioch = ftl_io_channel_get_ctx(dev->ioch);
     148         [ -  + ]:          6 :         SPDK_CU_ASSERT_FATAL(ioch != NULL);
     149                 :            : 
     150                 :          6 :         ioch->cq = spdk_ring_create(0, 1024, 0);
     151                 :            : 
     152                 :          6 :         dev->conf = g_default_conf;
     153                 :          6 :         dev->xfer_size = xfer_size;
     154                 :          6 :         dev->base_bdev_desc = (struct spdk_bdev_desc *)0xdeadbeef;
     155                 :          6 :         dev->nv_cache.bdev_desc = (struct spdk_bdev_desc *)0xdead1234;
     156                 :          6 :         spdk_io_device_register(dev, channel_create_cb, channel_destroy_cb, 0, NULL);
     157                 :          6 :         spdk_io_device_register(dev->base_bdev_desc, channel_create_cb, channel_destroy_cb, 0, NULL);
     158                 :          6 :         spdk_io_device_register(dev->nv_cache.bdev_desc, channel_create_cb, channel_destroy_cb, 0, NULL);
     159                 :            : 
     160                 :          6 :         TAILQ_INIT(&dev->ioch_queue);
     161                 :            : 
     162                 :          6 :         return dev;
     163                 :            : }
     164                 :            : 
     165                 :            : static void
     166                 :          6 : free_device(struct spdk_ftl_dev *dev)
     167                 :            : {
     168                 :            :         struct ftl_io_channel *ioch;
     169                 :            : 
     170                 :          6 :         ioch = ftl_io_channel_get_ctx(dev->ioch);
     171                 :          6 :         spdk_ring_free(ioch->cq);
     172                 :          6 :         free(ioch);
     173                 :            : 
     174                 :          6 :         spdk_io_device_unregister(dev, NULL);
     175                 :          6 :         spdk_io_device_unregister(dev->base_bdev_desc, NULL);
     176                 :          6 :         spdk_io_device_unregister(dev->nv_cache.bdev_desc, NULL);
     177                 :            : 
     178         [ -  + ]:          6 :         while (!TAILQ_EMPTY(&dev->ioch_queue)) {
     179         [ #  # ]:          0 :                 TAILQ_REMOVE(&dev->ioch_queue, TAILQ_FIRST(&dev->ioch_queue), entry);
     180                 :            :         }
     181                 :            : 
     182                 :          6 :         free_threads();
     183                 :            : 
     184                 :          6 :         free(dev->ioch);
     185                 :          6 :         free(dev->sb);
     186                 :          6 :         free(dev);
     187                 :          6 : }
     188                 :            : 
     189                 :            : static void
     190                 :          9 : setup_io(struct ftl_io *io, struct spdk_ftl_dev *dev, spdk_ftl_fn cb, void *ctx)
     191                 :            : {
     192                 :          9 :         io->dev = dev;
     193                 :          9 :         io->user_fn = cb;
     194                 :          9 :         io->cb_ctx = ctx;
     195                 :          9 :         io->flags = 0;
     196                 :          9 :         io->ioch = dev->ioch;
     197                 :          9 : }
     198                 :            : 
     199                 :            : static void
     200                 :          9 : io_complete_cb(void *ctx, int status)
     201                 :            : {
     202                 :          9 :         *(int *)ctx = status;
     203                 :          9 : }
     204                 :            : 
     205                 :            : static void
     206                 :          3 : test_completion(void)
     207                 :            : {
     208                 :            :         struct spdk_ftl_dev *dev;
     209                 :            :         struct ftl_io_channel *ioch;
     210                 :          3 :         struct ftl_io io = { 0 }, *io_ring;
     211                 :          3 :         int req, status = 0;
     212                 :            : 
     213                 :          3 :         dev = setup_device(1, FTL_NUM_LBA_IN_BLOCK);
     214                 :          3 :         ioch = ftl_io_channel_get_ctx(dev->ioch);
     215                 :            : 
     216                 :            :         /* Setup IO and 'send' NUM_REQUESTS subrequests */
     217                 :          3 :         setup_io(&io, dev, io_complete_cb, &status);
     218                 :          3 :         io.status = -EIO;
     219                 :            : 
     220                 :            : #define NUM_REQUESTS 16
     221         [ +  + ]:         51 :         for (req = 0; req < NUM_REQUESTS; ++req) {
     222                 :         48 :                 ftl_io_inc_req(&io);
     223                 :         48 :                 CU_ASSERT_FALSE(ftl_io_done(&io));
     224                 :            :         }
     225                 :            : 
     226                 :          3 :         CU_ASSERT_EQUAL(io.req_cnt, NUM_REQUESTS);
     227                 :            : 
     228                 :            :         /* Complete all but one subrequest, make sure io still not marked as done */
     229         [ +  + ]:         48 :         for (req = 0; req < (NUM_REQUESTS - 1); ++req) {
     230                 :         45 :                 ftl_io_dec_req(&io);
     231                 :         45 :                 CU_ASSERT_FALSE(ftl_io_done(&io));
     232                 :            :         }
     233                 :            : 
     234                 :          3 :         CU_ASSERT_EQUAL(io.req_cnt, 1);
     235                 :            : 
     236                 :            :         /* Complete last subrequest, make sure it appears on the completion queue */
     237                 :          3 :         ftl_io_dec_req(&io);
     238                 :          3 :         CU_ASSERT_TRUE(ftl_io_done(&io));
     239                 :            : 
     240                 :          3 :         ftl_io_complete(&io);
     241                 :            : 
     242                 :          3 :         CU_ASSERT_EQUAL(spdk_ring_count(ioch->cq), 1);
     243                 :            : 
     244                 :            :         /* Dequeue and check if the completion callback changes the status, this is usually done via poller */
     245                 :          3 :         spdk_ring_dequeue(ioch->cq, (void **)&io_ring, 1);
     246                 :          3 :         io_ring->user_fn(io_ring->cb_ctx, io_ring->status);
     247                 :            : 
     248                 :          3 :         CU_ASSERT_EQUAL(status, -EIO);
     249                 :            : 
     250                 :          3 :         free_device(dev);
     251                 :          3 : }
     252                 :            : 
     253                 :            : static void
     254                 :          3 : test_multiple_ios(void)
     255                 :            : {
     256                 :            :         struct spdk_ftl_dev *dev;
     257                 :            :         struct ftl_io_channel *ioch;
     258                 :          3 :         struct ftl_io io[2] = { 0 }, *io_ring[2];
     259                 :          3 :         int status = -1;
     260                 :            : 
     261                 :          3 :         dev = setup_device(1, FTL_NUM_LBA_IN_BLOCK);
     262                 :          3 :         ioch = ftl_io_channel_get_ctx(dev->ioch);
     263                 :            : 
     264                 :            :         /* Send t2o IOs and check if both are in the completion queue */
     265                 :          3 :         setup_io(&io[0], dev, io_complete_cb, &status);
     266                 :          3 :         CU_ASSERT_EQUAL(spdk_ring_count(ioch->cq), 0);
     267                 :            : 
     268                 :          3 :         ftl_io_complete(io);
     269                 :          3 :         CU_ASSERT_EQUAL(spdk_ring_count(ioch->cq), 1);
     270                 :            : 
     271                 :          3 :         setup_io(&io[1], dev, io_complete_cb, &status);
     272                 :            : 
     273                 :          3 :         ftl_io_complete(&io[1]);
     274                 :          3 :         CU_ASSERT_EQUAL(spdk_ring_count(ioch->cq), 2);
     275                 :            : 
     276                 :            :         /* Dequeue and check if the completion callback changes the status, this is usually done via poller */
     277                 :          3 :         spdk_ring_dequeue(ioch->cq, (void **)io_ring, 2);
     278                 :          3 :         status = -1;
     279                 :          3 :         io_ring[0]->user_fn(io_ring[0]->cb_ctx, io_ring[0]->status);
     280                 :          3 :         CU_ASSERT_EQUAL(status, 0);
     281                 :          3 :         status = -1;
     282                 :          3 :         io_ring[1]->user_fn(io_ring[1]->cb_ctx, io_ring[1]->status);
     283                 :          3 :         CU_ASSERT_EQUAL(status, 0);
     284                 :            : 
     285                 :          3 :         free_device(dev);
     286                 :          3 : }
     287                 :            : 
     288                 :            : int
     289                 :          3 : main(int argc, char **argv)
     290                 :            : {
     291                 :            :         CU_pSuite suite;
     292                 :            :         unsigned int num_failures;
     293                 :            : 
     294                 :          3 :         CU_initialize_registry();
     295                 :            : 
     296                 :          3 :         suite = CU_add_suite("ftl_io_suite", NULL, NULL);
     297                 :            : 
     298                 :            : 
     299                 :          3 :         CU_ADD_TEST(suite, test_completion);
     300                 :          3 :         CU_ADD_TEST(suite, test_multiple_ios);
     301                 :            : 
     302                 :          3 :         num_failures = spdk_ut_run_tests(argc, argv, NULL);
     303                 :          3 :         CU_cleanup_registry();
     304                 :            : 
     305                 :          3 :         return num_failures;
     306                 :            : }

Generated by: LCOV version 1.15