diff --git a/lib/ftl/ftl_core.c b/lib/ftl/ftl_core.c index 9dbb71462..c5dda3f77 100644 --- a/lib/ftl/ftl_core.c +++ b/lib/ftl/ftl_core.c @@ -292,6 +292,39 @@ ftl_release_batch(struct spdk_ftl_dev *dev, struct ftl_batch *batch) TAILQ_INSERT_TAIL(&dev->free_batches, batch, tailq); } +struct ftl_wbuf_entry *ftl_get_entry_from_addr(struct spdk_ftl_dev *dev, struct ftl_addr addr); + +struct ftl_wbuf_entry * +ftl_get_entry_from_addr(struct spdk_ftl_dev *dev, struct ftl_addr addr) +{ + struct ftl_io_channel *ioch; + uint64_t ioch_offset, entry_offset; + + ioch_offset = addr.cache_offset & ((1 << dev->ioch_shift) - 1); + entry_offset = addr.cache_offset >> dev->ioch_shift; + ioch = dev->ioch_array[ioch_offset]; + + assert(ioch_offset < dev->conf.max_io_channels); + assert(entry_offset < ioch->num_entries); + assert(addr.cached == 1); + + return &ioch->wbuf_entries[entry_offset]; +} + +struct ftl_addr ftl_get_addr_from_entry(struct ftl_wbuf_entry *entry); + +struct ftl_addr +ftl_get_addr_from_entry(struct ftl_wbuf_entry *entry) +{ + struct ftl_io_channel *ioch = entry->ioch; + struct ftl_addr addr = {}; + + addr.cached = 1; + addr.cache_offset = (uint64_t)entry->index << ioch->dev->ioch_shift | ioch->index; + + return addr; +} + static void ftl_io_cmpl_cb(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) { diff --git a/lib/ftl/ftl_core.h b/lib/ftl/ftl_core.h index 5485cd134..aed7353e5 100644 --- a/lib/ftl/ftl_core.h +++ b/lib/ftl/ftl_core.h @@ -219,6 +219,11 @@ struct spdk_ftl_dev { struct ftl_io_channel **ioch_array; TAILQ_HEAD(, ftl_io_channel) ioch_queue; uint64_t num_io_channels; + /* Value required to shift address of a write buffer entry to retrieve + * the IO channel it's part of. The other part of the address describes + * the offset of an entry within the IO channel's entry array. + */ + uint64_t ioch_shift; /* Write buffer batches */ #define FTL_BATCH_COUNT 4096 diff --git a/lib/ftl/ftl_init.c b/lib/ftl/ftl_init.c index 3d3576a98..7386c687e 100644 --- a/lib/ftl/ftl_init.c +++ b/lib/ftl/ftl_init.c @@ -1198,6 +1198,10 @@ ftl_dev_init_io_channel(struct spdk_ftl_dev *dev) struct ftl_batch *batch; uint32_t i; + /* Align the IO channels to nearest power of 2 to allow for easy addr bit shift */ + dev->conf.max_io_channels = spdk_align32pow2(dev->conf.max_io_channels); + dev->ioch_shift = spdk_u32log2(dev->conf.max_io_channels); + dev->ioch_array = calloc(dev->conf.max_io_channels, sizeof(*dev->ioch_array)); if (!dev->ioch_array) { SPDK_ERRLOG("Failed to allocate IO channel array\n"); diff --git a/test/unit/lib/ftl/ftl_io.c/ftl_io_ut.c b/test/unit/lib/ftl/ftl_io.c/ftl_io_ut.c index 665f7f12e..b5a3847b7 100644 --- a/test/unit/lib/ftl/ftl_io.c/ftl_io_ut.c +++ b/test/unit/lib/ftl/ftl_io.c/ftl_io_ut.c @@ -917,6 +917,88 @@ test_submit_batch(void) 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.rwb_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) { @@ -950,7 +1032,8 @@ main(int argc, char **argv) test_acquire_entry) == NULL || CU_add_test(suite, "test_submit_batch", test_submit_batch) == NULL - + || CU_add_test(suite, "test_entry_address", + test_entry_address) == NULL ) { CU_cleanup_registry(); return CU_get_error();