Skip to content

Commit 25db75b

Browse files
committed
add ctl to fixed provider
1 parent d4c2672 commit 25db75b

File tree

5 files changed

+204
-103
lines changed

5 files changed

+204
-103
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#ifndef UMF_PROVIDER_CTL_STATS_IMPL_H
9+
#define UMF_PROVIDER_CTL_STATS_IMPL_H 1
10+
#else
11+
#error __FILE__"should not be included more than once"
12+
#endif
13+
14+
#ifndef CTL_PROVIDER_TYPE
15+
#error "CTL_PROVIDER_TYPE must be defined"
16+
#endif
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
#include "ctl/ctl.h"
23+
#include <utils/utils_assert.h>
24+
25+
static int CTL_READ_HANDLER(peak_memory)(void *ctx,
26+
umf_ctl_query_source_t source,
27+
void *arg,
28+
umf_ctl_index_utlist_t *indexes,
29+
const char *extra_name,
30+
umf_ctl_query_type_t query_type) {
31+
/* suppress unused-parameter errors */
32+
(void)source, (void)indexes, (void)ctx, (void)extra_name, (void)query_type;
33+
34+
size_t *arg_out = arg;
35+
CTL_PROVIDER_TYPE *provider = (CTL_PROVIDER_TYPE *)ctx;
36+
COMPILE_ERROR_ON(sizeof(provider->stats.peak_memory) != sizeof(uint64_t));
37+
utils_atomic_load_acquire_u64((uint64_t *)&provider->stats.peak_memory,
38+
(uint64_t *)arg_out);
39+
return 0;
40+
}
41+
42+
static int CTL_READ_HANDLER(allocated_memory)(void *ctx,
43+
umf_ctl_query_source_t source,
44+
void *arg,
45+
umf_ctl_index_utlist_t *indexes,
46+
const char *extra_name,
47+
umf_ctl_query_type_t query_type) {
48+
/* suppress unused-parameter errors */
49+
(void)source, (void)indexes, (void)ctx, (void)extra_name, (void)query_type;
50+
51+
size_t *arg_out = arg;
52+
CTL_PROVIDER_TYPE *provider = (CTL_PROVIDER_TYPE *)ctx;
53+
COMPILE_ERROR_ON(sizeof(provider->stats.allocated_memory) !=
54+
sizeof(uint64_t));
55+
COMPILE_ERROR_ON(sizeof(*arg_out) != sizeof(uint64_t));
56+
utils_atomic_load_acquire_u64((uint64_t *)&provider->stats.allocated_memory,
57+
(uint64_t *)arg_out);
58+
return 0;
59+
}
60+
61+
static int CTL_RUNNABLE_HANDLER(reset)(void *ctx, umf_ctl_query_source_t source,
62+
void *arg,
63+
umf_ctl_index_utlist_t *indexes,
64+
const char *extra_name,
65+
umf_ctl_query_type_t query_type) {
66+
/* suppress unused-parameter errors */
67+
(void)source, (void)indexes, (void)arg, (void)extra_name, (void)query_type;
68+
69+
CTL_PROVIDER_TYPE *provider = (CTL_PROVIDER_TYPE *)ctx;
70+
size_t allocated;
71+
72+
COMPILE_ERROR_ON(sizeof(provider->stats.allocated_memory) !=
73+
sizeof(uint64_t));
74+
COMPILE_ERROR_ON(sizeof(allocated) != sizeof(uint64_t));
75+
76+
utils_atomic_load_acquire_u64((uint64_t *)&provider->stats.allocated_memory,
77+
(uint64_t *)&allocated);
78+
utils_atomic_store_release_u64((uint64_t *)&provider->stats.peak_memory,
79+
(uint64_t)allocated);
80+
81+
return 0;
82+
}
83+
84+
static const umf_ctl_node_t CTL_NODE(stats)[] = {
85+
CTL_LEAF_RO(allocated_memory), CTL_LEAF_RO(peak_memory),
86+
CTL_LEAF_RUNNABLE(reset), CTL_NODE_END};
87+
88+
static inline void provider_ctl_stats_alloc(CTL_PROVIDER_TYPE *provider,
89+
size_t size) {
90+
91+
COMPILE_ERROR_ON(sizeof(provider->stats.allocated_memory) !=
92+
sizeof(uint64_t));
93+
COMPILE_ERROR_ON(sizeof(provider->stats.peak_memory) != sizeof(uint64_t));
94+
COMPILE_ERROR_ON(sizeof(size) != sizeof(uint64_t));
95+
// TODO: Change to memory_order_relaxed when we will have a proper wrapper
96+
size_t allocated =
97+
utils_fetch_and_add_u64((uint64_t *)&provider->stats.allocated_memory,
98+
(uint64_t)size) +
99+
size;
100+
101+
uint64_t peak;
102+
utils_atomic_load_acquire_u64((uint64_t *)&provider->stats.peak_memory,
103+
&peak);
104+
105+
while (allocated > peak &&
106+
!utils_compare_exchange_u64((uint64_t *)&provider->stats.peak_memory,
107+
&peak, (uint64_t *)&allocated)) {
108+
/* If the compare-exchange fails, 'peak' is updated to the current value of peak_memory.
109+
We then re-check whether allocated is still greater than the updated peak value. */
110+
;
111+
}
112+
}
113+
114+
static inline void provider_ctl_stats_free(CTL_PROVIDER_TYPE *provider,
115+
size_t size) {
116+
117+
COMPILE_ERROR_ON(sizeof(size) != sizeof(uint64_t));
118+
COMPILE_ERROR_ON(sizeof(provider->stats.allocated_memory) !=
119+
sizeof(uint64_t));
120+
121+
// TODO: Change it to memory_order_relaxed when we will have a proper wrapper
122+
utils_fetch_and_sub_u64((uint64_t *)&provider->stats.allocated_memory,
123+
size);
124+
}
125+
126+
#ifdef __cplusplus
127+
}
128+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2025 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#ifndef UMF_PROVIDER_CTL_STATS_TYPE_H
9+
#define UMF_PROVIDER_CTL_STATS_TYPE_H 1
10+
11+
#include <stddef.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
typedef struct ctl_stats_t {
18+
size_t allocated_memory;
19+
size_t peak_memory;
20+
} ctl_stats_t;
21+
22+
#define CTL_STATS ctl_stats_t stats
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif
27+
#endif

