Skip to content

Commit c43ddc7

Browse files
committed
[CTL] Add support for defaults
1 parent 136dc39 commit c43ddc7

21 files changed

+802
-252
lines changed

benchmark/benchmark_umf.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ struct provider_interface {
4545
return;
4646
}
4747
umfCtlExec("umf.provider.by_handle.stats.peak_memory.reset", provider,
48-
NULL);
48+
NULL, 0);
4949
}
5050

5151
void postBench([[maybe_unused]] ::benchmark::State &state) {
5252
if (state.thread_index() != 0) {
5353
return;
5454
}
5555
size_t arg;
56-
umf_result_t ret = umfCtlGet(
57-
"umf.provider.by_handle.stats.allocated_memory", provider, &arg);
56+
umf_result_t ret =
57+
umfCtlGet("umf.provider.by_handle.stats.allocated_memory", provider,
58+
&arg, sizeof(arg));
5859
if (ret == UMF_RESULT_SUCCESS) {
5960
state.counters["provider_memory_allocated"] =
6061
static_cast<double>(arg);

include/umf/base.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,30 @@ typedef enum umf_ctl_query_type {
6666
/// @param name name of an attribute to be retrieved
6767
/// @param ctx pointer to the pool or the provider
6868
/// @param arg [out] pointer to the variable where the value will be stored
69+
/// @param size size of the value, depends on the context
6970
/// @return UMF_RESULT_SUCCESS on success or UMF_RESULT_ERROR_UNKNOWN on failure.
7071
///
71-
umf_result_t umfCtlGet(const char *name, void *ctx, void *arg);
72+
umf_result_t umfCtlGet(const char *name, void *ctx, void *arg, size_t size);
7273

7374
///
7475
/// @brief Set value of a specified attribute at the given name.
7576
/// @param name name of an attribute to be set
76-
/// @param ctx pointer to the pool or the provider
77+
/// @param ctx pointer to the pool or the provider, NULL for the 'default' path
7778
/// @param arg [in] pointer to the value that will be set
79+
/// @param size [in] size of the value, depends on the context
7880
/// @return UMF_RESULT_SUCCESS on success or UMF_RESULT_ERROR_UNKNOWN on failure.
7981
///
80-
umf_result_t umfCtlSet(const char *name, void *ctx, void *arg);
82+
umf_result_t umfCtlSet(const char *name, void *ctx, void *arg, size_t size);
8183

8284
///
8385
/// @brief Execute callback related with the specified attribute.
8486
/// @param name name of an attribute to be executed
8587
/// @param ctx pointer to the pool or the provider
8688
/// @param arg [in/out] pointer to the value, can be used as an input or output
89+
/// @param size [in] size of the value, depends on the context
8790
/// @return UMF_RESULT_SUCCESS on success or UMF_RESULT_ERROR_UNKNOWN on failure.
8891
///
89-
umf_result_t umfCtlExec(const char *name, void *ctx, void *arg);
92+
umf_result_t umfCtlExec(const char *name, void *ctx, void *arg, size_t size);
9093

9194
#ifdef __cplusplus
9295
}

include/umf/memory_pool_ops.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,27 @@ typedef struct umf_memory_pool_ops_t {
131131
/// The function is used to perform various control operations
132132
/// on the memory pool.
133133
///
134-
/// @param hPool handle to the memory pool.
134+
/// @param pool handle to the memory pool.
135135
/// @param operationType type of the operation to be performed.
136136
/// @param name name associated with the operation.
137137
/// @param arg argument for the operation.
138+
/// @param size size of the argument [optional - check path requirements]
138139
/// @param queryType type of the query to be performed.
139140
///
140141
/// @return umf_result_t result of the control operation.
141142
///
142-
umf_result_t (*ctl)(void *hPool, int operationType, const char *name,
143-
void *arg, umf_ctl_query_type_t queryType);
143+
umf_result_t (*ctl)(void *pool, int operationType, const char *name,
144+
void *arg, size_t size, umf_ctl_query_type_t queryType);
144145

145146
///
146-
/// @brief Get the name of the memory pool.
147-
/// @param pool pointer to the memory pool
148-
/// @return name of the memory pool
147+
/// @brief Retrieves the name of the memory pool [optional]
148+
/// @param pool valid pointer to the memory pool or NULL value
149+
/// \details
150+
/// * Implementations *must* return a literal null-terminated string.
151+
///
152+
/// * Implementations *must* return default pool name when NULL is provided,
153+
/// otherwise the pool's name is returned.
154+
/// @return A constant character string representing the pool's name.
149155
///
150156
const char *(*get_name)(void *pool);
151157
} umf_memory_pool_ops_t;

include/umf/memory_provider_ops.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,13 @@ typedef struct umf_memory_provider_ops_t {
259259
/// @param operationType type of the operation to be performed.
260260
/// @param name name associated with the operation.
261261
/// @param arg argument for the operation.
262+
/// @param size size of the argument [optional - check path requirements]
262263
/// @param queryType type of the query to be performed.
263264
///
264265
/// @return umf_result_t result of the control operation.
265266
///
266267
umf_result_t (*ctl)(void *hProvider, int operationType, const char *name,
267-
void *arg, umf_ctl_query_type_t queryType);
268+
void *arg, size_t size, umf_ctl_query_type_t queryType);
268269

269270
} umf_memory_provider_ops_t;
270271

src/ctl/ctl.c

Lines changed: 16 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
#include <stdio.h>
3737
#endif
3838

39-
#define CTL_MAX_ENTRIES 100
40-
4139
#define MAX_CONFIG_FILE_LEN (1 << 20) /* 1 megabyte */
4240

4341
#define CTL_STRING_QUERY_SEPARATOR ";"
@@ -49,21 +47,6 @@
4947
static int ctl_global_first_free = 0;
5048
static umf_ctl_node_t CTL_NODE(global)[CTL_MAX_ENTRIES];
5149

52-
/*
53-
* This is the top level node of the ctl tree structure. Each node can contain
54-
* children and leaf nodes.
55-
*
56-
* Internal nodes simply create a new path in the tree whereas child nodes are
57-
* the ones providing the read/write functionality by the means of callbacks.
58-
*
59-
* Each tree node must be NULL-terminated, CTL_NODE_END macro is provided for
60-
* convenience.
61-
*/
62-
struct ctl {
63-
umf_ctl_node_t root[CTL_MAX_ENTRIES];
64-
int first_free;
65-
};
66-
6750
void *Zalloc(size_t sz) {
6851
void *ptr = umf_ba_global_alloc(sz);
6952
if (ptr) {
@@ -81,36 +64,6 @@ char *Strdup(const char *s) {
8164
return p;
8265
}
8366

84-
umf_result_t umfCtlGet(const char *name, void *ctx, void *arg) {
85-
if (name == NULL || arg == NULL || ctx == NULL) {
86-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
87-
}
88-
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name, CTL_QUERY_READ,
89-
arg)
90-
? UMF_RESULT_ERROR_UNKNOWN
91-
: UMF_RESULT_SUCCESS;
92-
}
93-
94-
umf_result_t umfCtlSet(const char *name, void *ctx, void *arg) {
95-
if (name == NULL || arg == NULL || ctx == NULL) {
96-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
97-
}
98-
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name, CTL_QUERY_WRITE,
99-
arg)
100-
? UMF_RESULT_ERROR_UNKNOWN
101-
: UMF_RESULT_SUCCESS;
102-
}
103-
104-
umf_result_t umfCtlExec(const char *name, void *ctx, void *arg) {
105-
if (name == NULL || ctx == NULL) {
106-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
107-
}
108-
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name,
109-
CTL_QUERY_RUNNABLE, arg)
110-
? UMF_RESULT_ERROR_UNKNOWN
111-
: UMF_RESULT_SUCCESS;
112-
}
113-
11467
/*
11568
* ctl_find_node -- (internal) searches for a matching entry point in the
11669
* provided nodes
@@ -296,10 +249,10 @@ static void ctl_query_cleanup_real_args(const umf_ctl_node_t *n, void *real_arg,
296249
*/
297250
static int ctl_exec_query_read(void *ctx, const umf_ctl_node_t *n,
298251
umf_ctl_query_source_t source, void *arg,
299-
umf_ctl_index_utlist_t *indexes,
252+
size_t size, umf_ctl_index_utlist_t *indexes,
300253
const char *extra_name,
301254
umf_ctl_query_type_t query_type) {
302-
(void)extra_name, (void)query_type;
255+
(void)query_type;
303256
assert(n != NULL);
304257
assert(n->cb[CTL_QUERY_READ] != NULL);
305258
assert(MAX_CTL_QUERY_TYPE != query_type);
@@ -309,7 +262,7 @@ static int ctl_exec_query_read(void *ctx, const umf_ctl_node_t *n,
309262
return -1;
310263
}
311264

312-
return n->cb[CTL_QUERY_READ](ctx, source, arg, indexes, NULL,
265+
return n->cb[CTL_QUERY_READ](ctx, source, arg, size, indexes, extra_name,
313266
MAX_CTL_QUERY_TYPE);
314267
}
315268

@@ -318,10 +271,9 @@ static int ctl_exec_query_read(void *ctx, const umf_ctl_node_t *n,
318271
*/
319272
static int ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
320273
umf_ctl_query_source_t source, void *arg,
321-
umf_ctl_index_utlist_t *indexes,
274+
size_t size, umf_ctl_index_utlist_t *indexes,
322275
const char *extra_name,
323276
umf_ctl_query_type_t query_type) {
324-
(void)extra_name, (void)query_type;
325277
assert(n != NULL);
326278
assert(n->cb[CTL_QUERY_WRITE] != NULL);
327279
assert(MAX_CTL_QUERY_TYPE != query_type);
@@ -336,8 +288,8 @@ static int ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
336288
return -1;
337289
}
338290

339-
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, indexes, NULL,
340-
MAX_CTL_QUERY_TYPE);
291+
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, size, indexes,
292+
extra_name, MAX_CTL_QUERY_TYPE);
341293
ctl_query_cleanup_real_args(n, real_arg, source);
342294

