Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[experimental]Introduce mpool limit and env_allocator fallback to malloc #28

Open
wants to merge 3 commits into
base: experimental
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/env_ocf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

LIBNAME := ocfenv

CFLAGS += $(ENV_CFLAGS) -I$(CURDIR) -I$(CURDIR)/include -w
CFLAGS += $(ENV_CFLAGS) -I$(CURDIR) -I$(CURDIR)/include -w -DOCF_CONFIG_MEM_SAVER=1 -DOCF_CONFIG_MAX_CORES=64
C_SRCS = $(shell find -name \*.c)

LIB = $(call spdk_lib_list_to_static_libs,$(LIBNAME))
Expand Down
13 changes: 11 additions & 2 deletions lib/env_ocf/mpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct env_mpool *env_mpool_create(uint32_t hdr_size, uint32_t elem_size,
char name[RTE_MEMPOOL_NAMESIZE] = {};
int ret;
int size;
bool create;

struct env_mpool *mpool = env_zalloc(sizeof(struct env_mpool), ENV_MEM_NOIO);
if (!mpool)
Expand All @@ -81,9 +82,17 @@ struct env_mpool *env_mpool_create(uint32_t hdr_size, uint32_t elem_size,

size = hdr_size + (elem_size * (1 << i));

mpool->allocator[i] = env_allocator_create(size, name);
create = false;
if (limits && limits[i]) {
mpool->allocator[i] = env_allocator_create_extended(size,
name, limits[i]);
create = true;
} else if (!limits) {
mpool->allocator[i] = env_allocator_create(size, name);
create = true;
}

if (!mpool->allocator[i])
if (create && !mpool->allocator[i])
goto err;
}

Expand Down
59 changes: 44 additions & 15 deletions lib/env_ocf/ocf_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,54 @@
/* Use unique index for env allocators */
static env_atomic g_env_allocator_index = 0;

struct _env_allocator_item {
uint8_t from_malloc : 1;

char data[] __attribute__ ((aligned (__alignof__(uint64_t))));
};

void *
env_allocator_new(env_allocator *allocator)
{
void *mem = spdk_mempool_get(allocator->mempool);
struct _env_allocator_item *item = spdk_mempool_get(allocator->mempool);

if (spdk_likely(item)) {
item->from_malloc = 0;
} else {
item = spdk_malloc(allocator->element_size, 0, NULL,
SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
if (!item)
return NULL;
item->from_malloc = 1;
}

if (spdk_likely(mem)) {
memset(mem, 0, allocator->element_size);
if (spdk_likely(item)) {
memset(item->data, 0, allocator->element_size -
sizeof(struct _env_allocator_item));
}

return mem;
return &item->data;
}

void
env_allocator_del(env_allocator *allocator, void *ptr)
{
struct _env_allocator_item *item = container_of(ptr, struct _env_allocator_item, data);

if (spdk_likely(!item->from_malloc))
spdk_mempool_put(allocator->mempool, item);
else
spdk_free(item);
}

env_allocator *
env_allocator_create(uint32_t size, const char *name)
{
return env_allocator_create_extended(size, name, -1);
}

env_allocator *
env_allocator_create_extended(uint32_t size, const char *name, int limit)
{
env_allocator *allocator;
char qualified_name[128] = {0};
Expand All @@ -73,10 +107,12 @@ env_allocator_create(uint32_t size, const char *name)
if (!allocator) {
return NULL;
}
allocator->element_size = size + sizeof(struct _env_allocator_item);
allocator->element_count = ENV_ALLOCATOR_NBUFS;

allocator->mempool = spdk_mempool_create(qualified_name,
ENV_ALLOCATOR_NBUFS, size,
SPDK_MEMPOOL_DEFAULT_CACHE_SIZE,
allocator->element_count, allocator->element_size,
limit < 0 ? SPDK_MEMPOOL_DEFAULT_CACHE_SIZE : limit,
SPDK_ENV_SOCKET_ID_ANY);

if (!allocator->mempool) {
Expand All @@ -85,22 +121,15 @@ env_allocator_create(uint32_t size, const char *name)
return NULL;
}

allocator->element_size = size;

return allocator;
}

void
env_allocator_del(env_allocator *allocator, void *item)
{
spdk_mempool_put(allocator->mempool, item);
}

void
env_allocator_destroy(env_allocator *allocator)
{
if (allocator) {
if (ENV_ALLOCATOR_NBUFS - spdk_mempool_count(allocator->mempool)) {
if (allocator->element_count -
spdk_mempool_count(allocator->mempool)) {
SPDK_ERRLOG("Not all objects deallocated\n");
assert(false);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/env_ocf/ocf_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,12 @@ static inline uint64_t env_get_free_memory(void)
typedef struct {
struct spdk_mempool *mempool;
size_t element_size;
size_t element_count;
} env_allocator;

env_allocator *env_allocator_create_extended(uint32_t size, const char *name,
int limit);

env_allocator *env_allocator_create(uint32_t size, const char *name);

void env_allocator_destroy(env_allocator *allocator);
Expand Down