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_;
41const std::string& kEmptyString =
"";
42std::string program_path() {
44 auto len = readlink(
"/proc/self/exe", path,
sizeof(path) - 1);
49 return std::string(path);
53GlobalData::GlobalData() {
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_);
61 process_group_ =
"cyber_default_" + std::to_string(process_id_);
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();
74 process_group_ = process_group;
79 component_nums_ = component_nums;
84 sched_name_ = sched_name;
93 run_mode_ = RunMode::MODE_SIMULATION;
99 return run_mode_ == RunMode::MODE_REALITY;
103 return clock_mode_ == ClockMode::MODE_MOCK;
107 if (!config_.has_transport_conf() ||
108 !config_.transport_conf().has_shm_conf() ||
109 !config_.transport_conf().shm_conf().has_arena_shm_conf()) {
113 for (
auto arena_channel_conf : config_.transport_conf()
116 .arena_channel_conf()) {
117 if (channel_name == arena_channel_conf.channel_name()) {
127 cyber::common::GlobalData::Instance()->GetChannelById(channel_id);
132 std::string channel_name)
const& {
133 for (
auto arena_channel_conf : config_.transport_conf()
136 .arena_channel_conf()) {
137 if (channel_name == arena_channel_conf.channel_name()) {
138 return arena_channel_conf;
145 uint64_t channel_id)
const& {
147 cyber::common::GlobalData::Instance()->GetChannelById(channel_id);
148 return GetChannelArenaConf(channel_name);
151void GlobalData::InitHostInfo() {
152 char host_name[1024];
153 gethostname(host_name,
sizeof(host_name));
154 host_name_ = host_name;
156 host_ip_ =
"127.0.0.1";
160 const char* ip_env = getenv(
"CYBER_IP");
161 if (ip_env !=
nullptr) {
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_;
172 ifaddrs* ifaddr =
nullptr;
173 if (getifaddrs(&ifaddr) != 0) {
174 AERROR <<
"getifaddrs failed, we will use 127.0.0.1 as host ip.";
177 for (ifaddrs* ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
178 if (ifa->ifa_addr ==
nullptr) {
181 int family = ifa->ifa_addr->sa_family;
182 if (family != AF_INET) {
185 char addr[NI_MAXHOST] = {0};
186 if (getnameinfo(ifa->ifa_addr,
sizeof(sockaddr_in), addr, NI_MAXHOST, NULL,
187 0, NI_NUMERICHOST) != 0) {
190 std::string tmp_ip(addr);
191 std::string starts = tmp_ip.substr(0, 3);
192 if (starts !=
"127") {
198 AINFO <<
"host ip: " << host_ip_;
201bool GlobalData::InitConfig() {
204 AERROR <<
"read cyber default conf failed!";
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) {
222 AWARN <<
" Node name hash collision: " << node_name <<
" <=> " << *name;
224 node_id_map_.Set(
id, node_name);
229 std::string* node_name =
nullptr;
230 if (node_id_map_.Get(
id, &node_name)) {
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) {
245 AWARN <<
"Channel name hash collision: " << channel <<
" <=> " << *name;
247 channel_id_map_.Set(
id, channel);
252 std::string* channel =
nullptr;
253 if (channel_id_map_.Get(
id, &channel)) {
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) {
268 AWARN <<
"Service name hash collision: " << service <<
" <=> " << *name;
270 service_id_map_.Set(
id, service);
275 std::string* service =
nullptr;
276 if (service_id_map_.Get(
id, &service)) {
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) {
291 AWARN <<
"Task name hash collision: " << task_name <<
" <=> " << *name;
293 task_id_map_.Set(
id, task_name);
298 std::string* task_name =
nullptr;
299 if (task_id_map_.Get(
id, &task_name)) {
const std::string & HostIp() const
const std::string & SchedName() const
bool IsChannelEnableArenaShm(std::string channel_name) const
void SetSchedName(const std::string &sched_name)
int ComponentNums() const
bool IsRealityMode() const
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)
void DisableSimulationMode()
static uint64_t RegisterNode(const std::string &node_name)
void EnableSimulationMode()
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)
bool IsMockTimeMode() const
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,...
std::size_t Hash(const std::string &key)
std::string GetFileName(const std::string &path, const bool remove_extension)
std::string GetAbsolutePath(const std::string &prefix, const std::string &relative_path)
Get absolute path by concatenating prefix and relative_path.
const std::string WorkRoot()