Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(autoware_processing_time_checker): add a trigger to choice whether to output metrics to log folder #9479

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions system/autoware_processing_time_checker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ ros2 launch autoware_processing_time_checker processing_time_checker.launch.xml

{{ json_to_markdown("system/autoware_processing_time_checker/schema/processing_time_checker.schema.json") }}

If `output_metrics = true`, the node writes the statics of the processing_time measured during its lifetime to `<ros2_logging_directory>/autoware_metrics/<node_name>-<time_stamp>.json` when shut down.

## Assumptions / Known limits

TBD.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<launch>
<arg name="config_file" default="$(find-pkg-share autoware_processing_time_checker)/config/processing_time_checker.param.yaml"/>
<!-- # if output_metrics=true, metrics are written to `<ros2_logging_directory>/autoware_metrics/<node_name>-<time_stamp>.json`. -->
<arg name="output_metrics" default="false"/>

<node pkg="autoware_processing_time_checker" exec="processing_time_checker_node" name="processing_time_checker" output="screen">
<param from="$(var config_file)"/>
<param name="output_metrics" value="$(var output_metrics)"/>
</node>
</launch>
2 changes: 2 additions & 0 deletions system/autoware_processing_time_checker/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>autoware_cmake</buildtool_depend>

<depend>autoware_universe_utils</depend>
<depend>nlohmann-json-dev</depend>
<depend>rclcpp</depend>
<depend>rclcpp_components</depend>
<depend>tier4_debug_msgs</depend>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@

#include "processing_time_checker.hpp"

#include <nlohmann/json.hpp>
#include <rclcpp/rclcpp.hpp>

#include <chrono>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>

Expand All @@ -38,6 +42,7 @@
ProcessingTimeChecker::ProcessingTimeChecker(const rclcpp::NodeOptions & node_options)
: Node("processing_time_checker", node_options)
{
output_metrics_ = declare_parameter<bool>("output_metrics");

Check warning on line 45 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L45

Added line #L45 was not covered by tests
const double update_rate = declare_parameter<double>("update_rate");
const auto processing_time_topic_name_list =
declare_parameter<std::vector<std::string>>("processing_time_topic_name_list");
Expand All @@ -47,7 +52,7 @@

// extract module name from topic name
auto tmp_topic_name = processing_time_topic_name;
for (size_t i = 0; i < 4; ++i) { // 4 is enouh for the search depth

Check warning on line 55 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View workflow job for this annotation

GitHub Actions / spell-check-differential

Unknown word (enouh)
tmp_topic_name = remove_last_name(tmp_topic_name);
const auto module_name_candidate = get_last_name(tmp_topic_name);
// clang-format off
Expand All @@ -64,6 +69,7 @@
// register module name
if (module_name) {
module_name_map_.insert_or_assign(processing_time_topic_name, *module_name);
processing_time_accumulator_map_.insert_or_assign(*module_name, Accumulator<double>());

Check warning on line 72 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L72

Added line #L72 was not covered by tests
} else {
throw std::invalid_argument("The format of the processing time topic name is not correct.");
}
Expand All @@ -79,6 +85,7 @@
processing_time_topic_name, 1,
[this, &module_name]([[maybe_unused]] const Float64Stamped & msg) {
processing_time_map_.insert_or_assign(module_name, msg.data);
processing_time_accumulator_map_.at(module_name).add(msg.data);
}));
// clang-format on
}
Expand All @@ -90,6 +97,54 @@
this, get_clock(), period_ns, std::bind(&ProcessingTimeChecker::on_timer, this));
}

ProcessingTimeChecker::~ProcessingTimeChecker()

Check warning on line 100 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L100

