提交 389239b4 编写于 作者: A Aaron Xiao 提交者: Liangliang Zhang

Monitor: Migrate as cybertron TimerComponent.

上级 dbe70136
......@@ -3,12 +3,12 @@ load("//tools:cpplint.bzl", "cpplint")
package(default_visibility = ["//visibility:public"])
cc_library(
name = "monitor_lib",
name = "monitor",
srcs = ["monitor.cc"],
hdrs = ["monitor.h"],
deps = [
"//modules/common:apollo_app",
"//modules/common/adapters:adapter_manager",
"//framework:cybertron",
"//modules/common/util:message_util",
"//modules/monitor/common:recurrent_runner",
"//modules/monitor/hardware/gps:gps_monitor",
"//modules/monitor/hardware:resource_monitor",
......@@ -20,14 +20,7 @@ cc_library(
"//modules/monitor/software:summary_monitor",
"//modules/monitor/software:topic_monitor",
],
)
cc_binary(
name = "monitor",
srcs = ["main.cc"],
deps = [
":monitor_lib",
],
copts = ['-DMODULE_NAME=\\"monitor\\"'],
)
cpplint()
......@@ -41,48 +41,5 @@ void RecurrentRunner::Tick(const double current_time) {
}
}
RecurrentRunnerThread::RecurrentRunnerThread(const double interval)
: interval_ms_(interval * 1000) {
}
void RecurrentRunnerThread::RegisterRunner(
std::unique_ptr<RecurrentRunner> runner) {
runners_.emplace_back(std::move(runner));
}
void RecurrentRunnerThread::Start() {
CHECK(!thread_) << "Thread has already started.";
thread_.reset(new std::thread([this]() {
while (true) {
{
// Check stop sign.
std::lock_guard<std::mutex> guard(stop_mutex_);
if (stop_) {
return;
}
}
// Tick runners.
const double current_time = Clock::NowInSeconds();
MonitorManager::InitFrame(current_time);
for (auto &runner : runners_) {
runner->Tick(current_time);
}
std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms_));
}
}));
}
void RecurrentRunnerThread::Stop() {
CHECK(thread_) << "Thread has already stopped.";
{
std::lock_guard<std::mutex> guard(stop_mutex_);
stop_ = true;
}
thread_->join();
thread_.reset(nullptr);
}
} // namespace monitor
} // namespace apollo
......@@ -50,26 +50,6 @@ class RecurrentRunner {
double next_round_ = 0;
};
class RecurrentRunnerThread {
public:
explicit RecurrentRunnerThread(const double interval);
void RegisterRunner(std::unique_ptr<RecurrentRunner> runner);
// Start the thread of ticking all registered runners.
void Start();
// Stop the ticking thread.
void Stop();
private:
int64_t interval_ms_;
std::vector<std::unique_ptr<RecurrentRunner>> runners_;
std::unique_ptr<std::thread> thread_ = nullptr;
bool stop_ = false;
std::mutex stop_mutex_;
};
} // namespace monitor
} // namespace apollo
......
module_config {
module_library : "/apollo/bazel-bin/modules/monitor/libmonitor.so"
timer_components {
class_name : "Monitor"
config {
name: "monitor"
config_file_path: "/apollo/modules/monitor/conf/monitor_conf.pb.txt"
interval: 500
}
}
}
<cybertron>
<module>
<name>monitor</name>
<dag_conf>/apollo/modules/monitor/dag/monitor.dag</dag_conf>
<process_name>monitor</process_name>
</module>
</cybertron>
/******************************************************************************
* Copyright 2017 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "modules/monitor/monitor.h"
APOLLO_MAIN(apollo::monitor::Monitor);
......@@ -16,6 +16,7 @@
#include "modules/monitor/monitor.h"
#include "modules/common/adapters/adapter_manager.h"
#include "modules/common/time/time.h"
#include "modules/monitor/common/monitor_manager.h"
#include "modules/monitor/hardware/gps/gps_monitor.h"
#include "modules/monitor/hardware/resource_monitor.h"
......@@ -35,60 +36,53 @@ DEFINE_double(monitor_running_interval, 0.5, "Monitor running interval.");
namespace apollo {
namespace monitor {
using apollo::common::Status;
using apollo::common::adapter::AdapterManager;
using std::make_unique;
Monitor::Monitor() : monitor_thread_(FLAGS_monitor_running_interval) {
}
Status Monitor::Init() {
AdapterManager::Init(FLAGS_monitor_adapter_config_filename);
bool Monitor::Init() {
apollo::common::adapter::AdapterManager::Init(
FLAGS_monitor_adapter_config_filename);
// TODO(xiaoxq): Migrate CAN monitor.
// monitor_thread_.RegisterRunner(make_unique<CanMonitor>());
monitor_thread_.RegisterRunner(make_unique<GpsMonitor>());
monitor_thread_.RegisterRunner(make_unique<ProcessMonitor>());
// runners_.emplace_back(new CanMonitor());
runners_.emplace_back(new GpsMonitor());
runners_.emplace_back(new ProcessMonitor());
const auto &config = MonitorManager::GetConfig();
for (const auto &module : config.modules()) {
if (module.has_topic_conf()) {
auto *module_status = MonitorManager::GetModuleStatus(module.name());
monitor_thread_.RegisterRunner(make_unique<TopicMonitor>(
runners_.emplace_back(new TopicMonitor(
module.topic_conf(), module_status->mutable_topic_status()));
}
}
for (const auto &hardware : config.hardware()) {
if (hardware.has_topic_conf()) {
auto *hw_status = MonitorManager::GetHardwareStatus(hardware.name());
monitor_thread_.RegisterRunner(make_unique<TopicMonitor>(
runners_.emplace_back(new TopicMonitor(
hardware.topic_conf(), hw_status->mutable_topic_status()));
}
}
// Register resource monitor.
monitor_thread_.RegisterRunner(make_unique<ResourceMonitor>(
config.resource_conf()));
runners_.emplace_back(new ResourceMonitor(config.resource_conf()));
// Register online reporters.
if (MonitorManager::GetConfig().has_online_report_endpoint()) {
monitor_thread_.RegisterRunner(make_unique<VehicleStateReporter>());
runners_.emplace_back(new VehicleStateReporter());
}
// Register StaticInfo reporter.
monitor_thread_.RegisterRunner(make_unique<StaticInfoReporter>());
runners_.emplace_back(new StaticInfoReporter());
// Register the SummaryMonitor as last runner, so it will monitor all changes
// made by the previous runners.
monitor_thread_.RegisterRunner(make_unique<SummaryMonitor>());
return Status::OK();
runners_.emplace_back(new SummaryMonitor());
return true;
}
Status Monitor::Start() {
monitor_thread_.Start();
return Status::OK();
}
void Monitor::Stop() {
monitor_thread_.Stop();
bool Monitor::Proc() {
const double current_time = apollo::common::time::Clock::NowInSeconds();
MonitorManager::InitFrame(current_time);
for (auto &runner : runners_) {
runner->Tick(current_time);
}
return true;
}
} // namespace monitor
......
......@@ -20,7 +20,8 @@
#include <memory>
#include <string>
#include "modules/common/apollo_app.h"
#include "cybertron/component/timer_component.h"
#include "cybertron/cybertron.h"
#include "modules/monitor/common/recurrent_runner.h"
#include "modules/monitor/proto/system_status.pb.h"
......@@ -31,19 +32,16 @@
namespace apollo {
namespace monitor {
class Monitor : public apollo::common::ApolloApp {
class Monitor : public apollo::cybertron::TimerComponent {
public:
Monitor();
std::string Name() const override { return "SystemMonitor"; }
apollo::common::Status Init() override;
apollo::common::Status Start() override;
void Stop() override;
bool Init() override;
bool Proc() override;
private:
RecurrentRunnerThread monitor_thread_;
std::vector<std::unique_ptr<RecurrentRunner>> runners_;
};
CYBERTRON_REGISTER_COMPONENT(Monitor)
} // namespace monitor
} // namespace apollo
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册