Skip to content

[POC] trim #1318

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions include/umf/memory_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ umf_result_t umfPoolSetTag(umf_memory_pool_handle_t hPool, void *tag,
/// @return UMF_RESULT_SUCCESS on success.
umf_result_t umfPoolGetTag(umf_memory_pool_handle_t hPool, void **tag);

///
/// @brief Trims memory pool to keep at least \p minBytesToKeep bytes of memory
/// if possible.
/// @param hPool specified memory pool
/// @param minBytesToKeep minimum number of bytes to keep in the pool
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
umf_result_t umfPoolTrimMemory(umf_memory_pool_handle_t hPool,
size_t minBytesToKeep);

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 10 additions & 0 deletions include/umf/memory_pool_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ typedef struct umf_memory_pool_ops_t {
/// @return A constant character string representing the pool's name.
///
const char *(*ext_get_name)(void *pool);

///
/// @brief Trims memory of the pool, removing resources that are not needed
/// to keep the pool operational.
/// @param pool pointer to the memory pool
/// @param minBytesToKeep minimum number of bytes to keep in the pool if
/// possible.
/// @return UMF_RESULT_SUCCESS on success or appropriate error code on failure.
///
umf_result_t (*trim_memory)(void *pool, size_t minBytesToKeep);
} umf_memory_pool_ops_t;

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions src/libumf.def
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,4 @@ EXPORTS
umfJemallocPoolParamsDestroy
umfJemallocPoolParamsSetNumArenas
umfPoolGetName
umfPoolTrimMemory
1 change: 1 addition & 0 deletions src/libumf.map
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,5 @@ UMF_0.12 {
umfJemallocPoolParamsDestroy;
umfJemallocPoolParamsSetNumArenas;
umfPoolGetName;
umfPoolTrimMemory;
} UMF_0.11;
14 changes: 14 additions & 0 deletions src/memory_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
}

