Commit 372df452 authored by Jim Harris's avatar Jim Harris
Browse files

test: flesh out test_env implementation for mempools



Some modules rely on accurate memory buffer counts.
For example, bdev checks that all of its buffer pools
are back to original capacity during spdk_bdev_finish().

So flesh out the mempool implementation in test_env.c
to keep accurate counts of the number of buffers "available"
in the test mempool.  Note that test_env is only designed
for unit tests, so this functionality is not multi-thread
safe.

Still allow for NULL mempool pointers and just default
to old behavior in that case - some unit tests such as
the blobfs cache tree tests still rely on that behavior.

Signed-off-by: default avatarJim Harris <james.r.harris@intel.com>
Change-Id: I1d0ab49e16741c92d777d76f35e60271e4ad943a

Reviewed-on: https://review.gerrithub.io/377969


Reviewed-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
parent b1bebb50
Loading
Loading
Loading
Loading
+42 −10
Original line number Diff line number Diff line
@@ -153,44 +153,76 @@ spdk_memzone_free(const char *name)
	return 0;
}

struct test_mempool {
	size_t	count;
};

struct spdk_mempool *
spdk_mempool_create(const char *name, size_t count,
		    size_t ele_size, size_t cache_size, int socket_id)
{
	static int mp = 0;
	struct test_mempool *mp;

	mp = calloc(1, sizeof(*mp));
	if (mp == NULL) {
		return NULL;
	}

	mp->count = count;

	return (struct spdk_mempool *)&mp;
	return (struct spdk_mempool *)mp;
}

void
spdk_mempool_free(struct spdk_mempool *mp)
spdk_mempool_free(struct spdk_mempool *_mp)
{
	struct test_mempool *mp = (struct test_mempool *)_mp;

	free(mp);
}

void *
spdk_mempool_get(struct spdk_mempool *mp)
spdk_mempool_get(struct spdk_mempool *_mp)
{
	struct test_mempool *mp = (struct test_mempool *)_mp;
	void *buf;

	if (posix_memalign(&buf, 64, 0x1000)) {
		buf = NULL;
	if (mp && mp->count == 0) {
		return NULL;
	}

	if (posix_memalign(&buf, 64, 0x1000)) {
		return NULL;
	} else {
		if (mp) {
			mp->count--;
		}
		return buf;
	}
}

void
spdk_mempool_put(struct spdk_mempool *mp, void *ele)
spdk_mempool_put(struct spdk_mempool *_mp, void *ele)
{
	struct test_mempool *mp = (struct test_mempool *)_mp;

	if (mp) {
		mp->count++;
	}
	free(ele);
}

size_t
spdk_mempool_count(const struct spdk_mempool *mp)
spdk_mempool_count(const struct spdk_mempool *_mp)
{
	struct test_mempool *mp = (struct test_mempool *)_mp;

	if (mp) {
		return mp->count;
	} else {
		return 1024;
	}
}

uint64_t ut_tsc = 0;
uint64_t spdk_get_ticks(void)