Skip to content

Commit f89a074

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

18 files changed

+416
-52
lines changed

include/umf/base.h

Lines changed: 15 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,20 @@ 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+
int umfCtlGet(const char *name, void *ctx, void *arg);
62+
int umfCtlSet(const char *name, void *ctx, void *arg);
63+
int umfCtlExec(const char *name, void *ctx, void *arg);
64+
5165
#ifdef __cplusplus
5266
}
5367
#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: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <string.h>
2626

2727
#include "base_alloc/base_alloc_global.h"
28+
#include "umf/base.h"
2829
#include "utils/utils_common.h"
2930
#include "utlist.h"
3031

@@ -43,6 +44,7 @@
4344
#define CTL_QUERY_NODE_SEPARATOR "."
4445
#define CTL_VALUE_ARG_SEPARATOR ","
4546

47+
/* GLOBAL TREE */
4648
static int ctl_global_first_free = 0;
4749
static struct ctl_node CTL_NODE(global)[CTL_MAX_ENTRIES];
4850

@@ -78,6 +80,21 @@ char *Strdup(const char *s) {
7880
return p;
7981
}
8082

83+
int umfCtlGet(const char *name, void *ctx, void *arg) {
84+
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name, CTL_QUERY_READ,
85+
arg);
86+
}
87+
88+
int umfCtlSet(const char *name, void *ctx, void *arg) {
89+
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name, CTL_QUERY_WRITE,
90+
arg);
91+
}
92+
93+
int umfCtlExec(const char *name, void *ctx, void *arg) {
94+
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name,
95+
CTL_QUERY_RUNNABLE, arg);
96+
}
97+
8198
/*
8299
* ctl_find_node -- (internal) searches for a matching entry point in the
83100
* provided nodes
@@ -102,6 +119,9 @@ static const struct ctl_node *ctl_find_node(const struct ctl_node *nodes,
102119
* in the main ctl tree.
103120
*/
104121
while (node_name != NULL) {
122+
if (n != NULL && n->type == CTL_NODE_SUBTREE) {
123+
break;
124+
}
105125
char *endptr;
106126
/*
107127
* Ignore errno from strtol: FreeBSD returns EINVAL if no
@@ -128,6 +148,7 @@ static const struct ctl_node *ctl_find_node(const struct ctl_node *nodes,
128148
break;
129149
}
130150
}
151+
131152
if (n->name == NULL) {
132153
goto error;
133154
}
@@ -244,21 +265,29 @@ static void ctl_query_cleanup_real_args(const struct ctl_node *n,
244265
*/
245266
static int ctl_exec_query_read(void *ctx, const struct ctl_node *n,
246267
enum ctl_query_source source, void *arg,
247-
struct ctl_index_utlist *indexes) {
268+
struct ctl_index_utlist *indexes,
269+
char *extra_name,
270+
umf_ctl_query_type query_type) {
271+
(void)extra_name, (void)query_type;
248272
if (arg == NULL) {
249273
errno = EINVAL;
250274
return -1;
251275
}
252276

253-
return n->cb[CTL_QUERY_READ](ctx, source, arg, indexes);
277+
assert(MAX_CTL_QUERY_TYPE != query_type);
278+
return n->cb[CTL_QUERY_READ](ctx, source, arg, indexes, NULL,
279+
MAX_CTL_QUERY_TYPE);
254280
}
255281

256282
/*
257283
* ctl_exec_query_write -- (internal) calls the write callback of a node
258284
*/
259285
static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
260286
enum ctl_query_source source, void *arg,
261-
struct ctl_index_utlist *indexes) {
287+
struct ctl_index_utlist *indexes,
288+
char *extra_name,
289+
umf_ctl_query_type query_type) {
290+
(void)extra_name, (void)query_type;
262291
if (arg == NULL) {
263292
errno = EINVAL;
264293
return -1;
@@ -269,7 +298,9 @@ static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
269298
return -1;
270299
}
271300

272-
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, indexes);
301+
assert(MAX_CTL_QUERY_TYPE != query_type);
302+
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, indexes, NULL,
303+
MAX_CTL_QUERY_TYPE);
273304
ctl_query_cleanup_real_args(n, real_arg, source);
274305

275306
return ret;
@@ -280,24 +311,40 @@ static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
280311
*/
281312
static int ctl_exec_query_runnable(void *ctx, const struct ctl_node *n,
282313
enum ctl_query_source source, void *arg,
283-
struct ctl_index_utlist *indexes) {
284-
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, indexes);
314+
struct ctl_index_utlist *indexes,
315+
char *extra_name,
316+
umf_ctl_query_type query_type) {
317+
(void)extra_name, (void)query_type;
318+
assert(MAX_CTL_QUERY_TYPE != query_type);
319+
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, indexes, NULL,
320+
MAX_CTL_QUERY_TYPE);
321+
}
322+
323+
static int ctl_exec_query_subtree(void *ctx, const struct ctl_node *n,
324+
enum ctl_query_source source, void *arg,
325+
struct ctl_index_utlist *indexes,
326+
char *extra_name,
327+
umf_ctl_query_type query_type) {
328+
return n->cb[CTL_QUERY_SUBTREE](ctx, source, arg, indexes, extra_name,
329+
query_type);
285330
}
286331

287332
static int (*ctl_exec_query[MAX_CTL_QUERY_TYPE])(
288333
void *ctx, const struct ctl_node *n, enum ctl_query_source source,
289-
void *arg, struct ctl_index_utlist *indexes) = {
334+
void *arg, struct ctl_index_utlist *indexes, char *extra_name,
335+
umf_ctl_query_type query_type) = {
290336
ctl_exec_query_read,
291337
ctl_exec_query_write,
292338
ctl_exec_query_runnable,
339+
ctl_exec_query_subtree,
293340
};
294341

295342
/*
296343
* ctl_query -- (internal) parses the name and calls the appropriate methods
297344
* from the ctl tree
298345
*/
299346
int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
300-
const char *name, enum ctl_query_type type, void *arg) {
347+
const char *name, umf_ctl_query_type type, void *arg) {
301348
if (name == NULL) {
302349
errno = EINVAL;
303350
return -1;
@@ -324,13 +371,17 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
324371
n = ctl_find_node(ctl->root, name, indexes);
325372
}
326373

327-
if (n == NULL || n->type != CTL_NODE_LEAF || n->cb[type] == NULL) {
374+
if (n == NULL ||
375+
(n->type != CTL_NODE_LEAF && n->type != CTL_NODE_SUBTREE) ||
376+
n->cb[n->type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type] == NULL) {
328377
errno = EINVAL;
329378
goto out;
330379
}
331380

332-
ret = ctl_exec_query[type](ctx, n, source, arg, indexes);
333-
381+
char *extra_name = strstr(name, n->name);
382+
ret =
383+
ctl_exec_query[n->type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type](
384+
ctx, n, source, arg, indexes, extra_name, type);
334385
out:
335386
ctl_delete_indexes(indexes);
336387

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)