Apollo 10.0
自动驾驶开放平台
latency_recorder.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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
21
24
25namespace apollo {
26namespace common {
27
28LatencyRecorder::LatencyRecorder(const std::string& module_name)
29 : module_name_(module_name) {
30 records_.reset(new LatencyRecordMap);
31}
32
33void LatencyRecorder::AppendLatencyRecord(const uint64_t message_id,
34 const Time& begin_time,
35 const Time& end_time) {
36 // TODO(michael): ALERT for now for trouble shooting,
37 // CHECK_LT(begin_time, end_time) in the future to enforce the validation
38 if (begin_time >= end_time) {
39 // In Simulation mode, there might be large number of cases where
40 // begin_time == end_time, reduce the error frequency in this mode
41 static const int kErrorReduceBase = 1000;
42
43 // FIXME(storypku): IsRealityMode|MockTime
44 if (!cyber::common::GlobalData::Instance()->IsRealityMode()) {
45 AERROR_EVERY(kErrorReduceBase) << "latency begin_time: " << begin_time
46 << " >= end_time: " << end_time << ", "
47 << kErrorReduceBase << " times";
48 return;
49 }
50 AERROR << "latency begin_time: " << begin_time
51 << " >= end_time: " << end_time;
52 return;
53 }
54
55 static auto writer = CreateWriter();
56 if (writer == nullptr || message_id == 0) {
57 return;
58 }
59
60 std::lock_guard<std::mutex> lock(mutex_);
61
62 auto* latency_record = records_->add_latency_records();
63 latency_record->set_begin_time(begin_time.ToNanosecond());
64 latency_record->set_end_time(end_time.ToNanosecond());
65 latency_record->set_message_id(message_id);
66
67 const auto now = Clock::Now();
68 const apollo::cyber::Duration kPublishInterval(3.0);
69 if (now - current_time_ > kPublishInterval) {
70 PublishLatencyRecords(writer);
71 current_time_ = now;
72 }
73}
74
75std::shared_ptr<apollo::cyber::Writer<LatencyRecordMap>>
76LatencyRecorder::CreateWriter() {
77 const std::string node_name_prefix = "latency_recorder";
78 if (module_name_.empty()) {
79 AERROR << "missing module name for sending latency records";
80 return nullptr;
81 }
82 if (node_ == nullptr) {
83 current_time_ = Clock::Now();
84
85 node_ = apollo::cyber::CreateNode(absl::StrCat(
86 node_name_prefix, module_name_, current_time_.ToNanosecond()));
87 if (node_ == nullptr) {
88 AERROR << "unable to create node for latency recording";
89 return nullptr;
90 }
91 }
92 return node_->CreateWriter<LatencyRecordMap>(FLAGS_latency_recording_topic);
93}
94
95void LatencyRecorder::PublishLatencyRecords(
96 const std::shared_ptr<apollo::cyber::Writer<LatencyRecordMap>>& writer) {
97 records_->set_module_name(module_name_);
98 apollo::common::util::FillHeader("LatencyRecorderMap", records_.get());
99 writer->Write(*records_);
100 records_.reset(new LatencyRecordMap);
101}
102
103} // namespace common
104} // namespace apollo
void AppendLatencyRecord(const uint64_t message_id, const apollo::cyber::Time &begin_time, const apollo::cyber::Time &end_time)
a singleton clock that can be used to get the current timestamp.
Definition clock.h:39
static Time Now()
PRECISION >= 1000000 means the precision is at least 1us.
Definition clock.cc:40
Cyber has builtin time type Time.
Definition time.h:31
uint64_t ToNanosecond() const
convert time to nanosecond.
Definition time.cc:83
#define AERROR
Definition log.h:44
#define AERROR_EVERY(freq)
Definition log.h:86
Some string util functions.
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition cyber.cc:33
class register implement
Definition arena_queue.h:37