Skip to content

Commit ec65bcc

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

18 files changed

+408
-51
lines changed

include/umf/base.h

Lines changed: 24 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
@@ -48,6 +48,29 @@ 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+
typedef enum umf_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+
} umf_ctl_query_type;
60+
61+
// struct ctl;
62+
63+
// enum ctl_query_source;
64+
65+
// int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
66+
// const char *name, typedef enum umf_ctl_query_type type, void *arg);
67+
68+
int umfCtlGet(const char *name, void *ctx, void *arg);
69+
70+
// void umfCtlSet(const char *name, ...) { (void)name; }
71+
72+
// void umfCtlExec(const char *name, ...) { (void)name; }
73+
5174
#ifdef __cplusplus
5275
}
5376
#endif

include/umf/memory_pool_ops.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@
1717
extern "C" {
1818
#endif
1919

20+
/// @brief This structure comprises function pointers used by corresponding umfMemoryPool calls.
21+
typedef struct umf_memory_pool_ext_ops_t {
22+
///
23+
/// @brief Control operation for the memory provider.
24+
/// The function is used to perform various control operations
25+
/// on the memory provider.
26+
///
27+
/// @param hPool handle to the memory provider.
28+
/// @param operationType type of the operation to be performed.
29+
/// @param name name associated with the operation.
30+
/// @param arg argument for the operation.
31+
/// @param extra_name additional name associated with the operation.
32+
/// @param query_type type of the query to be performed.
33+
///
34+
/// @return umf_result_t result of the control operation.
35+
///
36+
umf_result_t (*ctl)(void *hPool, int operationType, const char *name,
37+
void *arg, umf_ctl_query_type query_type);
38+
} umf_memory_pool_ext_ops_t;
39+
2040
///
2141
/// @brief This structure comprises function pointers used by corresponding umfPool*
2242
/// calls. Each memory pool implementation should initialize all function
@@ -120,6 +140,11 @@ typedef struct umf_memory_pool_ops_t {
120140
/// The value is undefined if the previous allocation was successful.
121141
///
122142
umf_result_t (*get_last_allocation_error)(void *pool);
143+
144+
///
145+
/// @brief Optional ops
146+
///
147+
umf_memory_provider_ext_ops_t ext;
123148
} umf_memory_pool_ops_t;
124149

125150
#ifdef __cplusplus

include/umf/memory_provider_ops.h

Lines changed: 18 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,23 @@ 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, umf_ctl_query_type query_type);
96+
8197
} umf_memory_provider_ext_ops_t;
8298

8399
///

src/ctl/ctl.c

Lines changed: 51 additions & 11 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

@@ -78,6 +79,11 @@ char *Strdup(const char *s) {
7879
return p;
7980
}
8081

82+
int umfCtlGet(const char *name, void *ctx, void *arg) {
83+
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name, CTL_QUERY_READ,
84+
arg);
85+
}
86+
8187
/*
8288
* ctl_find_node -- (internal) searches for a matching entry point in the
8389
* provided nodes
@@ -102,6 +108,9 @@ static const struct ctl_node *ctl_find_node(const struct ctl_node *nodes,
102108
* in the main ctl tree.
103109
*/
104110
while (node_name != NULL) {
111+
if (n != NULL && n->type == CTL_NODE_SUBTREE) {
112+
break;
113+
}
105114
char *endptr;
106115
/*
107116
* Ignore errno from strtol: FreeBSD returns EINVAL if no
@@ -128,6 +137,7 @@ static const struct ctl_node *ctl_find_node(const struct ctl_node *nodes,
128137
break;
129138
}
130139
}
140+
131141
if (n->name == NULL) {
132142
goto error;
133143
}
@@ -244,21 +254,29 @@ static void ctl_query_cleanup_real_args(const struct ctl_node *n,
244254
*/
245255
static int ctl_exec_query_read(void *ctx, const struct ctl_node *n,
246256
enum ctl_query_source source, void *arg,
247-
struct ctl_index_utlist *indexes) {
257+
struct ctl_index_utlist *indexes,
258+
char *extra_name,
259+
umf_ctl_query_type query_type) {
260+
(void)extra_name, (void)query_type;
248261
if (arg == NULL) {
249262
errno = EINVAL;
250263
return -1;
251264
}
252265

253-
return n->cb[CTL_QUERY_READ](ctx, source, arg, indexes);
266+
assert(MAX_CTL_QUERY_TYPE != query_type);
267+
return n->cb[CTL_QUERY_READ](ctx, source, arg, indexes, NULL,
268+
MAX_CTL_QUERY_TYPE);
254269
}
255270

256271
/*
257272
* ctl_exec_query_write -- (internal) calls the write callback of a node
258273
*/
259274
static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
260275
enum ctl_query_source source, void *arg,
261-
struct ctl_index_utlist *indexes) {
276+
struct ctl_index_utlist *indexes,
277+
char *extra_name,
278+
umf_ctl_query_type query_type) {
279+
(void)extra_name, (void)query_type;
262280
if (arg == NULL) {
263281
errno = EINVAL;
264282
return -1;
@@ -269,7 +287,9 @@ static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
269287
return -1;
270288
}
271289

272-
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, indexes);
290+
assert(MAX_CTL_QUERY_TYPE != query_type);
291+
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, indexes, NULL,
292+
MAX_CTL_QUERY_TYPE);
273293
ctl_query_cleanup_real_args(n, real_arg, source);
274294