343295
return ret;
@@ -348,31 +300,31 @@ static int ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
348300
*/
349301
static int ctl_exec_query_runnable(void *ctx, const umf_ctl_node_t *n,
350302
umf_ctl_query_source_t source, void *arg,
351-
umf_ctl_index_utlist_t *indexes,
303+
size_t size, umf_ctl_index_utlist_t *indexes,
352304
const char *extra_name,
353305
umf_ctl_query_type_t query_type) {
354-
(void)extra_name, (void)query_type;
355306
assert(n != NULL);
356307
assert(n->cb[CTL_QUERY_RUNNABLE] != NULL);
357308
assert(MAX_CTL_QUERY_TYPE != query_type);
358-
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, indexes, NULL,
359-
MAX_CTL_QUERY_TYPE);
309+
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, size, indexes,
310+
extra_name, MAX_CTL_QUERY_TYPE);
360311
}
361312

362313
static int ctl_exec_query_subtree(void *ctx, const umf_ctl_node_t *n,
363314
umf_ctl_query_source_t source, void *arg,
364-
umf_ctl_index_utlist_t *indexes,
315+
size_t size, umf_ctl_index_utlist_t *indexes,
365316
const char *extra_name,
366317
umf_ctl_query_type_t query_type) {
367318
assert(n != NULL);
368319
assert(n->cb[CTL_QUERY_SUBTREE] != NULL);
369320
assert(MAX_CTL_QUERY_TYPE != query_type);
370-
return n->cb[CTL_QUERY_SUBTREE](ctx, source, arg, indexes, extra_name,
321+
return n->cb[CTL_QUERY_SUBTREE](ctx, source, arg, size, indexes, extra_name,
371322
query_type);
372323
}
373324

374325
typedef int (*umf_ctl_exec_query_t)(void *ctx, const umf_ctl_node_t *n,
375326
umf_ctl_query_source_t source, void *arg,
327+
size_t size,
376328
umf_ctl_index_utlist_t *indexes,
377329
const char *extra_name,
378330
umf_ctl_query_type_t query_type);
@@ -389,7 +341,8 @@ static umf_ctl_exec_query_t ctl_exec_query[MAX_CTL_QUERY_TYPE] = {
389341
* from the ctl tree
390342
*/
391343
int ctl_query(struct ctl *ctl, void *ctx, umf_ctl_query_source_t source,
392-
const char *name, umf_ctl_query_type_t type, void *arg) {
344+
const char *name, umf_ctl_query_type_t type, void *arg,
345+
size_t size) {
393346
if (name == NULL) {
394347
errno = EINVAL;
395348
return -1;
@@ -426,10 +379,9 @@ int ctl_query(struct ctl *ctl, void *ctx, umf_ctl_query_source_t source,
426379
goto out;
427380
}
428381

429-
const char *extra_name = &name[0] + name_offset;
430382
ret =
431383
ctl_exec_query[n->type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type](
432-
ctx, n, source, arg, indexes, extra_name, type);
384+
ctx, n, source, arg, size, indexes, name + name_offset, type);
433385
out:
434386
ctl_delete_indexes(indexes);
435387

@@ -496,7 +448,7 @@ static int ctl_load_config(struct ctl *ctl, void *ctx, char *buf) {
496448
}
497449

498450
r = ctl_query(ctl, ctx, CTL_QUERY_CONFIG_INPUT, name, CTL_QUERY_WRITE,
499-
value);
451+
value, 0);
500452

501453
if (r < 0 && ctx != NULL) {
502454
return -1;
@@ -590,24 +542,6 @@ int ctl_load_config_from_file(struct ctl *ctl, void *ctx,
590542
}
591543
#endif
592544

593-
/*
594-
* ctl_new -- allocates and initializes ctl data structures
595-
*/
596-
struct ctl *ctl_new(void) {
597-
struct ctl *c = Zalloc(sizeof(struct ctl));
598-
if (c == NULL) {
599-
return NULL;
600-
}
601-
602-
c->first_free = 0;
603-
return c;
604-
}
605-
606-
/*
607-
* ctl_delete -- deletes ctl
608-
*/
609-
void ctl_delete(struct ctl *c) { umf_ba_global_free(c); }
610-
611545
/*
612546
* ctl_parse_ll -- (internal) parses and returns a long long signed integer
613547
*/

src/ctl/ctl.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
extern "C" {
2828
#endif
2929

30-
struct ctl;
30+
#define CTL_MAX_ENTRIES 100
3131

3232
typedef struct ctl_index_utlist {
3333
const char *name;
@@ -46,7 +46,7 @@ typedef enum ctl_query_source {
4646
} umf_ctl_query_source_t;
4747

4848
typedef int (*node_callback)(void *ctx, umf_ctl_query_source_t type, void *arg,
49-
umf_ctl_index_utlist_t *indexes,
49+
size_t size, umf_ctl_index_utlist_t *indexes,
5050
const char *extra_name,
5151
umf_ctl_query_type_t query_type);
5252

@@ -98,8 +98,20 @@ typedef struct ctl_node {
9898
const struct ctl_node *children;
9999
} umf_ctl_node_t;
100100

101-
struct ctl *ctl_new(void);
102-
void ctl_delete(struct ctl *stats);
101+
/*
102+
* This is the top level node of the ctl tree structure. Each node can contain
103+
* children and leaf nodes.
104+
*
105+
* Internal nodes simply create a new path in the tree whereas child nodes are
106+
* the ones providing the read/write functionality by the means of callbacks.
107+
*
108+
* Each tree node must be NULL-terminated, CTL_NODE_END macro is provided for
109+
* convenience.
110+
*/
111+
struct ctl {
112+
umf_ctl_node_t root[CTL_MAX_ENTRIES];
113+
int first_free;
114+
};
103115

104116
void initialize_global_ctl(void);
105117

@@ -138,7 +150,8 @@ int ctl_arg_string(const void *arg, void *dest, size_t dest_size);
138150
#define CTL_NODE(name, ...) ctl_node_##__VA_ARGS__##_##name
139151

140152
int ctl_query(struct ctl *ctl, void *ctx, umf_ctl_query_source_t source,
141-
const char *name, umf_ctl_query_type_t type, void *arg);
153+
const char *name, umf_ctl_query_type_t type, void *arg,
154+
size_t size);
142155

143156
/* Declaration of a new child node */
144157
#define CTL_CHILD(name, ...) \

0 commit comments

Comments
 (0)