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(system): create a package to monitor component containers #7094

Merged
merged 54 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
d391fda
feat(component_monitor): create package
mebasoglu May 20, 2024
c8170f3
feat(component_monitor): read and publish cpu and mem usage rate
mebasoglu May 20, 2024
e79aed9
fix(component_monitor): use pid of current process
mebasoglu May 20, 2024
3b735ac
feat(component_monitor): publish usage as custom message
mebasoglu May 20, 2024
e68356c
refactor(component_monitor): use pidstat instead of top
mebasoglu May 21, 2024
b561438
refactor(component_monitor): create function to convert numbers to float
mebasoglu May 21, 2024
08802f7
fix(component_monitor): change command name to pidstat
mebasoglu May 21, 2024
e23d66c
refactor(component_monitor): create function to convert numbers to ui…
mebasoglu May 21, 2024
d4344cc
refactor(component_monitor): add const
mebasoglu May 21, 2024
584d838
refactor(component_monitor): tidy up code
mebasoglu May 21, 2024
d65c841
feat(component_monitor): publish only when there is a subscriber
mebasoglu May 21, 2024
b789bde
chore(component_monitor): explain env variable
mebasoglu May 21, 2024
616285e
chore(component_monitor): create launch file
mebasoglu May 21, 2024
d757d01
fix(component_monitor): get and publish stats only if there is a subs…
mebasoglu May 21, 2024
739cffa
style(pre-commit): autofix
pre-commit-ci[bot] May 22, 2024
7483a94
fix: package name according to guidelines
mebasoglu May 22, 2024
4a45160
fix: project structure according to guidelines
mebasoglu May 22, 2024
7d180d9
feat: add header and pid fields to msg
mebasoglu May 22, 2024
c72c3c2
chore: add license and fix cspell
mebasoglu May 23, 2024
db6eb56
chore: include headers to fix cpplint
mebasoglu May 23, 2024
ae7d73c
fix: package name
mebasoglu May 23, 2024
996ff5e
fix: pidstat params
mebasoglu May 23, 2024
a15b053
feat: change command to top again
mebasoglu May 23, 2024
62e83ee
style(pre-commit): autofix
pre-commit-ci[bot] May 23, 2024
7d77641
chore: fix cspell
mebasoglu May 23, 2024
bbd501c
chore(autoware_component_monitor): add description
mebasoglu Jun 5, 2024
e0c7708
feat: add try-catch block in order not to crash the whole component
mebasoglu Jun 5, 2024
3594478
chore: fix spell errors
mebasoglu Jun 5, 2024
231079d
refactor(autoware_component_monitor): make methods const and static
mebasoglu Jun 5, 2024
3f89054
refactor: remove delay field on top command
mebasoglu Jun 5, 2024
b02c41f
chore: add readme
mebasoglu Jun 5, 2024
8a75611
style(pre-commit): autofix
pre-commit-ci[bot] Jun 5, 2024
93bcb55
refactor: change msg type
mebasoglu Jun 6, 2024
ebf8103
feat: add system wide mem usage
mebasoglu Jun 6, 2024
014a319
refactor: change msg name
mebasoglu Jun 6, 2024
86e8f46
chore: add kibibytes to cspell ignore list
mebasoglu Jun 11, 2024
1c253cd
chore: fix readme
mebasoglu Jun 11, 2024
2ccb0a2
refactor: use type aliases inside the namespace
mebasoglu Jun 12, 2024
f167237
improve
Jun 13, 2024
7c46216
style(pre-commit): autofix
pre-commit-ci[bot] Jun 13, 2024
f22c3dd
launch update
Jun 13, 2024
2577af0
style(pre-commit): autofix
pre-commit-ci[bot] Jun 13, 2024
ed150ab
fix
Jun 13, 2024
b1276fc
style(pre-commit): autofix
pre-commit-ci[bot] Jun 13, 2024
0ebe6d9
feat: set publish rate as 5.0
mebasoglu Jun 24, 2024
aaae490
refactor: remove unused header
mebasoglu Jun 24, 2024
8c17836
refactor: move environment variables to outside run_system_command
mebasoglu Jun 24, 2024
b4db0fe
refactor: make function and argument const
mebasoglu Jun 24, 2024
fee2d04
refactor: remove namespace alias to make the code more explicit
mebasoglu Jun 24, 2024
1472973
feat: create tests for unit conversions
mebasoglu Jun 24, 2024
69b80a0
fix: make gib_to_bytes use ull numbers
mebasoglu Jun 24, 2024
39dc9c5
style(pre-commit): autofix
pre-commit-ci[bot] Jun 24, 2024
09b7142
change order
Jul 12, 2024
a1e9f36
add comment
Jul 12, 2024
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
31 changes: 31 additions & 0 deletions system/autoware_component_monitor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.8)
project(autoware_component_monitor)