275295
return ret;
@@ -280,24 +300,40 @@ static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
280300
*/
281301
static int ctl_exec_query_runnable(void *ctx, const struct ctl_node *n,
282302
enum ctl_query_source source, void *arg,
283-
struct ctl_index_utlist *indexes) {
284-
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, indexes);
303+
struct ctl_index_utlist *indexes,
304+
char *extra_name,
305+
umf_ctl_query_type query_type) {
306+
(void)extra_name, (void)query_type;
307+
assert(MAX_CTL_QUERY_TYPE != query_type);
308+
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, indexes, NULL,
309+
MAX_CTL_QUERY_TYPE);
310+
}
311+
312+
static int ctl_exec_query_subtree(void *ctx, const struct ctl_node *n,
313+
enum ctl_query_source source, void *arg,
314+
struct ctl_index_utlist *indexes,
315+
char *extra_name,
316+
umf_ctl_query_type query_type) {
317+
return n->cb[CTL_QUERY_SUBTREE](ctx, source, arg, indexes, extra_name,
318+
query_type);
285319
}
286320

287321
static int (*ctl_exec_query[MAX_CTL_QUERY_TYPE])(
288322
void *ctx, const struct ctl_node *n, enum ctl_query_source source,
289-
void *arg, struct ctl_index_utlist *indexes) = {
323+
void *arg, struct ctl_index_utlist *indexes, char *extra_name,
324+
umf_ctl_query_type query_type) = {
290325
ctl_exec_query_read,
291326
ctl_exec_query_write,
292327
ctl_exec_query_runnable,
328+
ctl_exec_query_subtree,
293329
};
294330

295331
/*
296332
* ctl_query -- (internal) parses the name and calls the appropriate methods
297333
* from the ctl tree
298334
*/
299335
int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
300-
const char *name, enum ctl_query_type type, void *arg) {
336+
const char *name, umf_ctl_query_type type, void *arg) {
301337
if (name == NULL) {
302338
errno = EINVAL;
303339
return -1;
@@ -324,13 +360,17 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
324360
n = ctl_find_node(ctl->root, name, indexes);
325361
}
326362

327-
if (n == NULL || n->type != CTL_NODE_LEAF || n->cb[type] == NULL) {
363+
if (n == NULL ||
364+
(n->type != CTL_NODE_LEAF && n->type != CTL_NODE_SUBTREE) ||
365+
n->cb[n->type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type] == NULL) {
328366
errno = EINVAL;
329367
goto out;
330368
}
331369

332-
ret = ctl_exec_query[type](ctx, n, source, arg, indexes);
333-
370+
char *extra_name = strstr(name, n->name);
371+
ret =
372+
ctl_exec_query[n->type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type](
373+
ctx, n, source, arg, indexes, extra_name, type);
334374
out:
335375
ctl_delete_indexes(indexes);
336376

src/ctl/ctl.h

Lines changed: 31 additions & 15 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,16 @@ 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,
50-
51-
MAX_CTL_QUERY_TYPE
52-
};
53-
5448
typedef int (*node_callback)(void *ctx, enum ctl_query_source type, void *arg,
55-
struct ctl_index_utlist *indexes);
49+
struct ctl_index_utlist *indexes, char *extra_name,
50+
umf_ctl_query_type query_type);
5651

