Skip to content

Commit 408f2c4

Browse files
committed
[CTL] Add support for defaults
1 parent 5075645 commit 408f2c4

20 files changed

+301
-96
lines changed

benchmark/benchmark_umf.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct provider_interface {
5353
}
5454
size_t arg;
5555
umf_result_t ret = umfCtlGet(
56-
"umf.provider.by_handle.stats.allocated_memory", provider, &arg);
56+
"umf.provider.by_handle.stats.allocated_memory", provider, &arg, 0);
5757
if (ret == UMF_RESULT_SUCCESS) {
5858
state.counters["provider_memory_allocated"] =
5959
static_cast<double>(arg);

include/umf/base.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,20 @@ 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 [in/out] size of the value
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
7677
/// @param ctx pointer to the pool or the provider
7778
/// @param arg [in] pointer to the value that will be set
79+
/// @param size [in] size of the value
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.

include/umf/memory_pool.h

+7
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ umf_memory_pool_handle_t umfPoolByPtr(const void *ptr);
165165
umf_result_t umfPoolGetMemoryProvider(umf_memory_pool_handle_t hPool,
166166
umf_memory_provider_handle_t *hProvider);
167167

168+
///
169+
/// @brief Retrieve name of a given memory \p hPool.
170+
/// @param hPool handle to the memory pool
171+
/// @return pointer to a string containing the name of the \p hPool
172+
///
173+
const char *umfPoolGetName(umf_memory_pool_handle_t hPool);
174+
168175
///
169176
/// @brief Set a custom tag on the memory pool that can be later retrieved using umfPoolGetTag.
170177
/// @param hPool specified memory pool

include/umf/memory_pool_ops.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,20 @@ typedef struct umf_memory_pool_ops_t {
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.
138139
/// @param queryType type of the query to be performed.
139140
///
140141
/// @return umf_result_t result of the control operation.
141142
///
142143
umf_result_t (*ctl)(void *hPool, int operationType, const char *name,
143-
void *arg, umf_ctl_query_type_t queryType);
144+
void *arg, size_t size, umf_ctl_query_type_t queryType);
145+
146+
///
147+
/// @brief Get the name of the memory pool.
148+
/// @param pool pointer to the memory pool
149+
/// @return name of the memory pool
150+
///
151+
const char *(*get_name)(void *pool);
144152
} umf_memory_pool_ops_t;
145153

146154
#ifdef __cplusplus

src/ctl/ctl.c

+28-39
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,22 +64,23 @@ char *Strdup(const char *s) {
8164
return p;
8265
}
8366

84-
umf_result_t umfCtlGet(const char *name, void *ctx, void *arg) {
67+
umf_result_t umfCtlGet(const char *name, void *ctx, void *arg, size_t size) {
8568
if (name == NULL || arg == NULL || ctx == NULL) {
8669
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
8770
}
8871
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name, CTL_QUERY_READ,
89-
arg)
72+
arg, size)
9073
? UMF_RESULT_ERROR_UNKNOWN
9174
: UMF_RESULT_SUCCESS;
9275
}
9376

94-
umf_result_t umfCtlSet(const char *name, void *ctx, void *arg) {
95-
if (name == NULL || arg == NULL || ctx == NULL) {
77+
umf_result_t umfCtlSet(const char *name, void *ctx, void *arg, size_t size) {
78+
// Context can be NULL when setting defaults
79+
if (name == NULL || arg == NULL || size == 0) {
9680
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
9781
}
9882
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name, CTL_QUERY_WRITE,
99-
arg)
83+
arg, size)
10084
? UMF_RESULT_ERROR_UNKNOWN
10185
: UMF_RESULT_SUCCESS;
10286
}
@@ -106,7 +90,7 @@ umf_result_t umfCtlExec(const char *name, void *ctx, void *arg) {
10690
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
10791
}
10892
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name,
109-
CTL_QUERY_RUNNABLE, arg)
93+
CTL_QUERY_RUNNABLE, arg, 0)
11094
? UMF_RESULT_ERROR_UNKNOWN
11195
: UMF_RESULT_SUCCESS;
11296
}
@@ -287,10 +271,10 @@ static void ctl_query_cleanup_real_args(const umf_ctl_node_t *n, void *real_arg,
287271
*/
288272
static int ctl_exec_query_read(void *ctx, const umf_ctl_node_t *n,
289273
umf_ctl_query_source_t source, void *arg,
290-
umf_ctl_index_utlist_t *indexes,
274+
size_t size, umf_ctl_index_utlist_t *indexes,
291275
const char *extra_name,
292276
umf_ctl_query_type_t query_type) {
293-
(void)extra_name, (void)query_type;
277+
(void)extra_name, (void)query_type, (void)size;
294278
assert(n != NULL);
295279
assert(n->cb[CTL_QUERY_READ] != NULL);
296280
assert(MAX_CTL_QUERY_TYPE != query_type);
@@ -300,7 +284,7 @@ static int ctl_exec_query_read(void *ctx, const umf_ctl_node_t *n,
300284
return -1;
301285
}
302286

303-
return n->cb[CTL_QUERY_READ](ctx, source, arg, indexes, NULL,
287+
return n->cb[CTL_QUERY_READ](ctx, source, arg, size, indexes, extra_name,
304288
MAX_CTL_QUERY_TYPE);
305289
}
306290

@@ -309,10 +293,10 @@ static int ctl_exec_query_read(void *ctx, const umf_ctl_node_t *n,
309293
*/
310294
static int ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
311295
umf_ctl_query_source_t source, void *arg,
312-
umf_ctl_index_utlist_t *indexes,
296+
size_t size, umf_ctl_index_utlist_t *indexes,
313297
const char *extra_name,
314298
umf_ctl_query_type_t query_type) {
315-
(void)extra_name, (void)query_type;
299+
(void)extra_name, (void)query_type, (void)size;
316300
assert(n != NULL);
317301
assert(n->cb[CTL_QUERY_WRITE] != NULL);
318302
assert(MAX_CTL_QUERY_TYPE != query_type);
@@ -327,8 +311,8 @@ static int ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
327311
return -1;
328312
}
329313

330-
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, indexes, NULL,
331-
MAX_CTL_QUERY_TYPE);
314+
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, size, indexes,
315+
extra_name, MAX_CTL_QUERY_TYPE);
332316
ctl_query_cleanup_real_args(n, real_arg, source);
333317

