Skip to content

Commit 9c35d11

Browse files
authored
Merge pull request #1441 from lplewa/dj_stats
[disjoint] Add CTL memory used/reserved metrics
2 parents f38a42d + b24d8e9 commit 9c35d11

File tree

3 files changed

+355
-8
lines changed

3 files changed

+355
-8
lines changed

src/pool/pool_disjoint.c

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ static char *DEFAULT_NAME = "disjoint";
3333
struct ctl disjoint_ctl_root;
3434
static UTIL_ONCE_FLAG ctl_initialized = UTIL_ONCE_FLAG_INIT;
3535

36+
// Disable name ctl for 1.0 release
37+
#if 0
3638
static umf_result_t CTL_READ_HANDLER(name)(void *ctx,
3739
umf_ctl_query_source_t source,
3840
void *arg, size_t size,
@@ -70,12 +72,88 @@ static umf_result_t CTL_WRITE_HANDLER(name)(void *ctx,
7072

7173
return UMF_RESULT_SUCCESS;
7274
}
75+
#endif
76+
static umf_result_t
77+
CTL_READ_HANDLER(used_memory)(void *ctx, umf_ctl_query_source_t source,
78+
void *arg, size_t size,
79+
umf_ctl_index_utlist_t *indexes) {
80+
(void)source, (void)indexes;
81+
disjoint_pool_t *pool = (disjoint_pool_t *)ctx;
82+
83+
if (arg == NULL || size != sizeof(size_t)) {
84+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
85+
}
86+
87+
size_t used_memory = 0;
88+
89+
// Calculate used memory across all buckets
90+
for (size_t i = 0; i < pool->buckets_num; i++) {
91+
bucket_t *bucket = pool->buckets[i];
92+
utils_mutex_lock(&bucket->bucket_lock);
93+
94+
// Count allocated chunks in available slabs
95+
slab_list_item_t *it;
96+
for (it = bucket->available_slabs; it != NULL; it = it->next) {
97+
slab_t *slab = it->val;
98+
used_memory += slab->num_chunks_allocated * bucket->size;
99+
}
100+
101+
// Count allocated chunks in unavailable slabs (all chunks allocated)
102+
for (it = bucket->unavailable_slabs; it != NULL; it = it->next) {
103+
slab_t *slab = it->val;
104+
used_memory += slab->num_chunks_allocated * bucket->size;
105+
}
106+
107+
utils_mutex_unlock(&bucket->bucket_lock);
108+
}
109+
110+
*(size_t *)arg = used_memory;
111+
return UMF_RESULT_SUCCESS;
112+
}
113+
114+
static umf_result_t
115+
CTL_READ_HANDLER(reserved_memory)(void *ctx, umf_ctl_query_source_t source,
116+
void *arg, size_t size,
117+
umf_ctl_index_utlist_t *indexes) {
118+
(void)source, (void)indexes;
119+
disjoint_pool_t *pool = (disjoint_pool_t *)ctx;
120+
121+
if (arg == NULL || size != sizeof(size_t)) {
122+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
123+
}
124+
125+
size_t reserved_memory = 0;
126+
127+
// Calculate reserved memory across all buckets
128+
for (size_t i = 0; i < pool->buckets_num; i++) {
129+
bucket_t *bucket = pool->buckets[i];
130+
utils_mutex_lock(&bucket->bucket_lock);
131+
132+
// Count all slabs (both available and unavailable)
133+
slab_list_item_t *it;
134+
for (it = bucket->available_slabs; it != NULL; it = it->next) {
135+
slab_t *slab = it->val;
136+
reserved_memory += slab->slab_size;
137+
}
138+
139+
for (it = bucket->unavailable_slabs; it != NULL; it = it->next) {
140+
slab_t *slab = it->val;
141+
reserved_memory += slab->slab_size;
142+
}
143+
144+
utils_mutex_unlock(&bucket->bucket_lock);
145+
}
146+
147+
*(size_t *)arg = reserved_memory;
148+
return UMF_RESULT_SUCCESS;
149+
}
73150

74-
static const umf_ctl_node_t CTL_NODE(disjoint)[] = {CTL_LEAF_RW(name),
75-
CTL_NODE_END};
151+
static const umf_ctl_node_t CTL_NODE(stats)[] = {CTL_LEAF_RO(used_memory),
152+
CTL_LEAF_RO(reserved_memory)};
76153

77154
static void initialize_disjoint_ctl(void) {
78-
CTL_REGISTER_MODULE(&disjoint_ctl_root, disjoint);
155+
CTL_REGISTER_MODULE(&disjoint_ctl_root, stats);
156+
// CTL_REGISTER_MODULE(&disjoint_ctl_root, name);
79157
}
80158

81159
umf_result_t disjoint_pool_ctl(void *hPool,

test/ctl/ctl_api.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ TEST_F(CtlTest, ctlDefaultPoolOverwrite) {
288288
ASSERT_EQ(std::string(output), values.back());
289289
}
290290

291-
TEST_F(CtlTest, ctlNameValidation) {
291+
TEST_F(CtlTest, DISABLED_ctlNameValidation) {
292292
std::string name = "umf.pool.default.disjoint.name";
293293
std::string value = "new_disjoint_pool_name";
294294
umf_disjoint_pool_params_handle_t params = NULL;
@@ -311,7 +311,7 @@ TEST_F(CtlTest, ctlNameValidation) {
311311
p.freeResources();
312312
}
313313

314-
TEST_F(CtlTest, ctlSizeValidation) {
314+
TEST_F(CtlTest, DISABLED_ctlSizeValidation) {
315315
std::string name = "umf.pool.default.disjoint.name";
316316
std::string value = "1234567890";
317317
umf_disjoint_pool_params_handle_t params = NULL;
@@ -340,7 +340,7 @@ TEST_F(CtlTest, ctlSizeValidation) {
340340
p.freeResources();
341341
}
342342

343-
TEST_F(CtlTest, ctlExecInvalidSize) {
343+
TEST_F(CtlTest, DISABLED_ctlExecInvalidSize) {
344344
std::string name = "umf.pool.default.disjoint.name";
345345
ASSERT_EQ(umfCtlSet(name.c_str(), (void *)"test_value", 0),
346346
UMF_RESULT_ERROR_INVALID_ARGUMENT);

0 commit comments

Comments
 (0)