Skip to content

Commit d9bca45

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

File tree

9 files changed

+185
-29
lines changed

9 files changed

+185
-29
lines changed

include/umf/memory_provider_ops.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*
88
*/
99

10+
#include "../ctl/ctl.h"
1011
#ifndef UMF_MEMORY_PROVIDER_OPS_H
1112
#define UMF_MEMORY_PROVIDER_OPS_H 1
1213

@@ -78,6 +79,9 @@ typedef struct umf_memory_provider_ext_ops_t {
7879
umf_result_t (*allocation_split)(void *hProvider, void *ptr,
7980
size_t totalSize, size_t firstSize);
8081

82+
umf_result_t (*ctl)(void *hProvider, int operationType, const char *name,
83+
void *arg, char *extra_name, enum ctl_query_type query_type);
84+
8185
} umf_memory_provider_ext_ops_t;
8286

8387
///

src/ctl/ctl.c

Lines changed: 45 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,26 @@ 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, char *extra_name,
253+
enum ctl_query_type query_type) {
254+
(void)extra_name, (void)query_type;
248255
if (arg == NULL) {
249256
errno = EINVAL;
250257
return -1;
251258
}
252259

253-
return n->cb[CTL_QUERY_READ](ctx, source, arg, indexes);
260+
assert(MAX_CTL_QUERY_TYPE != query_type);
261+
return n->cb[CTL_QUERY_READ](ctx, source, arg, indexes, NULL, MAX_CTL_QUERY_TYPE);
254262
}
255263

256264
/*
257265
* ctl_exec_query_write -- (internal) calls the write callback of a node
258266
*/
259267
static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
260268
enum ctl_query_source source, void *arg,
261-
struct ctl_index_utlist *indexes) {
269+
struct ctl_index_utlist *indexes, char *extra_name,
270+
enum ctl_query_type query_type) {
271+
(void)extra_name, (void)query_type;
262272
if (arg == NULL) {
263273
errno = EINVAL;
264274
return -1;
@@ -269,7 +279,8 @@ static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
269279
return -1;
270280
}
271281

272-
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, indexes);
282+
assert(MAX_CTL_QUERY_TYPE != query_type);
283+
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, indexes, NULL, MAX_CTL_QUERY_TYPE);
273284
ctl_query_cleanup_real_args(n, real_arg, source);
274285

275286
return ret;
@@ -280,16 +291,27 @@ static int ctl_exec_query_write(void *ctx, const struct ctl_node *n,
280291
*/
281292
static int ctl_exec_query_runnable(void *ctx, const struct ctl_node *n,
282293
enum ctl_query_source source, void *arg,
283-
struct ctl_index_utlist *indexes) {
284-
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, indexes);
294+
struct ctl_index_utlist *indexes, char *extra_name,
295+
enum ctl_query_type query_type) {
296+
(void)extra_name, (void)query_type;
297+
assert(MAX_CTL_QUERY_TYPE != query_type);
298+
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, indexes, NULL, MAX_CTL_QUERY_TYPE);
299+
}
300+
301+
static int ctl_exec_query_subtree(void *ctx, const struct ctl_node *n,
302+
enum ctl_query_source source, void *arg,
303+
struct ctl_index_utlist *indexes, char *extra_name,
304+
enum ctl_query_type query_type) {
305+
return n->cb[CTL_QUERY_SUBTREE](ctx, source, arg, indexes, extra_name, query_type);
285306
}
286307

287308
static int (*ctl_exec_query[MAX_CTL_QUERY_TYPE])(
288309
void *ctx, const struct ctl_node *n, enum ctl_query_source source,
289-
void *arg, struct ctl_index_utlist *indexes) = {
310+
void *arg, struct ctl_index_utlist *indexes, char *extra_name, enum ctl_query_type query_type) = {
290311
ctl_exec_query_read,
291312
ctl_exec_query_write,
292313
ctl_exec_query_runnable,
314+
ctl_exec_query_subtree,
293315
};
294316

