Skip to content

Commit 163421a

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

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ if(NOT WINDOWS)
490490
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${KNOWN_BUILD_TYPES})
491491
endif()
492492

493+
set(CMAKE_C_STANDARD 11)
494+
493495
# For using the options listed in the OPTIONS_REQUIRING_CXX variable a C++17
494496
# compiler is required. Moreover, if these options are not set, CMake will set
495497
# up a strict C build, without C++ support.

src/pool/pool_disjoint.c

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

7287
static void initialize_disjoint_ctl(void) {
88+
atomic_init(&counter_alloc_balance, 0);
7389
CTL_REGISTER_MODULE(&disjoint_ctl_root, disjoint);
7490
}
7591

@@ -745,7 +761,7 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
745761
void *disjoint_pool_malloc(void *pool, size_t size) {
746762
disjoint_pool_t *hPool = (disjoint_pool_t *)pool;
747763
void *ptr = disjoint_pool_allocate(hPool, size);
748-
764+
atomic_fetch_add(&counter_alloc_balance, 1);
749765
return ptr;
750766
}
751767

@@ -779,6 +795,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
779795
}
780796

781797
if (alignment <= 1) {
798+
atomic_fetch_add(&counter_alloc_balance, 1);
782799
return disjoint_pool_allocate(pool, size);
783800
}
784801

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

810827
assert(ptr);
811828
utils_annotate_memory_undefined(ptr, size);
829+
atomic_fetch_add(&counter_alloc_balance, 1);
812830
return ptr;
813831
}
814832

@@ -847,6 +865,7 @@ void *disjoint_pool_aligned_malloc(void *pool, size_t size, size_t alignment) {
847865
(from_pool ? "pool" : "provider"), ptr);
848866
}
849867

868+
atomic_fetch_add(&counter_alloc_balance, 1);
850869
return aligned_ptr;
851870
}
852871

@@ -914,6 +933,8 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
914933
if (ret != UMF_RESULT_SUCCESS) {
915934
TLS_last_allocation_error = ret;
916935
LOG_ERR("deallocation from the memory provider failed");
936+
} else {
937+
atomic_fetch_sub(&counter_alloc_balance, 1);
917938
}
918939

919940
return ret;
@@ -958,6 +979,7 @@ umf_result_t disjoint_pool_free(void *pool, void *ptr) {
958979
disjoint_pool->params.cur_pool_size);
959980
}
960981

982+
atomic_fetch_sub(&counter_alloc_balance, 1);
961983
return UMF_RESULT_SUCCESS;
962984
}
963985

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)