Apollo 10.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/benchmark/benchmark_msg.pb.h"
23#include "cyber/cyber.h"
24
25#if __has_include("gperftools/profiler.h")
26#include "gperftools/heap-profiler.h"
27#include "gperftools/malloc_extension.h"
28#include "gperftools/profiler.h"
29#endif
30
32
33std::string BINARY_NAME = "cyber_benchmark_reader"; // NOLINT
34
36bool enable_cpuprofile = false;
37bool enable_heapprofile = false;
38std::string profile_filename = "cyber_benchmark_reader_cpu.prof"; // NOLINT
39std::string heapprofile_filename = "cyber_benchmark_reader_mem.prof"; // NOLINT
40
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
61void 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) {
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
134int main(int argc, char** argv) {
135 GetOptions(argc, argv);
136 google::SetCommandLineOption("bvar_dump_interval", "1");
137 apollo::cyber::Init(argv[0],
138 BINARY_NAME + "_" + std::to_string(nums_of_reader));
139
140 apollo::cyber::ReaderConfig reader_config;
141 reader_config.channel_name = "/apollo/cyber/benchmark";
142 reader_config.qos_profile.set_depth(10);
143
144 std::vector<std::shared_ptr<apollo::cyber::Reader<BenchmarkMsg>>> vec;
145
146 // for (int i = 0; i < nums_of_reader; i++) {
147 std::string node_name = BINARY_NAME + "-" + std::to_string(nums_of_reader);
148 auto node = apollo::cyber::CreateNode(node_name);
149 vec.push_back(std::move(node->CreateReader<BenchmarkMsg>(
150 reader_config, [](const std::shared_ptr<BenchmarkMsg> m) {})));
151 // }
152
153#ifndef NO_TCMALLOC
154#ifdef BASE_PROFILER_H_
155 if (enable_cpuprofile) {
156 ProfilerStart(profile_filename.c_str());
157 }
158 if (enable_heapprofile) {
159 HeapProfilerStart(heapprofile_filename.c_str());
160 }
161#endif
162#endif
163
165
166#ifndef NO_TCMALLOC
167#ifdef BASE_PROFILER_H_
168 if (enable_cpuprofile) {
169 ProfilerStop();
170 }
171 if (enable_heapprofile) {
172 HeapProfilerDump("Befor shutdown");
173 HeapProfilerStop();
174 }
175#endif
176#endif
177
178 return 0;
179}
bool enable_heapprofile
void DisplayUsage()
int main(int argc, char **argv)
int nums_of_reader
void GetOptions(const int argc, char *const argv[])
std::string heapprofile_filename
std::string profile_filename
std::string BINARY_NAME
bool enable_cpuprofile
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
void WaitForShutdown()
Definition state.h:50
bool Init(const char *binary_name, const std::string &dag_info)
Definition init.cc:98
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition cyber.cc:33