From 80a1c19b1ae2e7d66bcd4cb280807c680a26a02e Mon Sep 17 00:00:00 2001 From: Gaurav Harsha Date: Thu, 6 Nov 2025 11:31:46 -0500 Subject: [PATCH 1/2] append reset functionality for flexible operation inside and outside other active events --- src/green/utils/timing.h | 27 ++++++++++++++++++++++----- test/utils_test.cpp | 7 ++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/green/utils/timing.h b/src/green/utils/timing.h index b5e5a5a..df316fc 100644 --- a/src/green/utils/timing.h +++ b/src/green/utils/timing.h @@ -202,17 +202,34 @@ namespace green::utils { } + /** + * @brief reset the `duration` and `active` status of the given event and all its children + * + * @param event + */ + void reset_event_and_children(event_t& event) { + event.duration = 0.0; + event.active = false; + for (auto& child_kv : event.children) { + reset_event_and_children(*child_kv.second); + } + } /** - * @brief reset the `duration` attribute of all child events of the current event + * @brief reset the `duration` and `active` status of all the children events + * of the current event * If there is no current event (i.e., at the root level when `_current_event` is nullptr), - * this method returns silently and does nothing. + * this method resets all root events and their children. */ void reset() { - if (!_current_event) return; + if (!_current_event) { + for (auto& kv : _root_events) { + reset_event_and_children(*kv.second); + } + return; + } for (auto& kv : _current_event->children) { - kv.second->duration = 0.0; - kv.second->active = false; + reset_event_and_children(*kv.second); } } diff --git a/test/utils_test.cpp b/test/utils_test.cpp index 278a573..78392ad 100644 --- a/test/utils_test.cpp +++ b/test/utils_test.cpp @@ -135,7 +135,7 @@ TEST_CASE("Timing") { statistic.start("CHILD2"); std::this_thread::sleep_for(std::chrono::milliseconds(500)); statistic.end(); - statistic.reset(); + statistic.reset(); // reset during active ROOT event only resets its children statistic.end(); double duration_root = statistic.event("ROOT").duration; @@ -144,5 +144,10 @@ TEST_CASE("Timing") { REQUIRE(duration_child1 == 0.0); REQUIRE(duration_child2 == 0.0); + REQUIRE(duration_root > 0.0); + + statistic.reset(); // reset at root level resets all events + duration_root = statistic.event("ROOT").duration; + REQUIRE(duration_root == 0.0); } } From 05c7a8e244515e876defc359273e978a91dc1412 Mon Sep 17 00:00:00 2001 From: Gaurav Harsha Date: Thu, 6 Nov 2025 11:44:07 -0500 Subject: [PATCH 2/2] Update src/green/utils/timing.h documentation Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/green/utils/timing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/green/utils/timing.h b/src/green/utils/timing.h index df316fc..2932b1f 100644 --- a/src/green/utils/timing.h +++ b/src/green/utils/timing.h @@ -205,7 +205,7 @@ namespace green::utils { /** * @brief reset the `duration` and `active` status of the given event and all its children * - * @param event + * @param event The event to reset along with all its children. */ void reset_event_and_children(event_t& event) { event.duration = 0.0;