Apollo 10.0
自动驾驶开放平台
config_manager.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 *****************************************************************************/
17
19#include "cyber/common/file.h"
20#include "cyber/common/log.h"
23
24namespace apollo {
25namespace perception {
26namespace lib {
27
30
31ConfigManager::ConfigManager() {
32 work_root_ = FLAGS_work_root;
33
34 // For start at arbitrary path
35 if (work_root_.empty()) {
36 work_root_ = cyber::common::GetEnv("MODULE_PATH");
37 if (work_root_.empty()) {
38 work_root_ = cyber::common::GetEnv("CYBER_PATH");
39 }
40 }
41}
42
44 MutexLock lock(&mutex_);
45 return InitInternal();
46}
47
48bool ConfigManager::InitInternal() {
49 if (inited_) {
50 return true;
51 }
52 for (auto iter = model_config_map_.begin(); iter != model_config_map_.end();
53 ++iter) {
54 delete iter->second;
55 }
56 model_config_map_.clear();
57
58 std::string config_module_path =
59 GetAbsolutePath(work_root_, FLAGS_config_manager_path);
60 AINFO << "WORK_ROOT: " << work_root_
61 << " config_root_path: " << config_module_path;
62
63 std::vector<std::string> model_config_files;
64 if (!algorithm::GetFileList(config_module_path, "config_manager.config",
65 &model_config_files)) {
66 AERROR << "config_root_path : " << config_module_path
67 << " get file list error.";
68 return false;
69 }
70
71 for (const auto &model_config_file : model_config_files) {
72 ModelConfigFileListProto file_list_proto;
73 if (!GetProtoFromASCIIFile(model_config_file, &file_list_proto)) {
74 AERROR << "Invalid ModelConfigFileListProto file: " << model_config_file;
75 return false;
76 }
77
78 for (const std::string &model_config_path :
79 file_list_proto.model_config_path()) {
80 const std::string abs_path =
81 GetAbsolutePath(work_root_, model_config_path);
82 MultiModelConfigProto multi_model_config_proto;
83 if (!GetProtoFromASCIIFile(abs_path, &multi_model_config_proto)) {
84 AERROR << "Invalid MultiModelConfigProto file: " << abs_path;
85 return false;
86 }
87
88 for (const ModelConfigProto &model_config_proto :
89 multi_model_config_proto.model_configs()) {
90 ModelConfig *model_config = new ModelConfig();
91 if (!model_config->Reset(model_config_proto)) {
92 return false;
93 }
94
95 AINFO << "load ModelConfig succ. name: " << model_config->name();
96
97 auto result =
98 model_config_map_.emplace(model_config->name(), model_config);
99 if (!result.second) {
100 AWARN << "duplicate ModelConfig, name: " << model_config->name();
101 return false;
102 }
103 }
104 }
105 }
106
107 AINFO << "finish to load ModelConfigs. NumModels: "
108 << model_config_map_.size();
109
110 inited_ = true;
111
112 return true;
113}
114
116 MutexLock lock(&mutex_);
117 inited_ = false;
118 return InitInternal();
119}
120
121bool ConfigManager::GetModelConfig(const std::string &model_name,
122 const ModelConfig **model_config) {
123 if (!inited_ && !Init()) {
124 return false;
125 }
126
127 auto citer = model_config_map_.find(model_name);
128 if (citer == model_config_map_.end()) {
129 return false;
130 }
131 *model_config = citer->second;
132 return true;
133}
134
136 for (auto iter = model_config_map_.begin(); iter != model_config_map_.end();
137 ++iter) {
138 delete iter->second;
139 }
140}
141
143 name_ = proto.name();
144 version_ = proto.version();
145
146 integer_param_map_.clear();
147 string_param_map_.clear();
148 double_param_map_.clear();
149 float_param_map_.clear();
150 bool_param_map_.clear();
151 array_integer_param_map_.clear();
152 array_string_param_map_.clear();
153 array_double_param_map_.clear();
154 array_float_param_map_.clear();
155 array_bool_param_map_.clear();
156
157 for (const KeyValueInt &pair : proto.integer_params()) {
158 integer_param_map_.emplace(pair.name(), pair.value());
159 }
160
161 for (const KeyValueString &pair : proto.string_params()) {
162 string_param_map_.emplace(pair.name(), pair.value());
163 }
164
165 for (const KeyValueDouble &pair : proto.double_params()) {
166 double_param_map_.emplace(pair.name(), pair.value());
167 }
168
169 for (const KeyValueFloat &pair : proto.float_params()) {
170 float_param_map_.emplace(pair.name(), pair.value());
171 }
172
173 for (const KeyValueBool &pair : proto.bool_params()) {
174 bool_param_map_.emplace(pair.name(), pair.value());
175 }
176
177 for (const KeyValueArrayInt &pair : proto.array_integer_params()) {
178 std::vector<int> values;
179 RepeatedToVector(pair.values(), &values);
180 array_integer_param_map_.emplace(pair.name(), values);
181 }
182
183 for (const KeyValueArrayString &pair : proto.array_string_params()) {
184 std::vector<std::string> values;
185 values.reserve(pair.values_size());
186 for (const std::string &value : pair.values()) {
187 values.push_back(value);
188 }
189 array_string_param_map_.emplace(pair.name(), values);
190 }
191
192 for (const KeyValueArrayDouble &pair : proto.array_double_params()) {
193 std::vector<double> values;
194 RepeatedToVector(pair.values(), &values);
195 array_double_param_map_.emplace(pair.name(), values);
196 }
197
198 for (const KeyValueArrayFloat &pair : proto.array_float_params()) {
199 std::vector<float> values;
200 RepeatedToVector(pair.values(), &values);
201 array_float_param_map_.emplace(pair.name(), values);
202 }
203
204 for (const KeyValueArrayBool &pair : proto.array_bool_params()) {
205 std::vector<bool> values;
206 RepeatedToVector(pair.values(), &values);
207 array_bool_param_map_.emplace(pair.name(), values);
208 }
209
210 AINFO << "reset ModelConfig. model_name: " << name_
211 << " integer_param_map's size: " << integer_param_map_.size()
212 << " string_param_map's size: " << string_param_map_.size()
213 << " double_param_map's size: " << double_param_map_.size()
214 << " float_param_map's size: " << float_param_map_.size()
215 << " bool_param_map's size: " << bool_param_map_.size()
216 << " array_integer_param_map's size: "
217 << array_integer_param_map_.size()
218 << " array_string_param_map's size: " << array_string_param_map_.size()
219 << " array_double_param_map's size: " << array_double_param_map_.size()
220 << " array_float_param_map's size: " << array_float_param_map_.size()
221 << " array_bool_param_map's size: " << array_bool_param_map_.size();
222
223 return true;
224}
225
226} // namespace lib
227} // namespace perception
228} // namespace apollo
bool GetModelConfig(const std::string &model_name, const ModelConfig **model_config)
bool Reset(const apollo::perception::ModelConfigProto &proto)
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
bool GetProtoFromASCIIFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as ascii representation of protobufs,...
Definition file.cc:89
std::string GetAbsolutePath(const std::string &prefix, const std::string &relative_path)
Get absolute path by concatenating prefix and relative_path.
Definition file.cc:179
std::string GetEnv(const std::string &var_name, const std::string &default_value="")
Definition environment.h:29
bool GetFileList(const std::string &path, const std::string &suffix, std::vector< std::string > *files)
Definition io_util.cc:163
class register implement
Definition arena_queue.h:37