5752
enum ctl_node_type {
5853
CTL_NODE_UNKNOWN,
5954
CTL_NODE_NAMED,
6055
CTL_NODE_LEAF,
6156
CTL_NODE_INDEXED,
57+
CTL_NODE_SUBTREE,
6258

6359
MAX_CTL_NODE
6460
};
@@ -104,6 +100,8 @@ struct ctl_node {
104100
struct ctl *ctl_new(void);
105101
void ctl_delete(struct ctl *stats);
106102

103+
void initialize_global_ctl(void);
104+
107105
int ctl_load_config_from_string(struct ctl *ctl, void *ctx,
108106
const char *cfg_string);
109107
int ctl_load_config_from_file(struct ctl *ctl, void *ctx, const char *cfg_file);
@@ -139,7 +137,7 @@ int ctl_arg_string(const void *arg, void *dest, size_t dest_size);
139137
#define CTL_NODE(name, ...) ctl_node_##__VA_ARGS__##_##name
140138

141139
int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
142-
const char *name, enum ctl_query_type type, void *arg);
140+
const char *name, umf_ctl_query_type type, void *arg);
143141

144142
/* Declaration of a new child node */
145143
#define CTL_CHILD(name, ...) \
@@ -161,6 +159,8 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
161159

162160
#define CTL_RUNNABLE_HANDLER(name, ...) ctl_##__VA_ARGS__##_##name##_runnable
163161

162+
#define CTL_SUBTREE_HANDLER(name, ...) ctl_##__VA_ARGS__##_##name##_subtree
163+
164164
#define CTL_ARG(name) ctl_arg_##name
165165

166166
/*
@@ -170,7 +170,8 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
170170
#define CTL_LEAF_RO(name, ...) \
171171
{ \
172172
CTL_STR(name), CTL_NODE_LEAF, \
173-
{CTL_READ_HANDLER(name, __VA_ARGS__), NULL, NULL}, NULL, NULL \
173+
{CTL_READ_HANDLER(name, __VA_ARGS__), NULL, NULL, NULL}, NULL, \
174+
NULL \
174175
}
175176

176177
/*
@@ -180,7 +181,7 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
180181
#define CTL_LEAF_WO(name, ...) \
181182
{ \
182183
CTL_STR(name), CTL_NODE_LEAF, \
183-
{NULL, CTL_WRITE_HANDLER(name, __VA_ARGS__), NULL}, \
184+
{NULL, CTL_WRITE_HANDLER(name, __VA_ARGS__), NULL, NULL}, \
184185
&CTL_ARG(name), NULL \
185186
}
186187

@@ -191,7 +192,22 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
191192
#define CTL_LEAF_RUNNABLE(name, ...) \
192193
{ \
193194
CTL_STR(name), CTL_NODE_LEAF, \
194-
{NULL, NULL, CTL_RUNNABLE_HANDLER(name, __VA_ARGS__)}, NULL, NULL \
195+
{NULL, NULL, CTL_RUNNABLE_HANDLER(name, __VA_ARGS__), NULL}, NULL, \
196+
NULL \
197+
}
198+
199+
#define CTL_LEAF_SUBTREE(name, ...) \
200+
{ \
201+
CTL_STR(name), CTL_NODE_SUBTREE, \
202+
{NULL, NULL, NULL, CTL_SUBTREE_HANDLER(name, __VA_ARGS__)}, NULL, \
203+
NULL \
204+
}
205+
206+
#define CTL_LEAF_SUBTREE2(name, fun, ...) \
207+
{ \
208+
CTL_STR(name), CTL_NODE_SUBTREE, \
209+
{NULL, NULL, NULL, CTL_SUBTREE_HANDLER(fun, __VA_ARGS__)}, NULL, \
210+
NULL \
195211
}
196212

197213
/*
@@ -201,7 +217,7 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
201217
#define CTL_LEAF_RW(name) \
202218
{ \
203219
CTL_STR(name), CTL_NODE_LEAF, \
204-
{CTL_READ_HANDLER(name), CTL_WRITE_HANDLER(name), NULL}, \
220+
{CTL_READ_HANDLER(name), CTL_WRITE_HANDLER(name), NULL, NULL}, \
205221
&CTL_ARG(name), NULL \
206222
}
207223

0 commit comments

Comments
 (0)