Apollo 10.0
自动驾驶开放平台
init.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
17#include "cyber/init.h"
18
19#include <libgen.h>
20#include <sys/types.h>
21#include <unistd.h>
22
23#include <csignal>
24#include <cstdio>
25#include <cstdlib>
26#include <ctime>
27#include <filesystem>
28#include <memory>
29#include <string>
30
31#include "gflags/gflags.h"
32
33#include "cyber/proto/clock.pb.h"
34
35#include "cyber/binary.h"
36#include "cyber/common/file.h"
40#include "cyber/node/node.h"
44#include "cyber/sysmo/sysmo.h"
45#include "cyber/task/task.h"
46#include "cyber/time/clock.h"
49
50namespace apollo {
51namespace cyber {
52
53namespace {
54
55const std::string& kClockChannel = "/clock";
56const std::string& kClockNode = "clock";
57
58bool g_atexit_registered = false;
59std::mutex g_mutex;
60std::unique_ptr<Node> clock_node;
61
62logger::AsyncLogger* async_logger = nullptr;
63
64void InitLogger(const char* binary_name) {
65 const char* slash = strrchr(binary_name, '/');
66 if (slash) {
68 } else {
70 }
71
72 // Init glog
73 google::InitGoogleLogging(binary_name);
74 google::SetLogDestination(google::ERROR, "");
75 google::SetLogDestination(google::WARNING, "");
76 google::SetLogDestination(google::FATAL, "");
77
78 // Init async logger
79 async_logger = new ::apollo::cyber::logger::AsyncLogger(
80 google::base::GetLogger(FLAGS_minloglevel));
81 google::base::SetLogger(FLAGS_minloglevel, async_logger);
82 async_logger->Start();
83}
84
85void StopLogger() { delete async_logger; }
86
87} // namespace
88
89void OnShutdown(int sig) {
90 (void)sig;
91 if (GetState() != STATE_SHUTDOWN) {
93 }
94}
95
96void ExitHandle() { Clear(); }
97
98bool Init(const char* binary_name, const std::string& dag_info) {
99 const char* apollo_runtime_path = std::getenv("APOLLO_RUNTIME_PATH");
100 if (apollo_runtime_path != nullptr) {
101 if (std::filesystem::is_directory(
102 std::filesystem::status(apollo_runtime_path))) {
103 std::filesystem::current_path(apollo_runtime_path);
104 }
105 }
106
107 std::lock_guard<std::mutex> lg(g_mutex);
108 if (GetState() != STATE_UNINITIALIZED) {
109 return false;
110 }
111
112 InitLogger(binary_name);
113 auto thread = const_cast<std::thread*>(async_logger->LogThread());
114 scheduler::Instance()->SetInnerThreadAttr("async_log", thread);
115 SysMo::Instance();
116 std::signal(SIGINT, OnShutdown);
117 // Register exit handlers
118 if (!g_atexit_registered) {
119 if (std::atexit(ExitHandle) != 0) {
120 AERROR << "Register exit handle failed";
121 return false;
122 }
123 AINFO << "Register exit handle succ.";
124 g_atexit_registered = true;
125 }
127
128 auto global_data = GlobalData::Instance();
129 if (global_data->IsMockTimeMode()) {
130 auto node_name = kClockNode + std::to_string(getpid());
131 clock_node = std::unique_ptr<Node>(new Node(node_name));
132 auto cb =
133 [](const std::shared_ptr<const apollo::cyber::proto::Clock>& msg) {
134 if (msg->has_clock()) {
135 Clock::Instance()->SetNow(Time(msg->clock()));
136 }
137 };
138 clock_node->CreateReader<apollo::cyber::proto::Clock>(kClockChannel, cb);
139 }
140
141 if (dag_info != "") {
142 std::string dump_path;
143 if (dag_info.length() > 200) {
144 std::string truncated = dag_info.substr(0, 200);
145 dump_path = common::GetEnv("APOLLO_ENV_WORKROOT", "/apollo") + "/dumps/" +
146 truncated;
147 } else {
148 dump_path = common::GetEnv("APOLLO_ENV_WORKROOT", "/apollo") + "/dumps/" +
149 dag_info;
150 }
151 google::SetCommandLineOption("bvar_dump_file", dump_path.c_str());
152 } else {
153 statistics::Statistics::Instance()->DisableChanVar();
154 }
155 google::SetCommandLineOption("bvar_dump_exclude", "*qps");
156 google::SetCommandLineOption("bvar_dump", "true");
157
158 return true;
159}
160
161void Clear() {
162 std::lock_guard<std::mutex> lg(g_mutex);
164 return;
165 }
166 SysMo::CleanUp();
167 TaskManager::CleanUp();
168 TimingWheel::CleanUp();
170 service_discovery::TopologyManager::CleanUp();
171 transport::Transport::CleanUp();
172 StopLogger();
174}
175
176} // namespace cyber
177} // namespace apollo
Node is the fundamental building block of Cyber RT.
Definition node.h:44
Cyber has builtin time type Time.
Definition time.h:31
void SetInnerThreadAttr(const std::string &name, std::thread *thr)
Definition scheduler.cc:90
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
void SetName(const std::string &name)
Definition binary.cc:35
std::string GetEnv(const std::string &var_name, const std::string &default_value="")
Definition environment.h:29
void OnShutdown(int sig)
Definition init.cc:89
void SetState(const State &state)
Definition state.cc:30
void ExitHandle()
Definition init.cc:96
@ STATE_INITIALIZED
Definition state.h:36
@ STATE_SHUTDOWN
Definition state.h:38
@ STATE_SHUTTING_DOWN
Definition state.h:37
@ STATE_UNINITIALIZED
Definition state.h:35
void Clear()
Definition init.cc:161
bool Init(const char *binary_name, const std::string &dag_info)
Definition init.cc:98
State GetState()
Definition state.cc:28
class register implement
Definition arena_queue.h:37