find_package(autoware_cmake REQUIRED)
autoware_package()

find_package(Boost REQUIRED COMPONENTS
filesystem
)

ament_auto_add_library(${PROJECT_NAME} SHARED
src/component_monitor_node.cpp
)
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})

rclcpp_components_register_node(${PROJECT_NAME}
PLUGIN "autoware::component_monitor::ComponentMonitor"
EXECUTABLE ${PROJECT_NAME}_node
)

if(BUILD_TESTING)
ament_add_ros_isolated_gtest(test_unit_conversions test/test_unit_conversions.cpp)
target_link_libraries(test_unit_conversions ${PROJECT_NAME})
target_include_directories(test_unit_conversions PRIVATE src)
endif()

ament_auto_package(
INSTALL_TO_SHARE
config
launch
)
84 changes: 84 additions & 0 deletions system/autoware_component_monitor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# autoware_component_monitor

The `autoware_component_monitor` package allows monitoring system usage of component containers.
The composable node inside the package is attached to a component container, and it publishes CPU and memory usage of
the container.

## Inputs / Outputs

### Input

None.

### Output

| Name | Type | Description |
| -------------------------- | -------------------------------------------------- | ---------------------- |
| `~/component_system_usage` | `autoware_internal_msgs::msg::ResourceUsageReport` | CPU, Memory usage etc. |

## Parameters

### Core Parameters

{{ json_to_markdown("system/autoware_component_monitor/schema/component_monitor.schema.json") }}

## How to use

Add it as a composable node in your launch file:

```xml

<launch>
<group>
<push-ros-namespace namespace="your_namespace"/>
...

<load_composable_node target="$(var container_name)">
<composable_node pkg="autoware_component_monitor"
plugin="autoware::component_monitor::ComponentMonitor"
name="component_monitor">
<param from="$(find-pkg-share autoware_component_monitor)/config/component_monitor.param.yaml"/>
</composable_node>
</load_composable_node>

...
</group>
</launch>
```

### Quick testing

You can test the package by running the following command:

```bash
ros2 component load <container_name> autoware_component_monitor autoware::component_monitor::ComponentMonitor -p publish_rate:=10.0 --node-namespace <namespace>

# Example usage
ros2 component load /pointcloud_container autoware_component_monitor autoware::component_monitor::ComponentMonitor -p publish_rate:=10.0 --node-namespace /pointcloud_container
```

## How it works

The package uses the `top` command under the hood.
`top -b -n 1 -E k -p PID` command is run at 10 Hz to get the system usage of the process.

- `-b` activates the batch mode. By default, `top` doesn't exit and prints to stdout periodically. Batch mode allows
exiting the program.
- `-n` number of times should `top` prints the system usage in batch mode.
- `-p` specifies the PID of the process to monitor.
- `-E k` changes the memory unit in the summary section to KiB.

Here is a sample output:

```text
top - 13:57:26 up 3:14, 1 user, load average: 1,09, 1,10, 1,04
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0,0 us, 0,8 sy, 0,0 ni, 99,2 id, 0,0 wa, 0,0 hi, 0,0 si, 0,0 st
KiB Mem : 65532208 total, 35117428 free, 17669824 used, 12744956 buff/cache
KiB Swap: 39062524 total, 39062524 free, 0 used. 45520816 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3352 meb 20 0 2905940 1,2g 39292 S 0,0 2,0 23:24.01 awesome
```

We get 5th, 8th fields from the last line, which are RES, %CPU respectively.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**:
ros__parameters:
publish_rate: 5.0 # Hz
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<launch>
<arg name="param_file" default="$(find-pkg-share autoware_component_monitor)/config/component_monitor.param.yaml"/>

<node_container pkg="rclcpp_components" exec="component_container_mt" name="component_monitor_container" namespace="/">
<composable_node pkg="autoware_component_monitor" plugin="autoware::component_monitor::ComponentMonitor" name="component_monitor">
<param from="$(var param_file)"/>
</composable_node>
</node_container>
</launch>
25 changes: 25 additions & 0 deletions system/autoware_component_monitor/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>autoware_component_monitor</name>
<version>0.0.0</version>
<description>A ROS 2 package to monitor system usage of component containers.</description>
<maintainer email="[email protected]">Mehmet Emin Başoğlu</maintainer>
<license>Apache-2.0</license>

<buildtool_depend>ament_cmake_auto</buildtool_depend>
<buildtool_depend>autoware_cmake</buildtool_depend>

