From 37eece7a11aa488bd05f3971b3d944c393f8b832 Mon Sep 17 00:00:00 2001 From: duongtd Date: Thu, 20 Aug 2026 20:19:18 +0700 Subject: [PATCH] =?UTF-8?q?feat:=20debug=20metrics=20updateMap=20=E2=80=94?= =?UTF-8?q?=20lock=5Fwait/lock=5Fhold,=20window=20cells,=20empty=20cycles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit debug_level (0/1/2) trên báo cáo performance định kỳ: 0 giữ nguyên output cũ; 1 thêm lock_wait (tranh chấp mutex với planner) tách khỏi lock_hold (việc của chính costmap), kích thước window update và số cycle rỗng — cycle rỗng giờ vẫn được đếm + report nên costmap idle (global) không còn biến mất khỏi log; 2 thêm per-layer bounds_grow [m^2] chỉ mặt layer nào nở vùng update. Số đo sim: lock_wait chiếm 75-90% cycle local costmap khi PRIEST solve (median 7.5 ms, max 53 ms) trong khi lock_hold chỉ ~1.8 ms — căn cứ trực tiếp cho snapshot API ở commit sau. Co-Authored-By: Claude Fable 5 --- include/robot_costmap_2d/layered_costmap.h | 20 +++ src/layered_costmap.cpp | 158 +++++++++++++++++---- 2 files changed, 149 insertions(+), 29 deletions(-) diff --git a/include/robot_costmap_2d/layered_costmap.h b/include/robot_costmap_2d/layered_costmap.h index 223dab0..407908e 100755 --- a/include/robot_costmap_2d/layered_costmap.h +++ b/include/robot_costmap_2d/layered_costmap.h @@ -75,6 +75,12 @@ public: void setPerformanceMetrics(bool enabled, double reporting_period_seconds); + /** @brief Debug verbosity for the periodic performance report. + * 0 keeps the legacy output untouched; 1 adds lock-wait/hold, update-window + * size and empty-cycle counters; 2 adds per-layer bounds-growth lines. + * Only effective while performance metrics are enabled. */ + void setDebugLevel(int level); + inline const std::string& getGlobalFrameID() const noexcept { return global_frame_; @@ -165,6 +171,10 @@ private: std::uint64_t costs_nanoseconds = 0; std::uint64_t bounds_calls = 0; std::uint64_t costs_calls = 0; + // debug_level >= 2: how much this layer grew the update rect [m^2], + // clamped to the map extent so the inflation full-map request stays finite + double bounds_grow_m2_sum = 0.0; + double bounds_grow_m2_max = 0.0; }; void resetPerformanceMetrics(); @@ -188,12 +198,22 @@ private: bool performance_metrics_enabled_ = false; double performance_metrics_period_seconds_ = 5.0; + int debug_level_ = 0; std::chrono::steady_clock::time_point performance_window_start_; std::uint64_t performance_cycle_nanoseconds_ = 0; std::uint64_t performance_reset_nanoseconds_ = 0; std::uint64_t performance_cycles_ = 0; std::vector performance_cycle_samples_; std::vector layer_performance_; + // debug_level >= 1: master-mutex acquisition time is the contention signal + // (planners hold the same mutex while reading costs), hold time is our own + // cost; empty cycles are updates that found no dirty bounds and did no work. + std::uint64_t lock_wait_nanoseconds_ = 0; + std::uint64_t lock_wait_max_nanoseconds_ = 0; + std::uint64_t lock_hold_nanoseconds_ = 0; + std::uint64_t window_cells_sum_ = 0; + std::uint64_t window_cells_max_ = 0; + std::uint64_t performance_empty_cycles_ = 0; }; } // namespace robot_costmap_2d diff --git a/src/layered_costmap.cpp b/src/layered_costmap.cpp index 815d364..67aedd6 100755 --- a/src/layered_costmap.cpp +++ b/src/layered_costmap.cpp @@ -79,6 +79,11 @@ namespace robot_costmap_2d resetPerformanceMetrics(); } + void LayeredCostmap::setDebugLevel(int level) + { + debug_level_ = std::max(0, std::min(level, 2)); + } + void LayeredCostmap::resetPerformanceMetrics() { performance_window_start_ = std::chrono::steady_clock::now(); @@ -88,11 +93,21 @@ namespace robot_costmap_2d performance_cycle_samples_.clear(); performance_cycle_samples_.reserve(128); layer_performance_.assign(plugins_.size(), LayerPerformance()); + lock_wait_nanoseconds_ = 0; + lock_wait_max_nanoseconds_ = 0; + lock_hold_nanoseconds_ = 0; + window_cells_sum_ = 0; + window_cells_max_ = 0; + performance_empty_cycles_ = 0; } void LayeredCostmap::maybeReportPerformance() { - if (!performance_metrics_enabled_ || performance_cycles_ == 0) + // debug_level >= 1 also reports windows with only empty cycles, so an idle + // costmap (e.g. the global one with obstacle updates disabled) stays visible. + const bool has_activity = performance_cycles_ > 0 || + (debug_level_ >= 1 && performance_empty_cycles_ > 0); + if (!performance_metrics_enabled_ || !has_activity) return; const auto now = std::chrono::steady_clock::now(); @@ -100,34 +115,70 @@ namespace robot_costmap_2d if (elapsed < performance_metrics_period_seconds_) return; - const double average_cycle_ms = - static_cast(performance_cycle_nanoseconds_) / performance_cycles_ / 1.0e6; - const double average_reset_ms = - static_cast(performance_reset_nanoseconds_) / performance_cycles_ / 1.0e6; - std::sort(performance_cycle_samples_.begin(), performance_cycle_samples_.end()); - const auto percentile_ms = [this](double percentile) { - if (performance_cycle_samples_.empty()) - return 0.0; - const std::size_t index = static_cast( - percentile * static_cast(performance_cycle_samples_.size() - 1)); - return static_cast(performance_cycle_samples_[index]) / 1.0e6; - }; - robot::log_info( - "Costmap performance: cycles=%llu avg_cycle_ms=%.3f p95_cycle_ms=%.3f " - "p99_cycle_ms=%.3f avg_reset_ms=%.3f\n", - static_cast(performance_cycles_), average_cycle_ms, - percentile_ms(0.95), percentile_ms(0.99), average_reset_ms); - - for (std::size_t i = 0; i < plugins_.size() && i < layer_performance_.size(); ++i) + if (performance_cycles_ > 0) { - const LayerPerformance& stats = layer_performance_[i]; - const double average_bounds_ms = stats.bounds_calls == 0 ? 0.0 : - static_cast(stats.bounds_nanoseconds) / stats.bounds_calls / 1.0e6; - const double average_costs_ms = stats.costs_calls == 0 ? 0.0 : - static_cast(stats.costs_nanoseconds) / stats.costs_calls / 1.0e6; + const double average_cycle_ms = + static_cast(performance_cycle_nanoseconds_) / performance_cycles_ / 1.0e6; + const double average_reset_ms = + static_cast(performance_reset_nanoseconds_) / performance_cycles_ / 1.0e6; + std::sort(performance_cycle_samples_.begin(), performance_cycle_samples_.end()); + const auto percentile_ms = [this](double percentile) { + if (performance_cycle_samples_.empty()) + return 0.0; + const std::size_t index = static_cast( + percentile * static_cast(performance_cycle_samples_.size() - 1)); + return static_cast(performance_cycle_samples_[index]) / 1.0e6; + }; robot::log_info( - "Costmap layer [%s]: avg_bounds_ms=%.3f avg_costs_ms=%.3f\n", - plugins_[i]->getName().c_str(), average_bounds_ms, average_costs_ms); + "Costmap performance: cycles=%llu avg_cycle_ms=%.3f p95_cycle_ms=%.3f " + "p99_cycle_ms=%.3f avg_reset_ms=%.3f\n", + static_cast(performance_cycles_), average_cycle_ms, + percentile_ms(0.95), percentile_ms(0.99), average_reset_ms); + + for (std::size_t i = 0; i < plugins_.size() && i < layer_performance_.size(); ++i) + { + const LayerPerformance& stats = layer_performance_[i]; + const double average_bounds_ms = stats.bounds_calls == 0 ? 0.0 : + static_cast(stats.bounds_nanoseconds) / stats.bounds_calls / 1.0e6; + const double average_costs_ms = stats.costs_calls == 0 ? 0.0 : + static_cast(stats.costs_nanoseconds) / stats.costs_calls / 1.0e6; + robot::log_info( + "Costmap layer [%s]: avg_bounds_ms=%.3f avg_costs_ms=%.3f\n", + plugins_[i]->getName().c_str(), average_bounds_ms, average_costs_ms); + } + } + + if (debug_level_ >= 1) + { + const std::uint64_t lock_samples = performance_cycles_ + performance_empty_cycles_; + const double average_lock_wait_ms = lock_samples == 0 ? 0.0 : + static_cast(lock_wait_nanoseconds_) / lock_samples / 1.0e6; + const double max_lock_wait_ms = static_cast(lock_wait_max_nanoseconds_) / 1.0e6; + const double average_lock_hold_ms = performance_cycles_ == 0 ? 0.0 : + static_cast(lock_hold_nanoseconds_) / performance_cycles_ / 1.0e6; + const std::uint64_t average_window_cells = performance_cycles_ == 0 ? 0 : + window_cells_sum_ / performance_cycles_; + robot::log_info( + "Costmap debug: lock_wait avg=%.3f max=%.3f [ms], lock_hold avg=%.3f [ms], " + "window avg=%llu max=%llu [cells], empty_cycles=%llu\n", + average_lock_wait_ms, max_lock_wait_ms, average_lock_hold_ms, + static_cast(average_window_cells), + static_cast(window_cells_max_), + static_cast(performance_empty_cycles_)); + } + + if (debug_level_ >= 2 && performance_cycles_ > 0) + { + for (std::size_t i = 0; i < plugins_.size() && i < layer_performance_.size(); ++i) + { + const LayerPerformance& stats = layer_performance_[i]; + if (stats.bounds_calls == 0) + continue; + const double average_grow = stats.bounds_grow_m2_sum / stats.bounds_calls; + robot::log_info( + "Costmap debug layer [%s]: bounds_grow avg=%.2f max=%.2f [m^2]\n", + plugins_[i]->getName().c_str(), average_grow, stats.bounds_grow_m2_max); + } } resetPerformanceMetrics(); @@ -161,6 +212,18 @@ namespace robot_costmap_2d // Lock for the remainder of this function, some plugins (e.g. VoxelLayer) // implement thread unsafe updateBounds() functions. boost::unique_lock lock(*(costmap_.getMutex())); + // Time spent acquiring the master mutex is the contention with planner + // reads — measured separately from our own work (hold time) so the two + // show up as distinct numbers in the debug report. + const auto lock_acquired = performance_metrics_enabled_ ? std::chrono::steady_clock::now() : + std::chrono::steady_clock::time_point(); + if (performance_metrics_enabled_) + { + const std::uint64_t wait_ns = static_cast( + std::chrono::duration_cast(lock_acquired - cycle_start).count()); + lock_wait_nanoseconds_ += wait_ns; + lock_wait_max_nanoseconds_ = std::max(lock_wait_max_nanoseconds_, wait_ns); + } // if we're using a rolling buffer costmap... we need to update the origin using the robot's position if (rolling_window_) @@ -196,6 +259,28 @@ namespace robot_costmap_2d std::chrono::duration_cast( std::chrono::steady_clock::now() - bounds_start).count(); ++layer_performance_[plugin_index].bounds_calls; + if (debug_level_ >= 2 && plugin_index < layer_performance_.size()) + { + // Rect area clamped to the map extent, so the inflation layer's + // "reinflate everything" request (+/-FLT_MAX) stays finite. + const auto clamped_area = [this](double mnx, double mny, double mxx, double mxy) { + const double ox = costmap_.getOriginX(); + const double oy = costmap_.getOriginY(); + const double ex = ox + costmap_.getSizeInMetersX(); + const double ey = oy + costmap_.getSizeInMetersY(); + const double cx0 = std::max(mnx, ox), cy0 = std::max(mny, oy); + const double cx1 = std::min(mxx, ex), cy1 = std::min(mxy, ey); + return (cx1 > cx0 && cy1 > cy0) ? (cx1 - cx0) * (cy1 - cy0) : 0.0; + }; + const double grow = clamped_area(minx_, miny_, maxx_, maxy_) - + clamped_area(prev_minx, prev_miny, prev_maxx, prev_maxy); + if (grow > 0.0) + { + layer_performance_[plugin_index].bounds_grow_m2_sum += grow; + layer_performance_[plugin_index].bounds_grow_m2_max = + std::max(layer_performance_[plugin_index].bounds_grow_m2_max, grow); + } + } } if (minx_ > prev_minx || miny_ > prev_miny || maxx_ < prev_maxx || maxy_ < prev_maxy) { @@ -217,7 +302,16 @@ namespace robot_costmap_2d yn = std::min(int(costmap_.getSizeInCellsY()), yn + 1); if (xn < x0 || yn < y0) + { + // No dirty region this cycle (idle costmap): count it so the debug + // report still fires and the idle costmap stays visible in the log. + if (performance_metrics_enabled_) + { + ++performance_empty_cycles_; + maybeReportPerformance(); + } return; + } const auto reset_start = performance_metrics_enabled_ ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point(); @@ -256,12 +350,18 @@ namespace robot_costmap_2d if (performance_metrics_enabled_) { + const auto cycle_end = std::chrono::steady_clock::now(); const std::uint64_t cycle_nanoseconds = static_cast( - std::chrono::duration_cast( - std::chrono::steady_clock::now() - cycle_start).count()); + std::chrono::duration_cast(cycle_end - cycle_start).count()); performance_cycle_nanoseconds_ += cycle_nanoseconds; performance_cycle_samples_.push_back(cycle_nanoseconds); ++performance_cycles_; + lock_hold_nanoseconds_ += static_cast( + std::chrono::duration_cast(cycle_end - lock_acquired).count()); + const std::uint64_t window_cells = + static_cast(xn - x0) * static_cast(yn - y0); + window_cells_sum_ += window_cells; + window_cells_max_ = std::max(window_cells_max_, window_cells); maybeReportPerformance(); } }