334318
return ret;
@@ -339,31 +323,32 @@ static int ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
339323
*/
340324
static int ctl_exec_query_runnable(void *ctx, const umf_ctl_node_t *n,
341325
umf_ctl_query_source_t source, void *arg,
342-
umf_ctl_index_utlist_t *indexes,
326+
size_t size, umf_ctl_index_utlist_t *indexes,
343327
const char *extra_name,
344328
umf_ctl_query_type_t query_type) {
345329
(void)extra_name, (void)query_type;
346330
assert(n != NULL);
347331
assert(n->cb[CTL_QUERY_RUNNABLE] != NULL);
348332
assert(MAX_CTL_QUERY_TYPE != query_type);
349-
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, indexes, NULL,
350-
MAX_CTL_QUERY_TYPE);
333+
return n->cb[CTL_QUERY_RUNNABLE](ctx, source, arg, size, indexes,
334+
extra_name, MAX_CTL_QUERY_TYPE);
351335
}
352336

353337
static int ctl_exec_query_subtree(void *ctx, const umf_ctl_node_t *n,
354338
umf_ctl_query_source_t source, void *arg,
355-
umf_ctl_index_utlist_t *indexes,
339+
size_t size, umf_ctl_index_utlist_t *indexes,
356340
const char *extra_name,
357341
umf_ctl_query_type_t query_type) {
358342
assert(n != NULL);
359343
assert(n->cb[CTL_QUERY_SUBTREE] != NULL);
360344
assert(MAX_CTL_QUERY_TYPE != query_type);
361-
return n->cb[CTL_QUERY_SUBTREE](ctx, source, arg, indexes, extra_name,
345+
return n->cb[CTL_QUERY_SUBTREE](ctx, source, arg, size, indexes, extra_name,
362346
query_type);
363347
}
364348

365349
typedef int (*umf_ctl_exec_query_t)(void *ctx, const umf_ctl_node_t *n,
366350
umf_ctl_query_source_t source, void *arg,
351+
size_t size,
367352
umf_ctl_index_utlist_t *indexes,
368353
const char *extra_name,
369354
umf_ctl_query_type_t query_type);
@@ -380,7 +365,8 @@ static umf_ctl_exec_query_t ctl_exec_query[MAX_CTL_QUERY_TYPE] = {
380365
* from the ctl tree
381366
*/
382367
int ctl_query(struct ctl *ctl, void *ctx, umf_ctl_query_source_t source,
383-
const char *name, umf_ctl_query_type_t type, void *arg) {
368+
const char *name, umf_ctl_query_type_t type, void *arg,
369+
size_t size) {
384370
if (name == NULL) {
385371
errno = EINVAL;
386372
return -1;
@@ -416,11 +402,14 @@ int ctl_query(struct ctl *ctl, void *ctx, umf_ctl_query_source_t source,
416402
errno = EINVAL;
417403
goto out;
418404
}
405+
const char *extra_name = &name[0];
406+
if (strstr(extra_name, "by_handle") != NULL) {
407+
extra_name = &name[0] + name_offset;
408+
}
419409

420-
const char *extra_name = &name[0] + name_offset;
421410
ret =
422411
ctl_exec_query[n->type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type](
423-
ctx, n, source, arg, indexes, extra_name, type);
412+
ctx, n, source, arg, size, indexes, extra_name, type);
424413
out:
425414
ctl_delete_indexes(indexes);
426415

@@ -487,7 +476,7 @@ static int ctl_load_config(struct ctl *ctl, void *ctx, char *buf) {
487476
}
488477

489478
r = ctl_query(ctl, ctx, CTL_QUERY_CONFIG_INPUT, name, CTL_QUERY_WRITE,
490-
value);
479+
value, 0);
491480

492481
if (r < 0 && ctx != NULL) {
493482
return -1;

src/ctl/ctl.h

+20-4
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,23 @@ typedef struct ctl_node {
9898
const struct ctl_node *children;
9999
} umf_ctl_node_t;
100100

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+
};
115+
101116
struct ctl *ctl_new(void);
102-
void ctl_delete(struct ctl *stats);
117+
void ctl_delete(struct ctl *c);
103118

104119
void initialize_global_ctl(void);
105120

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

140155
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);
156+
const char *name, umf_ctl_query_type_t type, void *arg,
157+
size_t size);
142158

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

src/libumf.def

+1
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,4 @@ EXPORTS
140140
umfCtlExec
141141
umfCtlGet
142142
umfCtlSet
143+
umfPoolGetName

src/libumf.map

+1
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,5 @@ UMF_0.12 {
140140
umfCtlExec;
141141
umfCtlGet;
142142
umfCtlSet;
143+
umfPoolGetName;
143144
} UMF_0.11;

0 commit comments

Comments
 (0)