Apollo 10.0
自动驾驶开放平台
global_data.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 <arpa/inet.h>
20#include <ifaddrs.h>
21#include <netdb.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25#include <cstdlib>
26#include <functional>
27
29#include "cyber/common/file.h"
30
31namespace apollo {
32namespace cyber {
33namespace common {
34
35AtomicHashMap<uint64_t, std::string, 512> GlobalData::node_id_map_;
36AtomicHashMap<uint64_t, std::string, 256> GlobalData::channel_id_map_;
37AtomicHashMap<uint64_t, std::string, 256> GlobalData::service_id_map_;
38AtomicHashMap<uint64_t, std::string, 256> GlobalData::task_id_map_;
39
40namespace {
41const std::string& kEmptyString = "";
42std::string program_path() {
43 char path[PATH_MAX];
44 auto len = readlink("/proc/self/exe", path, sizeof(path) - 1);
45 if (len == -1) {
46 return kEmptyString;
47 }
48 path[len] = '\0';
49 return std::string(path);
50}
51} // namespace
52
53GlobalData::GlobalData() {
54 InitHostInfo();
55 ACHECK(InitConfig());
56 process_id_ = getpid();
57 auto prog_path = program_path();
58 if (!prog_path.empty()) {
59 process_group_ = GetFileName(prog_path) + "_" + std::to_string(process_id_);
60 } else {
61 process_group_ = "cyber_default_" + std::to_string(process_id_);
62 }
63
64 const auto& run_mode_conf = config_.run_mode_conf();
65 run_mode_ = run_mode_conf.run_mode();
66 clock_mode_ = run_mode_conf.clock_mode();
67}
68
70
71int GlobalData::ProcessId() const { return process_id_; }
72
73void GlobalData::SetProcessGroup(const std::string& process_group) {
74 process_group_ = process_group;
75}
76const std::string& GlobalData::ProcessGroup() const { return process_group_; }
77
78void GlobalData::SetComponentNums(const int component_nums) {
79 component_nums_ = component_nums;
80}
81int GlobalData::ComponentNums() const { return component_nums_; }
82
83void GlobalData::SetSchedName(const std::string& sched_name) {
84 sched_name_ = sched_name;
85}
86const std::string& GlobalData::SchedName() const { return sched_name_; }
87
88const std::string& GlobalData::HostIp() const { return host_ip_; }
89
90const std::string& GlobalData::HostName() const { return host_name_; }
91
93 run_mode_ = RunMode::MODE_SIMULATION;
94}
95
96void GlobalData::DisableSimulationMode() { run_mode_ = RunMode::MODE_REALITY; }
97
99 return run_mode_ == RunMode::MODE_REALITY;
100}
101
103 return clock_mode_ == ClockMode::MODE_MOCK;
104}
105
106bool GlobalData::IsChannelEnableArenaShm(std::string channel_name) const {
107 if (!config_.has_transport_conf() ||
108 !config_.transport_conf().has_shm_conf() ||
109 !config_.transport_conf().shm_conf().has_arena_shm_conf()) {
110 return false;
111 }
112 bool found = false;
113 for (auto arena_channel_conf : config_.transport_conf()
114 .shm_conf()
115 .arena_shm_conf()
116 .arena_channel_conf()) {
117 if (channel_name == arena_channel_conf.channel_name()) {
118 found = true;
119 break;
120 }
121 }
122 return found;
123}
124
125bool GlobalData::IsChannelEnableArenaShm(uint64_t channel_id) const {
126 auto channel_name =
127 cyber::common::GlobalData::Instance()->GetChannelById(channel_id);
128 return IsChannelEnableArenaShm(channel_name);
129}
130
132 std::string channel_name) const& {
133 for (auto arena_channel_conf : config_.transport_conf()
134 .shm_conf()
135 .arena_shm_conf()
136 .arena_channel_conf()) {
137 if (channel_name == arena_channel_conf.channel_name()) {
138 return arena_channel_conf;
139 }
140 }
142}
143
145 uint64_t channel_id) const& {
146 auto channel_name =
147 cyber::common::GlobalData::Instance()->GetChannelById(channel_id);
148 return GetChannelArenaConf(channel_name);
149}
150
151void GlobalData::InitHostInfo() {
152 char host_name[1024];
153 gethostname(host_name, sizeof(host_name));
154 host_name_ = host_name;
155
156 host_ip_ = "127.0.0.1";
157
158 // if we have exported a non-loopback CYBER_IP, we will use it firstly,
159 // otherwise, we try to find first non-loopback ipv4 addr.
160 const char* ip_env = getenv("CYBER_IP");
161 if (ip_env != nullptr) {
162 // maybe we need to verify ip_env
163 std::string ip_env_str(ip_env);
164 std::string starts = ip_env_str.substr(0, 3);
165 if (starts != "127") {
166 host_ip_ = ip_env_str;
167 AINFO << "host ip: " << host_ip_;
168 return;
169 }
170 }
171
172 ifaddrs* ifaddr = nullptr;
173 if (getifaddrs(&ifaddr) != 0) {
174 AERROR << "getifaddrs failed, we will use 127.0.0.1 as host ip.";
175 return;
176 }
177 for (ifaddrs* ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
178 if (ifa->ifa_addr == nullptr) {
179 continue;
180 }
181 int family = ifa->ifa_addr->sa_family;
182 if (family != AF_INET) {
183 continue;
184 }
185 char addr[NI_MAXHOST] = {0};
186 if (getnameinfo(ifa->ifa_addr, sizeof(sockaddr_in), addr, NI_MAXHOST, NULL,
187 0, NI_NUMERICHOST) != 0) {
188 continue;
189 }
190 std::string tmp_ip(addr);
191 std::string starts = tmp_ip.substr(0, 3);
192 if (starts != "127") {
193 host_ip_ = tmp_ip;
194 break;
195 }
196 }
197 freeifaddrs(ifaddr);
198 AINFO << "host ip: " << host_ip_;
199}
200
201bool GlobalData::InitConfig() {
202 auto config_path = GetAbsolutePath(WorkRoot(), "conf/cyber.pb.conf");
203 if (!GetProtoFromFile(config_path, &config_)) {
204 AERROR << "read cyber default conf failed!";
205 return false;
206 }
207
208 return true;
209}
210
211const CyberConfig& GlobalData::Config() const { return config_; }
212
213uint64_t GlobalData::RegisterNode(const std::string& node_name) {
214 auto id = Hash(node_name);
215 while (node_id_map_.Has(id)) {
216 std::string* name = nullptr;
217 node_id_map_.Get(id, &name);
218 if (node_name == *name) {
219 break;
220 }
221 ++id;
222 AWARN << " Node name hash collision: " << node_name << " <=> " << *name;
223 }
224 node_id_map_.Set(id, node_name);
225 return id;
226}
227
228std::string GlobalData::GetNodeById(uint64_t id) {
229 std::string* node_name = nullptr;
230 if (node_id_map_.Get(id, &node_name)) {
231 return *node_name;
232 }
233 return kEmptyString;
234}
235
236uint64_t GlobalData::RegisterChannel(const std::string& channel) {
237 auto id = Hash(channel);
238 while (channel_id_map_.Has(id)) {
239 std::string* name = nullptr;
240 channel_id_map_.Get(id, &name);
241 if (channel == *name) {
242 break;
243 }
244 ++id;
245 AWARN << "Channel name hash collision: " << channel << " <=> " << *name;
246 }
247 channel_id_map_.Set(id, channel);
248 return id;
249}
250
251std::string GlobalData::GetChannelById(uint64_t id) {
252 std::string* channel = nullptr;
253 if (channel_id_map_.Get(id, &channel)) {
254 return *channel;
255 }
256 return kEmptyString;
257}
258
259uint64_t GlobalData::RegisterService(const std::string& service) {
260 auto id = Hash(service);
261 while (service_id_map_.Has(id)) {
262 std::string* name = nullptr;
263 service_id_map_.Get(id, &name);
264 if (service == *name) {
265 break;
266 }
267 ++id;
268 AWARN << "Service name hash collision: " << service << " <=> " << *name;
269 }
270 service_id_map_.Set(id, service);
271 return id;
272}
273
274std::string GlobalData::GetServiceById(uint64_t id) {
275 std::string* service = nullptr;
276 if (service_id_map_.Get(id, &service)) {
277 return *service;
278 }
279 return kEmptyString;
280}
281
282uint64_t GlobalData::RegisterTaskName(const std::string& task_name) {
283 auto id = Hash(task_name);
284 while (task_id_map_.Has(id)) {
285 std::string* name = nullptr;
286 task_id_map_.Get(id, &name);
287 if (task_name == *name) {
288 break;
289 }
290 ++id;
291 AWARN << "Task name hash collision: " << task_name << " <=> " << *name;
292 }
293 task_id_map_.Set(id, task_name);
294 return id;
295}
296
297std::string GlobalData::GetTaskNameById(uint64_t id) {
298 std::string* task_name = nullptr;
299 if (task_id_map_.Get(id, &task_name)) {
300 return *task_name;
301 }
302 return kEmptyString;
303}
304
305} // namespace common
306} // namespace cyber
307} // namespace apollo
const std::string & HostIp() const
const std::string & SchedName() const
bool IsChannelEnableArenaShm(std::string channel_name) const
void SetSchedName(const std::string &sched_name)
static std::string GetServiceById(uint64_t id)
void SetProcessGroup(const std::string &process_group)
const std::string & HostName() const
const std::string & ProcessGroup() const
static std::string GetChannelById(uint64_t id)
static uint64_t RegisterNode(const std::string &node_name)
static std::string GetTaskNameById(uint64_t id)
apollo::cyber::proto::ArenaChannelConf GetChannelArenaConf(std::string channel_name) const &
static uint64_t RegisterService(const std::string &service)
static std::string GetNodeById(uint64_t id)
static uint64_t RegisterChannel(const std::string &channel)
void SetComponentNums(const int component_nums)
const CyberConfig & Config() const
static uint64_t RegisterTaskName(const std::string &task_name)
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
std::size_t Hash(const std::string &key)
Definition util.h:27
std::string GetFileName(const std::string &path, const bool remove_extension)
Definition file.cc:415
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
const std::string WorkRoot()
Definition environment.h:40
class register implement
Definition arena_queue.h:37