Skip to content

Commit d7d4208

Browse files
committed
lib: add command line parser, allow setting global timeline buffer size
1 parent 7f1f94f commit d7d4208

File tree

9 files changed

+250
-6
lines changed

9 files changed

+250
-6
lines changed

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ set(HAWKTRACER_LISTENERS_SOURCES
3939
set(HAWKTRACER_CORE_SOURCES
4040
alloc.c
4141
bag.c
42+
command_line_parser.c
4243
event_id_provider.cpp
4344
event_utils.c
4445
events.c

lib/command_line_parser.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include "internal/command_line_parser.h"
2+
#include "internal/global_timeline.h"
3+
4+
#include <errno.h>
5+
#include <stdlib.h>
6+
#include <stdio.h>
7+
#include <string.h>
8+
9+
typedef HT_ErrorCode(*HT_CommandLineArgumentParser)(int, char**, int);
10+
11+
typedef struct
12+
{
13+
const char* argument;
14+
const char* help;
15+
HT_CommandLineArgumentParser parser;
16+
HT_Boolean is_flag;
17+
} HT_CommandLineArgument;
18+
19+
static HT_ErrorCode print_help(int argc, char** argv, int pos);
20+
static HT_ErrorCode set_global_timeline_buffer_size(int argc, char** argv, int pos);
21+
22+
HT_CommandLineArgument arguments[] = {
23+
{
24+
"--ht-global-timeline-buffer-size",
25+
"Set Global Timeline buffer size",
26+
set_global_timeline_buffer_size,
27+
HT_FALSE
28+
},
29+
{
30+
"--ht-help", "Print this help and exits the process",
31+
print_help,
32+
HT_TRUE
33+
}
34+
};
35+
36+
static HT_ErrorCode
37+
set_global_timeline_buffer_size(int argc, char** argv, int pos)
38+
{
39+
if (pos + 1 >= argc)
40+
{
41+
return HT_ERR_MISSING_ARGUMENT;
42+
}
43+
44+
const char* s = argv[pos + 1];
45+
char* end;
46+
errno = 0;
47+
unsigned long value = strtoul(s, &end, 10);
48+
49+
if (errno == ERANGE ||
50+
(sizeof(size_t) < sizeof(unsigned long) && (unsigned long)SIZE_MAX < value))
51+
{
52+
return HT_ERR_OUT_OF_RANGE;
53+
}
54+
55+
if (end - s == 0)
56+
{
57+
return HT_ERR_INVALID_FORMAT;
58+
}
59+
60+
ht_global_timeline_set_buffer_size((size_t) value);
61+
return HT_ERR_OK;
62+
}
63+
64+
static HT_ErrorCode
65+
print_help(int argc, char** argv, int pos)
66+
{
67+
HT_UNUSED(argc);
68+
HT_UNUSED(argv);
69+
HT_UNUSED(pos);
70+
71+
printf("HawkTracer options:\n");
72+
73+
for (unsigned int x = 0; x < sizeof(arguments) / sizeof(arguments[0]); x++)
74+
{
75+
printf(" %s %s %s\n",
76+
arguments[x].argument,
77+
arguments[x].is_flag ? " " : "VALUE",
78+
arguments[x].help);
79+
}
80+
81+
exit(0);
82+
83+
return HT_ERR_OK;
84+
}
85+
86+
void
87+
ht_command_line_parse_args(int argc, char** argv)
88+
{
89+
for (int i = 1; i < argc; i++)
90+
{
91+
for (unsigned int x = 0; x < sizeof(arguments) / sizeof(arguments[0]); x++)
92+
{
93+
const char* argname = arguments[x].argument;
94+
if (strcmp(argname, argv[i]) != 0)
95+
{
96+
continue;
97+
}
98+
99+
HT_ErrorCode error = arguments[x].parser(argc, argv, i);
100+
101+
if (error != HT_ERR_OK)
102+
{
103+
printf("Failed to process argument %s. Error code: %d\n",
104+
argname, error);
105+
}
106+
107+
if (arguments[x].is_flag == HT_FALSE)
108+
{
109+
i++;
110+
}
111+
break;
112+
}
113+
}
114+
}

lib/global_timeline.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1+
#include "internal/global_timeline.h"
12
#include "hawktracer/global_timeline.h"
23

4+
static size_t global_timeline_buffer_size = 1024;
5+
6+
void
7+
ht_global_timeline_set_buffer_size(size_t buffer_size)
8+
{
9+
global_timeline_buffer_size = buffer_size;
10+
}
11+
12+
size_t
13+
ht_global_timeline_get_buffer_size(void)
14+
{
15+
return global_timeline_buffer_size;
16+
}
17+
318
static HT_Timeline* _ht_global_timeline_create(void)
419
{
5-
HT_Timeline* c_timeline = ht_timeline_create(1024, HT_FALSE, HT_TRUE, "HT_GlobalTimeline", NULL);
20+
HT_Timeline* c_timeline = ht_timeline_create(global_timeline_buffer_size, HT_FALSE, HT_TRUE, "HT_GlobalTimeline", NULL);
621

722
ht_feature_callstack_enable(c_timeline);
823
ht_feature_cached_string_enable(c_timeline, HT_FALSE);

lib/include/hawktracer/base_types.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ typedef enum
6161
/** Format of an input data is invalid. */
6262
HT_ERR_INVALID_FORMAT,
6363
/** Invalid argument */
64-
HT_ERR_INVALID_ARGUMENT
64+
HT_ERR_INVALID_ARGUMENT,
65+
/** Out of range */
66+
HT_ERR_OUT_OF_RANGE,
67+
/** Missing argument */
68+
HT_ERR_MISSING_ARGUMENT
6569
} HT_ErrorCode;
6670

6771
/** Defines supported byte ordering */
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef HAWKTRACER_INTERNAL_COMMAND_LINE_PARSER_H
2+
#define HAWKTRACER_INTERNAL_COMMAND_LINE_PARSER_H
3+
4+
#include <hawktracer/macros.h>
5+
#include <hawktracer/base_types.h>
6+
7+
HT_DECLS_BEGIN
8+
9+
void ht_command_line_parse_args(int argc, char** argv);
10+
11+
HT_DECLS_END
12+
13+
#endif /* HAWKTRACER_INTERNAL_COMMAND_LINE_PARSER_H */
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef HAWKTRACER_INTERNAL_GLOBAL_TIMELINE_H
2+
#define HAWKTRACER_INTERNAL_GLOBAL_TIMELINE_H
3+
4+
#include <hawktracer/macros.h>
5+
6+
#include <stddef.h>
7+
8+
HT_DECLS_BEGIN
9+
10+
void ht_global_timeline_set_buffer_size(size_t buffer_size);
11+
12+
size_t ht_global_timeline_get_buffer_size(void);
13+
14+
HT_DECLS_END
15+
16+
#endif /* HAWKTRACER_INTERNAL_GLOBAL_TIMELINE_H */

lib/init.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
#include "hawktracer/scoped_tracepoint.h"
44
#include "internal/registry.h"
55
#include "internal/feature.h"
6+
#include "internal/command_line_parser.h"
67

78
#ifdef HT_USE_PTHREADS
89
# include "hawktracer/posix_mapped_tracepoint.h"
910
#endif
1011

12+
1113
static int _ht_init_counter = 0;
1214

1315
void
1416
ht_init(int argc, char** argv)
1517
{
16-
/* For future use */
17-
HT_UNUSED(argc);
18-
HT_UNUSED(argv);
18+
ht_command_line_parse_args(argc, argv);
1919

2020
ht_registry_init();
2121

@@ -44,7 +44,6 @@ ht_is_initialized(void)
4444
return _ht_init_counter > 0;
4545
}
4646

47-
4847
void
4948
ht_deinit(void)
5049
{

tests/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(LIB_TEST_SOURCES
44
${CMAKE_CURRENT_SOURCE_DIR}/test_alloc.cpp
55
${CMAKE_CURRENT_SOURCE_DIR}/test_allocator.cpp
66
${CMAKE_CURRENT_SOURCE_DIR}/test_bag.cpp
7+
${CMAKE_CURRENT_SOURCE_DIR}/test_command_line_parser.cpp
78
${CMAKE_CURRENT_SOURCE_DIR}/test_duration_conversion.cpp
89
${CMAKE_CURRENT_SOURCE_DIR}/test_feature_cached_string.cpp
910
${CMAKE_CURRENT_SOURCE_DIR}/test_feature_callstack.cpp
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <internal/command_line_parser.h>
2+
#include <internal/global_timeline.h>
3+
4+
#include "test_common.h"
5+
6+
class TestCommandLineParserLib : public ::testing::Test
7+
{
8+
protected:
9+
void SetUp() override
10+
{
11+
_buff_size = ht_global_timeline_get_buffer_size();
12+
}
13+
14+
void TearDown() override
15+
{
16+
ht_global_timeline_set_buffer_size(_buff_size);
17+
}
18+
19+
size_t _buff_size;
20+
};
21+
22+
TEST_F(TestCommandLineParserLib, SettingBufferSizeShouldPassForValidInput)
23+
{
24+
// Arrange
25+
const char* args[] = {"app", "--ht-global-timeline-buffer-size", "76"};
26+
27+
// Act
28+
ht_command_line_parse_args(3, (char**)args);
29+
30+
// Assert
31+
ASSERT_EQ(76u, ht_global_timeline_get_buffer_size());
32+
33+
// Cleanup
34+
ht_global_timeline_set_buffer_size(_buff_size);
35+
}
36+
37+
TEST_F(TestCommandLineParserLib, SettingBufferSizeShouldFailForOutOfRangeValue)
38+
{
39+
// Arrange
40+
const char* args[] = {"app", "--ht-global-timeline-buffer-size", "9999999999999999999999999999"};
41+
42+
// Act
43+
ht_command_line_parse_args(3, (char**)args);
44+
45+
// Assert
46+
ASSERT_EQ(_buff_size, ht_global_timeline_get_buffer_size());
47+
}
48+
49+
TEST_F(TestCommandLineParserLib, SettingBufferSizeShouldFailForInvalidString)
50+
{
51+
// Arrange
52+
const char* args[] = {"app", "--ht-global-timeline-buffer-size", "test"};
53+
54+
// Act
55+
ht_command_line_parse_args(3, (char**)args);
56+
57+
// Assert
58+
ASSERT_EQ(_buff_size, ht_global_timeline_get_buffer_size());
59+
}
60+
61+
TEST_F(TestCommandLineParserLib, SettingBufferSizeShouldFailIfValueIsMissing)
62+
{
63+
// Arrange
64+
const char* args[] = {"app", "--ht-global-timeline-buffer-size"};
65+
66+
// Act
67+
ht_command_line_parse_args(2, (char**)args);
68+
69+
// Assert
70+
ASSERT_EQ(_buff_size, ht_global_timeline_get_buffer_size());
71+
}
72+
73+
TEST_F(TestCommandLineParserLib, PassingInvalidArgumentShouldSkipTheArgument)
74+
{
75+
// Arrange
76+
const char* args[] = {"app", "--ht-non-existing-parameter"};
77+
78+
// Act
79+
ht_command_line_parse_args(2, (char**)args);
80+
}
81+

0 commit comments

Comments
 (0)