Skip to content

Commit 10f9451

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

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

src/pool/pool_disjoint.c

Lines changed: 26 additions & 3 deletions
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,25 @@ static int CTL_WRITE_HANDLER(name)(void *ctx, umf_ctl_query_source_t source,
6668
return 0;
6769
}
6870

69-
static const umf_ctl_node_t CTL_NODE(disjoint)[] = {CTL_LEAF_RW(name),
70-
CTL_NODE_END};
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)(
76+
void *ctx, umf_ctl_query_source_t source, void *arg, size_t size,
77+
umf_ctl_index_utlist_t *indexes, const char *extra_name,
78+
umf_ctl_query_type_t queryType) {
79+
(void)ctx, (void)source, (void)size, (void)indexes, (void)extra_name,
80+
(void)queryType;
81+
*(int *)arg = atomic_load(&counter_alloc_balance);
82+
return 0;
83+
}
84+
85+
static const umf_ctl_node_t CTL_NODE(disjoint)[] = {
86+
CTL_LEAF_RW(name), CTL_LEAF_RO(allocation_balance), CTL_NODE_END};
7187

7288
static void initialize_disjoint_ctl(void) {
89+
atomic_init(&counter_alloc_balance, 0);
7390
CTL_REGISTER_MODULE(&disjoint_ctl_root, disjoint);
7491
}
7592

@@ -745,7 +762,7 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
745762
void *disjoint_pool_malloc(void *pool, size_t size) {
746763
disjoint_pool_t *hPool = (disjoint_pool_t *)pool;
747764
void *ptr = disjoint_pool_allocate(hPool, size);
748-
765+
atomic_fetch_add(&counter_alloc_balance, 1);
749766
return ptr;
750767
}
751768

@@ -779,6 +796,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
779796
}
780797

781798
if (alignment <= 1) {
799+
atomic_fetch_add(&counter_alloc_balance, 1);
782800
return disjoint_pool_allocate(pool, size);
783801
}
784802

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

810828
assert(ptr);
811829
utils_annotate_memory_undefined(ptr, size);
830+
atomic_fetch_add(&counter_alloc_balance, 1);
812831
return ptr;
813832
}
814833

@@ -847,6 +866,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
847866
(from_pool ? "pool" : "provider"), ptr);
848867
}
849868

869+
atomic_fetch_add(&counter_alloc_balance, 1);
850870
return aligned_ptr;
851871
}
852872

@@ -914,6 +934,8 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
914934
if (ret != UMF_RESULT_SUCCESS) {
915935
TLS_last_allocation_error = ret;
916936
LOG_ERR("deallocation from the memory provider failed");
937+
} else {
938+
atomic_fetch_sub(&counter_alloc_balance, 1);
917939
}
918940

919941
return ret;
@@ -958,6 +980,7 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
958980
disjoint_pool->params.cur_pool_size);
959981
}
960982

983+
atomic_fetch_sub(&counter_alloc_balance, 1);
961984
return UMF_RESULT_SUCCESS;
962985
}
963986

test/pools/disjoint_pool_ctl.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,63 @@ 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,
110+
sizeof(allocation_balance)));
111+
ASSERT_EQ(allocation_balance, 0);
112+
113+
// Allocate some memory from the pool
114+
size_t allocation_size = 1024; // 1 KB
115+
const int max_allocations = 10;
116+
void *ptr[max_allocations] = {nullptr};
117+
int i = 0;
118+
while (i < max_allocations) {
119+
ptr[i] = umfPoolMalloc(poolWrapper.get(), allocation_size);
120+
ASSERT_NE(ptr[i], nullptr);
121+
++i;
122+
}
123+
124+
// Check the allocation balance after allocations
125+
ASSERT_SUCCESS(umfCtlGet("umf.pool.by_handle.disjoint.allocation_balance",
126+
poolWrapper.get(), &allocation_balance,
127+
sizeof(allocation_balance)));
128+
ASSERT_EQ(allocation_balance, i);
129+
130+
// Check balance after freeing the allocations
131+
for (int j = 0; j < max_allocations; ++j) {
132+
if (ptr[j]) {
133+
umfPoolFree(poolWrapper.get(), ptr[j]);
134+
}
135+
}
136+
ASSERT_SUCCESS(umfCtlGet("umf.pool.by_handle.disjoint.allocation_balance",
137+
poolWrapper.get(), &allocation_balance,
138+
sizeof(allocation_balance)));
139+
ASSERT_EQ(allocation_balance, 0);
140+
141+
ASSERT_SUCCESS(umfDisjointPoolParamsDestroy(params));
142+
ASSERT_SUCCESS(umfOsMemoryProviderParamsDestroy(os_memory_provider_params));
143+
}
144+
88145
TEST_F(test, disjointCtlName) {
89146
umf_os_memory_provider_params_handle_t os_memory_provider_params = nullptr;
90147
if (UMF_RESULT_ERROR_NOT_SUPPORTED ==

0 commit comments

Comments
 (0)