Commit 87731e11 authored by Konrad Sztyber's avatar Konrad Sztyber Committed by Tomasz Zawadzki
Browse files

lib/ftl: write buffer entry address calculation



Two functions were added:
 - ftl_get_addr_from_entry() returning an address pointing at a given
   entry
 - and it's inverse, ftl_get_entry_from_addr(), returning a write buffer
   entry for a given address

Change-Id: Iea1e2e59f240f50183fdb549280a0f0b722efbef
Signed-off-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/906


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarWojciech Malikowski <wojciech.malikowski@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
parent 35fdf2dd
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -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)
{
+5 −0
Original line number Diff line number Diff line
@@ -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
+4 −0
Original line number Diff line number Diff line
@@ -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");
+84 −1
Original line number Diff line number Diff line
@@ -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();