From 25d66d8a2a80fdf7ffbdb956c1dc904927d08fd2 Mon Sep 17 00:00:00 2001 From: ito-san Date: Mon, 25 Nov 2024 08:48:45 +0900 Subject: [PATCH] fix(mem_monitor): improve handling data --- .../system_monitor/src/mem_monitor/mem_monitor.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/system/system_monitor/src/mem_monitor/mem_monitor.cpp b/system/system_monitor/src/mem_monitor/mem_monitor.cpp index 0118963e4c0c5..b678a99e3f0c2 100644 --- a/system/system_monitor/src/mem_monitor/mem_monitor.cpp +++ b/system/system_monitor/src/mem_monitor/mem_monitor.cpp @@ -63,7 +63,10 @@ MemMonitor::MemMonitor(const rclcpp::NodeOptions & options) } } -void MemMonitor::update() { updater_.force_update(); } +void MemMonitor::update() +{ + updater_.force_update(); +} void MemMonitor::checkUsage(diagnostic_updater::DiagnosticStatusWrapper & stat) { @@ -137,8 +140,10 @@ void MemMonitor::checkSwapUsage(diagnostic_updater::DiagnosticStatusWrapper & st } // Check if Swap Usage - const auto swap_usage = static_cast(map["Swap: usage"]) / 1e+2; - int level = DiagStatus::OK; + const auto swap_usage = (map["Swap: total"] > 0) ? static_cast(map["Swap: used"]) / + static_cast(map["Swap: total"]) + : 0.0; + int level = DiagStatus::OK; if (swap_usage >= swap_usage_error_) { level = std::max(level, static_cast(DiagStatus::ERROR)); @@ -146,7 +151,7 @@ void MemMonitor::checkSwapUsage(diagnostic_updater::DiagnosticStatusWrapper & st level = std::max(level, static_cast(DiagStatus::WARN)); } - stat.addf("Swap: usage", "%.2f%%", static_cast(map["Swap: usage"])); + stat.addf("Swap: usage", "%.2f%%", swap_usage * 1e+2); stat.add("Swap: total", toHumanReadable(std::to_string(map["Swap: total"]))); stat.add("Swap: used", toHumanReadable(std::to_string(map["Swap: used"]))); stat.add("Swap: free", toHumanReadable(std::to_string(map["Swap: free"]))); @@ -300,7 +305,6 @@ std::string MemMonitor::readUsage(std::map & map) map["Swap: total"] = swap_total; map["Swap: used"] = swap_used; map["Swap: free"] = swap_free; - map["Swap: usage"] = (swap_total > 0) ? static_cast(swap_used) / swap_total * 1e+2 : 0.0; size_t total_total = mem_total + swap_total; size_t total_used = mem_used + swap_used;