28LatencyRecorder::LatencyRecorder(
const std::string& module_name)
29 : module_name_(module_name) {
34 const Time& begin_time,
35 const Time& end_time) {
38 if (begin_time >= end_time) {
41 static const int kErrorReduceBase = 1000;
44 if (!cyber::common::GlobalData::Instance()->IsRealityMode()) {
45 AERROR_EVERY(kErrorReduceBase) <<
"latency begin_time: " << begin_time
46 <<
" >= end_time: " << end_time <<
", "
47 << kErrorReduceBase <<
" times";
50 AERROR <<
"latency begin_time: " << begin_time
51 <<
" >= end_time: " << end_time;
55 static auto writer = CreateWriter();
56 if (writer ==
nullptr || message_id == 0) {
60 std::lock_guard<std::mutex> lock(mutex_);
62 auto* latency_record = records_->add_latency_records();
63 latency_record->set_begin_time(begin_time.
ToNanosecond());
65 latency_record->set_message_id(message_id);
69 if (now - current_time_ > kPublishInterval) {
70 PublishLatencyRecords(writer);
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";
82 if (node_ ==
nullptr) {
86 node_name_prefix, module_name_, current_time_.
ToNanosecond()));
87 if (node_ ==
nullptr) {
88 AERROR <<
"unable to create node for latency recording";
92 return node_->CreateWriter<LatencyRecordMap>(FLAGS_latency_recording_topic);
95void LatencyRecorder::PublishLatencyRecords(
97 records_->set_module_name(module_name_);
98 apollo::common::util::FillHeader(
"LatencyRecorderMap", records_.get());
99 writer->Write(*records_);
100 records_.reset(
new LatencyRecordMap);
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.
static Time Now()
PRECISION >= 1000000 means the precision is at least 1us.
Cyber has builtin time type Time.
uint64_t ToNanosecond() const
convert time to nanosecond.
#define AERROR_EVERY(freq)
Some string util functions.
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)