diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/spdk/test/unit/lib/ftl | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/spdk/test/unit/lib/ftl')
20 files changed, 2933 insertions, 0 deletions
diff --git a/src/spdk/test/unit/lib/ftl/Makefile b/src/spdk/test/unit/lib/ftl/Makefile new file mode 100644 index 000000000..57745c450 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/Makefile @@ -0,0 +1,44 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../..) +include $(SPDK_ROOT_DIR)/mk/spdk.common.mk + +DIRS-y = ftl_ppa ftl_band.c ftl_reloc.c ftl_wptr ftl_md ftl_io.c + +.PHONY: all clean $(DIRS-y) + +all: $(DIRS-y) +clean: $(DIRS-y) + +include $(SPDK_ROOT_DIR)/mk/spdk.subdirs.mk diff --git a/src/spdk/test/unit/lib/ftl/common/utils.c b/src/spdk/test/unit/lib/ftl/common/utils.c new file mode 100644 index 000000000..dda828df8 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/common/utils.c @@ -0,0 +1,173 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk_internal/thread.h" + +#include "spdk/ftl.h" +#include "ftl/ftl_core.h" + +struct base_bdev_geometry { + size_t write_unit_size; + size_t zone_size; + size_t optimal_open_zones; + size_t blockcnt; +}; + +extern struct base_bdev_geometry g_geo; + +struct spdk_ftl_dev *test_init_ftl_dev(const struct base_bdev_geometry *geo); +struct ftl_band *test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id, size_t zone_size); +void test_free_ftl_dev(struct spdk_ftl_dev *dev); +void test_free_ftl_band(struct ftl_band *band); +uint64_t test_offset_from_addr(struct ftl_addr addr, struct ftl_band *band); + +DEFINE_STUB(spdk_bdev_desc_get_bdev, struct spdk_bdev *, (struct spdk_bdev_desc *desc), NULL); + +uint64_t +spdk_bdev_get_zone_size(const struct spdk_bdev *bdev) +{ + return g_geo.zone_size; +} + +uint32_t +spdk_bdev_get_optimal_open_zones(const struct spdk_bdev *bdev) +{ + return g_geo.optimal_open_zones; +} + +struct spdk_ftl_dev * +test_init_ftl_dev(const struct base_bdev_geometry *geo) +{ + struct spdk_ftl_dev *dev; + + dev = calloc(1, sizeof(*dev)); + SPDK_CU_ASSERT_FATAL(dev != NULL); + + dev->xfer_size = geo->write_unit_size; + dev->core_thread = spdk_thread_create("unit_test_thread", NULL); + spdk_set_thread(dev->core_thread); + dev->ioch = calloc(1, sizeof(*dev->ioch) + + sizeof(struct ftl_io_channel *)); + dev->num_bands = geo->blockcnt / (geo->zone_size * geo->optimal_open_zones); + dev->bands = calloc(dev->num_bands, sizeof(*dev->bands)); + SPDK_CU_ASSERT_FATAL(dev->bands != NULL); + + dev->lba_pool = spdk_mempool_create("ftl_ut", 2, 0x18000, + SPDK_MEMPOOL_DEFAULT_CACHE_SIZE, + SPDK_ENV_SOCKET_ID_ANY); + SPDK_CU_ASSERT_FATAL(dev->lba_pool != NULL); + + LIST_INIT(&dev->free_bands); + LIST_INIT(&dev->shut_bands); + + return dev; +} + +struct ftl_band * +test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id, size_t zone_size) +{ + struct ftl_band *band; + struct ftl_zone *zone; + + SPDK_CU_ASSERT_FATAL(dev != NULL); + SPDK_CU_ASSERT_FATAL(id < dev->num_bands); + + band = &dev->bands[id]; + band->dev = dev; + band->id = id; + + band->state = FTL_BAND_STATE_CLOSED; + LIST_INSERT_HEAD(&dev->shut_bands, band, list_entry); + CIRCLEQ_INIT(&band->zones); + + band->lba_map.vld = spdk_bit_array_create(ftl_get_num_blocks_in_band(dev)); + SPDK_CU_ASSERT_FATAL(band->lba_map.vld != NULL); + + band->zone_buf = calloc(ftl_get_num_punits(dev), sizeof(*band->zone_buf)); + SPDK_CU_ASSERT_FATAL(band->zone_buf != NULL); + + band->reloc_bitmap = spdk_bit_array_create(ftl_get_num_bands(dev)); + SPDK_CU_ASSERT_FATAL(band->reloc_bitmap != NULL); + + for (size_t i = 0; i < ftl_get_num_punits(dev); ++i) { + zone = &band->zone_buf[i]; + zone->info.state = SPDK_BDEV_ZONE_STATE_FULL; + zone->info.zone_id = zone_size * (id * ftl_get_num_punits(dev) + i); + CIRCLEQ_INSERT_TAIL(&band->zones, zone, circleq); + band->num_zones++; + } + + pthread_spin_init(&band->lba_map.lock, PTHREAD_PROCESS_PRIVATE); + return band; +} + +void +test_free_ftl_dev(struct spdk_ftl_dev *dev) +{ + struct spdk_thread *thread; + + SPDK_CU_ASSERT_FATAL(dev != NULL); + free(dev->ioch); + + thread = dev->core_thread; + + spdk_set_thread(thread); + spdk_thread_exit(thread); + while (!spdk_thread_is_exited(thread)) { + spdk_thread_poll(thread, 0, 0); + } + spdk_thread_destroy(thread); + spdk_mempool_free(dev->lba_pool); + free(dev->bands); + free(dev); +} + +void +test_free_ftl_band(struct ftl_band *band) +{ + SPDK_CU_ASSERT_FATAL(band != NULL); + spdk_bit_array_free(&band->lba_map.vld); + spdk_bit_array_free(&band->reloc_bitmap); + free(band->zone_buf); + spdk_dma_free(band->lba_map.dma_buf); +} + +uint64_t +test_offset_from_addr(struct ftl_addr addr, struct ftl_band *band) +{ + struct spdk_ftl_dev *dev = band->dev; + + CU_ASSERT_EQUAL(ftl_addr_get_band(dev, addr), band->id); + + return addr.offset - band->id * ftl_get_num_blocks_in_band(dev); +} diff --git a/src/spdk/test/unit/lib/ftl/ftl_band.c/.gitignore b/src/spdk/test/unit/lib/ftl/ftl_band.c/.gitignore new file mode 100644 index 000000000..aa8820632 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_band.c/.gitignore @@ -0,0 +1 @@ +ftl_band_ut diff --git a/src/spdk/test/unit/lib/ftl/ftl_band.c/Makefile b/src/spdk/test/unit/lib/ftl/ftl_band.c/Makefile new file mode 100644 index 000000000..4d4195105 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_band.c/Makefile @@ -0,0 +1,38 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +TEST_FILE = ftl_band_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/ftl/ftl_band.c/ftl_band_ut.c b/src/spdk/test/unit/lib/ftl/ftl_band.c/ftl_band_ut.c new file mode 100644 index 000000000..d4f299e5b --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_band.c/ftl_band_ut.c @@ -0,0 +1,307 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" +#include "common/lib/test_env.c" + +#include "ftl/ftl_core.c" +#include "ftl/ftl_band.c" +#include "../common/utils.c" + +#define TEST_BAND_IDX 68 +#define TEST_LBA 0x68676564 + +struct base_bdev_geometry g_geo = { + .write_unit_size = 16, + .optimal_open_zones = 9, + .zone_size = 100, + .blockcnt = 1500 * 100 * 8, +}; + +static struct spdk_ftl_dev *g_dev; +static struct ftl_band *g_band; + +static void +setup_band(void) +{ + int rc; + + g_dev = test_init_ftl_dev(&g_geo); + g_band = test_init_ftl_band(g_dev, TEST_BAND_IDX, g_geo.zone_size); + rc = ftl_band_alloc_lba_map(g_band); + CU_ASSERT_EQUAL_FATAL(rc, 0); +} + +static void +cleanup_band(void) +{ + test_free_ftl_band(g_band); + test_free_ftl_dev(g_dev); +} + +static struct ftl_addr +addr_from_punit(uint64_t punit) +{ + struct ftl_addr addr = {}; + + addr.offset = punit * g_geo.zone_size; + return addr; +} + +static void +test_band_block_offset_from_addr_base(void) +{ + struct ftl_addr addr; + uint64_t offset, i, flat_lun = 0; + + setup_band(); + for (i = 0; i < ftl_get_num_punits(g_dev); ++i) { + addr = addr_from_punit(i); + addr.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + + offset = ftl_band_block_offset_from_addr(g_band, addr); + CU_ASSERT_EQUAL(offset, flat_lun * ftl_get_num_blocks_in_zone(g_dev)); + flat_lun++; + } + cleanup_band(); +} + +static void +test_band_block_offset_from_addr_offset(void) +{ + struct ftl_addr addr; + uint64_t offset, expect, i, j; + + setup_band(); + for (i = 0; i < ftl_get_num_punits(g_dev); ++i) { + for (j = 0; j < g_geo.zone_size; ++j) { + addr = addr_from_punit(i); + addr.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev) + j; + + offset = ftl_band_block_offset_from_addr(g_band, addr); + + expect = test_offset_from_addr(addr, g_band); + CU_ASSERT_EQUAL(offset, expect); + } + } + cleanup_band(); +} + +static void +test_band_addr_from_block_offset(void) +{ + struct ftl_addr addr, expect; + uint64_t offset, i, j; + + setup_band(); + for (i = 0; i < ftl_get_num_punits(g_dev); ++i) { + for (j = 0; j < g_geo.zone_size; ++j) { + expect = addr_from_punit(i); + expect.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev) + j; + + offset = ftl_band_block_offset_from_addr(g_band, expect); + addr = ftl_band_addr_from_block_offset(g_band, offset); + + CU_ASSERT_EQUAL(addr.offset, expect.offset); + } + } + cleanup_band(); +} + +static void +test_band_set_addr(void) +{ + struct ftl_lba_map *lba_map; + struct ftl_addr addr; + uint64_t offset = 0; + + setup_band(); + lba_map = &g_band->lba_map; + addr = addr_from_punit(0); + addr.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + + CU_ASSERT_EQUAL(lba_map->num_vld, 0); + + offset = test_offset_from_addr(addr, g_band); + + ftl_band_set_addr(g_band, TEST_LBA, addr); + CU_ASSERT_EQUAL(lba_map->num_vld, 1); + CU_ASSERT_EQUAL(lba_map->map[offset], TEST_LBA); + CU_ASSERT_TRUE(spdk_bit_array_get(lba_map->vld, offset)); + + addr.offset += g_geo.zone_size; + offset = test_offset_from_addr(addr, g_band); + ftl_band_set_addr(g_band, TEST_LBA + 1, addr); + CU_ASSERT_EQUAL(lba_map->num_vld, 2); + CU_ASSERT_EQUAL(lba_map->map[offset], TEST_LBA + 1); + CU_ASSERT_TRUE(spdk_bit_array_get(lba_map->vld, offset)); + addr.offset -= g_geo.zone_size; + offset = test_offset_from_addr(addr, g_band); + CU_ASSERT_TRUE(spdk_bit_array_get(lba_map->vld, offset)); + cleanup_band(); +} + +static void +test_invalidate_addr(void) +{ + struct ftl_lba_map *lba_map; + struct ftl_addr addr; + uint64_t offset[2]; + + setup_band(); + lba_map = &g_band->lba_map; + addr = addr_from_punit(0); + addr.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + offset[0] = test_offset_from_addr(addr, g_band); + + ftl_band_set_addr(g_band, TEST_LBA, addr); + CU_ASSERT_EQUAL(lba_map->num_vld, 1); + CU_ASSERT_TRUE(spdk_bit_array_get(lba_map->vld, offset[0])); + ftl_invalidate_addr(g_band->dev, addr); + CU_ASSERT_EQUAL(lba_map->num_vld, 0); + CU_ASSERT_FALSE(spdk_bit_array_get(lba_map->vld, offset[0])); + + offset[0] = test_offset_from_addr(addr, g_band); + ftl_band_set_addr(g_band, TEST_LBA, addr); + addr.offset += g_geo.zone_size; + offset[1] = test_offset_from_addr(addr, g_band); + ftl_band_set_addr(g_band, TEST_LBA + 1, addr); + CU_ASSERT_EQUAL(lba_map->num_vld, 2); + CU_ASSERT_TRUE(spdk_bit_array_get(lba_map->vld, offset[0])); + CU_ASSERT_TRUE(spdk_bit_array_get(lba_map->vld, offset[1])); + ftl_invalidate_addr(g_band->dev, addr); + CU_ASSERT_EQUAL(lba_map->num_vld, 1); + CU_ASSERT_TRUE(spdk_bit_array_get(lba_map->vld, offset[0])); + CU_ASSERT_FALSE(spdk_bit_array_get(lba_map->vld, offset[1])); + cleanup_band(); +} + +static void +test_next_xfer_addr(void) +{ + struct ftl_addr addr, result, expect; + + setup_band(); + /* Verify simple one block incremention */ + addr = addr_from_punit(0); + addr.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + expect = addr; + expect.offset += 1; + + result = ftl_band_next_xfer_addr(g_band, addr, 1); + CU_ASSERT_EQUAL(result.offset, expect.offset); + + /* Verify jumping between zones */ + expect = addr_from_punit(1); + expect.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + result = ftl_band_next_xfer_addr(g_band, addr, g_dev->xfer_size); + CU_ASSERT_EQUAL(result.offset, expect.offset); + + /* Verify jumping works with unaligned offsets */ + expect = addr_from_punit(1); + expect.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev) + 3; + result = ftl_band_next_xfer_addr(g_band, addr, g_dev->xfer_size + 3); + CU_ASSERT_EQUAL(result.offset, expect.offset); + + /* Verify jumping from last zone to the first one */ + expect = addr_from_punit(0); + expect.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev) + g_dev->xfer_size; + addr = addr_from_punit(ftl_get_num_punits(g_dev) - 1); + addr.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + result = ftl_band_next_xfer_addr(g_band, addr, g_dev->xfer_size); + CU_ASSERT_EQUAL(result.offset, expect.offset); + + /* Verify jumping from last zone to the first one with unaligned offset */ + expect = addr_from_punit(0); + expect.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + expect.offset += g_dev->xfer_size + 2; + addr = addr_from_punit(ftl_get_num_punits(g_dev) - 1); + addr.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + result = ftl_band_next_xfer_addr(g_band, addr, g_dev->xfer_size + 2); + CU_ASSERT_EQUAL(result.offset, expect.offset); + + /* Verify large offset spanning across the whole band multiple times */ + expect = addr_from_punit(0); + expect.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + expect.offset += g_dev->xfer_size * 5 + 4; + addr = addr_from_punit(0); + addr.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + addr.offset += g_dev->xfer_size * 2 + 1; + result = ftl_band_next_xfer_addr(g_band, addr, 3 * g_dev->xfer_size * + ftl_get_num_punits(g_dev) + 3); + CU_ASSERT_EQUAL(result.offset, expect.offset); + + /* Remove one zone and verify it's skipped properly */ + g_band->zone_buf[1].info.state = SPDK_BDEV_ZONE_STATE_OFFLINE; + CIRCLEQ_REMOVE(&g_band->zones, &g_band->zone_buf[1], circleq); + g_band->num_zones--; + expect = addr_from_punit(2); + expect.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + expect.offset += g_dev->xfer_size * 5 + 4; + addr = addr_from_punit(0); + addr.offset += TEST_BAND_IDX * ftl_get_num_blocks_in_band(g_dev); + addr.offset += g_dev->xfer_size * 2 + 1; + result = ftl_band_next_xfer_addr(g_band, addr, 3 * g_dev->xfer_size * + (ftl_get_num_punits(g_dev) - 1) + g_dev->xfer_size + 3); + CU_ASSERT_EQUAL(result.offset, expect.offset); + cleanup_band(); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("ftl_band_suite", NULL, NULL); + + + CU_ADD_TEST(suite, test_band_block_offset_from_addr_base); + CU_ADD_TEST(suite, test_band_block_offset_from_addr_offset); + CU_ADD_TEST(suite, test_band_addr_from_block_offset); + CU_ADD_TEST(suite, test_band_set_addr); + CU_ADD_TEST(suite, test_invalidate_addr); + CU_ADD_TEST(suite, test_next_xfer_addr); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + + return num_failures; +} diff --git a/src/spdk/test/unit/lib/ftl/ftl_io.c/.gitignore b/src/spdk/test/unit/lib/ftl/ftl_io.c/.gitignore new file mode 100644 index 000000000..c5e09253e --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_io.c/.gitignore @@ -0,0 +1 @@ +ftl_io_ut diff --git a/src/spdk/test/unit/lib/ftl/ftl_io.c/Makefile b/src/spdk/test/unit/lib/ftl/ftl_io.c/Makefile new file mode 100644 index 000000000..e06a186b1 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_io.c/Makefile @@ -0,0 +1,38 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +TEST_FILE = ftl_io_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/ftl/ftl_io.c/ftl_io_ut.c b/src/spdk/test/unit/lib/ftl/ftl_io.c/ftl_io_ut.c new file mode 100644 index 000000000..81288de60 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_io.c/ftl_io_ut.c @@ -0,0 +1,1068 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" +#include "common/lib/ut_multithread.c" + +#include "ftl/ftl_io.c" +#include "ftl/ftl_init.c" +#include "ftl/ftl_core.c" +#include "ftl/ftl_band.c" + +DEFINE_STUB(spdk_bdev_io_get_append_location, uint64_t, (struct spdk_bdev_io *bdev_io), 0); +DEFINE_STUB(spdk_bdev_desc_get_bdev, struct spdk_bdev *, (struct spdk_bdev_desc *desc), NULL); +DEFINE_STUB(spdk_bdev_get_optimal_open_zones, uint32_t, (const struct spdk_bdev *b), 1); +DEFINE_STUB(spdk_bdev_zone_appendv, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, + struct iovec *iov, int iovcnt, uint64_t zone_id, uint64_t num_blocks, + spdk_bdev_io_completion_cb cb, void *cb_arg), 0); +DEFINE_STUB(spdk_bdev_get_zone_size, uint64_t, (const struct spdk_bdev *b), 1024); +DEFINE_STUB(spdk_bdev_zone_management, int, (struct spdk_bdev_desc *desc, + struct spdk_io_channel *ch, uint64_t zone_id, enum spdk_bdev_zone_action action, + spdk_bdev_io_completion_cb cb, void *cb_arg), 0); +DEFINE_STUB_V(spdk_bdev_free_io, (struct spdk_bdev_io *bdev_io)); +DEFINE_STUB(spdk_bdev_read_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, + void *buf, uint64_t offset_blocks, uint64_t num_blocks, + spdk_bdev_io_completion_cb cb, void *cb_arg), 0); +DEFINE_STUB(spdk_bdev_write_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, + void *buf, uint64_t offset_blocks, uint64_t num_blocks, spdk_bdev_io_completion_cb cb, + void *cb_arg), 0); +DEFINE_STUB(spdk_bdev_write_blocks_with_md, int, (struct spdk_bdev_desc *desc, + struct spdk_io_channel *ch, void *buf, void *md, uint64_t offset_blocks, + uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg), 0); +DEFINE_STUB(spdk_bdev_writev_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, + struct iovec *iov, int iovcnt, uint64_t offset_blocks, uint64_t num_blocks, + spdk_bdev_io_completion_cb cb, void *cb_arg), 0); +DEFINE_STUB(spdk_bdev_get_num_blocks, uint64_t, (const struct spdk_bdev *bdev), 1024); +DEFINE_STUB(spdk_bdev_get_md_size, uint32_t, (const struct spdk_bdev *bdev), 0); +DEFINE_STUB(spdk_bdev_get_block_size, uint32_t, (const struct spdk_bdev *bdev), 4096); +#if defined(FTL_META_DEBUG) +DEFINE_STUB(ftl_band_validate_md, bool, (struct ftl_band *band), true); +#endif +#if defined(DEBUG) +DEFINE_STUB_V(ftl_trace_submission, (struct spdk_ftl_dev *dev, const struct ftl_io *io, + struct ftl_addr addr, size_t addr_cnt)); +DEFINE_STUB_V(ftl_trace_limits, (struct spdk_ftl_dev *dev, int limit, size_t num_free)); +DEFINE_STUB(ftl_trace_alloc_id, uint64_t, (struct spdk_ftl_dev *dev), 0); +DEFINE_STUB_V(ftl_trace_completion, (struct spdk_ftl_dev *dev, const struct ftl_io *io, + enum ftl_trace_completion type)); +DEFINE_STUB_V(ftl_trace_wbuf_fill, (struct spdk_ftl_dev *dev, const struct ftl_io *io)); +#endif + +struct spdk_io_channel * +spdk_bdev_get_io_channel(struct spdk_bdev_desc *bdev_desc) +{ + return spdk_get_io_channel(bdev_desc); +} + +static int +channel_create_cb(void *io_device, void *ctx) +{ + return 0; +} + +static void +channel_destroy_cb(void *io_device, void *ctx) +{} + +static struct spdk_ftl_dev * +setup_device(uint32_t num_threads, uint32_t xfer_size) +{ + struct spdk_ftl_dev *dev; + struct _ftl_io_channel *_ioch; + struct ftl_io_channel *ioch; + int rc; + + allocate_threads(num_threads); + set_thread(0); + + dev = calloc(1, sizeof(*dev)); + SPDK_CU_ASSERT_FATAL(dev != NULL); + + dev->core_thread = spdk_get_thread(); + dev->ioch = calloc(1, sizeof(*_ioch) + sizeof(struct spdk_io_channel)); + SPDK_CU_ASSERT_FATAL(dev->ioch != NULL); + + _ioch = (struct _ftl_io_channel *)(dev->ioch + 1); + ioch = _ioch->ioch = calloc(1, sizeof(*ioch)); + SPDK_CU_ASSERT_FATAL(ioch != NULL); + + ioch->elem_size = sizeof(struct ftl_md_io); + ioch->io_pool = spdk_mempool_create("io-pool", 4096, ioch->elem_size, 0, 0); + + SPDK_CU_ASSERT_FATAL(ioch->io_pool != NULL); + + dev->conf = g_default_conf; + dev->xfer_size = xfer_size; + dev->base_bdev_desc = (struct spdk_bdev_desc *)0xdeadbeef; + spdk_io_device_register(dev->base_bdev_desc, channel_create_cb, channel_destroy_cb, 0, NULL); + + rc = ftl_dev_init_io_channel(dev); + CU_ASSERT_EQUAL(rc, 0); + + return dev; +} + +static void +free_device(struct spdk_ftl_dev *dev) +{ + struct ftl_io_channel *ioch; + + ioch = ftl_io_channel_get_ctx(dev->ioch); + spdk_mempool_free(ioch->io_pool); + free(ioch); + + spdk_io_device_unregister(dev, NULL); + spdk_io_device_unregister(dev->base_bdev_desc, NULL); + free_threads(); + + free(dev->ioch_array); + free(dev->iov_buf); + free(dev->ioch); + free(dev); +} + +static void +setup_io(struct ftl_io *io, struct spdk_ftl_dev *dev, ftl_io_fn cb, void *ctx) +{ + io->dev = dev; + io->cb_fn = cb; + io->cb_ctx = ctx; +} + +static struct ftl_io * +alloc_io(struct spdk_ftl_dev *dev, ftl_io_fn cb, void *ctx) +{ + struct ftl_io *io; + + io = ftl_io_alloc(dev->ioch); + SPDK_CU_ASSERT_FATAL(io != NULL); + setup_io(io, dev, cb, ctx); + + return io; +} + +static void +io_complete_cb(struct ftl_io *io, void *ctx, int status) +{ + *(int *)ctx = status; +} + +static void +test_completion(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_io_channel *ioch; + struct ftl_io *io; + int req, status = 0; + size_t pool_size; + + dev = setup_device(1, 16); + ioch = ftl_io_channel_get_ctx(dev->ioch); + pool_size = spdk_mempool_count(ioch->io_pool); + + io = alloc_io(dev, io_complete_cb, &status); + io->status = -EIO; + +#define NUM_REQUESTS 16 + for (req = 0; req < NUM_REQUESTS; ++req) { + ftl_io_inc_req(io); + CU_ASSERT_FALSE(ftl_io_done(io)); + } + + CU_ASSERT_EQUAL(io->req_cnt, NUM_REQUESTS); + + for (req = 0; req < (NUM_REQUESTS - 1); ++req) { + ftl_io_dec_req(io); + CU_ASSERT_FALSE(ftl_io_done(io)); + } + + CU_ASSERT_EQUAL(io->req_cnt, 1); + + ftl_io_dec_req(io); + CU_ASSERT_TRUE(ftl_io_done(io)); + + ftl_io_complete(io); + CU_ASSERT_EQUAL(status, -EIO); + + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size); + + free_device(dev); +} + +static void +test_alloc_free(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_io_channel *ioch; + struct ftl_io *parent, *child; + int parent_status = -1; + size_t pool_size; + + dev = setup_device(1, 16); + ioch = ftl_io_channel_get_ctx(dev->ioch); + pool_size = spdk_mempool_count(ioch->io_pool); + + parent = alloc_io(dev, io_complete_cb, &parent_status); + SPDK_CU_ASSERT_FATAL(parent != NULL); + child = ftl_io_alloc_child(parent); + SPDK_CU_ASSERT_FATAL(child != NULL); + + ftl_io_free(child); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size - 1); + + child = ftl_io_alloc_child(parent); + SPDK_CU_ASSERT_FATAL(child != NULL); + ftl_io_complete(child); + CU_ASSERT_EQUAL(parent_status, -1); + ftl_io_complete(parent); + CU_ASSERT_EQUAL(parent_status, 0); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size); + + parent_status = -1; + parent = alloc_io(dev, io_complete_cb, &parent_status); + SPDK_CU_ASSERT_FATAL(parent != NULL); + child = ftl_io_alloc_child(parent); + SPDK_CU_ASSERT_FATAL(child != NULL); + + ftl_io_free(child); + CU_ASSERT_EQUAL(parent_status, -1); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size - 1); + ftl_io_complete(parent); + CU_ASSERT_EQUAL(parent_status, 0); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size); + + free_device(dev); +} + +static void +test_child_requests(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_io_channel *ioch; +#define MAX_CHILDREN 16 + struct ftl_io *parent, *child[MAX_CHILDREN]; + int status[MAX_CHILDREN + 1], i; + size_t pool_size; + + dev = setup_device(1, 16); + ioch = ftl_io_channel_get_ctx(dev->ioch); + pool_size = spdk_mempool_count(ioch->io_pool); + + /* Verify correct behaviour when children finish first */ + parent = alloc_io(dev, io_complete_cb, &status[0]); + parent->status = 0; + + ftl_io_inc_req(parent); + status[0] = -1; + + for (i = 0; i < MAX_CHILDREN; ++i) { + status[i + 1] = -1; + + child[i] = ftl_io_alloc_child(parent); + SPDK_CU_ASSERT_FATAL(child[i] != NULL); + setup_io(child[i], dev, io_complete_cb, &status[i + 1]); + child[i]->status = 0; + + ftl_io_inc_req(child[i]); + } + + CU_ASSERT_FALSE(ftl_io_done(parent)); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size - MAX_CHILDREN - 1); + + for (i = 0; i < MAX_CHILDREN; ++i) { + CU_ASSERT_FALSE(ftl_io_done(child[i])); + ftl_io_dec_req(child[i]); + CU_ASSERT_TRUE(ftl_io_done(child[i])); + CU_ASSERT_FALSE(ftl_io_done(parent)); + + ftl_io_complete(child[i]); + CU_ASSERT_FALSE(ftl_io_done(parent)); + CU_ASSERT_EQUAL(status[i + 1], 0); + } + + CU_ASSERT_EQUAL(status[0], -1); + + ftl_io_dec_req(parent); + CU_ASSERT_EQUAL(parent->req_cnt, 0); + CU_ASSERT_TRUE(ftl_io_done(parent)); + + ftl_io_complete(parent); + CU_ASSERT_EQUAL(status[0], 0); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size); + + + /* Verify correct behaviour when parent finishes first */ + parent = alloc_io(dev, io_complete_cb, &status[0]); + parent->status = 0; + + ftl_io_inc_req(parent); + status[0] = -1; + + for (i = 0; i < MAX_CHILDREN; ++i) { + status[i + 1] = -1; + + child[i] = ftl_io_alloc_child(parent); + SPDK_CU_ASSERT_FATAL(child[i] != NULL); + setup_io(child[i], dev, io_complete_cb, &status[i + 1]); + child[i]->status = 0; + + ftl_io_inc_req(child[i]); + } + + CU_ASSERT_FALSE(ftl_io_done(parent)); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size - MAX_CHILDREN - 1); + + ftl_io_dec_req(parent); + CU_ASSERT_TRUE(ftl_io_done(parent)); + CU_ASSERT_EQUAL(parent->req_cnt, 0); + + ftl_io_complete(parent); + CU_ASSERT_EQUAL(status[0], -1); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size - MAX_CHILDREN - 1); + + for (i = 0; i < MAX_CHILDREN; ++i) { + CU_ASSERT_FALSE(ftl_io_done(child[i])); + ftl_io_dec_req(child[i]); + CU_ASSERT_TRUE(ftl_io_done(child[i])); + + ftl_io_complete(child[i]); + CU_ASSERT_EQUAL(status[i + 1], 0); + } + + CU_ASSERT_EQUAL(status[0], 0); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size); + + free_device(dev); +} + +static void +test_child_status(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_io_channel *ioch; + struct ftl_io *parent, *child[2]; + int parent_status, child_status[2]; + size_t pool_size, i; + + dev = setup_device(1, 16); + ioch = ftl_io_channel_get_ctx(dev->ioch); + pool_size = spdk_mempool_count(ioch->io_pool); + + /* Verify the first error is returned by the parent */ + parent = alloc_io(dev, io_complete_cb, &parent_status); + parent->status = 0; + + for (i = 0; i < 2; ++i) { + child[i] = ftl_io_alloc_child(parent); + SPDK_CU_ASSERT_FATAL(child[i] != NULL); + setup_io(child[i], dev, io_complete_cb, &child_status[i]); + } + + child[0]->status = -3; + child[1]->status = -4; + + ftl_io_complete(child[1]); + ftl_io_complete(child[0]); + ftl_io_complete(parent); + + CU_ASSERT_EQUAL(child_status[0], -3); + CU_ASSERT_EQUAL(child_status[1], -4); + CU_ASSERT_EQUAL(parent_status, -4); + + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size); + + /* Verify parent's status is kept if children finish successfully */ + parent = alloc_io(dev, io_complete_cb, &parent_status); + parent->status = -1; + + for (i = 0; i < 2; ++i) { + child[i] = ftl_io_alloc_child(parent); + SPDK_CU_ASSERT_FATAL(child[i] != NULL); + setup_io(child[i], dev, io_complete_cb, &child_status[i]); + } + + child[0]->status = 0; + child[1]->status = 0; + + ftl_io_complete(parent); + ftl_io_complete(child[1]); + ftl_io_complete(child[0]); + + CU_ASSERT_EQUAL(child_status[0], 0); + CU_ASSERT_EQUAL(child_status[1], 0); + CU_ASSERT_EQUAL(parent_status, -1); + + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size); + + /* Verify parent's status is kept if children fail too */ + parent = alloc_io(dev, io_complete_cb, &parent_status); + parent->status = -1; + + for (i = 0; i < 2; ++i) { + child[i] = ftl_io_alloc_child(parent); + SPDK_CU_ASSERT_FATAL(child[i] != NULL); + setup_io(child[i], dev, io_complete_cb, &child_status[i]); + } + + child[0]->status = -3; + child[1]->status = -4; + + ftl_io_complete(parent); + ftl_io_complete(child[1]); + ftl_io_complete(child[0]); + + CU_ASSERT_EQUAL(child_status[0], -3); + CU_ASSERT_EQUAL(child_status[1], -4); + CU_ASSERT_EQUAL(parent_status, -1); + + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size); + + free_device(dev); +} + +static void +test_multi_generation(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_io_channel *ioch; +#define MAX_GRAND_CHILDREN 32 + struct ftl_io *parent, *child[MAX_CHILDREN], *gchild[MAX_CHILDREN * MAX_GRAND_CHILDREN]; + int parent_status, child_status[MAX_CHILDREN], gchild_status[MAX_CHILDREN * MAX_GRAND_CHILDREN]; + size_t pool_size; + int i, j; + + dev = setup_device(1, 16); + ioch = ftl_io_channel_get_ctx(dev->ioch); + pool_size = spdk_mempool_count(ioch->io_pool); + + /* Verify correct behaviour when children finish first */ + parent = alloc_io(dev, io_complete_cb, &parent_status); + parent->status = 0; + + ftl_io_inc_req(parent); + parent_status = -1; + + for (i = 0; i < MAX_CHILDREN; ++i) { + child_status[i] = -1; + + child[i] = ftl_io_alloc_child(parent); + SPDK_CU_ASSERT_FATAL(child[i] != NULL); + setup_io(child[i], dev, io_complete_cb, &child_status[i]); + child[i]->status = 0; + + + for (j = 0; j < MAX_GRAND_CHILDREN; ++j) { + struct ftl_io *io = ftl_io_alloc_child(child[i]); + SPDK_CU_ASSERT_FATAL(io != NULL); + + gchild[i * MAX_GRAND_CHILDREN + j] = io; + gchild_status[i * MAX_GRAND_CHILDREN + j] = -1; + setup_io(io, dev, io_complete_cb, &gchild_status[i * MAX_GRAND_CHILDREN + j]); + io->status = 0; + + ftl_io_inc_req(io); + } + + ftl_io_inc_req(child[i]); + } + + for (i = 0; i < MAX_CHILDREN; ++i) { + CU_ASSERT_FALSE(ftl_io_done(child[i])); + ftl_io_dec_req(child[i]); + CU_ASSERT_TRUE(ftl_io_done(child[i])); + + ftl_io_complete(child[i]); + CU_ASSERT_FALSE(ftl_io_done(parent)); + CU_ASSERT_EQUAL(child_status[i], -1); + + for (j = 0; j < MAX_GRAND_CHILDREN; ++j) { + struct ftl_io *io = gchild[i * MAX_GRAND_CHILDREN + j]; + + CU_ASSERT_FALSE(ftl_io_done(io)); + ftl_io_dec_req(io); + CU_ASSERT_TRUE(ftl_io_done(io)); + ftl_io_complete(io); + CU_ASSERT_EQUAL(gchild_status[i * MAX_GRAND_CHILDREN + j], 0); + } + + CU_ASSERT_EQUAL(child_status[i], 0); + } + + ftl_io_dec_req(parent); + CU_ASSERT_TRUE(ftl_io_done(parent)); + ftl_io_complete(parent); + CU_ASSERT_EQUAL(parent_status, 0); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size); + + /* Verify correct behaviour when parents finish first */ + parent = alloc_io(dev, io_complete_cb, &parent_status); + parent->status = 0; + parent_status = -1; + + for (i = 0; i < MAX_CHILDREN; ++i) { + child_status[i] = -1; + + child[i] = ftl_io_alloc_child(parent); + SPDK_CU_ASSERT_FATAL(child[i] != NULL); + setup_io(child[i], dev, io_complete_cb, &child_status[i]); + child[i]->status = 0; + + for (j = 0; j < MAX_GRAND_CHILDREN; ++j) { + struct ftl_io *io = ftl_io_alloc_child(child[i]); + SPDK_CU_ASSERT_FATAL(io != NULL); + + gchild[i * MAX_GRAND_CHILDREN + j] = io; + gchild_status[i * MAX_GRAND_CHILDREN + j] = -1; + setup_io(io, dev, io_complete_cb, &gchild_status[i * MAX_GRAND_CHILDREN + j]); + io->status = 0; + + ftl_io_inc_req(io); + } + + CU_ASSERT_TRUE(ftl_io_done(child[i])); + ftl_io_complete(child[i]); + CU_ASSERT_EQUAL(child_status[i], -1); + } + + CU_ASSERT_TRUE(ftl_io_done(parent)); + ftl_io_complete(parent); + CU_ASSERT_EQUAL(parent_status, -1); + + for (i = 0; i < MAX_CHILDREN; ++i) { + for (j = 0; j < MAX_GRAND_CHILDREN; ++j) { + struct ftl_io *io = gchild[i * MAX_GRAND_CHILDREN + j]; + + CU_ASSERT_FALSE(ftl_io_done(io)); + ftl_io_dec_req(io); + CU_ASSERT_TRUE(ftl_io_done(io)); + ftl_io_complete(io); + CU_ASSERT_EQUAL(gchild_status[i * MAX_GRAND_CHILDREN + j], 0); + } + + CU_ASSERT_EQUAL(child_status[i], 0); + } + + CU_ASSERT_EQUAL(parent_status, 0); + CU_ASSERT_EQUAL(spdk_mempool_count(ioch->io_pool), pool_size); + + free_device(dev); +} + +static void +test_io_channel_create(void) +{ + struct spdk_ftl_dev *dev; + struct spdk_io_channel *ioch, **ioch_array; + struct ftl_io_channel *ftl_ioch; + uint32_t ioch_idx; + + dev = setup_device(g_default_conf.max_io_channels + 1, 16); + + ioch = spdk_get_io_channel(dev); + CU_ASSERT(ioch != NULL); + CU_ASSERT_EQUAL(dev->num_io_channels, 1); + spdk_put_io_channel(ioch); + poll_threads(); + CU_ASSERT_EQUAL(dev->num_io_channels, 0); + + ioch_array = calloc(dev->conf.max_io_channels, sizeof(*ioch_array)); + SPDK_CU_ASSERT_FATAL(ioch != NULL); + + for (ioch_idx = 0; ioch_idx < dev->conf.max_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + ioch = ioch_array[ioch_idx] = spdk_get_io_channel(dev); + SPDK_CU_ASSERT_FATAL(ioch != NULL); + poll_threads(); + + ftl_ioch = ftl_io_channel_get_ctx(ioch); + CU_ASSERT_EQUAL(ftl_ioch->index, ioch_idx); + } + + CU_ASSERT_EQUAL(dev->num_io_channels, dev->conf.max_io_channels); + set_thread(dev->conf.max_io_channels); + ioch = spdk_get_io_channel(dev); + CU_ASSERT_EQUAL(dev->num_io_channels, dev->conf.max_io_channels); + CU_ASSERT_EQUAL(ioch, NULL); + + for (ioch_idx = 0; ioch_idx < dev->conf.max_io_channels; ioch_idx += 2) { + set_thread(ioch_idx); + spdk_put_io_channel(ioch_array[ioch_idx]); + ioch_array[ioch_idx] = NULL; + poll_threads(); + } + + poll_threads(); + CU_ASSERT_EQUAL(dev->num_io_channels, dev->conf.max_io_channels / 2); + + for (ioch_idx = 0; ioch_idx < dev->conf.max_io_channels; ioch_idx++) { + set_thread(ioch_idx); + + if (ioch_array[ioch_idx] == NULL) { + ioch = ioch_array[ioch_idx] = spdk_get_io_channel(dev); + SPDK_CU_ASSERT_FATAL(ioch != NULL); + poll_threads(); + + ftl_ioch = ftl_io_channel_get_ctx(ioch); + CU_ASSERT_EQUAL(ftl_ioch->index, ioch_idx); + } + } + + for (ioch_idx = 0; ioch_idx < dev->conf.max_io_channels; ioch_idx++) { + set_thread(ioch_idx); + spdk_put_io_channel(ioch_array[ioch_idx]); + } + + poll_threads(); + CU_ASSERT_EQUAL(dev->num_io_channels, 0); + + free(ioch_array); + free_device(dev); +} + +static void +test_acquire_entry(void) +{ + struct spdk_ftl_dev *dev; + struct spdk_io_channel *ioch, **ioch_array; + struct ftl_io_channel *ftl_ioch; + struct ftl_wbuf_entry *entry, **entries; + uint32_t num_entries, num_io_channels = 2; + uint32_t ioch_idx, entry_idx, tmp_idx; + + dev = setup_device(num_io_channels, 16); + + num_entries = dev->conf.write_buffer_size / FTL_BLOCK_SIZE; + entries = calloc(num_entries * num_io_channels, sizeof(*entries)); + SPDK_CU_ASSERT_FATAL(entries != NULL); + ioch_array = calloc(num_io_channels, sizeof(*ioch_array)); + SPDK_CU_ASSERT_FATAL(ioch_array != NULL); + + /* Acquire whole buffer of internal entries */ + entry_idx = 0; + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + ioch_array[ioch_idx] = spdk_get_io_channel(dev); + SPDK_CU_ASSERT_FATAL(ioch_array[ioch_idx] != NULL); + ftl_ioch = ftl_io_channel_get_ctx(ioch_array[ioch_idx]); + poll_threads(); + + for (tmp_idx = 0; tmp_idx < num_entries; ++tmp_idx) { + entries[entry_idx++] = ftl_acquire_wbuf_entry(ftl_ioch, FTL_IO_INTERNAL); + CU_ASSERT(entries[entry_idx - 1] != NULL); + } + + entry = ftl_acquire_wbuf_entry(ftl_ioch, FTL_IO_INTERNAL); + CU_ASSERT(entry == NULL); + } + + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + + for (tmp_idx = 0; tmp_idx < num_entries; ++tmp_idx) { + ftl_release_wbuf_entry(entries[ioch_idx * num_entries + tmp_idx]); + entries[ioch_idx * num_entries + tmp_idx] = NULL; + } + + spdk_put_io_channel(ioch_array[ioch_idx]); + } + poll_threads(); + + /* Do the same for user entries */ + entry_idx = 0; + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + ioch_array[ioch_idx] = spdk_get_io_channel(dev); + SPDK_CU_ASSERT_FATAL(ioch_array[ioch_idx] != NULL); + ftl_ioch = ftl_io_channel_get_ctx(ioch_array[ioch_idx]); + poll_threads(); + + for (tmp_idx = 0; tmp_idx < num_entries; ++tmp_idx) { + entries[entry_idx++] = ftl_acquire_wbuf_entry(ftl_ioch, 0); + CU_ASSERT(entries[entry_idx - 1] != NULL); + } + + entry = ftl_acquire_wbuf_entry(ftl_ioch, 0); + CU_ASSERT(entry == NULL); + } + + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + + for (tmp_idx = 0; tmp_idx < num_entries; ++tmp_idx) { + ftl_release_wbuf_entry(entries[ioch_idx * num_entries + tmp_idx]); + entries[ioch_idx * num_entries + tmp_idx] = NULL; + } + + spdk_put_io_channel(ioch_array[ioch_idx]); + } + poll_threads(); + + /* Verify limits */ + entry_idx = 0; + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + ioch_array[ioch_idx] = spdk_get_io_channel(dev); + SPDK_CU_ASSERT_FATAL(ioch_array[ioch_idx] != NULL); + ftl_ioch = ftl_io_channel_get_ctx(ioch_array[ioch_idx]); + poll_threads(); + + ftl_ioch->qdepth_limit = num_entries / 2; + for (tmp_idx = 0; tmp_idx < num_entries / 2; ++tmp_idx) { + entries[entry_idx++] = ftl_acquire_wbuf_entry(ftl_ioch, 0); + CU_ASSERT(entries[entry_idx - 1] != NULL); + } + + entry = ftl_acquire_wbuf_entry(ftl_ioch, 0); + CU_ASSERT(entry == NULL); + + for (; tmp_idx < num_entries; ++tmp_idx) { + entries[entry_idx++] = ftl_acquire_wbuf_entry(ftl_ioch, FTL_IO_INTERNAL); + CU_ASSERT(entries[entry_idx - 1] != NULL); + } + } + + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + + for (tmp_idx = 0; tmp_idx < num_entries; ++tmp_idx) { + ftl_release_wbuf_entry(entries[ioch_idx * num_entries + tmp_idx]); + entries[ioch_idx * num_entries + tmp_idx] = NULL; + } + + spdk_put_io_channel(ioch_array[ioch_idx]); + } + poll_threads(); + + /* Verify acquire/release */ + set_thread(0); + ioch = spdk_get_io_channel(dev); + SPDK_CU_ASSERT_FATAL(ioch != NULL); + ftl_ioch = ftl_io_channel_get_ctx(ioch); + poll_threads(); + + for (entry_idx = 0; entry_idx < num_entries; ++entry_idx) { + entries[entry_idx] = ftl_acquire_wbuf_entry(ftl_ioch, 0); + CU_ASSERT(entries[entry_idx] != NULL); + } + + entry = ftl_acquire_wbuf_entry(ftl_ioch, 0); + CU_ASSERT(entry == NULL); + + for (entry_idx = 0; entry_idx < num_entries / 2; ++entry_idx) { + ftl_release_wbuf_entry(entries[entry_idx]); + entries[entry_idx] = NULL; + } + + for (; entry_idx < num_entries; ++entry_idx) { + entries[entry_idx - num_entries / 2] = ftl_acquire_wbuf_entry(ftl_ioch, 0); + CU_ASSERT(entries[entry_idx - num_entries / 2] != NULL); + } + + for (entry_idx = 0; entry_idx < num_entries; ++entry_idx) { + ftl_release_wbuf_entry(entries[entry_idx]); + entries[entry_idx] = NULL; + } + + spdk_put_io_channel(ioch); + poll_threads(); + + free(ioch_array); + free(entries); + free_device(dev); +} + +static void +test_submit_batch(void) +{ + struct spdk_ftl_dev *dev; + struct spdk_io_channel **_ioch_array; + struct ftl_io_channel **ioch_array; + struct ftl_wbuf_entry *entry; + struct ftl_batch *batch, *batch2; + uint32_t num_io_channels = 16; + uint32_t ioch_idx, tmp_idx, entry_idx; + uint64_t ioch_bitmap; + size_t num_entries; + + dev = setup_device(num_io_channels, num_io_channels); + + _ioch_array = calloc(num_io_channels, sizeof(*_ioch_array)); + SPDK_CU_ASSERT_FATAL(_ioch_array != NULL); + ioch_array = calloc(num_io_channels, sizeof(*ioch_array)); + SPDK_CU_ASSERT_FATAL(ioch_array != NULL); + + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + _ioch_array[ioch_idx] = spdk_get_io_channel(dev); + SPDK_CU_ASSERT_FATAL(_ioch_array[ioch_idx] != NULL); + ioch_array[ioch_idx] = ftl_io_channel_get_ctx(_ioch_array[ioch_idx]); + poll_threads(); + } + + /* Make sure the IO channels are not starved and entries are popped in RR fashion */ + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + + for (entry_idx = 0; entry_idx < dev->xfer_size; ++entry_idx) { + entry = ftl_acquire_wbuf_entry(ioch_array[ioch_idx], 0); + SPDK_CU_ASSERT_FATAL(entry != NULL); + + num_entries = spdk_ring_enqueue(ioch_array[ioch_idx]->submit_queue, + (void **)&entry, 1, NULL); + CU_ASSERT(num_entries == 1); + } + } + + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + for (tmp_idx = 0; tmp_idx < ioch_idx; ++tmp_idx) { + set_thread(tmp_idx); + + while (spdk_ring_count(ioch_array[tmp_idx]->submit_queue) < dev->xfer_size) { + entry = ftl_acquire_wbuf_entry(ioch_array[tmp_idx], 0); + SPDK_CU_ASSERT_FATAL(entry != NULL); + + num_entries = spdk_ring_enqueue(ioch_array[tmp_idx]->submit_queue, + (void **)&entry, 1, NULL); + CU_ASSERT(num_entries == 1); + } + } + + set_thread(ioch_idx); + + batch = ftl_get_next_batch(dev); + SPDK_CU_ASSERT_FATAL(batch != NULL); + + TAILQ_FOREACH(entry, &batch->entries, tailq) { + CU_ASSERT(entry->ioch == ioch_array[ioch_idx]); + } + + ftl_release_batch(dev, batch); + + CU_ASSERT(spdk_ring_count(ioch_array[ioch_idx]->free_queue) == + ioch_array[ioch_idx]->num_entries); + } + + for (ioch_idx = 0; ioch_idx < num_io_channels - 1; ++ioch_idx) { + batch = ftl_get_next_batch(dev); + SPDK_CU_ASSERT_FATAL(batch != NULL); + ftl_release_batch(dev, batch); + } + + /* Make sure the batch can be built from entries from any IO channel */ + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + entry = ftl_acquire_wbuf_entry(ioch_array[ioch_idx], 0); + SPDK_CU_ASSERT_FATAL(entry != NULL); + + num_entries = spdk_ring_enqueue(ioch_array[ioch_idx]->submit_queue, + (void **)&entry, 1, NULL); + CU_ASSERT(num_entries == 1); + } + + batch = ftl_get_next_batch(dev); + SPDK_CU_ASSERT_FATAL(batch != NULL); + + ioch_bitmap = 0; + TAILQ_FOREACH(entry, &batch->entries, tailq) { + ioch_bitmap |= 1 << entry->ioch->index; + } + + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + CU_ASSERT((ioch_bitmap & (1 << ioch_array[ioch_idx]->index)) != 0); + } + ftl_release_batch(dev, batch); + + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + CU_ASSERT(spdk_ring_count(ioch_array[ioch_idx]->free_queue) == + ioch_array[ioch_idx]->num_entries); + } + + /* Make sure pending batches are prioritized */ + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + + while (spdk_ring_count(ioch_array[ioch_idx]->submit_queue) < dev->xfer_size) { + entry = ftl_acquire_wbuf_entry(ioch_array[ioch_idx], 0); + SPDK_CU_ASSERT_FATAL(entry != NULL); + num_entries = spdk_ring_enqueue(ioch_array[ioch_idx]->submit_queue, + (void **)&entry, 1, NULL); + CU_ASSERT(num_entries == 1); + } + } + + batch = ftl_get_next_batch(dev); + SPDK_CU_ASSERT_FATAL(batch != NULL); + + TAILQ_INSERT_TAIL(&dev->pending_batches, batch, tailq); + batch2 = ftl_get_next_batch(dev); + SPDK_CU_ASSERT_FATAL(batch2 != NULL); + + CU_ASSERT(TAILQ_EMPTY(&dev->pending_batches)); + CU_ASSERT(batch == batch2); + + batch = ftl_get_next_batch(dev); + SPDK_CU_ASSERT_FATAL(batch != NULL); + + ftl_release_batch(dev, batch); + ftl_release_batch(dev, batch2); + + for (ioch_idx = 2; ioch_idx < num_io_channels; ++ioch_idx) { + batch = ftl_get_next_batch(dev); + SPDK_CU_ASSERT_FATAL(batch != NULL); + ftl_release_batch(dev, batch); + } + + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + spdk_put_io_channel(_ioch_array[ioch_idx]); + } + poll_threads(); + + free(_ioch_array); + free(ioch_array); + free_device(dev); +} + +static void +test_entry_address(void) +{ + struct spdk_ftl_dev *dev; + struct spdk_io_channel **ioch_array; + struct ftl_io_channel *ftl_ioch; + struct ftl_wbuf_entry **entry_array; + struct ftl_addr addr; + uint32_t num_entries, num_io_channels = 7; + uint32_t ioch_idx, entry_idx; + + dev = setup_device(num_io_channels, num_io_channels); + ioch_array = calloc(num_io_channels, sizeof(*ioch_array)); + SPDK_CU_ASSERT_FATAL(ioch_array != NULL); + + num_entries = dev->conf.write_buffer_size / FTL_BLOCK_SIZE; + entry_array = calloc(num_entries, sizeof(*entry_array)); + SPDK_CU_ASSERT_FATAL(entry_array != NULL); + + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + ioch_array[ioch_idx] = spdk_get_io_channel(dev); + SPDK_CU_ASSERT_FATAL(ioch_array[ioch_idx] != NULL); + poll_threads(); + } + + for (ioch_idx = 0; ioch_idx < num_io_channels; ++ioch_idx) { + set_thread(ioch_idx); + ftl_ioch = ftl_io_channel_get_ctx(ioch_array[ioch_idx]); + + for (entry_idx = 0; entry_idx < num_entries; ++entry_idx) { + entry_array[entry_idx] = ftl_acquire_wbuf_entry(ftl_ioch, 0); + SPDK_CU_ASSERT_FATAL(entry_array[entry_idx] != NULL); + + addr = ftl_get_addr_from_entry(entry_array[entry_idx]); + CU_ASSERT(addr.cached == 1); + CU_ASSERT((addr.cache_offset >> dev->ioch_shift) == entry_idx); + CU_ASSERT((addr.cache_offset & ((1 << dev->ioch_shift) - 1)) == ioch_idx); + CU_ASSERT(entry_array[entry_idx] == ftl_get_entry_from_addr(dev, addr)); + } + + for (entry_idx = 0; entry_idx < num_entries; ++entry_idx) { + ftl_release_wbuf_entry(entry_array[entry_idx]); + } + } + + for (ioch_idx = 0; ioch_idx < num_io_channels; ioch_idx += 2) { + set_thread(ioch_idx); + spdk_put_io_channel(ioch_array[ioch_idx]); + ioch_array[ioch_idx] = NULL; + } + poll_threads(); + + for (ioch_idx = 1; ioch_idx < num_io_channels; ioch_idx += 2) { + set_thread(ioch_idx); + ftl_ioch = ftl_io_channel_get_ctx(ioch_array[ioch_idx]); + + for (entry_idx = 0; entry_idx < num_entries; ++entry_idx) { + entry_array[entry_idx] = ftl_acquire_wbuf_entry(ftl_ioch, 0); + SPDK_CU_ASSERT_FATAL(entry_array[entry_idx] != NULL); + + addr = ftl_get_addr_from_entry(entry_array[entry_idx]); + CU_ASSERT(addr.cached == 1); + CU_ASSERT(entry_array[entry_idx] == ftl_get_entry_from_addr(dev, addr)); + } + + for (entry_idx = 0; entry_idx < num_entries; ++entry_idx) { + ftl_release_wbuf_entry(entry_array[entry_idx]); + } + } + + for (ioch_idx = 1; ioch_idx < num_io_channels; ioch_idx += 2) { + set_thread(ioch_idx); + spdk_put_io_channel(ioch_array[ioch_idx]); + } + poll_threads(); + + free(entry_array); + free(ioch_array); + free_device(dev); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("ftl_io_suite", NULL, NULL); + + + CU_ADD_TEST(suite, test_completion); + CU_ADD_TEST(suite, test_alloc_free); + CU_ADD_TEST(suite, test_child_requests); + CU_ADD_TEST(suite, test_child_status); + CU_ADD_TEST(suite, test_multi_generation); + CU_ADD_TEST(suite, test_io_channel_create); + CU_ADD_TEST(suite, test_acquire_entry); + CU_ADD_TEST(suite, test_submit_batch); + CU_ADD_TEST(suite, test_entry_address); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + + return num_failures; +} diff --git a/src/spdk/test/unit/lib/ftl/ftl_md/.gitignore b/src/spdk/test/unit/lib/ftl/ftl_md/.gitignore new file mode 100644 index 000000000..8f0f690f0 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_md/.gitignore @@ -0,0 +1 @@ +ftl_md_ut diff --git a/src/spdk/test/unit/lib/ftl/ftl_md/Makefile b/src/spdk/test/unit/lib/ftl/ftl_md/Makefile new file mode 100644 index 000000000..1ad632aff --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_md/Makefile @@ -0,0 +1,38 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +TEST_FILE = ftl_md_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/ftl/ftl_md/ftl_md_ut.c b/src/spdk/test/unit/lib/ftl/ftl_md/ftl_md_ut.c new file mode 100644 index 000000000..20f3a28c9 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_md/ftl_md_ut.c @@ -0,0 +1,150 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" +#include "common/lib/test_env.c" + +#include "ftl/ftl_band.c" +#include "../common/utils.c" + +struct base_bdev_geometry g_geo = { + .write_unit_size = 16, + .optimal_open_zones = 12, + .zone_size = 100, + .blockcnt = 1500 * 100 * 12, +}; + +static void +setup_band(struct ftl_band **band, const struct base_bdev_geometry *geo) +{ + int rc; + struct spdk_ftl_dev *dev; + + dev = test_init_ftl_dev(&g_geo); + *band = test_init_ftl_band(dev, 0, geo->zone_size); + rc = ftl_band_alloc_lba_map(*band); + SPDK_CU_ASSERT_FATAL(rc == 0); + (*band)->state = FTL_BAND_STATE_PREP; + ftl_band_clear_lba_map(*band); +} + +static void +cleanup_band(struct ftl_band *band) +{ + struct spdk_ftl_dev *dev = band->dev; + + test_free_ftl_band(band); + test_free_ftl_dev(dev); +} + +static void +test_md_unpack(void) +{ + struct ftl_band *band; + struct ftl_lba_map *lba_map; + + setup_band(&band, &g_geo); + + lba_map = &band->lba_map; + SPDK_CU_ASSERT_FATAL(lba_map->dma_buf); + + ftl_pack_head_md(band); + CU_ASSERT_EQUAL(ftl_unpack_head_md(band), FTL_MD_SUCCESS); + + ftl_pack_tail_md(band); + CU_ASSERT_EQUAL(ftl_unpack_tail_md(band), FTL_MD_SUCCESS); + + cleanup_band(band); +} + +static void +test_md_unpack_fail(void) +{ + struct ftl_band *band; + struct ftl_lba_map *lba_map; + struct ftl_md_hdr *hdr; + + setup_band(&band, &g_geo); + + lba_map = &band->lba_map; + SPDK_CU_ASSERT_FATAL(lba_map->dma_buf); + + /* check crc */ + ftl_pack_tail_md(band); + /* flip last bit of lba_map */ + *((char *)lba_map->dma_buf + ftl_tail_md_num_blocks(band->dev) * FTL_BLOCK_SIZE - 1) ^= 0x1; + CU_ASSERT_EQUAL(ftl_unpack_tail_md(band), FTL_MD_INVALID_CRC); + + /* check invalid version */ + hdr = lba_map->dma_buf; + ftl_pack_tail_md(band); + hdr->ver++; + CU_ASSERT_EQUAL(ftl_unpack_tail_md(band), FTL_MD_INVALID_VER); + + /* check wrong UUID */ + ftl_pack_head_md(band); + hdr->uuid.u.raw[0] ^= 0x1; + CU_ASSERT_EQUAL(ftl_unpack_head_md(band), FTL_MD_NO_MD); + + /* check invalid size */ + ftl_pack_tail_md(band); + g_geo.zone_size--; + CU_ASSERT_EQUAL(ftl_unpack_tail_md(band), FTL_MD_INVALID_SIZE); + + cleanup_band(band); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("ftl_meta_suite", NULL, NULL); + + + CU_ADD_TEST(suite, test_md_unpack); + CU_ADD_TEST(suite, test_md_unpack_fail); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + + return num_failures; +} diff --git a/src/spdk/test/unit/lib/ftl/ftl_ppa/.gitignore b/src/spdk/test/unit/lib/ftl/ftl_ppa/.gitignore new file mode 100644 index 000000000..7f07c7f98 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_ppa/.gitignore @@ -0,0 +1 @@ +ftl_ppa_ut diff --git a/src/spdk/test/unit/lib/ftl/ftl_ppa/Makefile b/src/spdk/test/unit/lib/ftl/ftl_ppa/Makefile new file mode 100644 index 000000000..f8df5209e --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_ppa/Makefile @@ -0,0 +1,38 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +TEST_FILE = ftl_ppa_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/ftl/ftl_ppa/ftl_ppa_ut.c b/src/spdk/test/unit/lib/ftl/ftl_ppa/ftl_ppa_ut.c new file mode 100644 index 000000000..dae57abcd --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_ppa/ftl_ppa_ut.c @@ -0,0 +1,226 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" +#include "common/lib/test_env.c" + +#include "ftl/ftl_core.h" + +#define L2P_TABLE_SIZE 1024 + +static struct spdk_ftl_dev *g_dev; + +DEFINE_STUB(spdk_bdev_desc_get_bdev, struct spdk_bdev *, (struct spdk_bdev_desc *desc), NULL); + +uint64_t +spdk_bdev_get_zone_size(const struct spdk_bdev *bdev) +{ + if (g_dev->addr_len > 32) { + return 1ULL << 32; + } + + return 1024; +} + +uint32_t +spdk_bdev_get_optimal_open_zones(const struct spdk_bdev *bdev) +{ + return 100; +} + +static struct spdk_ftl_dev * +test_alloc_dev(size_t size) +{ + struct spdk_ftl_dev *dev; + + dev = calloc(1, sizeof(*dev)); + + dev->num_lbas = L2P_TABLE_SIZE; + dev->l2p = calloc(L2P_TABLE_SIZE, size); + + return dev; +} + +static int +setup_l2p_32bit(void) +{ + g_dev = test_alloc_dev(sizeof(uint32_t)); + g_dev->addr_len = 24; + return 0; +} + +static int +setup_l2p_64bit(void) +{ + g_dev = test_alloc_dev(sizeof(uint64_t)); + g_dev->addr_len = 63; + return 0; +} + +static void +clean_l2p(void) +{ + size_t l2p_elem_size; + + if (ftl_addr_packed(g_dev)) { + l2p_elem_size = sizeof(uint32_t); + } else { + l2p_elem_size = sizeof(uint64_t); + } + memset(g_dev->l2p, 0, g_dev->num_lbas * l2p_elem_size); +} + +static int +cleanup(void) +{ + free(g_dev->l2p); + free(g_dev); + g_dev = NULL; + return 0; +} + +static void +test_addr_pack32(void) +{ + struct ftl_addr orig = {}, addr; + + /* Check valid address transformation */ + orig.offset = 4; + addr = ftl_addr_to_packed(g_dev, orig); + CU_ASSERT_TRUE(addr.offset <= UINT32_MAX); + CU_ASSERT_FALSE(addr.pack.cached); + addr = ftl_addr_from_packed(g_dev, addr); + CU_ASSERT_FALSE(ftl_addr_invalid(addr)); + CU_ASSERT_EQUAL(addr.offset, orig.offset); + + /* Check invalid address transformation */ + orig = ftl_to_addr(FTL_ADDR_INVALID); + addr = ftl_addr_to_packed(g_dev, orig); + CU_ASSERT_TRUE(addr.offset <= UINT32_MAX); + addr = ftl_addr_from_packed(g_dev, addr); + CU_ASSERT_TRUE(ftl_addr_invalid(addr)); + + /* Check cached entry offset transformation */ + orig.cached = 1; + orig.cache_offset = 1024; + addr = ftl_addr_to_packed(g_dev, orig); + CU_ASSERT_TRUE(addr.offset <= UINT32_MAX); + CU_ASSERT_TRUE(addr.pack.cached); + addr = ftl_addr_from_packed(g_dev, addr); + CU_ASSERT_FALSE(ftl_addr_invalid(addr)); + CU_ASSERT_TRUE(ftl_addr_cached(addr)); + CU_ASSERT_EQUAL(addr.offset, orig.offset); + clean_l2p(); +} + +static void +test_addr_invalid(void) +{ + struct ftl_addr addr; + size_t i; + + /* Set every other LBA as invalid */ + for (i = 0; i < L2P_TABLE_SIZE; i += 2) { + ftl_l2p_set(g_dev, i, ftl_to_addr(FTL_ADDR_INVALID)); + } + + /* Check every even LBA is invalid while others are fine */ + for (i = 0; i < L2P_TABLE_SIZE; ++i) { + addr = ftl_l2p_get(g_dev, i); + + if (i % 2 == 0) { + CU_ASSERT_TRUE(ftl_addr_invalid(addr)); + } else { + CU_ASSERT_FALSE(ftl_addr_invalid(addr)); + } + } + clean_l2p(); +} + +static void +test_addr_cached(void) +{ + struct ftl_addr addr; + size_t i; + + /* Set every other LBA is cached */ + for (i = 0; i < L2P_TABLE_SIZE; i += 2) { + addr.cached = 1; + addr.cache_offset = i; + ftl_l2p_set(g_dev, i, addr); + } + + /* Check every even LBA is cached while others are not */ + for (i = 0; i < L2P_TABLE_SIZE; ++i) { + addr = ftl_l2p_get(g_dev, i); + + if (i % 2 == 0) { + CU_ASSERT_TRUE(ftl_addr_cached(addr)); + CU_ASSERT_EQUAL(addr.cache_offset, i); + } else { + CU_ASSERT_FALSE(ftl_addr_cached(addr)); + } + } + clean_l2p(); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite32 = NULL, suite64 = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite32 = CU_add_suite("ftl_addr32_suite", setup_l2p_32bit, cleanup); + + + suite64 = CU_add_suite("ftl_addr64_suite", setup_l2p_64bit, cleanup); + + + CU_ADD_TEST(suite32, test_addr_pack32); + CU_ADD_TEST(suite32, test_addr_invalid); + CU_ADD_TEST(suite32, test_addr_cached); + CU_ADD_TEST(suite64, test_addr_invalid); + CU_ADD_TEST(suite64, test_addr_cached); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + + return num_failures; +} diff --git a/src/spdk/test/unit/lib/ftl/ftl_reloc.c/.gitignore b/src/spdk/test/unit/lib/ftl/ftl_reloc.c/.gitignore new file mode 100644 index 000000000..439602062 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_reloc.c/.gitignore @@ -0,0 +1 @@ +ftl_reloc_ut diff --git a/src/spdk/test/unit/lib/ftl/ftl_reloc.c/Makefile b/src/spdk/test/unit/lib/ftl/ftl_reloc.c/Makefile new file mode 100644 index 000000000..ed4188107 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_reloc.c/Makefile @@ -0,0 +1,38 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +TEST_FILE = ftl_reloc_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/ftl/ftl_reloc.c/ftl_reloc_ut.c b/src/spdk/test/unit/lib/ftl/ftl_reloc.c/ftl_reloc_ut.c new file mode 100644 index 000000000..26a423882 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_reloc.c/ftl_reloc_ut.c @@ -0,0 +1,508 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" +#include "common/lib/test_env.c" + +#include "ftl/ftl_reloc.c" +#include "../common/utils.c" + +#define MAX_ACTIVE_RELOCS 5 +#define MAX_RELOC_QDEPTH 31 + +struct base_bdev_geometry g_geo = { + .write_unit_size = 16, + .optimal_open_zones = 12, + .zone_size = 100, + .blockcnt = 1500 * 100 * 12, +}; + +DEFINE_STUB(ftl_dev_tail_md_disk_size, size_t, (const struct spdk_ftl_dev *dev), 1); +DEFINE_STUB(ftl_addr_is_written, bool, (struct ftl_band *band, struct ftl_addr addr), true); +DEFINE_STUB_V(ftl_band_set_state, (struct ftl_band *band, enum ftl_band_state state)); +DEFINE_STUB_V(ftl_free_io, (struct ftl_io *io)); +#if defined(DEBUG) +DEFINE_STUB_V(ftl_trace_lba_io_init, (struct spdk_ftl_dev *dev, const struct ftl_io *io)); +#endif + +int +ftl_band_alloc_lba_map(struct ftl_band *band) +{ + struct spdk_ftl_dev *dev = band->dev; + + ftl_band_acquire_lba_map(band); + band->lba_map.map = spdk_mempool_get(dev->lba_pool); + + return 0; +} + +void +ftl_band_release_lba_map(struct ftl_band *band) +{ + struct spdk_ftl_dev *dev = band->dev; + + band->lba_map.ref_cnt--; + spdk_mempool_put(dev->lba_pool, band->lba_map.map); + band->lba_map.map = NULL; +} + +void +ftl_band_acquire_lba_map(struct ftl_band *band) +{ + band->lba_map.ref_cnt++; +} + +size_t +ftl_lba_map_num_blocks(const struct spdk_ftl_dev *dev) +{ + return spdk_divide_round_up(ftl_get_num_blocks_in_band(dev) * sizeof(uint64_t), FTL_BLOCK_SIZE); +} + +int +ftl_band_read_lba_map(struct ftl_band *band, size_t offset, + size_t num_blocks, ftl_io_fn fn, void *ctx) +{ + fn(ctx, ctx, 0); + return 0; +} + +uint64_t +ftl_band_block_offset_from_addr(struct ftl_band *band, struct ftl_addr addr) +{ + return test_offset_from_addr(addr, band); +} + +struct ftl_addr +ftl_band_addr_from_block_offset(struct ftl_band *band, uint64_t block_off) +{ + struct ftl_addr addr = {}; + + addr.offset = block_off + band->id * ftl_get_num_blocks_in_band(band->dev); + return addr; +} + +void +ftl_io_read(struct ftl_io *io) +{ + io->cb_fn(io, io->cb_ctx, 0); + free(io); +} + +void +ftl_io_write(struct ftl_io *io) +{ + io->cb_fn(io, io->cb_ctx, 0); + free(io->lba.vector); + free(io); +} + +struct ftl_io * +ftl_io_init_internal(const struct ftl_io_init_opts *opts) +{ + struct ftl_io *io = opts->io; + + if (!io) { + io = calloc(1, opts->size); + } + + SPDK_CU_ASSERT_FATAL(io != NULL); + + io->dev = opts->dev; + io->band = opts->band; + io->flags = opts->flags; + io->cb_fn = opts->cb_fn; + io->cb_ctx = io; + io->num_blocks = opts->num_blocks; + memcpy(&io->iov, &opts->iovs, sizeof(io->iov)); + io->iov_cnt = opts->iovcnt; + + if (opts->flags & FTL_IO_VECTOR_LBA) { + io->lba.vector = calloc(io->num_blocks, sizeof(uint64_t)); + SPDK_CU_ASSERT_FATAL(io->lba.vector != NULL); + } + + return io; +} + +struct ftl_io * +ftl_io_alloc(struct spdk_io_channel *ch) +{ + size_t io_size = sizeof(struct ftl_md_io); + + return malloc(io_size); +} + +void +ftl_io_reinit(struct ftl_io *io, ftl_io_fn fn, void *ctx, int flags, int type) +{ + io->cb_fn = fn; + io->cb_ctx = ctx; + io->type = type; +} + +static void +single_reloc_move(struct ftl_band_reloc *breloc) +{ + /* Process read */ + ftl_process_reloc(breloc); + /* Process lba map read */ + ftl_process_reloc(breloc); + /* Process write */ + ftl_process_reloc(breloc); +} + +static void +add_to_active_queue(struct ftl_reloc *reloc, struct ftl_band_reloc *breloc) +{ + TAILQ_REMOVE(&reloc->pending_queue, breloc, entry); + breloc->state = FTL_BAND_RELOC_STATE_ACTIVE; + TAILQ_INSERT_HEAD(&reloc->active_queue, breloc, entry); +} + +static void +setup_reloc(struct spdk_ftl_dev **_dev, struct ftl_reloc **_reloc, + const struct base_bdev_geometry *geo) +{ + size_t i; + struct spdk_ftl_dev *dev; + struct ftl_reloc *reloc; + + dev = test_init_ftl_dev(geo); + + dev->conf.max_active_relocs = MAX_ACTIVE_RELOCS; + dev->conf.max_reloc_qdepth = MAX_RELOC_QDEPTH; + + SPDK_CU_ASSERT_FATAL(ftl_get_num_bands(dev) > 0); + + for (i = 0; i < ftl_get_num_bands(dev); ++i) { + test_init_ftl_band(dev, i, geo->zone_size); + } + + reloc = ftl_reloc_init(dev); + dev->reloc = reloc; + CU_ASSERT_PTR_NOT_NULL_FATAL(reloc); + ftl_reloc_resume(reloc); + + *_dev = dev; + *_reloc = reloc; +} + +static void +cleanup_reloc(struct spdk_ftl_dev *dev, struct ftl_reloc *reloc) +{ + size_t i; + + for (i = 0; i < ftl_get_num_bands(reloc->dev); ++i) { + SPDK_CU_ASSERT_FATAL(reloc->brelocs[i].state == FTL_BAND_RELOC_STATE_INACTIVE); + } + + ftl_reloc_free(reloc); + + for (i = 0; i < ftl_get_num_bands(dev); ++i) { + test_free_ftl_band(&dev->bands[i]); + } + test_free_ftl_dev(dev); +} + +static void +set_band_valid_map(struct ftl_band *band, size_t offset, size_t num_blocks) +{ + struct ftl_lba_map *lba_map = &band->lba_map; + size_t i; + + SPDK_CU_ASSERT_FATAL(lba_map != NULL); + for (i = offset; i < offset + num_blocks; ++i) { + spdk_bit_array_set(lba_map->vld, i); + lba_map->num_vld++; + } +} + +static void +test_reloc_iter_full(void) +{ + size_t num_blocks, num_iters, reminder, i; + struct spdk_ftl_dev *dev; + struct ftl_reloc *reloc; + struct ftl_band_reloc *breloc; + struct ftl_band *band; + struct ftl_addr addr; + + setup_reloc(&dev, &reloc, &g_geo); + + g_geo.zone_size = 100; + breloc = &reloc->brelocs[0]; + band = breloc->band; + + set_band_valid_map(band, 0, ftl_get_num_blocks_in_band(dev)); + + ftl_reloc_add(reloc, band, 0, ftl_get_num_blocks_in_band(dev), 0, true); + + CU_ASSERT_EQUAL(breloc->num_blocks, ftl_get_num_blocks_in_band(dev)); + + num_iters = ftl_get_num_punits(dev) * + (ftl_get_num_blocks_in_zone(dev) / reloc->xfer_size); + + for (i = 0; i < num_iters; i++) { + num_blocks = ftl_reloc_next_blocks(breloc, &addr); + CU_ASSERT_EQUAL(num_blocks, reloc->xfer_size); + } + + num_iters = ftl_get_num_punits(dev); + + /* ftl_reloc_next_blocks is searching for maximum xfer_size */ + /* contiguous valid logic blocks in zone, so we can end up */ + /* with some reminder if number of logical blocks in zone */ + /* is not divisible by xfer_size */ + reminder = ftl_get_num_blocks_in_zone(dev) % reloc->xfer_size; + for (i = 0; i < num_iters; i++) { + num_blocks = ftl_reloc_next_blocks(breloc, &addr); + CU_ASSERT_EQUAL(reminder, num_blocks); + } + + /* num_blocks should remain intact since all the blocks are valid */ + CU_ASSERT_EQUAL(breloc->num_blocks, ftl_get_num_blocks_in_band(dev)); + breloc->state = FTL_BAND_RELOC_STATE_INACTIVE; + + cleanup_reloc(dev, reloc); +} + +static void +test_reloc_empty_band(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_reloc *reloc; + struct ftl_band_reloc *breloc; + struct ftl_band *band; + + setup_reloc(&dev, &reloc, &g_geo); + + breloc = &reloc->brelocs[0]; + band = breloc->band; + + ftl_reloc_add(reloc, band, 0, ftl_get_num_blocks_in_band(dev), 0, true); + + CU_ASSERT_EQUAL(breloc->num_blocks, 0); + + cleanup_reloc(dev, reloc); +} + +static void +test_reloc_full_band(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_reloc *reloc; + struct ftl_band_reloc *breloc; + struct ftl_band *band; + size_t num_moves, num_iters, num_block, i; + + setup_reloc(&dev, &reloc, &g_geo); + + breloc = &reloc->brelocs[0]; + band = breloc->band; + num_moves = MAX_RELOC_QDEPTH * reloc->xfer_size; + num_iters = ftl_get_num_blocks_in_band(dev) / num_moves; + + set_band_valid_map(band, 0, ftl_get_num_blocks_in_band(dev)); + + ftl_reloc_add(reloc, band, 0, ftl_get_num_blocks_in_band(dev), 0, true); + + CU_ASSERT_EQUAL(breloc->num_blocks, ftl_get_num_blocks_in_band(dev)); + + ftl_reloc_prep(breloc); + add_to_active_queue(reloc, breloc); + + for (i = 1; i <= num_iters; ++i) { + single_reloc_move(breloc); + num_block = ftl_get_num_blocks_in_band(dev) - (i * num_moves); + CU_ASSERT_EQUAL(breloc->num_blocks, num_block); + + } + + /* Process reminder blocks */ + single_reloc_move(breloc); + /* Drain move queue */ + ftl_reloc_process_moves(breloc); + + CU_ASSERT_EQUAL(breloc->num_blocks, 0); + CU_ASSERT_TRUE(ftl_reloc_done(breloc)); + ftl_reloc_release(breloc); + + cleanup_reloc(dev, reloc); +} + +static void +test_reloc_scatter_band(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_reloc *reloc; + struct ftl_band_reloc *breloc; + struct ftl_band *band; + size_t num_iters, i; + + setup_reloc(&dev, &reloc, &g_geo); + + breloc = &reloc->brelocs[0]; + band = breloc->band; + num_iters = spdk_divide_round_up(ftl_get_num_blocks_in_band(dev), MAX_RELOC_QDEPTH * 2); + + for (i = 0; i < ftl_get_num_blocks_in_band(dev); ++i) { + if (i % 2) { + set_band_valid_map(band, i, 1); + } + } + + ftl_reloc_add(reloc, band, 0, ftl_get_num_blocks_in_band(dev), 0, true); + ftl_reloc_prep(breloc); + add_to_active_queue(reloc, breloc); + + CU_ASSERT_EQUAL(breloc->num_blocks, ftl_get_num_blocks_in_band(dev)); + + for (i = 0; i < num_iters ; ++i) { + single_reloc_move(breloc); + } + + ftl_process_reloc(breloc); + CU_ASSERT_EQUAL(breloc->num_blocks, 0); + CU_ASSERT_TRUE(ftl_reloc_done(breloc)); + + cleanup_reloc(dev, reloc); +} + +static void +test_reloc_zone(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_reloc *reloc; + struct ftl_band_reloc *breloc; + struct ftl_band *band; + size_t num_io, num_iters, num_block, i; + + setup_reloc(&dev, &reloc, &g_geo); + + breloc = &reloc->brelocs[0]; + band = breloc->band; + /* High priority band have allocated lba map */ + band->high_prio = 1; + ftl_band_alloc_lba_map(band); + num_io = MAX_RELOC_QDEPTH * reloc->xfer_size; + num_iters = ftl_get_num_blocks_in_zone(dev) / num_io; + + set_band_valid_map(band, 0, ftl_get_num_blocks_in_band(dev)); + + ftl_reloc_add(reloc, band, ftl_get_num_blocks_in_zone(dev) * 3, + ftl_get_num_blocks_in_zone(dev), 1, false); + add_to_active_queue(reloc, breloc); + + CU_ASSERT_EQUAL(breloc->num_blocks, ftl_get_num_blocks_in_zone(dev)); + + for (i = 1; i <= num_iters ; ++i) { + single_reloc_move(breloc); + num_block = ftl_get_num_blocks_in_zone(dev) - (i * num_io); + + CU_ASSERT_EQUAL(breloc->num_blocks, num_block); + } + + /* In case num_blocks_in_zone % num_io != 0 one extra iteration is needed */ + single_reloc_move(breloc); + /* Drain move queue */ + ftl_reloc_process_moves(breloc); + + CU_ASSERT_EQUAL(breloc->num_blocks, 0); + CU_ASSERT_TRUE(ftl_reloc_done(breloc)); + ftl_reloc_release(breloc); + + cleanup_reloc(dev, reloc); +} + +static void +test_reloc_single_block(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_reloc *reloc; + struct ftl_band_reloc *breloc; + struct ftl_band *band; +#define TEST_RELOC_OFFSET 6 + + setup_reloc(&dev, &reloc, &g_geo); + + breloc = &reloc->brelocs[0]; + band = breloc->band; + + set_band_valid_map(band, TEST_RELOC_OFFSET, 1); + + ftl_reloc_add(reloc, band, TEST_RELOC_OFFSET, 1, 0, false); + SPDK_CU_ASSERT_FATAL(breloc == TAILQ_FIRST(&reloc->pending_queue)); + ftl_reloc_prep(breloc); + add_to_active_queue(reloc, breloc); + + CU_ASSERT_EQUAL(breloc->num_blocks, 1); + + single_reloc_move(breloc); + /* Drain move queue */ + ftl_reloc_process_moves(breloc); + + CU_ASSERT_EQUAL(breloc->num_blocks, 0); + CU_ASSERT_TRUE(ftl_reloc_done(breloc)); + ftl_reloc_release(breloc); + + cleanup_reloc(dev, reloc); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("ftl_band_suite", NULL, NULL); + + + CU_ADD_TEST(suite, test_reloc_iter_full); + CU_ADD_TEST(suite, test_reloc_empty_band); + CU_ADD_TEST(suite, test_reloc_full_band); + CU_ADD_TEST(suite, test_reloc_scatter_band); + CU_ADD_TEST(suite, test_reloc_zone); + CU_ADD_TEST(suite, test_reloc_single_block); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + + return num_failures; +} diff --git a/src/spdk/test/unit/lib/ftl/ftl_wptr/.gitignore b/src/spdk/test/unit/lib/ftl/ftl_wptr/.gitignore new file mode 100644 index 000000000..8f1f46756 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_wptr/.gitignore @@ -0,0 +1 @@ +ftl_wptr_ut diff --git a/src/spdk/test/unit/lib/ftl/ftl_wptr/Makefile b/src/spdk/test/unit/lib/ftl/ftl_wptr/Makefile new file mode 100644 index 000000000..42bf7c602 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_wptr/Makefile @@ -0,0 +1,38 @@ +# +# BSD LICENSE +# +# Copyright (c) Intel Corporation. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../../../..) + +TEST_FILE = ftl_wptr_ut.c + +include $(SPDK_ROOT_DIR)/mk/spdk.unittest.mk diff --git a/src/spdk/test/unit/lib/ftl/ftl_wptr/ftl_wptr_ut.c b/src/spdk/test/unit/lib/ftl/ftl_wptr/ftl_wptr_ut.c new file mode 100644 index 000000000..ccee312a2 --- /dev/null +++ b/src/spdk/test/unit/lib/ftl/ftl_wptr/ftl_wptr_ut.c @@ -0,0 +1,223 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/stdinc.h" + +#include "spdk_cunit.h" +#include "common/lib/test_env.c" + +#include "ftl/ftl_core.c" +#include "ftl/ftl_band.c" +#include "ftl/ftl_init.c" +#include "../common/utils.c" + +struct base_bdev_geometry g_geo = { + .write_unit_size = 16, + .optimal_open_zones = 12, + .zone_size = 128, + .blockcnt = 20 * 128 * 12, +}; + +#if defined(DEBUG) +DEFINE_STUB(ftl_band_validate_md, bool, (struct ftl_band *band), true); +DEFINE_STUB_V(ftl_trace_limits, (struct spdk_ftl_dev *dev, int limit, size_t num_free)); + +DEFINE_STUB_V(ftl_trace_completion, (struct spdk_ftl_dev *dev, const struct ftl_io *io, + enum ftl_trace_completion completion)); +DEFINE_STUB_V(ftl_trace_write_band, (struct spdk_ftl_dev *dev, const struct ftl_band *band)); +DEFINE_STUB_V(ftl_trace_submission, (struct spdk_ftl_dev *dev, const struct ftl_io *io, + struct ftl_addr addr, size_t addr_cnt)); +#endif +DEFINE_STUB_V(spdk_bdev_free_io, (struct spdk_bdev_io *bdev_io)); +DEFINE_STUB_V(ftl_io_dec_req, (struct ftl_io *io)); +DEFINE_STUB_V(ftl_io_inc_req, (struct ftl_io *io)); +DEFINE_STUB_V(ftl_io_fail, (struct ftl_io *io, int status)); +DEFINE_STUB_V(ftl_reloc_add, (struct ftl_reloc *reloc, struct ftl_band *band, size_t offset, + size_t num_blocks, int prio, bool defrag)); +DEFINE_STUB_V(ftl_io_process_error, (struct ftl_io *io, const struct spdk_nvme_cpl *status)); +DEFINE_STUB(spdk_bdev_get_num_blocks, uint64_t, (const struct spdk_bdev *bdev), 0); +DEFINE_STUB(spdk_bdev_zone_management, int, (struct spdk_bdev_desc *desc, + struct spdk_io_channel *ch, + uint64_t zone_id, enum spdk_bdev_zone_action action, + spdk_bdev_io_completion_cb cb, void *cb_arg), 0); +DEFINE_STUB(spdk_bdev_io_get_append_location, uint64_t, (struct spdk_bdev_io *bdev_io), 0); + +struct spdk_io_channel * +spdk_bdev_get_io_channel(struct spdk_bdev_desc *bdev_desc) +{ + return spdk_get_io_channel(bdev_desc); +} + +struct ftl_io * +ftl_io_erase_init(struct ftl_band *band, size_t num_blocks, ftl_io_fn cb) +{ + struct ftl_io *io; + + io = calloc(1, sizeof(struct ftl_io)); + SPDK_CU_ASSERT_FATAL(io != NULL); + + io->dev = band->dev; + io->band = band; + io->cb_fn = cb; + io->num_blocks = 1; + + return io; +} + +void +ftl_io_advance(struct ftl_io *io, size_t num_blocks) +{ + io->pos += num_blocks; +} + +void +ftl_io_complete(struct ftl_io *io) +{ + io->cb_fn(io, NULL, 0); + free(io); +} + +static void +setup_wptr_test(struct spdk_ftl_dev **dev, const struct base_bdev_geometry *geo) +{ + struct spdk_ftl_dev *t_dev; + struct _ftl_io_channel *_ioch; + size_t i; + + t_dev = test_init_ftl_dev(geo); + for (i = 0; i < ftl_get_num_bands(t_dev); ++i) { + test_init_ftl_band(t_dev, i, geo->zone_size); + t_dev->bands[i].state = FTL_BAND_STATE_CLOSED; + ftl_band_set_state(&t_dev->bands[i], FTL_BAND_STATE_FREE); + } + + _ioch = (struct _ftl_io_channel *)(t_dev->ioch + 1); + _ioch->ioch = calloc(1, sizeof(*_ioch->ioch)); + SPDK_CU_ASSERT_FATAL(_ioch->ioch != NULL); + + *dev = t_dev; +} + +static void +cleanup_wptr_test(struct spdk_ftl_dev *dev) +{ + struct _ftl_io_channel *_ioch; + size_t i; + + for (i = 0; i < ftl_get_num_bands(dev); ++i) { + dev->bands[i].lba_map.segments = NULL; + test_free_ftl_band(&dev->bands[i]); + } + + _ioch = (struct _ftl_io_channel *)(dev->ioch + 1); + free(_ioch->ioch); + + test_free_ftl_dev(dev); +} + +static void +test_wptr(void) +{ + struct spdk_ftl_dev *dev; + struct ftl_wptr *wptr; + struct ftl_band *band; + struct ftl_io io = { 0 }; + size_t xfer_size; + size_t zone, block, offset, i; + int rc; + + setup_wptr_test(&dev, &g_geo); + + xfer_size = dev->xfer_size; + ftl_add_wptr(dev); + for (i = 0; i < ftl_get_num_bands(dev); ++i) { + wptr = LIST_FIRST(&dev->wptr_list); + band = wptr->band; + ftl_band_set_state(band, FTL_BAND_STATE_OPENING); + ftl_band_set_state(band, FTL_BAND_STATE_OPEN); + io.band = band; + io.dev = dev; + + for (block = 0, offset = 0; block < ftl_get_num_blocks_in_zone(dev) / xfer_size; ++block) { + for (zone = 0; zone < band->num_zones; ++zone) { + CU_ASSERT_EQUAL(wptr->offset, offset); + ftl_wptr_advance(wptr, xfer_size); + offset += xfer_size; + } + } + + CU_ASSERT_EQUAL(band->state, FTL_BAND_STATE_FULL); + + ftl_band_set_state(band, FTL_BAND_STATE_CLOSING); + + /* Call the metadata completion cb to force band state change */ + /* and removal of the actual wptr */ + ftl_md_write_cb(&io, NULL, 0); + CU_ASSERT_EQUAL(band->state, FTL_BAND_STATE_CLOSED); + CU_ASSERT_TRUE(LIST_EMPTY(&dev->wptr_list)); + + rc = ftl_add_wptr(dev); + + /* There are no free bands during the last iteration, so */ + /* there'll be no new wptr allocation */ + if (i == (ftl_get_num_bands(dev) - 1)) { + CU_ASSERT_EQUAL(rc, -1); + } else { + CU_ASSERT_EQUAL(rc, 0); + } + } + + cleanup_wptr_test(dev); +} + +int +main(int argc, char **argv) +{ + CU_pSuite suite = NULL; + unsigned int num_failures; + + CU_set_error_action(CUEA_ABORT); + CU_initialize_registry(); + + suite = CU_add_suite("ftl_wptr_suite", NULL, NULL); + + + CU_ADD_TEST(suite, test_wptr); + + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + num_failures = CU_get_number_of_failures(); + CU_cleanup_registry(); + + return num_failures; +} |