Skip to content

Commit 18c4254

Browse files
committed
[CTL] Add CTL by handle
1 parent e31c856 commit 18c4254

File tree

11 files changed

+239
-43
lines changed

11 files changed

+239
-43
lines changed

include/umf/base.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ typedef enum umf_result_t {
4848
UMF_RESULT_ERROR_UNKNOWN = 0x7ffffffe ///< Unknown or internal error
4949
} umf_result_t;
5050

51+
/// @brief Type for determine the type of the CTL query
52+
enum ctl_query_type {
53+
CTL_QUERY_READ,
54+
CTL_QUERY_WRITE,
55+
CTL_QUERY_RUNNABLE,
56+
CTL_QUERY_SUBTREE,
57+
58+
MAX_CTL_QUERY_TYPE
59+
};
60+
5161
#ifdef __cplusplus
5262
}
5363
#endif

include/umf/memory_provider_ops.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/*
22
*
3-
* Copyright (C) 2023-2024 Intel Corporation
3+
* Copyright (C) 2023-2025 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
77
*
88
*/
9-
109
#ifndef UMF_MEMORY_PROVIDER_OPS_H
1110
#define UMF_MEMORY_PROVIDER_OPS_H 1
1211

@@ -78,6 +77,24 @@ typedef struct umf_memory_provider_ext_ops_t {
7877
umf_result_t (*allocation_split)(void *hProvider, void *ptr,
7978
size_t totalSize, size_t firstSize);
8079

80+
///
81+
/// @brief Control operation for the memory provider.
82+
/// The function is used to perform various control operations
83+
/// on the memory provider.
84+
///
85+
/// @param hProvider handle to the memory provider.
86+
/// @param operationType type of the operation to be performed.
87+
/// @param name name associated with the operation.
88+
/// @param arg argument for the operation.
89+
/// @param extra_name additional name associated with the operation.
90+
/// @param query_type type of the query to be performed.
91+
///
92+
/// @return umf_result_t result of the control operation.
93+
///
94+
umf_result_t (*ctl)(void *hProvider, int operationType, const char *name,
95+
void *arg, char *extra_name,
96+
enum ctl_query_type query_type);
97+
8198
} umf_memory_provider_ext_ops_t;
8299

83100
///

src/ctl/ctl.c

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#define CTL_QUERY_NODE_SEPARATOR "."
4444
#define CTL_VALUE_ARG_SEPARATOR ","
4545

46+
/* GLOBAL TREE */
4647
static int ctl_global_first_free = 0;
4748
static struct ctl_node CTL_NODE(global)[CTL_MAX_ENTRIES];
4849

@@ -102,6 +103,9 @@ static const struct ctl_node *ctl_find_node(const struct ctl_node *nodes,
102103
* in the main ctl tree.
103104
*/
104105
while (node_name != NULL) {
106+
if (n != NULL && n->type == CTL_NODE_SUBTREE) {
107+
break;
108+
}
105109
char *endptr;
106110
/*
107111
* Ignore errno from strtol: FreeBSD returns EINVAL if no
@@ -128,6 +132,7 @@ static const struct ctl_node *ctl_find_node(const struct ctl_node *nodes,
128132
break;
129133
}
130134
}
135+
131136
if (n->name == NULL) {
132137
goto error;
133138
}
@@ -244,21 +249,29 @@ static void ctl_query_cleanup_real_args(const struct ctl_node *n,
244249
*/
245250
static int ctl_exec_query_read(void *ctx, const struct ctl_node *n,
246251
enum ctl_query_source source, void *arg,
247-
struct ctl_index_utlist *indexes) {
252+
struct ctl_index_utlist *indexes,
253+
char *extra_name,
254+
enum ctl_query_type query_type) {
255+
(void)extra_name, (void)query_type;
248256
if (arg == NULL) {
249257
errno = EINVAL;
250258
return -1;
251259
}
252260

253-
return n->cb[CTL_QUERY_READ](ctx, source, arg, indexes);
261+
assert(MAX_CTL_QUERY_TYPE != query_type);
262+
return n->cb[CTL_QUERY_READ](ctx, source, arg, indexes, NULL,
263+
MAX_CTL_QUERY_TYPE);
254264
}
255265

256266
/*
257267
* ctl_exec_query_write -- (internal) calls the write callback of a node
258268
*/
259269
static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
260270
enum ctl_query_source source, void *arg,
261-
struct ctl_index_utlist *indexes) {
271+
struct ctl_index_utlist *indexes,
272+
char *extra_name,
273+
enum ctl_query_type query_type) {
274+
(void)extra_name, (void)query_type;
262275
if (arg == NULL) {
263276
errno = EINVAL;
264277
return -1;
@@ -269,7 +282,9 @@ static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
269282
return -1;
270283
}
271284

272-
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, indexes);
285+
assert(MAX_CTL_QUERY_TYPE != query_type);
286+
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, indexes, NULL,
287+
MAX_CTL_QUERY_TYPE);
273288
ctl_query_cleanup_real_args(n, real_arg, source);
274289

