Apollo
11.0
自动驾驶开放平台
cyber_benchmark_reader.cc
浏览该文件的文档.
1
/******************************************************************************
2
* Copyright 2024 The Apollo Authors. All Rights Reserved.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*****************************************************************************/
16
17
#include <getopt.h>
18
#include <libgen.h>
19
#include <string>
20
#include <vector>
21
22
#include "
cyber/cyber.h
"
23
#include "cyber/benchmark/benchmark_msg.pb.h"
24
25
#if __has_include("gperftools/profiler.h")
26
#include "gperftools/profiler.h"
27
#include "gperftools/heap-profiler.h"
28
#include "gperftools/malloc_extension.h"
29
#endif
30
31
using
apollo::cyber::benchmark::BenchmarkMsg
;
32
33
std::string
BINARY_NAME
=
"cyber_benchmark_reader"
;
// NOLINT
34
35
int
nums_of_reader
= 1;
36
bool
enable_cpuprofile
=
false
;
37
bool
enable_heapprofile
=
false
;
38
std::string
profile_filename
=
"cyber_benchmark_reader_cpu.prof"
;
// NOLINT
39
std::string
heapprofile_filename
=
"cyber_benchmark_reader_mem.prof"
;
// NOLINT
40
41
void
DisplayUsage
() {
42
AINFO
<<
"Usage: \n "
<<
BINARY_NAME
<<
" [OPTION]...\n"
43
<<
"Description: \n"
44
<<
" -h, --help: help information \n"
45
<<
" -n, --nums_of_reader=nums_of_reader: numbers of reader, "
46
"default value is 1\n"
47
<<
" -c, --cpuprofile: enable gperftools cpu profile\n"
48
<<
" -o, --profile_filename=filename: the filename to dump the "
49
"profile to, default value is cyber_benchmark_writer_cpu.prof. "
50
"Only work with -c option\n"
51
<<
" -H, --heapprofile: enable gperftools heap profile\n"
52
<<
" -O, --heapprofile_filename=filename: the filename "
53
" to dump the profile to, default value is "
54
"cyber_benchmark_writer_mem.prof. Only work with -H option\n"
55
<<
"Example:\n"
56
<<
" "
<<
BINARY_NAME
<<
" -h\n"
57
<<
" "
<<
BINARY_NAME
<<
" -n 1\n"
58
<<
" "
<<
BINARY_NAME
<<
" -n 10 -c -H "
;
59
}
60
61
void
GetOptions
(
const
int
argc,
char
*
const
argv[]) {
62
opterr = 0;
// extern int opterr
63
int
long_index = 0;
64
const
std::string short_opts =
"hn:co:HO:"
;
65
static
const
struct
option long_opts[] = {
66
{
"help"
, no_argument,
nullptr
,
'h'
},
67
{
"nums_of_reader"
, required_argument,
nullptr
,
'n'
},
68
{
"cpuprofile"
, no_argument,
nullptr
,
'c'
},
69
{
"profile_filename"
, required_argument,
nullptr
,
'o'
},
70
{
"heapprofile"
, no_argument,
nullptr
,
'H'
},
71
{
"heapprofile_filename"
, required_argument,
nullptr
,
'O'
},
72
{NULL, no_argument,
nullptr
, 0}};
73
74
// log command for info
75
std::string cmd(
""
);
76
for
(
int
i = 0; i < argc; ++i) {
77
cmd += argv[i];
78
cmd +=
" "
;
79
}
80
AINFO
<<
"command: "
<< cmd;
81
82
if
(1 == argc) {
83
DisplayUsage
();
84
exit(0);
85
}
86
87
do
{
88
int
opt =
89
getopt_long(argc, argv, short_opts.c_str(), long_opts, &long_index);
90
if
(opt == -1) {
91
break
;
92
}
93
switch
(opt) {
94
case
'n'
:
95
nums_of_reader
= std::stoi(std::string(optarg));
96
if
(
nums_of_reader
< 0) {
97
AERROR
<<
"Invalid numbers of reader. It should be grater than 0"
;
98
exit(-1);
99
}
100
break
;
101
case
'c'
:
102
#ifndef BASE_PROFILER_H_
103
AWARN
<<
"gperftools not installed, ignore perf parameters"
;
104
#endif
105
enable_cpuprofile
=
true
;
106
break
;
107
case
'o'
:
108
profile_filename
= std::string(optarg);
109
break
;
110
case
'H'
:
111
#ifndef BASE_PROFILER_H_
112
AWARN
<<
"gperftools not installed, ignore perf parameters"
;
113
#endif
114
enable_heapprofile
=
true
;
115
break
;
116
case
'O'
:
117
heapprofile_filename
= std::string(optarg);
118
break
;
119
case
'h'
:
120
DisplayUsage
();
121
exit(0);
122
default
:
123
break
;
124
}
125
}
while
(
true
);
126
127
if
(optind < argc) {
128
AINFO
<<
"Found non-option ARGV-element \""
<< argv[optind++] <<
"\""
;
129
DisplayUsage
();
130
exit(1);
131
}
132
}
133
134
int
main
(
int
argc,
char
** argv) {
135
GetOptions
(argc, argv);
136
apollo::cyber::Init
(argv[0],
BINARY_NAME
);
137
138
apollo::cyber::ReaderConfig
reader_config;
139
reader_config.
channel_name
=
"/apollo/cyber/benchmark"
;
140
141
std::vector<std::shared_ptr<apollo::cyber::Reader<BenchmarkMsg>>> vec;
142
143
for
(
int
i = 0; i <
nums_of_reader
; i++) {
144
std::string node_name =
BINARY_NAME
+
"-"
+ std::to_string(i);
145
auto
node =
apollo::cyber::CreateNode
(node_name);
146
vec.push_back(std::move(node->CreateReader<
BenchmarkMsg
>(
147
reader_config, [](
const
std::shared_ptr<BenchmarkMsg> m){})));
148
}
149
150
#ifndef NO_TCMALLOC
151
#ifdef BASE_PROFILER_H_
152
if
(
enable_cpuprofile
) {
153
ProfilerStart(
profile_filename
.c_str());
154
}
155
if
(
enable_heapprofile
) {
156
HeapProfilerStart(
heapprofile_filename
.c_str());
157
}
158
#endif
159
#endif
160
161
apollo::cyber::WaitForShutdown
();
162
163
#ifndef NO_TCMALLOC
164
#ifdef BASE_PROFILER_H_
165
if
(
enable_cpuprofile
) {
166
ProfilerStop();
167
}
168
if
(
enable_heapprofile
) {
169
HeapProfilerDump(
"Befor shutdown"
);
170
HeapProfilerStop();
171
}
172
#endif
173
#endif
174
175
return
0;
176
}
cyber.h
enable_heapprofile
bool enable_heapprofile
Definition
cyber_benchmark_reader.cc:37
DisplayUsage
void DisplayUsage()
Definition
cyber_benchmark_reader.cc:41
main
int main(int argc, char **argv)
Definition
cyber_benchmark_reader.cc:134
nums_of_reader
int nums_of_reader
Definition
cyber_benchmark_reader.cc:35
GetOptions
void GetOptions(const int argc, char *const argv[])
Definition
cyber_benchmark_reader.cc:61
heapprofile_filename
std::string heapprofile_filename
Definition
cyber_benchmark_reader.cc:39
profile_filename
std::string profile_filename
Definition
cyber_benchmark_reader.cc:38
BINARY_NAME
std::string BINARY_NAME
Definition
cyber_benchmark_reader.cc:33
enable_cpuprofile
bool enable_cpuprofile
Definition
cyber_benchmark_reader.cc:36
AERROR
#define AERROR
Definition
log.h:44
AINFO
#define AINFO
Definition
log.h:42
AWARN
#define AWARN
Definition
log.h:43
apollo::cyber::WaitForShutdown
void WaitForShutdown()
Definition
state.h:50
apollo::cyber::Init
bool Init(const char *binary_name, const std::string &dag_info)
Definition
init.cc:99
apollo::cyber::CreateNode
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition
cyber.cc:33
apollo::cyber::ReaderConfig
Definition
node_channel_impl.h:37
apollo::cyber::ReaderConfig::channel_name
std::string channel_name
Definition
node_channel_impl.h:53
apollo::cyber::benchmark::BenchmarkMsg
Definition
benchmark_msg.proto:5
cyber
benchmark
cyber_benchmark_reader.cc