src/provider/provider_fixed_memory.c

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "base_alloc_global.h"
2121
#include "coarse.h"
2222
#include "libumf.h"
23+
#include "provider_ctl_stats_type.h"
2324
#include "utils_common.h"
2425
#include "utils_concurrency.h"
2526
#include "utils_log.h"
@@ -30,6 +31,7 @@ typedef struct fixed_memory_provider_t {
3031
void *base; // base address of memory
3132
size_t size; // size of the memory region
3233
coarse_t *coarse; // coarse library handle
34+
CTL_STATS;
3335
} fixed_memory_provider_t;
3436

3537
// Fixed Memory provider settings struct
@@ -52,6 +54,16 @@ static __TLS fixed_last_native_error_t TLS_last_native_error;
5254
#define _UMF_FIXED_RESULT_ERROR_PURGE_FORCE_FAILED \
5355
(UMF_FIXED_RESULT_ERROR_PURGE_FORCE_FAILED - UMF_FIXED_RESULT_SUCCESS)
5456

57+
#define CTL_PROVIDER_TYPE fixed_memory_provider_t
58+
#include "provider_ctl_stats_impl.h"
59+
struct ctl *fixed_memory_ctl_root;
60+
static UTIL_ONCE_FLAG ctl_initialized = UTIL_ONCE_FLAG_INIT;
61+
62+
static void initialize_fixed_ctl(void) {
63+
fixed_memory_ctl_root = ctl_new();
64+
CTL_REGISTER_MODULE(fixed_memory_ctl_root, stats);
65+
}
66+
5567
static const char *Native_error_str[] = {
5668
[_UMF_FIXED_RESULT_SUCCESS] = "success",
5769
[_UMF_FIXED_RESULT_ERROR_PURGE_FORCE_FAILED] = "force purging failed"};
@@ -153,7 +165,14 @@ static umf_result_t fixed_alloc(void *provider, size_t size, size_t alignment,
153165
fixed_memory_provider_t *fixed_provider =
154166
(fixed_memory_provider_t *)provider;
155167

156-
return coarse_alloc(fixed_provider->coarse, size, alignment, resultPtr);
168+
umf_result_t ret =
169+
coarse_alloc(fixed_provider->coarse, size, alignment, resultPtr);
170+
171+
if (ret == UMF_RESULT_SUCCESS) {
172+
provider_ctl_stats_alloc(fixed_provider, size);
173+
}
174+
175+
return ret;
157176
}
158177

