Apollo 11.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 <ctime>
26#include <memory>
27#include <string>
28
29#include "cyber/proto/clock.pb.h"
30
31#include "cyber/binary.h"
32#include "cyber/common/file.h"
36#include "cyber/node/node.h"
39#include "cyber/sysmo/sysmo.h"
40#include "cyber/task/task.h"
41#include "cyber/time/clock.h"
45
46#include "gflags/gflags.h"
47
48namespace apollo {
49namespace cyber {
50
53
54namespace {
55
56const std::string& kClockChannel = "/clock";
57const std::string& kClockNode = "clock";
58
59bool g_atexit_registered = false;
60std::mutex g_mutex;
61std::unique_ptr<Node> clock_node;
62
63logger::AsyncLogger* async_logger = nullptr;
64
65void InitLogger(const char* binary_name) {
66 const char* slash = strrchr(binary_name, '/');
67 if (slash) {
69 } else {
71 }
72
73 // Init glog
74 google::InitGoogleLogging(binary_name);
75 google::SetLogDestination(google::ERROR, "");
76 google::SetLogDestination(google::WARNING, "");
77 google::SetLogDestination(google::FATAL, "");
78
79 // Init async logger
80 async_logger = new ::apollo::cyber::logger::AsyncLogger(
81 google::base::GetLogger(FLAGS_minloglevel));
82 google::base::SetLogger(FLAGS_minloglevel, async_logger);
83 async_logger->Start();
84}
85
86void StopLogger() { delete async_logger; }
87
88} // namespace
89
90void OnShutdown(int sig) {
91 (void)sig;
92 if (GetState() != STATE_SHUTDOWN) {
94 }
95}
96
97void ExitHandle() { Clear(); }
98
99bool Init(const char* binary_name, const std::string& dag_info) {
100 std::lock_guard<std::mutex> lg(g_mutex);
101 if (GetState() != STATE_UNINITIALIZED) {
102 return false;
103 }
104
105 InitLogger(binary_name);
106 auto thread = const_cast<std::thread*>(async_logger->LogThread());
107 scheduler::Instance()->SetInnerThreadAttr("async_log", thread);
108 SysMo::Instance();
109 std::signal(SIGINT, OnShutdown);
110 // Register exit handlers
111 if (!g_atexit_registered) {
112 if (std::atexit(ExitHandle) != 0) {
113 AERROR << "Register exit handle failed";
114 return false;
115 }
116 AINFO << "Register exit handle succ.";
117 g_atexit_registered = true;
118 }
120
121 auto global_data = GlobalData::Instance();
122 if (global_data->IsMockTimeMode()) {
123 auto node_name = kClockNode + std::to_string(getpid());
124 clock_node = std::unique_ptr<Node>(new Node(node_name));
125 auto cb =
126 [](const std::shared_ptr<const apollo::cyber::proto::Clock>& msg) {
127 if (msg->has_clock()) {
128 Clock::Instance()->SetNow(Time(msg->clock()));
129 }
130 };
131 clock_node->CreateReader<apollo::cyber::proto::Clock>(kClockChannel, cb);
132 }
133
134 if (dag_info != "") {
135 std::string dump_path;
136 if (dag_info.length() > 200) {
137 std::string truncated = dag_info.substr(0, 200);
138 dump_path = common::GetEnv(
139 "APOLLO_ENV_WORKROOT", "/apollo") + "/dumps/" + truncated;
140 } else {
141 dump_path = \
142 common::GetEnv("APOLLO_ENV_WORKROOT", "/apollo") + "/dumps/" + dag_info;
143 }
144 google::SetCommandLineOption("bvar_dump_file", dump_path.c_str());
145 } else {
146 statistics::Statistics::Instance()->DisableChanVar();
147 }
148 google::SetCommandLineOption("bvar_dump_exclude", "*qps");
149 google::SetCommandLineOption("bvar_dump", "true");
150
151 return true;
152}
153
154void Clear() {
155 std::lock_guard<std::mutex> lg(g_mutex);
157 return;
158 }
159 SysMo::CleanUp();
160 TaskManager::CleanUp();
161 TimingWheel::CleanUp();
163 service_discovery::TopologyManager::CleanUp();
164 transport::Transport::CleanUp();
165 StopLogger();
167}
168
169} // namespace cyber
170} // 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
elements in Cyber – Node, Channel, Service, Writer, Reader, Client and Server's relationship is prese...
#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:90
void SetState(const State &state)
Definition state.cc:30
void ExitHandle()
Definition init.cc:97
@ 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:154
bool Init(const char *binary_name, const std::string &dag_info)
Definition init.cc:99
State GetState()
Definition state.cc:28
class register implement
Definition arena_queue.h:37