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