Apollo 10.0
自动驾驶开放平台
summary_monitor.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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 "cyber/common/log.h"
23
24DEFINE_string(summary_monitor_name, "SummaryMonitor",
25 "Name of the summary monitor.");
26
27DEFINE_double(system_status_publish_interval, 1,
28 "SystemStatus publish interval.");
29
30namespace apollo {
31namespace monitor {
32
33void SummaryMonitor::EscalateStatus(const ComponentStatus::Status new_status,
34 const std::string& message,
35 ComponentStatus* current_status) {
36 // Overwrite priority: FATAL > ERROR > WARN > OK > UNKNOWN.
37 if (new_status > current_status->status()) {
38 current_status->set_status(new_status);
39 if (!message.empty()) {
40 current_status->set_message(message);
41 } else {
42 current_status->clear_message();
43 }
44 }
45}
46
47// Set interval to 0, so it runs every time when ticking.
49 : RecurrentRunner(FLAGS_summary_monitor_name, 0) {}
50
51void SummaryMonitor::RunOnce(const double current_time) {
52 auto manager = MonitorManager::Instance();
53 auto* status = manager->GetStatus();
54 // Escalate the summary status to the most severe one.
55 for (auto& component : *status->mutable_components()) {
56 auto* summary = component.second.mutable_summary();
57 const auto& process_status = component.second.process_status();
58 EscalateStatus(process_status.status(), process_status.message(), summary);
59 const auto& module_status = component.second.module_status();
60 EscalateStatus(module_status.status(), module_status.message(), summary);
61 const auto& channel_status = component.second.channel_status();
62 EscalateStatus(channel_status.status(), channel_status.message(), summary);
63 const auto& resource_status = component.second.resource_status();
64 EscalateStatus(resource_status.status(), resource_status.message(),
65 summary);
66 const auto& other_status = component.second.other_status();
67 EscalateStatus(other_status.status(), other_status.message(), summary);
68 }
69
70 // For global components
71 for (auto& component : *status->mutable_global_components()) {
72 auto* summary = component.second.mutable_summary();
73 const auto& process_status = component.second.process_status();
74 EscalateStatus(process_status.status(), process_status.message(), summary);
75 const auto& resource_status = component.second.resource_status();
76 EscalateStatus(resource_status.status(), resource_status.message(),
77 summary);
78 }
79
80 // Get fingerprint of current status.
81 // Don't use DebugString() which has known bug on Map field. The string
82 // doesn't change though the value has changed.
83 static std::hash<std::string> hash_fn;
84 std::string proto_bytes;
85 status->SerializeToString(&proto_bytes);
86 const size_t new_fp = hash_fn(proto_bytes);
87
88 if (system_status_fp_ != new_fp ||
89 current_time - last_broadcast_ > FLAGS_system_status_publish_interval) {
90 static auto writer =
91 manager->CreateWriter<SystemStatus>(FLAGS_system_status_topic);
92
93 apollo::common::util::FillHeader("SystemMonitor", status);
94 writer->Write(*status);
95 status->clear_header();
96 system_status_fp_ = new_fp;
97 last_broadcast_ = current_time;
98 }
99}
100
101} // namespace monitor
102} // namespace apollo
void RunOnce(const double current_time) override
static void EscalateStatus(const ComponentStatus::Status new_status, const std::string &message, ComponentStatus *current_status)
class register implement
Definition arena_queue.h:37
Some string util functions.
DEFINE_string(summary_monitor_name, "SummaryMonitor", "Name of the summary monitor.")
DEFINE_double(system_status_publish_interval, 1, "SystemStatus publish interval.")