275290
return ret;
@@ -280,16 +295,32 @@ static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
280295
*/
281296
static int ctl_exec_query_runnable(void *ctx, const struct ctl_node *n,
282297
enum ctl_query_source source, void *arg,
283-
struct ctl_index_utlist *indexes) {
284-
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, indexes);
298+
struct ctl_index_utlist *indexes,
299+
char *extra_name,
300+
enum ctl_query_type query_type) {
301+
(void)extra_name, (void)query_type;
302+
assert(MAX_CTL_QUERY_TYPE != query_type);
303+
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, indexes, NULL,
304+
MAX_CTL_QUERY_TYPE);
305+
}
306+
307+
static int ctl_exec_query_subtree(void *ctx, const struct ctl_node *n,
308+
enum ctl_query_source source, void *arg,
309+
struct ctl_index_utlist *indexes,
310+
char *extra_name,
311+
enum ctl_query_type query_type) {
312+
return n->cb[CTL_QUERY_SUBTREE](ctx, source, arg, indexes, extra_name,
313+
query_type);
285314
}
286315

287316
static int (*ctl_exec_query[MAX_CTL_QUERY_TYPE])(
288317
void *ctx, const struct ctl_node *n, enum ctl_query_source source,
289-
void *arg, struct ctl_index_utlist *indexes) = {
318+
void *arg, struct ctl_index_utlist *indexes, char *extra_name,
319+
enum ctl_query_type query_type) = {
290320
ctl_exec_query_read,
291321
ctl_exec_query_write,
292322
ctl_exec_query_runnable,
323+
ctl_exec_query_subtree,
293324
};
294325

