From 372df452a47dd4b7ba62cf5694e07c6fc70a066f Mon Sep 17 00:00:00 2001 From: Jim Harris Date: Mon, 11 Sep 2017 11:53:21 -0700 Subject: [PATCH] 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: Jim Harris Change-Id: I1d0ab49e16741c92d777d76f35e60271e4ad943a Reviewed-on: https://review.gerrithub.io/377969 Reviewed-by: Daniel Verkamp Reviewed-by: Ben Walker Tested-by: SPDK Automated Test System --- test/lib/test_env.c | 52 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/test/lib/test_env.c b/test/lib/test_env.c index 4836272b6..4bea47613 100644 --- a/test/lib/test_env.c +++ b/test/lib/test_env.c @@ -153,43 +153,75 @@ 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; - return (struct spdk_mempool *)∓ + mp = calloc(1, sizeof(*mp)); + if (mp == NULL) { + return NULL; + } + + mp->count = count; + + 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; } - return buf; + 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) { - return 1024; + struct test_mempool *mp = (struct test_mempool *)_mp; + + if (mp) { + return mp->count; + } else { + return 1024; + } } uint64_t ut_tsc = 0;