Apollo 10.0
自动驾驶开放平台
module_argument.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
18
20
21#include <getopt.h>
22#include <libgen.h>
23
24#if __has_include("gperftools/profiler.h")
25#include "gperftools/profiler.h"
26#endif
27
29
30namespace apollo {
31namespace cyber {
32namespace mainboard {
33
35 AINFO << "Usage: \n " << binary_name_ << " [OPTION]...\n"
36 << "Description: \n"
37 << " -h, --help: help information \n"
38 << " -d, --dag_conf=CONFIG_FILE: module dag config file\n"
39 << " -p, --process_group=process_group: the process "
40 "namespace for running this module, default in manager process\n"
41 << " -s, --sched_name=sched_name: sched policy "
42 "conf for hole process, sched_name should be conf in cyber.pb.conf\n"
43 << " --plugin=plugin_description_file_path: the description file of "
44 "plugin\n"
45 << " --disable_plugin_autoload : default enable autoload "
46 "mode of plugins, use disable_plugin_autoload to ingore autoload\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 ${process_group}_cpu.prof. Only work "
50 "with -c option\n"
51 << " -H, --heapprofile: enable gperftools heap profile\n"
52 << " -O, --heapprofile_filename=filename: the filename to dump the "
53 "profile to, default value is ${process_group}_mem.prof. Only work "
54 "with -c option\n"
55 << "Example:\n"
56 << " " << binary_name_ << " -h\n"
57 << " " << binary_name_ << " -d dag_conf_file1 -d dag_conf_file2 "
58 << "-p process_group -s sched_name\n"
59 << " " << binary_name_ << " --plugin plugin_xml_conf -d dag_conf ";
60}
61
62void ModuleArgument::ParseArgument(const int argc, char* const argv[]) {
63 binary_name_ = std::string(basename(argv[0]));
64 GetOptions(argc, argv);
65
66 if (process_group_.empty()) {
67 process_group_ = DEFAULT_process_group_;
68 }
69
70 if (sched_name_.empty()) {
71 sched_name_ = DEFAULT_sched_name_;
72 }
73
74 if (enable_cpuprofile_ && profile_filename_.empty()) {
75 auto pwd = common::GetEnv("PWD");
76 profile_filename_ = pwd + "/" + process_group_ + std::string("_cpu.prof");
77 }
78
79 if (profile_filename_[0] != '/') {
80 auto pwd = common::GetEnv("PWD");
81 profile_filename_ = pwd + "/" + profile_filename_;
82 }
83
84 if (enable_heapprofile_ && heapprofile_filename_.empty()) {
85 auto pwd = common::GetEnv("PWD");
86 heapprofile_filename_ =
87 pwd + "/" + process_group_ + std::string("_mem.prof");
88 }
89
90 if (heapprofile_filename_[0] != '/') {
91 auto pwd = common::GetEnv("PWD");
92 heapprofile_filename_ = pwd + "/" + heapprofile_filename_;
93 }
94
95 GlobalData::Instance()->SetProcessGroup(process_group_);
96 GlobalData::Instance()->SetSchedName(sched_name_);
97 AINFO << "binary_name_ is " << binary_name_ << ", process_group_ is "
98 << process_group_ << ", has " << dag_conf_list_.size() << " dag conf";
99 for (std::string& dag : dag_conf_list_) {
100 AINFO << "dag_conf: " << dag;
101 }
102}
103
104void ModuleArgument::GetOptions(const int argc, char* const argv[]) {
105 opterr = 0; // extern int opterr
106 int long_index = 0;
107 const std::string short_opts = "hd:p:s:co:HO:";
108 static const struct option long_opts[] = {
109 {"help", no_argument, nullptr, 'h'},
110 {"dag_conf", required_argument, nullptr, 'd'},
111 {"process_name", required_argument, nullptr, 'p'},
112 {"sched_name", required_argument, nullptr, 's'},
113 {"plugin", required_argument, nullptr, ARGS_OPT_CODE_PLUGIN},
114 {"disable_plugin_autoload", no_argument, nullptr,
115 ARGS_OPT_CODE_DISABLE_PLUGIN_AUTOLOAD},
116 {"cpuprofile", no_argument, nullptr, 'c'},
117 {"profile_filename", required_argument, nullptr, 'o'},
118 {"heapprofile", no_argument, nullptr, 'H'},
119 {"heapprofile_filename", required_argument, nullptr, 'O'},
120 {NULL, no_argument, nullptr, 0}};
121
122 // log command for info
123 std::string cmd("");
124 for (int i = 0; i < argc; ++i) {
125 cmd += argv[i];
126 cmd += " ";
127 }
128 AINFO << "command: " << cmd;
129
130 if (1 == argc) {
131 DisplayUsage();
132 exit(0);
133 }
134
135 do {
136 int opt =
137 getopt_long(argc, argv, short_opts.c_str(), long_opts, &long_index);
138 if (opt == -1) {
139 break;
140 }
141 switch (opt) {
142 case 'd':
143 dag_conf_list_.emplace_back(std::string(optarg));
144 for (int i = optind; i < argc; i++) {
145 if (*argv[i] != '-') {
146 dag_conf_list_.emplace_back(std::string(argv[i]));
147 } else {
148 break;
149 }
150 }
151 break;
152 case 'p':
153 process_group_ = std::string(optarg);
154 break;
155 case 's':
156 sched_name_ = std::string(optarg);
157 break;
158 case ARGS_OPT_CODE_PLUGIN:
159 plugin_description_list_.emplace_back(std::string(optarg));
160 break;
161 case ARGS_OPT_CODE_DISABLE_PLUGIN_AUTOLOAD:
162 disable_plugin_autoload_ = true;
163 break;
164 case 'c':
165#ifndef BASE_PROFILER_H_
166 AWARN << "gperftools not installed, ignore perf parameters";
167#endif
168 enable_cpuprofile_ = true;
169 break;
170 case 'o':
171 profile_filename_ = std::string(optarg);
172 break;
173 case 'H':
174#ifndef BASE_PROFILER_H_
175 AWARN << "gperftools not installed, ignore perf parameters";
176#endif
177 enable_heapprofile_ = true;
178 break;
179 case 'O':
180 heapprofile_filename_ = std::string(optarg);
181 break;
182 case 'h':
183 DisplayUsage();
184 exit(0);
185 default:
186 break;
187 }
188 } while (true);
189
190 if (optind < argc) {
191 AINFO << "Found non-option ARGV-element \"" << argv[optind++] << "\"";
192 DisplayUsage();
193 exit(1);
194 }
195
196 if (dag_conf_list_.empty()) {
197 AINFO << "-d parameter must be specified";
198 DisplayUsage();
199 exit(1);
200 }
201}
202
203} // namespace mainboard
204} // namespace cyber
205} // namespace apollo
void GetOptions(const int argc, char *const argv[])
void ParseArgument(int argc, char *const argv[])
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
std::string GetEnv(const std::string &var_name, const std::string &default_value="")
Definition environment.h:29
class register implement
Definition arena_queue.h:37