From 0aab8d789cbec165f7a7f6a7b4deb954163a90c2 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 14 Feb 2023 22:32:23 -0500 Subject: [PATCH] Add option to ignore negatives --- include/blt/profiling/profiler.h | 18 +++++++++++++++--- src/blt/profiling/profiler.cpp | 14 ++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/include/blt/profiling/profiler.h b/include/blt/profiling/profiler.h index 02c294b..2bd6748 100644 --- a/include/blt/profiling/profiler.h +++ b/include/blt/profiling/profiler.h @@ -20,7 +20,7 @@ namespace blt::profiling { struct capture_point { std::string name; - long point {}; + long point{}; }; struct capture_interval { @@ -35,19 +35,30 @@ namespace blt::profiling { }; void startInterval(const std::string& profileName, const std::string& intervalName); + void endInterval(const std::string& profileName, const std::string& intervalName); void point(const std::string& profileName, const std::string& pointName); capture_interval getInterval(const std::string& profileName, const std::string& intervalName); + std::vector getAllIntervals(const std::string& profileName, const std::string& intervalName); + profile getProfile(const std::string& profileName); - void printProfile(const std::string& profileName, blt::logging::LOG_LEVEL loggingLevel = logging::NONE, bool averageHistory = false); - void printOrderedProfile(const std::string& profileName, logging::LOG_LEVEL loggingLevel = logging::NONE, bool averageHistory = false); + void printProfile( + const std::string& profileName, blt::logging::LOG_LEVEL loggingLevel = logging::NONE, bool averageHistory = false, + bool ignoreNegatives = true + ); + + void printOrderedProfile( + const std::string& profileName, logging::LOG_LEVEL loggingLevel = logging::NONE, bool averageHistory = false, bool ignoreNegatives = true + ); void discardProfiles(); + void discardIntervals(const std::string& profileName); + void discardPoints(const std::string& profileName); class scoped_interval { @@ -58,6 +69,7 @@ namespace blt::profiling { scoped_interval(std::string profile, std::string interval): m_Profile(std::move(profile)), m_Interval(std::move(interval)) { blt::profiling::startInterval(m_Profile, m_Interval); } + ~scoped_interval() { blt::profiling::endInterval(m_Profile, m_Interval); } diff --git a/src/blt/profiling/profiler.cpp b/src/blt/profiling/profiler.cpp index eac0c1c..907edcf 100644 --- a/src/blt/profiling/profiler.cpp +++ b/src/blt/profiling/profiler.cpp @@ -53,7 +53,7 @@ namespace blt::profiling { logger << line; } - void printProfile(const std::string& profileName, blt::logging::LOG_LEVEL loggingLevel, bool averageHistory) { + void printProfile(const std::string& profileName, blt::logging::LOG_LEVEL loggingLevel, bool averageHistory, bool ignoreNegatives) { string::TableFormatter formatter {profileName}; formatter.addColumn({"Interval"}); formatter.addColumn({"Time (ns)"}); @@ -69,12 +69,16 @@ namespace blt::profiling { long total_difference = 0; for (const auto& h_interval : history) { const auto difference = h_interval.end - h_interval.start; + if (ignoreNegatives && difference < 0) + continue; total_difference += difference; } total_difference /= (long) history.size(); formatter.addRow({interval.first, std::to_string(total_difference), std::to_string((double) total_difference / 1000000.0)}); } else { const auto difference = interval.second.end - interval.second.start; + if (ignoreNegatives && difference < 0) + continue; formatter.addRow({interval.first, std::to_string(difference), std::to_string((double) difference / 1000000.0)}); } } @@ -98,25 +102,31 @@ namespace blt::profiling { return container1.difference < container2.difference; } - void printOrderedProfile(const std::string& profileName, logging::LOG_LEVEL loggingLevel, bool averageHistory) { + void printOrderedProfile(const std::string& profileName, logging::LOG_LEVEL loggingLevel, bool averageHistory, bool ignoreNegatives) { auto& profile = profiles[profileName]; const auto& intervals = profile.intervals; const auto& points = profile.points; std::vector unorderedIntervalVector; + // TODO: refactor to reduce nesting + for (const auto& interval : intervals) { if (averageHistory) { const auto& history = profile.historicalIntervals[interval.first]; long total_difference = 0; for (const auto& h_interval : history) { const auto difference = h_interval.end - h_interval.start; + if (ignoreNegatives && difference < 0) + continue; total_difference += difference; } total_difference /= (long) history.size(); unorderedIntervalVector.emplace_back(total_difference, std::string("(H) ") += interval.first); } else { const auto difference = interval.second.end - interval.second.start; + if (ignoreNegatives && difference < 0) + continue; unorderedIntervalVector.emplace_back(difference, interval.first); } }