<depend>autoware_internal_msgs</depend>
<depend>libboost-filesystem-dev</depend>
<depend>rclcpp</depend>
<depend>rclcpp_components</depend>

<test_depend>ament_cmake_ros</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>autoware_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Parameters for the Component Monitor node",
"type": "object",
"definitions": {
"component_monitor": {
"type": "object",
"properties": {
"publish_rate": {
"type": "number",
"default": "5.0",
"description": "Publish rate in Hz"
}
},
"required": ["publish_rate"]
}
},
"properties": {
"/**": {
"type": "object",
"properties": {
"ros__parameters": {
"$ref": "#/definitions/component_monitor"
}
},
"required": ["ros__parameters"]
}
},
"required": ["/**"]
}
177 changes: 177 additions & 0 deletions system/autoware_component_monitor/src/component_monitor_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// Copyright 2024 The Autoware Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "component_monitor_node.hpp"

#include "unit_conversions.hpp"

#include <rclcpp/rclcpp.hpp>

#include <autoware_internal_msgs/msg/resource_usage_report.hpp>

#include <boost/process.hpp>

#include <cctype>
#include <cstdint>
#include <exception>
#include <functional>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

namespace autoware::component_monitor
{
ComponentMonitor::ComponentMonitor(const rclcpp::NodeOptions & node_options)
: Node("component_monitor", node_options), publish_rate_(declare_parameter<double>("publish_rate"))

Check warning on line 38 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L37-L38

Added lines #L37 - L38 were not covered by tests
{
usage_pub_ =
create_publisher<ResourceUsageReport>("~/component_system_usage", rclcpp::SensorDataQoS());

Check warning on line 41 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L41

Added line #L41 was not covered by tests

// Make sure top ins installed and is in path
const auto path_top = boost::process::search_path("top");

Check warning on line 44 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L44

Added line #L44 was not covered by tests
if (path_top.empty()) {
RCLCPP_ERROR_STREAM(get_logger(), "Couldn't find 'top' in path.");
rclcpp::shutdown();

Check warning on line 47 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L46-L47

Added lines #L46 - L47 were not covered by tests
}

// Get the PID of the current process
int pid = getpid();

Check warning on line 51 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L51

Added line #L51 was not covered by tests

environment_ = boost::this_process::environment();
environment_["LC_NUMERIC"] = "en_US.UTF-8";

Check warning on line 54 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L53-L54

Added lines #L53 - L54 were not covered by tests

on_timer_tick_wrapped_ = std::bind(&ComponentMonitor::on_timer_tick, this, pid);

Check warning on line 56 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L56

Added line #L56 was not covered by tests

timer_ = rclcpp::create_timer(
this, get_clock(), rclcpp::Rate(publish_rate_).period(), on_timer_tick_wrapped_);

Check warning on line 59 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L58-L59

Added lines #L58 - L59 were not covered by tests
}

void ComponentMonitor::on_timer_tick(const int pid) const

Check warning on line 62 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L62

Added line #L62 was not covered by tests
{
if (usage_pub_->get_subscription_count() == 0) return;

try {
auto usage_msg = pid_to_report(pid);
usage_msg.header.stamp = this->now();
usage_msg.pid = pid;
usage_pub_->publish(usage_msg);
} catch (std::exception & e) {
RCLCPP_ERROR(get_logger(), "%s", e.what());
} catch (...) {
RCLCPP_ERROR(get_logger(), "An unknown error occurred.");

Check warning on line 74 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L67-L74

Added lines #L67 - L74 were not covered by tests
}
}

ComponentMonitor::ResourceUsageReport ComponentMonitor::pid_to_report(const pid_t & pid) const

Check warning on line 78 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L78

Added line #L78 was not covered by tests
{
const auto std_out = run_system_command("top -b -n 1 -E k -p " + std::to_string(pid));

Check warning on line 80 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L80

Added line #L80 was not covered by tests

const auto fields = parse_lines_into_words(std_out);

Check warning on line 82 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L82

Added line #L82 was not covered by tests

ResourceUsageReport report;
report.cpu_cores_utilized = std::stof(fields.back().at(8)) / 100.0f;
report.total_memory_bytes = unit_conversions::kib_to_bytes(std::stoul(fields.at(3).at(3)));
report.free_memory_bytes = unit_conversions::kib_to_bytes(std::stoul(fields.at(3).at(5)));
report.process_memory_bytes = parse_memory_res(fields.back().at(5));

Check warning on line 88 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L85-L88

Added lines #L85 - L88 were not covered by tests

return report;

Check warning on line 90 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L90

Added line #L90 was not covered by tests
}

std::stringstream ComponentMonitor::run_system_command(const std::string & cmd) const

Check warning on line 93 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L93

Added line #L93 was not covered by tests
{
int out_fd[2];
if (pipe2(out_fd, O_CLOEXEC) != 0) {
RCLCPP_ERROR_STREAM(get_logger(), "Error setting flags on out_fd");

Check warning on line 97 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L97

Added line #L97 was not covered by tests
}
boost::process::pipe out_pipe{out_fd[0], out_fd[1]};
boost::process::ipstream is_out{std::move(out_pipe)};

Check warning on line 100 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L99-L100

Added lines #L99 - L100 were not covered by tests

int err_fd[2];
if (pipe2(err_fd, O_CLOEXEC) != 0) {
RCLCPP_ERROR_STREAM(get_logger(), "Error setting flags on err_fd");

Check warning on line 104 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L104

Added line #L104 was not covered by tests
}
boost::process::pipe err_pipe{err_fd[0], err_fd[1]};
boost::process::ipstream is_err{std::move(err_pipe)};

Check warning on line 107 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L106-L107

Added lines #L106 - L107 were not covered by tests

boost::process::child c(
cmd, environment_, boost::process::std_out > is_out, boost::process::std_err > is_err);
c.wait();

Check warning on line 111 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L110-L111

Added lines #L110 - L111 were not covered by tests

if (c.exit_code() != 0) {
std::ostringstream os;
is_err >> os.rdbuf();
RCLCPP_ERROR_STREAM(get_logger(), "Error running command: " << os.str());

Check warning on line 116 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L114-L116

Added lines #L114 - L116 were not covered by tests
}

std::stringstream sstream;
sstream << is_out.rdbuf();
return sstream;

Check warning on line 121 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L119-L121

Added lines #L119 - L121 were not covered by tests
}

ComponentMonitor::VecVecStr ComponentMonitor::parse_lines_into_words(

Check warning on line 124 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L124

Added line #L124 was not covered by tests
const std::stringstream & std_out)
{
VecVecStr fields;
std::string line;
std::istringstream input{std_out.str()};

Check warning on line 129 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L129

Added line #L129 was not covered by tests

while (std::getline(input, line)) {
std::istringstream iss_line{line};

Check warning on line 132 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L132

Added line #L132 was not covered by tests
std::string word;
std::vector<std::string> words;

while (iss_line >> word) {
words.push_back(word);

Check warning on line 137 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L137

Added line #L137 was not covered by tests
}

fields.push_back(words);

Check warning on line 140 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L140

Added line #L140 was not covered by tests
}

return fields;

Check warning on line 143 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L143

Added line #L143 was not covered by tests
}

std::uint64_t ComponentMonitor::parse_memory_res(const std::string & mem_res)

Check warning on line 146 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L146

Added line #L146 was not covered by tests
{
// example 1: 12.3g
// example 2: 123 (without suffix, just bytes)
static const std::unordered_map<char, std::function<std::uint64_t(double)>> unit_map{
{'k', unit_conversions::kib_to_bytes<double>}, {'m', unit_conversions::mib_to_bytes<double>},
{'g', unit_conversions::gib_to_bytes<double>}, {'t', unit_conversions::tib_to_bytes<double>},
{'p', unit_conversions::pib_to_bytes<double>}, {'e', unit_conversions::eib_to_bytes<double>}};

Check warning on line 153 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L153

Added line #L153 was not covered by tests

if (std::isdigit(mem_res.back())) {
return std::stoull(mem_res); // Handle plain bytes without any suffix
}

// Extract the numeric part and the unit suffix
double value = std::stod(mem_res.substr(0, mem_res.size() - 1));
char suffix = mem_res.back();

Check warning on line 161 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L160-L161

Added lines #L160 - L161 were not covered by tests

// Find the appropriate function from the map
auto it = unit_map.find(suffix);
if (it != unit_map.end()) {
const auto & conversion_function = it->second;
return conversion_function(value);

Check warning on line 167 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L167

Added line #L167 was not covered by tests
}

// Throw an exception or handle the error as needed if the suffix is not recognized
throw std::runtime_error("Unsupported unit suffix: " + std::string(1, suffix));

Check warning on line 171 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L171

Added line #L171 was not covered by tests
}

} // namespace autoware::component_monitor

#include <rclcpp_components/register_node_macro.hpp>
RCLCPP_COMPONENTS_REGISTER_NODE(autoware::component_monitor::ComponentMonitor)

Check warning on line 177 in system/autoware_component_monitor/src/component_monitor_node.cpp

View check run for this annotation

Codecov / codecov/patch

system/autoware_component_monitor/src/component_monitor_node.cpp#L177

Added line #L177 was not covered by tests
Loading
Loading