Skip to content

Commit 41d4edb

Browse files
committed
[CTL] Add sample CTL test
1 parent cdd3d46 commit 41d4edb

File tree

7 files changed

+132
-4
lines changed

7 files changed

+132
-4
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ set(BA_SOURCES
2626
${CMAKE_CURRENT_SOURCE_DIR}/base_alloc/base_alloc_global.c)
2727

2828
set(CTL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ctl/alloc.c
29-
${CMAKE_CURRENT_SOURCE_DIR}/ctl/ctl.c)
29+
${CMAKE_CURRENT_SOURCE_DIR}/ctl/ctl.c
30+
${CMAKE_CURRENT_SOURCE_DIR}/ctl/ctl_debug.c)
3031

3132
if(LINUX)
3233
set(BA_SOURCES ${BA_SOURCES} ${CTL_SOURCES}

src/ctl/ctl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int ctl_arg_integer(const void *arg, void *dest, size_t dest_size);
115115

116116
#define CTL_ARG_LONG_LONG \
117117
{sizeof(long long), \
118-
{{0, sizeof(long long), ctl_arg_integer}, CTL_ARG_PARSER_END}};
118+
{{0, sizeof(long long), ctl_arg_integer}, CTL_ARG_PARSER_END}}
119119

120120
int ctl_arg_string(const void *arg, void *dest, size_t dest_size);
121121
#define CTL_ARG_STRING(len) \
@@ -191,13 +191,13 @@ int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
191191
#define CTL_LEAF_RW(name) \
192192
{ \
193193
CTL_STR(name), CTL_NODE_LEAF, \
194-
{CTL_READ_HANDLER(name), CTL_WRITE_HANDLER(name), NULL}, \
194+
{CTL_READ_HANDLER(name, ), CTL_WRITE_HANDLER(name, ), NULL}, \
195195
&CTL_ARG(name), NULL \
196196
}
197197

198198
#define CTL_REGISTER_MODULE(_ctl, name) \
199199
ctl_register_module_node((_ctl), CTL_STR(name), \
200-
(struct ctl_node *)CTL_NODE(name))
200+
(struct ctl_node *)CTL_NODE(name, ))
201201

202202
#ifdef __cplusplus
203203
}

src/ctl/ctl_debug.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.h"
9+
#include "ctl_debug.h"
10+
11+
static struct ctl* ctl_debug;
12+
13+
static int alloc_pattern = 0;
14+
15+
struct ctl* get_debug_ctl(void) {
16+
return ctl_debug;
17+
}
18+
19+
/*
20+
* CTL_WRITE_HANDLER(alloc_pattern) -- sets the alloc_pattern field in heap
21+
*/
22+
static int
23+
CTL_WRITE_HANDLER(alloc_pattern, )(void *ctx,
24+
enum ctl_query_source source, void *arg, struct ctl_indexes *indexes)
25+
{
26+
/* suppress unused-parameter errors */
27+
(void)source, (void)indexes, (void)ctx;
28+
29+
int arg_in = *(int *)arg;
30+
alloc_pattern = arg_in;
31+
return 0;
32+
}
33+
34+
/*
35+
* CTL_READ_HANDLER(alloc_pattern) -- returns alloc_pattern heap field
36+
*/
37+
static int
38+
CTL_READ_HANDLER(alloc_pattern, )(void *ctx,
39+
enum ctl_query_source source, void *arg, struct ctl_indexes *indexes)
40+
{
41+
/* suppress unused-parameter errors */
42+
(void)source, (void)indexes, (void)ctx;
43+
44+
int *arg_out = arg;
45+
*arg_out = alloc_pattern;
46+
return 0;
47+
}
48+
49+
static const struct ctl_argument CTL_ARG(alloc_pattern) = CTL_ARG_LONG_LONG;
50+
51+
static const struct ctl_node CTL_NODE(heap, )[] = {
52+
CTL_LEAF_RW(alloc_pattern),
53+
54+
CTL_NODE_END
55+
};
56+
57+
static const struct ctl_node CTL_NODE(debug, )[] = {
58+
CTL_CHILD(heap, ),
59+
60+
CTL_NODE_END
61+
};
62+
63+
/*
64+
* debug_ctl_register -- registers ctl nodes for "debug" module
65+
*/
66+
void
67+
debug_ctl_register(struct ctl *ctl)
68+
{
69+
CTL_REGISTER_MODULE(ctl, debug);
70+
}
71+
72+
void initialize_debug_ctl(void) {
73+
ctl_debug = ctl_new();
74+
debug_ctl_register(ctl_debug);
75+
}
76+
77+
void deinitialize_debug_ctl(void) {
78+
ctl_delete(ctl_debug);
79+
}

src/ctl/ctl_debug.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
void debug_ctl_register(struct ctl *ctl);
15+
struct ctl* get_debug_ctl(void);
16+
void initialize_debug_ctl(void);
17+
void deinitialize_debug_ctl(void);
18+
19+
#ifdef __cplusplus
20+
}
21+
#endif
22+
23+
#endif

test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ add_umf_test(
173173
SRCS utils/utils_log.cpp ${UMF_UTILS_SOURCES}
174174
LIBS ${UMF_LOGGER_LIBS})
175175

176+
add_umf_test(
177+
NAME ctl
178+
SRCS ctl/test.cpp
179+
LIBS ${UMF_UTILS_FOR_TEST}
180+
)
176181
add_umf_test(
177182
NAME utils_common
178183
SRCS utils/utils.cpp

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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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, "debug.heap.alloc_pattern=1");
11+
12+
int value = 0;
13+
ctl_query(ctl_handler, NULL, CTL_QUERY_PROGRAMMATIC, "debug.heap.alloc_pattern",
14+
CTL_QUERY_READ, &value);
15+
ASSERT_EQ(value, 1);
16+
17+
debug_ctl_register(ctl_handler);
18+
deinitialize_debug_ctl();
19+
}

0 commit comments

Comments
 (0)