Skip to content

Commit c56e141

Browse files
committed
[CTL] Add allocation counter for disjoint pool
1 parent 408069e commit c56e141

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/pool/pool_disjoint.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <ctype.h>
1010
#include <errno.h>
1111
#include <inttypes.h>
12+
#include <stdatomic.h>
1213
#include <stdlib.h>
1314
#include <string.h>
1415

@@ -33,6 +34,7 @@ static char *DEFAULT_NAME = "disjoint";
3334
struct ctl disjoint_ctl_root;
3435
static UTIL_ONCE_FLAG ctl_initialized = UTIL_ONCE_FLAG_INIT;
3536

37+
// CTL: name attribute
3638
static int CTL_READ_HANDLER(name)(void *ctx, umf_ctl_query_source_t source,
3739
void *arg, size_t size,
3840
umf_ctl_index_utlist_t *indexes,
@@ -66,10 +68,26 @@ static int CTL_WRITE_HANDLER(name)(void *ctx, umf_ctl_query_source_t source,
6668
return 0;
6769
}
6870

71+
// CTL: allocation counters
72+
atomic_int counter_alloc_balance;
73+
// atomic_fetch_add(&counter_alloc_balance, 1);
74+
75+
static int CTL_READ_HANDLER(allocation_balance)(void *ctx, umf_ctl_query_source_t source,
76+
void *arg, size_t size,
77+
umf_ctl_index_utlist_t *indexes,
78+
const char *extra_name,
79+
umf_ctl_query_type_t queryType) {
80+
(void)ctx, (void)source, (void)size, (void)indexes, (void)extra_name,(void)queryType;
81+
*(int *)arg = atomic_load(&counter_alloc_balance);
82+
return 0;
83+
}
84+
6985
static const umf_ctl_node_t CTL_NODE(disjoint)[] = {CTL_LEAF_RW(name),
86+
CTL_LEAF_RO(allocation_balance),
7087
CTL_NODE_END};
7188

7289
static void initialize_disjoint_ctl(void) {
90+
atomic_init(&counter_alloc_balance, 0);
7391
CTL_REGISTER_MODULE(&disjoint_ctl_root, disjoint);
7492
}
7593

@@ -745,7 +763,7 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
745763
void *disjoint_pool_malloc(void *pool, size_t size) {
746764
disjoint_pool_t *hPool = (disjoint_pool_t *)pool;
747765
void *ptr = disjoint_pool_allocate(hPool, size);
748-
766+
atomic_fetch_add(&counter_alloc_balance, 1);
749767
return ptr;
750768
}
751769

@@ -779,6 +797,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
779797
}
780798

781799
if (alignment <= 1) {
800+
atomic_fetch_add(&counter_alloc_balance, 1);
782801
return disjoint_pool_allocate(pool, size);
783802
}
784803

@@ -809,6 +828,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
809828

810829
assert(ptr);
811830
utils_annotate_memory_undefined(ptr, size);
831+
atomic_fetch_add(&counter_alloc_balance, 1);
812832
return ptr;
813833
}
814834

@@ -847,6 +867,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
847867
(from_pool ? "pool" : "provider"), ptr);
848868
}
849869

870+
atomic_fetch_add(&counter_alloc_balance, 1);
850871
return aligned_ptr;
851872
}
852873

@@ -914,6 +935,8 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
914935
if (ret != UMF_RESULT_SUCCESS) {
915936
TLS_last_allocation_error = ret;
916937
LOG_ERR("deallocation from the memory provider failed");
938+
} else {
939+
atomic_fetch_sub(&counter_alloc_balance, 1);
917940
}
918941

919942
return ret;
@@ -958,6 +981,7 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
958981
disjoint_pool->params.cur_pool_size);
959982
}
960983

984+
atomic_fetch_sub(&counter_alloc_balance, 1);
961985
return UMF_RESULT_SUCCESS;
962986
}
963987

test/pools/disjoint_pool_ctl.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,60 @@ class ProviderWrapper {
8585
void *m_params;
8686
};
8787

88+
TEST_F(test, disjointCtlAllocationBalance) {
89+
umf_os_memory_provider_params_handle_t os_memory_provider_params = nullptr;
90+
if (UMF_RESULT_ERROR_NOT_SUPPORTED ==
91+
umfOsMemoryProviderParamsCreate(&os_memory_provider_params)) {
92+
GTEST_SKIP() << "OS memory provider is not supported!";
93+
}
94+
95+
ProviderWrapper providerWrapper(umfOsMemoryProviderOps(),
96+
os_memory_provider_params);
97+
if (providerWrapper.get() == NULL) {
98+
GTEST_SKIP() << "OS memory provider is not supported!";
99+
}
100+
101+
umf_disjoint_pool_params_handle_t params = nullptr;
102+
ASSERT_SUCCESS(umfDisjointPoolParamsCreate(&params));
103+
PoolWrapper poolWrapper(providerWrapper.get(), umfDisjointPoolOps(),
104+
params);
105+
106+
// Check that the allocation balance is zero
107+
int allocation_balance = 0;
108+
ASSERT_SUCCESS(umfCtlGet("umf.pool.by_handle.disjoint.allocation_balance",
109+
poolWrapper.get(), &allocation_balance, sizeof(allocation_balance)));
110+
ASSERT_EQ(allocation_balance, 0);
111+
112+
// Allocate some memory from the pool
113+
size_t allocation_size = 1024; // 1 KB
114+
const int max_allocations = 10;
115+
void *ptr[max_allocations] = {nullptr};
116+
int i = 0;
117+
while (i < max_allocations) {
118+
ptr[i] = umfPoolMalloc(poolWrapper.get(), allocation_size);
119+
ASSERT_NE(ptr[i], nullptr);
120+
++i;
121+
}
122+
123+
// Check the allocation balance after allocations
124+
ASSERT_SUCCESS(umfCtlGet("umf.pool.by_handle.disjoint.allocation_balance",
125+
poolWrapper.get(), &allocation_balance, sizeof(allocation_balance)));
126+
ASSERT_EQ(allocation_balance, i);
127+
128+
// Check balance after freeing the allocations
129+
for (int j = 0; j < max_allocations; ++j) {
130+
if (ptr[j]) {
131+
umfPoolFree(poolWrapper.get(), ptr[j]);
132+
}
133+
}
134+
ASSERT_SUCCESS(umfCtlGet("umf.pool.by_handle.disjoint.allocation_balance",
135+
poolWrapper.get(), &allocation_balance, sizeof(allocation_balance)));
136+
ASSERT_EQ(allocation_balance, 0);
137+
138+
ASSERT_SUCCESS(umfDisjointPoolParamsDestroy(params));
139+
ASSERT_SUCCESS(umfOsMemoryProviderParamsDestroy(os_memory_provider_params));
140+
}
141+
88142
TEST_F(test, disjointCtlName) {
89143
umf_os_memory_provider_params_handle_t os_memory_provider_params = nullptr;
90144
if (UMF_RESULT_ERROR_NOT_SUPPORTED ==

0 commit comments

Comments
 (0)