Skip to content

Commit

Permalink
paladin: simplify code and finish the latency monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
michael4338 authored and xiaoxq committed Nov 7, 2019
1 parent ee9ab05 commit 83e4e92
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
1 change: 1 addition & 0 deletions modules/monitor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cc_binary(
"//modules/monitor/hardware:socket_can_monitor",
"//modules/monitor/software:channel_monitor",
"//modules/monitor/software:functional_safety_monitor",
"//modules/monitor/software:latency_monitor",
"//modules/monitor/software:localization_monitor",
"//modules/monitor/software:process_monitor",
"//modules/monitor/software:recorder_monitor",
Expand Down
3 changes: 3 additions & 0 deletions modules/monitor/monitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "modules/monitor/hardware/socket_can_monitor.h"
#include "modules/monitor/software/channel_monitor.h"
#include "modules/monitor/software/functional_safety_monitor.h"
#include "modules/monitor/software/latency_monitor.h"
#include "modules/monitor/software/localization_monitor.h"
#include "modules/monitor/software/process_monitor.h"
#include "modules/monitor/software/recorder_monitor.h"
Expand Down Expand Up @@ -60,6 +61,8 @@ bool Monitor::Init() {
if (FLAGS_enable_functional_safety) {
runners_.emplace_back(new FunctionalSafetyMonitor());
}
// Monitor message processing latencies across modules
runners_.emplace_back(new LatencyMonitor());

return true;
}
Expand Down
34 changes: 14 additions & 20 deletions modules/monitor/software/latency_monitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>

#include "cyber/common/log.h"
#include "modules/common/adapters/adapter_gflags.h"
Expand Down Expand Up @@ -51,32 +52,25 @@ void FillInStat(const std::string& module_name, const uint64_t duration,
}

std::vector<std::pair<std::string, uint64_t>> GetFlowTrackStats(
std::vector<std::tuple<std::string, uint64_t, uint64_t>>* module_durations,
std::set<std::tuple<uint64_t, uint64_t, std::string>>* module_durations,
uint64_t* total_duration) {
const std::string module_connector = "->";
std::vector<std::pair<std::string, uint64_t>> stats;

// Sort by begin_time
std::sort(module_durations->begin(), module_durations->end(),
[](const std::tuple<std::string, uint64_t, uint64_t>& t1,
const std::tuple<std::string, uint64_t, uint64_t>& t2) {
return std::get<1>(t1) < std::get<1>(t2);
});

// Generate a list of <module, duration> pair, for example:
// <percetpion: 100>, <perception->prediction: 10>, <prediction: 50>, ...
auto iter = module_durations->begin();
std::string module_name, next_name;
uint64_t begin_time = 0, end_time = 0, next_begin_time = 0;
std::string module_name, prev_name;
uint64_t begin_time = 0, end_time, prev_end_time = 0;
while (iter != module_durations->end()) {
std::tie(module_name, begin_time, end_time) = *iter;
FillInStat(module_name, end_time - begin_time, &stats, total_duration);
const auto iter_next = iter + 1;
if (iter_next != module_durations->end()) {
std::tie(next_name, next_begin_time, std::ignore) = *iter_next;
FillInStat(StrCat(module_name, module_connector, next_name),
next_begin_time - end_time, &stats, total_duration);
std::tie(begin_time, end_time, module_name) = *iter;
if (!prev_name.empty()) {
FillInStat(StrCat(prev_name, module_connector, module_name),
begin_time - prev_end_time, &stats, total_duration);
}
FillInStat(module_name, end_time - begin_time, &stats, total_duration);
prev_name = module_name;
prev_end_time = end_time;
++iter;
}

Expand Down Expand Up @@ -126,9 +120,9 @@ void LatencyMonitor::RunOnce(const double current_time) {
void LatencyMonitor::UpdateLatencyStat(
const std::shared_ptr<LatencyRecordMap>& records) {
for (const auto& record : records->latency_records()) {
track_map_[record.first].emplace_back(records->module_name(),
record.second.begin_time(),
record.second.end_time());
track_map_[record.first].emplace(record.second.begin_time(),
record.second.end_time(),
records->module_name());
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/monitor/software/latency_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#pragma once

#include <memory>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

#include "modules/common/latency_recorder/proto/latency_record.pb.h"
#include "modules/monitor/common/recurrent_runner.h"
Expand All @@ -41,7 +41,7 @@ class LatencyMonitor : public RecurrentRunner {

apollo::common::LatencyReport latency_report_;
std::unordered_map<uint64_t,
std::vector<std::tuple<std::string, uint64_t, uint64_t>>>
std::set<std::tuple<uint64_t, uint64_t, std::string>>>
track_map_;
double flush_time_ = 0.0;
};
Expand Down

0 comments on commit 83e4e92

Please sign in to comment.