umf_result_t umfPoolDestroy(umf_memory_pool_handle_t hPool) {
if (hPool == NULL) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

if (umf_ba_global_is_destroyed()) {
return UMF_RESULT_ERROR_UNKNOWN;
}
Expand Down Expand Up @@ -417,3 +421,13 @@ umf_result_t umfPoolGetTag(umf_memory_pool_handle_t hPool, void **tag) {
utils_mutex_unlock(&hPool->lock);
return UMF_RESULT_SUCCESS;
}

umf_result_t umfPoolTrimMemory(umf_memory_pool_handle_t hPool,
size_t minBytesToKeep) {
UMF_CHECK((hPool != NULL), UMF_RESULT_ERROR_INVALID_ARGUMENT);
if (hPool->ops.trim_memory == NULL) {
return UMF_RESULT_ERROR_NOT_SUPPORTED;
}

return hPool->ops.trim_memory(hPool->pool_priv, minBytesToKeep);
}
4 changes: 4 additions & 0 deletions src/memory_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ void assignOpsIpcDefaults(umf_memory_provider_ops_t *ops) {

static bool validateOps(const umf_memory_provider_ops_t *ops) {
// Validate mandatory operations one by one
if (ops->get_name == NULL) {
LOG_ERR("missing get_name function pointer\n");
return false;
}
CHECK_OP(ops, alloc);
CHECK_OP(ops, free);
CHECK_OP(ops, get_recommended_page_size);
Expand Down
42 changes: 42 additions & 0 deletions src/pool/pool_disjoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,47 @@ const char *disjoint_pool_get_name(void *pool) {
return hPool->params.name;
}

umf_result_t disjoint_pool_trim_memory(void *pool, size_t minBytesToKeep) {
disjoint_pool_t *hPool = (disjoint_pool_t *)pool;
if (hPool == NULL) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

for (size_t i = 0; i < hPool->buckets_num; i++) {
bucket_t *bucket = hPool->buckets[i];
utils_mutex_lock(&bucket->bucket_lock);

int skip = (int)minBytesToKeep;

// remove empty slabs from the pool
slab_list_item_t *it = NULL, *tmp = NULL;
LL_FOREACH_SAFE(bucket->available_slabs, it, tmp) {
slab_t *slab = it->val;
if (slab->num_chunks_allocated == 0) {
// skip first minBytesToKeep bytes from each bucket
if (skip > 0) {
skip -= (int)slab->slab_size;
continue;
}

// remove slab
pool_unregister_slab(hPool, slab);
DL_DELETE(bucket->available_slabs, it);
assert(bucket->available_slabs_num > 0);
bucket->available_slabs_num--;
destroy_slab(slab);

// update stats
bucket_update_stats(bucket, 0, -1);
}
}

utils_mutex_unlock(&bucket->bucket_lock);
}

return UMF_RESULT_SUCCESS;
}

static umf_memory_pool_ops_t UMF_DISJOINT_POOL_OPS = {
.version = UMF_VERSION_CURRENT,
.initialize = disjoint_pool_initialize,
Expand All @@ -1044,6 +1085,7 @@ static umf_memory_pool_ops_t UMF_DISJOINT_POOL_OPS = {
.get_last_allocation_error = disjoint_pool_get_last_allocation_error,
.ext_get_name = disjoint_pool_get_name,
.ext_ctl = disjoint_pool_ctl,
.trim_memory = disjoint_pool_trim_memory,
};

const umf_memory_pool_ops_t *umfDisjointPoolOps(void) {
Expand Down
24 changes: 24 additions & 0 deletions src/pool/pool_jemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,28 @@ static umf_result_t op_get_last_allocation_error(void *pool) {
return TLS_last_allocation_error;
}

static const char *op_get_name(void *pool) {
(void)pool; // not used
return "jemalloc";
}

static umf_result_t op_trim_memory(void *pool, size_t minBytesToKeep) {
(void)minBytesToKeep; // unused - TODO?

jemalloc_memory_pool_t *je_pool = (jemalloc_memory_pool_t *)pool;
for (size_t i = 0; i < je_pool->n_arenas; i++) {
char cmd[64];
unsigned arena = je_pool->arena_index[i];
snprintf(cmd, sizeof(cmd), "arena.%u.purge", arena);
if (je_mallctl(cmd, NULL, NULL, NULL, 0)) {
LOG_ERR("Could not purge jemalloc arena %u", arena);
return UMF_RESULT_ERROR_UNKNOWN;
}
}

return UMF_RESULT_SUCCESS;
}

static umf_memory_pool_ops_t UMF_JEMALLOC_POOL_OPS = {
.version = UMF_POOL_OPS_VERSION_CURRENT,
.initialize = op_initialize,
Expand All @@ -563,6 +585,8 @@ static umf_memory_pool_ops_t UMF_JEMALLOC_POOL_OPS = {
.malloc_usable_size = op_malloc_usable_size,
.free = op_free,
.get_last_allocation_error = op_get_last_allocation_error,
.ext_get_name = op_get_name,
.trim_memory = op_trim_memory,
};

const umf_memory_pool_ops_t *umfJemallocPoolOps(void) {
Expand Down
17 changes: 16 additions & 1 deletion src/pool/pool_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ static umf_result_t proxy_get_last_allocation_error(void *pool) {
return TLS_last_allocation_error;
}

static const char *proxy_get_name(void *pool) {
(void)pool; // not used
return "proxy";
}

static umf_result_t proxy_trim_memory(void *pool, size_t minBytesToKeep) {
(void)pool;
(void)minBytesToKeep;

return UMF_RESULT_SUCCESS;
}

static umf_memory_pool_ops_t UMF_PROXY_POOL_OPS = {
.version = UMF_POOL_OPS_VERSION_CURRENT,
.initialize = proxy_pool_initialize,
Expand All @@ -135,7 +147,10 @@ static umf_memory_pool_ops_t UMF_PROXY_POOL_OPS = {
.aligned_malloc = proxy_aligned_malloc,
.malloc_usable_size = proxy_malloc_usable_size,
.free = proxy_free,
.get_last_allocation_error = proxy_get_last_allocation_error};
.get_last_allocation_error = proxy_get_last_allocation_error,
.ext_get_name = proxy_get_name,
.trim_memory = proxy_trim_memory,
};

const umf_memory_pool_ops_t *umfProxyPoolOps(void) {
return &UMF_PROXY_POOL_OPS;
Expand Down
14 changes: 12 additions & 2 deletions src/pool/pool_scalable.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
#include <stdio.h>
#include <string.h>

#include <ctl/ctl.h>
#include <memory_pool_internal.h>
#include <umf/memory_pool.h>
#include <umf/memory_pool_ops.h>
#include <umf/memory_provider.h>
#include <umf/pools/pool_scalable.h>

#include "base_alloc_global.h"
#include "ctl/ctl.h"
#include "libumf.h"
#include "memory_pool_internal.h"
#include "pool_scalable_internal.h"
#include "utils_common.h"
#include "utils_concurrency.h"
Expand Down Expand Up @@ -60,6 +60,7 @@ typedef struct tbb_callbacks_t {
bool (*pool_destroy)(void *);
void *(*pool_identify)(void *object);
size_t (*pool_msize)(void *, void *);
int (*pool_allocation_command)(int, void *);
#ifdef _WIN32
HMODULE lib_handle;
#else
Expand Down Expand Up @@ -431,6 +432,14 @@ static const char *scalable_get_name(void *pool) {
return "scalable";
}

static umf_result_t scalable_trim_memory(void *pool, size_t minBytesToKeep) {
(void)pool; // unused
(void)minBytesToKeep; // unused

//scalable_allocation_command?
return UMF_RESULT_SUCCESS;
}

static umf_memory_pool_ops_t UMF_SCALABLE_POOL_OPS = {
.version = UMF_POOL_OPS_VERSION_CURRENT,
.initialize = tbb_pool_initialize,
Expand All @@ -444,6 +453,7 @@ static umf_memory_pool_ops_t UMF_SCALABLE_POOL_OPS = {
.get_last_allocation_error = tbb_get_last_allocation_error,
.ext_ctl = pool_ctl,
.ext_get_name = scalable_get_name,
.trim_memory = scalable_trim_memory,
};

const umf_memory_pool_ops_t *umfScalablePoolOps(void) {
Expand Down
4 changes: 4 additions & 0 deletions test/common/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ typedef struct pool_base_t {
umf_result_t get_last_allocation_error() noexcept {
return UMF_RESULT_SUCCESS;
}
umf_result_t
trim_memory([[maybe_unused]] size_t minBytesToKeep) noexcept {
return UMF_RESULT_SUCCESS;
}
} pool_base_t;

struct malloc_pool : public pool_base_t {
Expand Down
6 changes: 6 additions & 0 deletions test/common/pool_null.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ static umf_result_t nullGetLastStatus(void *pool) {
return UMF_RESULT_SUCCESS;
}

static const char *nullGetName(void *pool) {
(void)pool; // not used
return "null";
}

umf_memory_pool_ops_t UMF_NULL_POOL_OPS = {
.version = UMF_POOL_OPS_VERSION_CURRENT,
.initialize = nullInitialize,
Expand All @@ -76,5 +81,6 @@ umf_memory_pool_ops_t UMF_NULL_POOL_OPS = {
.aligned_malloc = nullAlignedMalloc,
.malloc_usable_size = nullMallocUsableSize,
.free = nullFree,
.ext_get_name = nullGetName,
.get_last_allocation_error = nullGetLastStatus,
};
6 changes: 6 additions & 0 deletions test/common/pool_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ static umf_result_t traceGetLastStatus(void *pool) {
return umfPoolGetLastAllocationError(trace_pool->params.hUpstreamPool);
}

static const char *traceGetName(void *pool) {
(void)pool; // not used
return "trace";
}

umf_memory_pool_ops_t UMF_TRACE_POOL_OPS = {
.version = UMF_POOL_OPS_VERSION_CURRENT,
.initialize = traceInitialize,
Expand All @@ -102,5 +107,6 @@ umf_memory_pool_ops_t UMF_TRACE_POOL_OPS = {
.aligned_malloc = traceAlignedMalloc,
.malloc_usable_size = traceMallocUsableSize,
.free = traceFree,
.ext_get_name = traceGetName,
.get_last_allocation_error = traceGetLastStatus,
};
12 changes: 9 additions & 3 deletions test/common/provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ typedef struct provider_base_t {
[[maybe_unused]] size_t *pageSize) noexcept {
return UMF_RESULT_ERROR_UNKNOWN;
}
const char *get_name() noexcept { return "base"; }
const char *get_name(/*[[maybe_unused]] void *provider*/) noexcept {
return "base";
}
umf_result_t ext_purge_lazy([[maybe_unused]] void *ptr,
[[maybe_unused]] size_t size) noexcept {
return UMF_RESULT_ERROR_UNKNOWN;
Expand Down Expand Up @@ -125,7 +127,9 @@ struct provider_ba_global : public provider_base_t {
umf_ba_global_free(ptr);
return UMF_RESULT_SUCCESS;
}
const char *get_name() noexcept { return "umf_ba_global"; }
const char *get_name(/*[[maybe_unused]] void *provider*/) noexcept {
return "umf_ba_global";
}
};

umf_memory_provider_ops_t BA_GLOBAL_PROVIDER_OPS =
Expand All @@ -150,7 +154,9 @@ struct provider_mock_out_of_mem : public provider_base_t {
umf_result_t free(void *ptr, size_t size) noexcept {
return helper_prov.free(ptr, size);
}
const char *get_name() noexcept { return "mock_out_of_mem"; }
const char *get_name(/*[[maybe_unused]] void *provider*/) noexcept {
return "mock_out_of_mem";
}
};

const umf_memory_provider_ops_t MOCK_OUT_OF_MEM_PROVIDER_OPS =
Expand Down
4 changes: 3 additions & 1 deletion test/ipcAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ struct provider_mock_ipc : public umf_test::provider_base_t {
return ret;
}

const char *get_name() noexcept { return "mock_ipc"; }
const char *get_name(/*[[maybe_unused]] void *provider*/) noexcept {
return "mock_ipc";
}

umf_result_t ext_get_ipc_handle_size(size_t *size) noexcept {
*size = sizeof(provider_ipc_data_t);
Expand Down
Loading
Loading