Added line #L100 was not covered by tests
{
if (!output_metrics_) {
return;

Check warning on line 103 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L103

Added line #L103 was not covered by tests
}

// generate json data
nlohmann::json j;
for (const auto & accumulator_iterator : processing_time_accumulator_map_) {
const auto module_name = accumulator_iterator.first;
const auto processing_time_accumulator = accumulator_iterator.second;
j[module_name + "/min"] = processing_time_accumulator.min();
j[module_name + "/max"] = processing_time_accumulator.max();
j[module_name + "/mean"] = processing_time_accumulator.mean();
j[module_name + "/count"] = processing_time_accumulator.count();
j[module_name + "/description"] = "processing time of " + module_name + "[ms]";

Check warning on line 115 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L110-L115

Added lines #L110 - L115 were not covered by tests
}

// get output folder
const std::string output_folder_str =
rclcpp::get_logging_directory().string() + "/autoware_metrics";

Check warning on line 120 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L120

Added line #L120 was not covered by tests
if (!std::filesystem::exists(output_folder_str)) {
if (!std::filesystem::create_directories(output_folder_str)) {
RCLCPP_ERROR(

Check warning on line 123 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L123

Added line #L123 was not covered by tests
this->get_logger(), "Failed to create directories: %s", output_folder_str.c_str());
return;

Check warning on line 125 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L125

Added line #L125 was not covered by tests
}
}

// get time stamp
std::time_t now_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm * local_time = std::localtime(&now_time_t);
std::ostringstream oss;
oss << std::put_time(local_time, "%Y-%m-%d-%H-%M-%S");

Check warning on line 133 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L130-L133

Added lines #L130 - L133 were not covered by tests
std::string cur_time_str = oss.str();

// Write metrics .json to file
const std::string output_file_str =
output_folder_str + "/autoware_processing_time_checker-" + cur_time_str + ".json";
std::ofstream f(output_file_str);

Check warning on line 139 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L138-L139

Added lines #L138 - L139 were not covered by tests
if (f.is_open()) {
f << j.dump(4);
f.close();

Check warning on line 142 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L141-L142

Added lines #L141 - L142 were not covered by tests
} else {
RCLCPP_ERROR(this->get_logger(), "Failed to open file: %s", output_file_str.c_str());

Check warning on line 144 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L144

Added line #L144 was not covered by tests
}
}

Check warning on line 146 in system/autoware_processing_time_checker/src/processing_time_checker.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_processing_time_checker/src/processing_time_checker.cpp#L146

Added line #L146 was not covered by tests

void ProcessingTimeChecker::on_timer()
{
// create MetricArrayMsg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef PROCESSING_TIME_CHECKER_HPP_
#define PROCESSING_TIME_CHECKER_HPP_

#include "autoware/universe_utils/math/accumulator.hpp"

#include <rclcpp/rclcpp.hpp>

#include <tier4_debug_msgs/msg/float64_stamped.hpp>
Expand All @@ -27,6 +29,7 @@

namespace autoware::processing_time_checker
{
using autoware::universe_utils::Accumulator;
using MetricMsg = tier4_metric_msgs::msg::Metric;
using MetricArrayMsg = tier4_metric_msgs::msg::MetricArray;
using tier4_debug_msgs::msg::Float64Stamped;
Expand All @@ -35,6 +38,7 @@ class ProcessingTimeChecker : public rclcpp::Node
{
public:
explicit ProcessingTimeChecker(const rclcpp::NodeOptions & node_options);
~ProcessingTimeChecker() override;

private:
void on_timer();
Expand All @@ -44,10 +48,15 @@ class ProcessingTimeChecker : public rclcpp::Node
rclcpp::Publisher<MetricArrayMsg>::SharedPtr metrics_pub_;
std::vector<rclcpp::Subscription<Float64Stamped>::SharedPtr> processing_time_subscribers_;

// parameters
bool output_metrics_;

// topic name - module name
std::unordered_map<std::string, std::string> module_name_map_{};
// module name - processing time
std::unordered_map<std::string, double> processing_time_map_{};
// module name - accumulator
std::unordered_map<std::string, Accumulator<double>> processing_time_accumulator_map_{};
};
} // namespace autoware::processing_time_checker

Expand Down
Loading