Skip to content

Commit 44ec630

Browse files
committed
[CTL] Add sample CTL test
1 parent c05eac0 commit 44ec630

File tree

5 files changed

+120
-4
lines changed

5 files changed

+120
-4
lines changed

src/ctl/ctl.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ int ctl_arg_integer(const void *arg, void *dest, size_t dest_size);
114114
{sizeof(int), {{0, sizeof(int), ctl_arg_integer}, CTL_ARG_PARSER_END}};
115115

116116
#define CTL_ARG_LONG_LONG \
117-
{sizeof(long long), \
118-
{{0, sizeof(long long), ctl_arg_integer}, CTL_ARG_PARSER_END}};
117+
{ \
118+
sizeof(long long), { \
119+
{0, sizeof(long long), ctl_arg_integer}, CTL_ARG_PARSER_END \
120+
} \
121+
}
119122

120123
int ctl_arg_string(const void *arg, void *dest, size_t dest_size);
121124
#define CTL_ARG_STRING(len) \
@@ -191,13 +194,13 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
191194
#define CTL_LEAF_RW(name) \
192195
{ \
193196
CTL_STR(name), CTL_NODE_LEAF, \
194-
{CTL_READ_HANDLER(name), CTL_WRITE_HANDLER(name), NULL}, \
197+
{CTL_READ_HANDLER(name, ), CTL_WRITE_HANDLER(name, ), NULL}, \
195198
&CTL_ARG(name), NULL \
196199
}
197200

198201
#define CTL_REGISTER_MODULE(_ctl, name) \
199202
ctl_register_module_node((_ctl), CTL_STR(name), \
200-
(struct ctl_node *)CTL_NODE(name))
203+
(struct ctl_node *)CTL_NODE(name, ))
201204

202205
#ifdef __cplusplus
203206
}

src/ctl/ctl_debug.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/* Copyright 2018-2021, Intel Corporation */
3+
4+
/*
5+
* ctl_debug.c -- implementation of the debug CTL namespace
6+
*/
7+
8+
#include "ctl_debug.h"
9+
#include "ctl.h"
10+
11+
static struct ctl *ctl_debug;
12+
13+
static int alloc_pattern = 0;
14+
15+
struct ctl *get_debug_ctl(void) { return ctl_debug; }
16+
17+
/*
18+
* CTL_WRITE_HANDLER(alloc_pattern) -- sets the alloc_pattern field in heap
19+
*/
20+
static int CTL_WRITE_HANDLER(alloc_pattern, )(void *ctx,
21+
enum ctl_query_source source,
22+
void *arg,
23+
struct ctl_indexes *indexes) {
24+
/* suppress unused-parameter errors */
25+
(void)source, (void)indexes, (void)ctx;
26+
27+
int arg_in = *(int *)arg;
28+
alloc_pattern = arg_in;
29+
return 0;
30+
}
31+
32+
/*
33+
* CTL_READ_HANDLER(alloc_pattern) -- returns alloc_pattern heap field
34+
*/
35+
static int CTL_READ_HANDLER(alloc_pattern, )(void *ctx,
36+
enum ctl_query_source source,
37+
void *arg,
38+
struct ctl_indexes *indexes) {
39+
/* suppress unused-parameter errors */
40+
(void)source, (void)indexes, (void)ctx;
41+
42+
int *arg_out = arg;
43+
*arg_out = alloc_pattern;
44+
return 0;
45+
}
46+
47+
static const struct ctl_argument CTL_ARG(alloc_pattern) = CTL_ARG_LONG_LONG;
48+
49+
static const struct ctl_node CTL_NODE(heap, )[] = {CTL_LEAF_RW(alloc_pattern),
50+
51+
CTL_NODE_END};
52+
53+
static const struct ctl_node CTL_NODE(debug, )[] = {CTL_CHILD(heap, ),
54+
55+
CTL_NODE_END};
56+
57+
/*
58+
* debug_ctl_register -- registers ctl nodes for "debug" module
59+
*/
60+
void debug_ctl_register(struct ctl *ctl) { CTL_REGISTER_MODULE(ctl, debug); }
61+
62+
void initialize_debug_ctl(void) {
63+
ctl_debug = ctl_new();
64+
debug_ctl_register(ctl_debug);
65+
}
66+
67+
void deinitialize_debug_ctl(void) { ctl_delete(ctl_debug); }

src/ctl/ctl_debug.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
/* Copyright 2018-2020, Intel Corporation */
3+
4+
/*
5+
* ctl_debug.h -- definitions for CTL test
6+
*/
7+
#ifndef LIBPMEMOBJ_CTL_DEBUG_H
8+
#define LIBPMEMOBJ_CTL_DEBUG_H 1
9+
10+
#include "ctl.h"
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
void debug_ctl_register(struct ctl *ctl);
17+
struct ctl *get_debug_ctl(void);
18+
void initialize_debug_ctl(void);
19+
void deinitialize_debug_ctl(void);
20+
21+
#ifdef __cplusplus
22+
}
23+
#endif
24+
25+
#endif

test/ctl/config.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
debug.heap.alloc_pattern=1

test/ctl/test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <common/base.hpp>
2+
#include <ctl/ctl.h>
3+
#include <ctl/ctl_debug.h>
4+
5+
using namespace umf_test;
6+
7+
TEST_F(test, ctl_debug) {
8+
initialize_debug_ctl();
9+
auto ctl_handler = get_debug_ctl();
10+
ctl_load_config_from_string(ctl_handler, NULL,
11+
"debug.heap.alloc_pattern=1");
12+
13+
int value = 0;
14+
ctl_query(ctl_handler, NULL, CTL_QUERY_PROGRAMMATIC,
15+
"debug.heap.alloc_pattern", CTL_QUERY_READ, &value);
16+
ASSERT_EQ(value, 1);
17+
18+
debug_ctl_register(ctl_handler);
19+
deinitialize_debug_ctl();
20+
}

0 commit comments

Comments
 (0)