159178
static void fixed_get_last_native_error(void *provider, const char **ppMessage,
@@ -250,7 +269,23 @@ static umf_result_t fixed_allocation_merge(void *provider, void *lowPtr,
250269
static umf_result_t fixed_free(void *provider, void *ptr, size_t size) {
251270
fixed_memory_provider_t *fixed_provider =
252271
(fixed_memory_provider_t *)provider;
253-
return coarse_free(fixed_provider->coarse, ptr, size);
272+
273+
umf_result_t ret = coarse_free(fixed_provider->coarse, ptr, size);
274+
275+
if (ret == UMF_RESULT_SUCCESS) {
276+
provider_ctl_stats_free(fixed_provider, size);
277+
}
278+
279+
return ret;
280+
}
281+
282+
static umf_result_t fixed_ctl(void *hProvider, int operationType,
283+
const char *name, void *arg,
284+
umf_ctl_query_type_t query_type) {
285+
(void)operationType; // unused
286+
utils_init_once(&ctl_initialized, initialize_fixed_ctl);
287+
return ctl_query(fixed_memory_ctl_root, hProvider, CTL_QUERY_PROGRAMMATIC,
288+
name, query_type, arg);
254289
}
255290

256291
static umf_memory_provider_ops_t UMF_FIXED_MEMORY_PROVIDER_OPS = {
@@ -271,7 +306,8 @@ static umf_memory_provider_ops_t UMF_FIXED_MEMORY_PROVIDER_OPS = {
271306
.ipc.get_ipc_handle = NULL,
272307
.ipc.put_ipc_handle = NULL,
273308
.ipc.open_ipc_handle = NULL,
274-
.ipc.close_ipc_handle = NULL};
309+
.ipc.close_ipc_handle = NULL,
310+
.ctl = fixed_ctl};
275311

276312
umf_memory_provider_ops_t *umfFixedMemoryProviderOps(void) {
277313
return &UMF_FIXED_MEMORY_PROVIDER_OPS;

src/provider/provider_os_memory.c

Lines changed: 6 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ umf_result_t umfOsMemoryProviderParamsSetPartitions(
102102
#include "utils_concurrency.h"
103103
#include "utils_log.h"
104104

105+
#define CTL_PROVIDER_TYPE os_memory_provider_t
106+
#include "provider_ctl_stats_impl.h"
107+
105108
#define NODESET_STR_BUF_LEN 1024
106109

107110
#define TLS_MSG_BUF_LEN 1024
@@ -189,70 +192,6 @@ static int CTL_READ_HANDLER(ipc_enabled)(void *ctx,
189192
return 0;
190193
}
191194

192-
static int CTL_READ_HANDLER(peak_memory)(void *ctx,
193-
umf_ctl_query_source_t source,
194-
void *arg,
195-
umf_ctl_index_utlist_t *indexes,
196-
const char *extra_name,
197-
umf_ctl_query_type_t query_type) {
198-
/* suppress unused-parameter errors */
199-
(void)source, (void)indexes, (void)ctx, (void)extra_name, (void)query_type;
200-
201-
size_t *arg_out = arg;
202-
os_memory_provider_t *os_provider = (os_memory_provider_t *)ctx;
203-
COMPILE_ERROR_ON(sizeof(os_provider->stats.peak_memory) !=
204-
sizeof(uint64_t));
205-
utils_atomic_load_acquire_u64((uint64_t *)&os_provider->stats.peak_memory,
206-
(uint64_t *)arg_out);
207-
return 0;
208-
}
209-
210-
static int CTL_READ_HANDLER(allocated_memory)(void *ctx,
211-
umf_ctl_query_source_t source,
212-
void *arg,
213-
umf_ctl_index_utlist_t *indexes,
214-
const char *extra_name,
215-
umf_ctl_query_type_t query_type) {
216-
/* suppress unused-parameter errors */
217-
(void)source, (void)indexes, (void)ctx, (void)extra_name, (void)query_type;
218-
219-
size_t *arg_out = arg;
220-
os_memory_provider_t *os_provider = (os_memory_provider_t *)ctx;
221-
COMPILE_ERROR_ON(sizeof(os_provider->stats.allocated_memory) !=
222-
sizeof(uint64_t));
223-
COMPILE_ERROR_ON(sizeof(*arg_out) != sizeof(uint64_t));
224-
utils_atomic_load_acquire_u64(
225-
(uint64_t *)&os_provider->stats.allocated_memory, (uint64_t *)arg_out);
226-
return 0;
227-
}
228-
229-
static int CTL_RUNNABLE_HANDLER(reset)(void *ctx, umf_ctl_query_source_t source,
230-
void *arg,
231-
umf_ctl_index_utlist_t *indexes,
232-
const char *extra_name,
233-
umf_ctl_query_type_t query_type) {
234-
/* suppress unused-parameter errors */
235-
(void)source, (void)indexes, (void)arg, (void)extra_name, (void)query_type;
236-
237-
os_memory_provider_t *os_provider = (os_memory_provider_t *)ctx;
238-
size_t allocated;
239-
240-
COMPILE_ERROR_ON(sizeof(os_provider->stats.allocated_memory) !=
241-
sizeof(uint64_t));
242-
COMPILE_ERROR_ON(sizeof(allocated) != sizeof(uint64_t));
243-
244-
utils_atomic_load_acquire_u64(
245-
(uint64_t *)&os_provider->stats.allocated_memory,
246-
(uint64_t *)&allocated);
247-
utils_atomic_store_release_u64((uint64_t *)&os_provider->stats.peak_memory,
248-
(uint64_t)allocated);
249-
250-
return 0;
251-
}
252-
static const umf_ctl_node_t CTL_NODE(stats)[] = {
253-
CTL_LEAF_RO(allocated_memory), CTL_LEAF_RO(peak_memory),
254-
CTL_LEAF_RUNNABLE(reset), CTL_NODE_END};
255-
256195
static const umf_ctl_node_t CTL_NODE(params)[] = {CTL_LEAF_RO(ipc_enabled),
257196
CTL_NODE_END};
258197

@@ -1176,29 +1115,7 @@ static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,
11761115

11771116
*resultPtr = addr;
11781117

1179-
COMPILE_ERROR_ON(sizeof(os_provider->stats.allocated_memory) !=
1180-
sizeof(uint64_t));
1181-
COMPILE_ERROR_ON(sizeof(os_provider->stats.peak_memory) !=
1182-
sizeof(uint64_t));
1183-
COMPILE_ERROR_ON(sizeof(size) != sizeof(uint64_t));
1184-
// TODO: Change to memory_order_relaxed when we will have a proper wrapper
1185-
size_t allocated =
1186-
utils_fetch_and_add_u64(
1187-
(uint64_t *)&os_provider->stats.allocated_memory, (uint64_t)size) +
1188-
size;
1189-
1190-
uint64_t peak;
1191-
utils_atomic_load_acquire_u64((uint64_t *)&os_provider->stats.peak_memory,
1192-
&peak);
1193-
1194-
while (allocated > peak && !utils_compare_exchange_u64(
1195-
(uint64_t *)&os_provider->stats.peak_memory,
1196-
&peak, (uint64_t *)&allocated)) {
1197-
/* If the compare-exchange fails, 'peak' is updated to the current value of peak_memory.
1198-
We then re-check whether allocated is still greater than the updated peak value. */
1199-
;
1200-
}
1201-
1118+
provider_ctl_stats_alloc(os_provider, size);
12021119
return UMF_RESULT_SUCCESS;
12031120

12041121
err_unmap:
@@ -1226,13 +1143,7 @@ static umf_result_t os_free(void *provider, void *ptr, size_t size) {
12261143
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
12271144
}
12281145

1229-
COMPILE_ERROR_ON(sizeof(size) != sizeof(uint64_t));
1230-
COMPILE_ERROR_ON(sizeof(os_provider->stats.allocated_memory) !=
1231-
sizeof(uint64_t));
1232-
1233-
// TODO: Change it to memory_order_relaxed when we will have a proper wrapper
1234-
utils_fetch_and_sub_u64((uint64_t *)&os_provider->stats.allocated_memory,
1235-
size);
1146+
provider_ctl_stats_free(os_provider, size);
12361147

12371148
return UMF_RESULT_SUCCESS;
12381149
}
@@ -1531,9 +1442,8 @@ static umf_result_t os_close_ipc_handle(void *provider, void *ptr,
15311442
static umf_result_t os_ctl(void *hProvider, int operationType, const char *name,
15321443
void *arg, umf_ctl_query_type_t query_type) {
15331444
(void)operationType; // unused
1534-
os_memory_provider_t *os_provider = (os_memory_provider_t *)hProvider;
15351445
utils_init_once(&ctl_initialized, initialize_os_ctl);
1536-
return ctl_query(os_memory_ctl_root, os_provider, CTL_QUERY_PROGRAMMATIC,
1446+
return ctl_query(os_memory_ctl_root, hProvider, CTL_QUERY_PROGRAMMATIC,
15371447
name, query_type, arg);
15381448
}
15391449

0 commit comments

Comments
 (0)