295317
/*
@@ -324,13 +346,14 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
324346
n = ctl_find_node(ctl->root, name, indexes);
325347
}
326348

327-
if (n == NULL || n->type != CTL_NODE_LEAF || n->cb[type] == NULL) {
349+
enum ctl_query_type query_callback_type = n->type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type;
350+
if (n == NULL || (n->type != CTL_NODE_LEAF && n->type != CTL_NODE_SUBTREE) || n->cb[query_callback_type] == NULL) {
328351
errno = EINVAL;
329352
goto out;
330353
}
331354

332-
ret = ctl_exec_query[type](ctx, n, source, arg, indexes);
333-
355+
char *extra_name = strstr(name, n->name);
356+
ret = ctl_exec_query[query_callback_type](ctx, n, source, arg, indexes, extra_name, type);
334357
out:
335358
ctl_delete_indexes(indexes);
336359

@@ -594,3 +617,15 @@ int ctl_arg_string(const void *arg, void *dest, size_t dest_size) {
594617

595618
return 0;
596619
}
620+
621+
void umfCtlGet(const char* name, ...) {
622+
(void)name;
623+
}
624+
625+
void umfCtlSet(const char* name, ...) {
626+
(void)name;
627+
}
628+
629+
void umfCtlExec(const char* name, ...) {
630+
(void)name;
631+
}

src/ctl/ctl.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,20 @@ enum ctl_query_type {
4747
CTL_QUERY_READ,
4848
CTL_QUERY_WRITE,
4949
CTL_QUERY_RUNNABLE,
50+
CTL_QUERY_SUBTREE,
5051

5152
MAX_CTL_QUERY_TYPE
5253
};
5354

5455
typedef int (*node_callback)(void *ctx, enum ctl_query_source type, void *arg,
55-
struct ctl_index_utlist *indexes);
56+
struct ctl_index_utlist *indexes, char *extra_name, enum ctl_query_type query_type);
5657

5758
enum ctl_node_type {
5859
CTL_NODE_UNKNOWN,
5960
CTL_NODE_NAMED,
6061
CTL_NODE_LEAF,
6162
CTL_NODE_INDEXED,
63+
CTL_NODE_SUBTREE,
6264

6365
MAX_CTL_NODE
6466
};
@@ -104,6 +106,8 @@ struct ctl_node {
104106
struct ctl *ctl_new(void);
105107
void ctl_delete(struct ctl *stats);
106108

109+
void initialize_global_ctl(void);
110+
107111
int ctl_load_config_from_string(struct ctl *ctl, void *ctx,
108112
const char *cfg_string);
109113
int ctl_load_config_from_file(struct ctl *ctl, void *ctx, const char *cfg_file);
@@ -161,6 +165,8 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
161165

162166
#define CTL_RUNNABLE_HANDLER(name, ...) ctl_##__VA_ARGS__##_##name##_runnable
163167

168+
#define CTL_SUBTREE_HANDLER(name, ...) ctl_##__VA_ARGS__##_##name##_subtree
169+
164170
#define CTL_ARG(name) ctl_arg_##name
165171

166172
/*
@@ -170,7 +176,7 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
170176
#define CTL_LEAF_RO(name, ...) \
171177
{ \
172178
CTL_STR(name), CTL_NODE_LEAF, \
173-
{CTL_READ_HANDLER(name, __VA_ARGS__), NULL, NULL}, NULL, NULL \
179+
{CTL_READ_HANDLER(name, __VA_ARGS__), NULL, NULL, NULL}, NULL, NULL \
174180
}
175181

176182
/*
@@ -180,7 +186,7 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
180186
#define CTL_LEAF_WO(name, ...) \
181187
{ \
182188
CTL_STR(name), CTL_NODE_LEAF, \
183-
{NULL, CTL_WRITE_HANDLER(name, __VA_ARGS__), NULL}, \
189+
{NULL, CTL_WRITE_HANDLER(name, __VA_ARGS__), NULL, NULL}, \
184190
&CTL_ARG(name), NULL \
185191
}
186192

@@ -191,7 +197,19 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
191197
#define CTL_LEAF_RUNNABLE(name, ...) \
192198
{ \
193199
CTL_STR(name), CTL_NODE_LEAF, \
194-
{NULL, NULL, CTL_RUNNABLE_HANDLER(name, __VA_ARGS__)}, NULL, NULL \
200+
{NULL, NULL, CTL_RUNNABLE_HANDLER(name, __VA_ARGS__), NULL}, NULL, NULL \
201+
}
202+
203+
#define CTL_LEAF_SUBTREE(name, ...) \
204+
{ \
205+
CTL_STR(name), CTL_NODE_SUBTREE, \
206+
{NULL, NULL, NULL, CTL_SUBTREE_HANDLER(name, __VA_ARGS__)}, NULL, NULL \
207+
}
208+
209+
#define CTL_LEAF_SUBTREE2(name, fun, ...) \
210+
{ \
211+
CTL_STR(name), CTL_NODE_SUBTREE, \
212+
{NULL, NULL, NULL, CTL_SUBTREE_HANDLER(fun, __VA_ARGS__)}, NULL, NULL \
195213
}
196214

197215
/*
@@ -201,7 +219,7 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
201219
#define CTL_LEAF_RW(name) \
202220
{ \
203221
CTL_STR(name), CTL_NODE_LEAF, \
204-
{CTL_READ_HANDLER(name), CTL_WRITE_HANDLER(name), NULL}, \
222+
{CTL_READ_HANDLER(name), CTL_WRITE_HANDLER(name), NULL, NULL}, \
205223
&CTL_ARG(name), NULL \
206224
}
207225

src/libumf.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "ipc_cache.h"
1414
#include "memspace_internal.h"
1515
#include "provider_tracking.h"
16+
#include "memory_provider_internal.h"
1617
#include "utils_common.h"
1718
#include "utils_log.h"
1819
#if !defined(UMF_NO_HWLOC)
@@ -23,6 +24,15 @@ umf_memory_tracker_handle_t TRACKER = NULL;
2324

2425
static unsigned long long umfRefCount = 0;
2526

27+
static struct ctl_node CTL_NODE(umf)[] = {
28+
CTL_CHILD(provider),
29+
CTL_NODE_END
30+
};
31+
32+
void initialize_global_ctl(void) {
33+
CTL_REGISTER_MODULE(NULL, umf);
34+
}
35+
2636
int umfInit(void) {
2737
if (utils_fetch_and_add64(&umfRefCount, 1) == 0) {
2838
utils_log_init();
@@ -41,6 +51,7 @@ int umfInit(void) {
4151
}
4252

4353
LOG_DEBUG("UMF IPC cache initialized");
54+
initialize_global_ctl();
4455
}
4556

4657
if (TRACKER) {

src/memory_provider.c

Lines changed: 18 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,23 @@ 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)(void *ctx, enum ctl_query_source source,
29+
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, /*unused*/NULL, arg, extra_name, query_type);
37+
return 0;
38+
}
39+
40+
struct ctl_node CTL_NODE(provider)[] = {
41+
CTL_LEAF_SUBTREE2(by_handle, by_handle_provider),
42+
CTL_NODE_END
43+
};
44+
2845
static umf_result_t umfDefaultPurgeLazy(void *provider, void *ptr,
2946
size_t size) {
3047
(void)provider;

src/memory_provider_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77
*
88
*/
99

10+
1011
#ifndef UMF_MEMORY_PROVIDER_INTERNAL_H
1112
#define UMF_MEMORY_PROVIDER_INTERNAL_H 1
1213

1314
#include <stdbool.h>
1415

1516
#include <umf/memory_provider.h>
1617

18+
#include "ctl/ctl.h"
1719
#ifdef __cplusplus
1820
extern "C" {
1921
#endif
2022

2123
void *umfMemoryProviderGetPriv(umf_memory_provider_handle_t hProvider);
2224
umf_memory_provider_handle_t *umfGetLastFailedMemoryProviderPtr(void);
2325

26+
extern struct ctl_node CTL_NODE(provider)[];
2427
#ifdef __cplusplus
2528
}
2629
#endif

0 commit comments

Comments
 (0)