LCOV - code coverage report
Current view: top level - spdk/test/unit/lib/bdev/raid - common.c (source / functions) Hit Total Coverage
Test: Combined Lines: 110 112 98.2 %
Date: 2024-07-13 02:54:37 Functions: 13 13 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 33 48 68.8 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2022 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk_internal/cunit.h"
       7                 :            : #include "spdk/stdinc.h"
       8                 :            : #include "bdev/raid/bdev_raid.h"
       9                 :            : 
      10                 :            : struct spdk_bdev_desc {
      11                 :            :         struct spdk_bdev *bdev;
      12                 :            : };
      13                 :            : 
      14                 :            : struct raid_params {
      15                 :            :         uint8_t num_base_bdevs;
      16                 :            :         uint64_t base_bdev_blockcnt;
      17                 :            :         uint32_t base_bdev_blocklen;
      18                 :            :         uint32_t strip_size;
      19                 :            :         uint32_t md_len;
      20                 :            : };
      21                 :            : 
      22                 :            : struct raid_params *g_params;
      23                 :            : size_t g_params_count;
      24                 :            : size_t g_params_size;
      25                 :            : 
      26                 :            : #define ARRAY_FOR_EACH(a, e) \
      27                 :            :         for (e = a; e < a + SPDK_COUNTOF(a); e++)
      28                 :            : 
      29                 :            : #define RAID_PARAMS_FOR_EACH(p) \
      30                 :            :         for (p = g_params; p < g_params + g_params_count; p++)
      31                 :            : 
      32                 :            : struct spdk_bdev *
      33                 :      20644 : spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc)
      34                 :            : {
      35                 :      20644 :         return desc->bdev;
      36                 :            : }
      37                 :            : 
      38                 :            : static int
      39                 :         16 : raid_test_params_alloc(size_t count)
      40                 :            : {
      41         [ -  + ]:         16 :         assert(g_params == NULL);
      42                 :            : 
      43                 :         16 :         g_params_size = count;
      44                 :         16 :         g_params_count = 0;
      45                 :         16 :         g_params = calloc(count, sizeof(*g_params));
      46                 :            : 
      47         [ +  - ]:         16 :         return g_params ? 0 : -ENOMEM;
      48                 :            : }
      49                 :            : 
      50                 :            : static void
      51                 :         16 : raid_test_params_free(void)
      52                 :            : {
      53                 :         16 :         g_params_count = 0;
      54                 :         16 :         g_params_size = 0;
      55                 :         16 :         free(g_params);
      56                 :         16 : }
      57                 :            : 
      58                 :            : static void
      59                 :        534 : raid_test_params_add(struct raid_params *params)
      60                 :            : {
      61         [ -  + ]:        534 :         assert(g_params_count < g_params_size);
      62                 :            : 
      63   [ -  +  -  + ]:        534 :         memcpy(g_params + g_params_count, params, sizeof(*params));
      64                 :        534 :         g_params_count++;
      65                 :        534 : }
      66                 :            : 
      67                 :            : static struct raid_bdev *
      68                 :       4434 : raid_test_create_raid_bdev(struct raid_params *params, struct raid_bdev_module *module)
      69                 :            : {
      70                 :            :         struct raid_bdev *raid_bdev;
      71                 :            :         struct raid_base_bdev_info *base_info;
      72                 :            : 
      73                 :       4434 :         raid_bdev = calloc(1, sizeof(*raid_bdev));
      74         [ -  + ]:       4434 :         SPDK_CU_ASSERT_FATAL(raid_bdev != NULL);
      75                 :            : 
      76                 :       4434 :         raid_bdev->module = module;
      77                 :       4434 :         raid_bdev->level = module->level;
      78                 :       4434 :         raid_bdev->num_base_bdevs = params->num_base_bdevs;
      79                 :            : 
      80   [ +  +  +  - ]:       4434 :         switch (raid_bdev->module->base_bdevs_constraint.type) {
      81                 :       2112 :         case CONSTRAINT_MAX_BASE_BDEVS_REMOVED:
      82                 :       4224 :                 raid_bdev->min_base_bdevs_operational = raid_bdev->num_base_bdevs -
      83                 :       2112 :                                                         raid_bdev->module->base_bdevs_constraint.value;
      84                 :       2112 :                 break;
      85                 :        144 :         case CONSTRAINT_MIN_BASE_BDEVS_OPERATIONAL:
      86                 :        144 :                 raid_bdev->min_base_bdevs_operational = raid_bdev->module->base_bdevs_constraint.value;
      87                 :        144 :                 break;
      88                 :       2178 :         case CONSTRAINT_UNSET:
      89                 :       2178 :                 raid_bdev->min_base_bdevs_operational = raid_bdev->num_base_bdevs;
      90                 :       2178 :                 break;
      91                 :          0 :         default:
      92                 :          0 :                 CU_FAIL_FATAL("unsupported raid constraint type");
      93                 :            :         };
      94                 :            : 
      95                 :       4434 :         raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs,
      96                 :            :                                            sizeof(struct raid_base_bdev_info));
      97         [ -  + ]:       4434 :         SPDK_CU_ASSERT_FATAL(raid_bdev->base_bdev_info != NULL);
      98                 :            : 
      99         [ +  + ]:      22218 :         RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
     100                 :            :                 struct spdk_bdev *bdev;
     101                 :            :                 struct spdk_bdev_desc *desc;
     102                 :            : 
     103                 :      17784 :                 bdev = calloc(1, sizeof(*bdev));
     104         [ -  + ]:      17784 :                 SPDK_CU_ASSERT_FATAL(bdev != NULL);
     105                 :      17784 :                 bdev->blockcnt = params->base_bdev_blockcnt;
     106                 :      17784 :                 bdev->blocklen = params->base_bdev_blocklen;
     107                 :            : 
     108                 :      17784 :                 desc = calloc(1, sizeof(*desc));
     109         [ -  + ]:      17784 :                 SPDK_CU_ASSERT_FATAL(desc != NULL);
     110                 :      17784 :                 desc->bdev = bdev;
     111                 :            : 
     112                 :      17784 :                 base_info->desc = desc;
     113                 :      17784 :                 base_info->data_offset = 0;
     114                 :      17784 :                 base_info->data_size = bdev->blockcnt;
     115                 :            :         }
     116                 :            : 
     117                 :       4434 :         raid_bdev->strip_size = params->strip_size;
     118                 :       4434 :         raid_bdev->strip_size_kb = params->strip_size * params->base_bdev_blocklen / 1024;
     119                 :       4434 :         raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size);
     120                 :       4434 :         raid_bdev->blocklen_shift = spdk_u32log2(params->base_bdev_blocklen);
     121                 :       4434 :         raid_bdev->bdev.blocklen = params->base_bdev_blocklen;
     122                 :       4434 :         raid_bdev->bdev.md_len = params->md_len;
     123                 :            : 
     124                 :       4434 :         return raid_bdev;
     125                 :            : }
     126                 :            : 
     127                 :            : static void
     128                 :       4434 : raid_test_delete_raid_bdev(struct raid_bdev *raid_bdev)
     129                 :            : {
     130                 :            :         struct raid_base_bdev_info *base_info;
     131                 :            : 
     132         [ +  + ]:      22218 :         RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
     133                 :      17784 :                 free(base_info->desc->bdev);
     134                 :      17784 :                 free(base_info->desc);
     135                 :            :         }
     136                 :       4434 :         free(raid_bdev->base_bdev_info);
     137                 :       4434 :         free(raid_bdev);
     138                 :       4434 : }
     139                 :            : 
     140                 :            : struct raid_bdev_io_channel {
     141                 :            :         struct spdk_io_channel **_base_channels;
     142                 :            :         struct spdk_io_channel *_module_channel;
     143                 :            : };
     144                 :            : 
     145                 :            : struct spdk_io_channel *
     146                 :     177592 : raid_bdev_channel_get_base_channel(struct raid_bdev_io_channel *raid_ch, uint8_t idx)
     147                 :            : {
     148                 :     177592 :         return raid_ch->_base_channels[idx];
     149                 :            : }
     150                 :            : 
     151                 :            : void *
     152                 :      38472 : raid_bdev_channel_get_module_ctx(struct raid_bdev_io_channel *raid_ch)
     153                 :            : {
     154                 :      38472 :         return spdk_io_channel_get_ctx(raid_ch->_module_channel);
     155                 :            : }
     156                 :            : 
     157                 :            : static struct raid_bdev_io_channel *
     158                 :       3900 : raid_test_create_io_channel(struct raid_bdev *raid_bdev)
     159                 :            : {
     160                 :            :         struct raid_bdev_io_channel *raid_ch;
     161                 :            :         uint8_t i;
     162                 :            : 
     163                 :       3900 :         raid_ch = calloc(1, sizeof(*raid_ch));
     164         [ -  + ]:       3900 :         SPDK_CU_ASSERT_FATAL(raid_ch != NULL);
     165                 :            : 
     166                 :       3900 :         raid_ch->_base_channels = calloc(raid_bdev->num_base_bdevs, sizeof(struct spdk_io_channel *));
     167         [ -  + ]:       3900 :         SPDK_CU_ASSERT_FATAL(raid_ch->_base_channels != NULL);
     168                 :            : 
     169         [ +  + ]:      19656 :         for (i = 0; i < raid_bdev->num_base_bdevs; i++) {
     170                 :      15756 :                 raid_ch->_base_channels[i] = (void *)1;
     171                 :            :         }
     172                 :            : 
     173         [ +  + ]:       3900 :         if (raid_bdev->module->get_io_channel) {
     174                 :       1920 :                 raid_ch->_module_channel = raid_bdev->module->get_io_channel(raid_bdev);
     175         [ -  + ]:       1920 :                 SPDK_CU_ASSERT_FATAL(raid_ch->_module_channel != NULL);
     176                 :            :         }
     177                 :            : 
     178                 :       3900 :         return raid_ch;
     179                 :            : }
     180                 :            : 
     181                 :            : static void
     182                 :       3900 : raid_test_destroy_io_channel(struct raid_bdev_io_channel *raid_ch)
     183                 :            : {
     184                 :       3900 :         free(raid_ch->_base_channels);
     185                 :            : 
     186         [ +  + ]:       3900 :         if (raid_ch->_module_channel) {
     187                 :       1920 :                 spdk_put_io_channel(raid_ch->_module_channel);
     188                 :       1920 :                 poll_threads();
     189                 :            :         }
     190                 :            : 
     191                 :       3900 :         free(raid_ch);
     192                 :       3900 : }
     193                 :            : 
     194                 :            : static void
     195                 :      51920 : raid_test_bdev_io_init(struct raid_bdev_io *raid_io, struct raid_bdev *raid_bdev,
     196                 :            :                        struct raid_bdev_io_channel *raid_ch,
     197                 :            :                        enum spdk_bdev_io_type type, uint64_t offset_blocks,
     198                 :            :                        uint64_t num_blocks, struct iovec *iovs, int iovcnt, void *md_buf)
     199                 :            : {
     200         [ -  + ]:      51920 :         memset(raid_io, 0, sizeof(*raid_io));
     201                 :            : 
     202                 :      51920 :         raid_io->raid_bdev = raid_bdev;
     203                 :      51920 :         raid_io->raid_ch = raid_ch;
     204                 :            : 
     205                 :      51920 :         raid_io->type = type;
     206                 :      51920 :         raid_io->offset_blocks = offset_blocks;
     207                 :      51920 :         raid_io->num_blocks = num_blocks;
     208                 :      51920 :         raid_io->iovs = iovs;
     209                 :      51920 :         raid_io->iovcnt = iovcnt;
     210                 :      51920 :         raid_io->md_buf = md_buf;
     211                 :            : 
     212                 :      51920 :         raid_io->base_bdev_io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
     213                 :      51920 : }
     214                 :            : 
     215                 :            : /* needs to be implemented in module unit test files */
     216                 :            : static void raid_test_bdev_io_complete(struct raid_bdev_io *raid_io,
     217                 :            :                                        enum spdk_bdev_io_status status);
     218                 :            : 
     219                 :            : void
     220                 :      46988 : raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status)
     221                 :            : {
     222         [ +  + ]:      46988 :         if (raid_io->completion_cb != NULL) {
     223                 :       2664 :                 raid_io->completion_cb(raid_io, status);
     224                 :            :         } else {
     225                 :      44324 :                 raid_test_bdev_io_complete(raid_io, status);
     226                 :            :         }
     227                 :      46988 : }
     228                 :            : 
     229                 :            : bool
     230                 :      92700 : raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed,
     231                 :            :                            enum spdk_bdev_io_status status)
     232                 :            : {
     233         [ -  + ]:      92700 :         SPDK_CU_ASSERT_FATAL(raid_io->base_bdev_io_remaining >= completed);
     234                 :      92700 :         raid_io->base_bdev_io_remaining -= completed;
     235                 :            : 
     236         [ +  + ]:      92700 :         if (status != SPDK_BDEV_IO_STATUS_SUCCESS) {
     237                 :      14272 :                 raid_io->base_bdev_io_status = status;
     238                 :            :         }
     239                 :            : 
     240         [ +  + ]:      92700 :         if (raid_io->base_bdev_io_remaining == 0) {
     241                 :      23340 :                 raid_bdev_io_complete(raid_io, raid_io->base_bdev_io_status);
     242                 :      23340 :                 return true;
     243                 :            :         } else {
     244                 :      69360 :                 return false;
     245                 :            :         }
     246                 :            : }

Generated by: LCOV version 1.14