295326
/*
@@ -324,13 +355,17 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
324355
n = ctl_find_node(ctl->root, name, indexes);
325356
}
326357

327-
if (n == NULL || n->type != CTL_NODE_LEAF || n->cb[type] == NULL) {
358+
if (n == NULL ||
359+
(n->type != CTL_NODE_LEAF && n->type != CTL_NODE_SUBTREE) ||
360+
n->cb[n->type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type] == NULL) {
328361
errno = EINVAL;
329362
goto out;
330363
}
331364

332-
ret = ctl_exec_query[type](ctx, n, source, arg, indexes);
333-
365+
char *extra_name = strstr(name, n->name);
366+
ret =
367+
ctl_exec_query[n->type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type](
368+
ctx, n, source, arg, indexes, extra_name, type);
334369
out:
335370
ctl_delete_indexes(indexes);
336371

@@ -594,3 +629,9 @@ int ctl_arg_string(const void *arg, void *dest, size_t dest_size) {
594629

595630
return 0;
596631
}
632+
633+
void umfCtlGet(const char *name, ...) { (void)name; }
634+
635+
void umfCtlSet(const char *name, ...) { (void)name; }
636+
637+
void umfCtlExec(const char *name, ...) { (void)name; }

src/ctl/ctl.h

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2016-2024 Intel Corporation
3+
* Copyright (C) 2016-2025 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -18,6 +18,8 @@
1818
#ifndef UMF_CTL_H
1919
#define UMF_CTL_H 1
2020

21+
#include <umf/memory_pool.h>
22+
2123
#include <errno.h>
2224
#include <stddef.h>
2325

@@ -43,22 +45,25 @@ enum ctl_query_source {
4345
MAX_CTL_QUERY_SOURCE
4446
};
4547

46-
enum ctl_query_type {
47-
CTL_QUERY_READ,
48-
CTL_QUERY_WRITE,
49-
CTL_QUERY_RUNNABLE,
48+
// enum ctl_query_type {
49+
// CTL_QUERY_READ,
50+
// CTL_QUERY_WRITE,
51+
// CTL_QUERY_RUNNABLE,
52+
// CTL_QUERY_SUBTREE,
5053

51-
MAX_CTL_QUERY_TYPE
52-
};
54+
// MAX_CTL_QUERY_TYPE
55+
// };
5356

5457
typedef int (*node_callback)(void *ctx, enum ctl_query_source type, void *arg,
55-
struct ctl_index_utlist *indexes);
58+
struct ctl_index_utlist *indexes, char *extra_name,
59+
enum ctl_query_type query_type);
5660

5761
enum ctl_node_type {
5862
CTL_NODE_UNKNOWN,
5963
CTL_NODE_NAMED,
6064
CTL_NODE_LEAF,
6165
CTL_NODE_INDEXED,
66+
CTL_NODE_SUBTREE,
6267

6368
MAX_CTL_NODE
6469
};
@@ -104,6 +109,8 @@ struct ctl_node {
104109
struct ctl *ctl_new(void);
105110
void ctl_delete(struct ctl *stats);
106111

112+
void initialize_global_ctl(void);
113+
107114
int ctl_load_config_from_string(struct ctl *ctl, void *ctx,
108115
const char *cfg_string);
109116
int ctl_load_config_from_file(struct ctl *ctl, void *ctx, const char *cfg_file);
@@ -161,6 +168,8 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
161168

162169
#define CTL_RUNNABLE_HANDLER(name, ...) ctl_##__VA_ARGS__##_##name##_runnable
163170

171+
#define CTL_SUBTREE_HANDLER(name, ...) ctl_##__VA_ARGS__##_##name##_subtree
172+
164173
#define CTL_ARG(name) ctl_arg_##name
165174

166175
/*
@@ -170,7 +179,8 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
170179
#define CTL_LEAF_RO(name, ...) \
171180
{ \
172181
CTL_STR(name), CTL_NODE_LEAF, \
173-
{CTL_READ_HANDLER(name, __VA_ARGS__), NULL, NULL}, NULL, NULL \
182+
{CTL_READ_HANDLER(name, __VA_ARGS__), NULL, NULL, NULL}, NULL, \
183+
NULL \
174184
}
175185

176186
/*
@@ -180,7 +190,7 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
180190
#define CTL_LEAF_WO(name, ...) \
181191
{ \
182192
CTL_STR(name), CTL_NODE_LEAF, \
183-
{NULL, CTL_WRITE_HANDLER(name, __VA_ARGS__), NULL}, \
193+
{NULL, CTL_WRITE_HANDLER(name, __VA_ARGS__), NULL, NULL}, \
184194
&CTL_ARG(name), NULL \
185195
}
186196

@@ -191,7 +201,22 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
191201
#define CTL_LEAF_RUNNABLE(name, ...) \
192202
{ \
193203
CTL_STR(name), CTL_NODE_LEAF, \
194-
{NULL, NULL, CTL_RUNNABLE_HANDLER(name, __VA_ARGS__)}, NULL, NULL \
204+
{NULL, NULL, CTL_RUNNABLE_HANDLER(name, __VA_ARGS__), NULL}, NULL, \
205+
NULL \
206+
}
207+
208+
#define CTL_LEAF_SUBTREE(name, ...) \
209+
{ \
210+
CTL_STR(name), CTL_NODE_SUBTREE, \
211+
{NULL, NULL, NULL, CTL_SUBTREE_HANDLER(name, __VA_ARGS__)}, NULL, \
212+
NULL \
213+
}
214+
215+
#define CTL_LEAF_SUBTREE2(name, fun, ...) \
216+
{ \
217+
CTL_STR(name), CTL_NODE_SUBTREE, \
218+
{NULL, NULL, NULL, CTL_SUBTREE_HANDLER(fun, __VA_ARGS__)}, NULL, \
219+
NULL \
195220
}
196221

197222
/*
@@ -201,7 +226,7 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
201226
#define CTL_LEAF_RW(name) \
202227
{ \
203228
CTL_STR(name), CTL_NODE_LEAF, \
204-
{CTL_READ_HANDLER(name), CTL_WRITE_HANDLER(name), NULL}, \
229+
{CTL_READ_HANDLER(name), CTL_WRITE_HANDLER(name), NULL, NULL}, \
205230
&CTL_ARG(name), NULL \
206231
}
207232

src/libumf.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2024 Intel Corporation
3+
* Copyright (C) 2024-2025 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -11,6 +11,7 @@
1111

1212
#include "base_alloc_global.h"
1313
#include "ipc_cache.h"
14+
#include "memory_provider_internal.h"
1415
#include "memspace_internal.h"
1516
#include "provider_tracking.h"
1617
#include "utils_common.h"
@@ -23,6 +24,10 @@ umf_memory_tracker_handle_t TRACKER = NULL;
2324

2425
static unsigned long long umfRefCount = 0;
2526

27+
static struct ctl_node CTL_NODE(umf)[] = {CTL_CHILD(provider), CTL_NODE_END};
28+
29+
void initialize_global_ctl(void) { CTL_REGISTER_MODULE(NULL, umf); }
30+
2631
int umfInit(void) {
2732
if (utils_fetch_and_add64(&umfRefCount, 1) == 0) {
2833
utils_log_init();
@@ -41,6 +46,7 @@ int umfInit(void) {
4146
}
4247

4348
LOG_DEBUG("UMF IPC cache initialized");
49+
initialize_global_ctl();
4450
}
4551

4652
if (TRACKER) {

src/memory_provider.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2023-2024 Intel Corporation
3+
* Copyright (C) 2023-2025 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -25,6 +25,22 @@ typedef struct umf_memory_provider_t {
2525
void *provider_priv;
2626
} umf_memory_provider_t;
2727

28+
static int CTL_SUBTREE_HANDLER(by_handle_provider)(
29+
void *ctx, enum ctl_query_source source, void *arg,
30+
struct ctl_index_utlist *indexes, char *extra_name,
31+
enum ctl_query_type query_type) {
32+
(void)indexes, (void)source;
33+
umf_memory_provider_handle_t hProvider = (umf_memory_provider_handle_t)ctx;
34+
// umf_result_t (*ctl)(void *hProvider, int operationType, const char *name,
35+
// void *arg, char *extra_name, enum ctl_query_type query_type);
36+
hProvider->ops.ext.ctl(hProvider->provider_priv, /*unused*/ 0,
37+
/*unused*/ NULL, arg, extra_name, query_type);
38+
return 0;
39+
}
40+
41+
struct ctl_node CTL_NODE(provider)[] = {
42+
CTL_LEAF_SUBTREE2(by_handle, by_handle_provider), CTL_NODE_END};
43+
2844
static umf_result_t umfDefaultPurgeLazy(void *provider, void *ptr,
2945
size_t size) {
3046
(void)provider;

src/memory_provider_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
#include <umf/memory_provider.h>
1616

17+
#include "ctl/ctl.h"
1718
#ifdef __cplusplus
1819
extern "C" {
1920
#endif
2021

2122
void *umfMemoryProviderGetPriv(umf_memory_provider_handle_t hProvider);
2223
umf_memory_provider_handle_t *umfGetLastFailedMemoryProviderPtr(void);
2324

25+
extern struct ctl_node CTL_NODE(provider)[];
2426
#ifdef __cplusplus
2527
}
2628
#endif

0 commit comments

Comments
 (0)