From ed3dfb3dc3cc3a2f8ec61e0250b55643c5ab15bb Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 14 Feb 2023 22:22:48 -0500 Subject: [PATCH] Introduce scoped intervals --- include/blt/profiling/profiler.h | 41 +++++++++++++++++++++----------- src/blt/profiling/profiler.cpp | 12 +++++----- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/include/blt/profiling/profiler.h b/include/blt/profiling/profiler.h index b460cc4..5bb8ba3 100644 --- a/include/blt/profiling/profiler.h +++ b/include/blt/profiling/profiler.h @@ -18,20 +18,20 @@ */ namespace blt::profiling { - struct CapturePoint { + struct capture_point { std::string name; long point {}; }; - struct CaptureInterval { + struct capture_interval { long start; long end; }; - struct Profile { - std::unordered_map intervals; - std::unordered_map> historicalIntervals; - blt::flat_queue points; + struct profile { + std::unordered_map intervals; + std::unordered_map> historicalIntervals; + blt::flat_queue points; }; void startInterval(const std::string& profileName, const std::string& intervalName); @@ -39,9 +39,9 @@ namespace blt::profiling { void point(const std::string& profileName, const std::string& pointName); - CaptureInterval 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); + 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); void printOrderedProfile(const std::string& profileName, logging::LOG_LEVEL loggingLevel = logging::NONE); @@ -49,6 +49,19 @@ namespace blt::profiling { void discardProfiles(); void discardIntervals(const std::string& profileName); void discardPoints(const std::string& profileName); + + class scoped_interval { + private: + std::string m_Profile; + std::string m_Interval; + public: + 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); + } + }; } #endif //BLT_PROFILER_H @@ -60,11 +73,11 @@ namespace blt::profiling { #define BLT_PRINT_PROFILE(profileName, ...) #define BLT_PRINT_ORDERED(profileName, ...) #else - #define BLT_START_INTERVAL(profileName, intervalName) blt::profiling::startInterval(profileName, intervalName); - #define BLT_END_INTERVAL(profileName, intervalName) blt::profiling::endInterval(profileName, intervalName); - #define BLT_POINT(profileName, pointName) blt::profiling::point(profileName, pointName); - #define BLT_PRINT_PROFILE(profileName, ...) blt::profiling::printProfile(profileName, ##__VA_ARGS__); - #define BLT_PRINT_ORDERED(profileName, ...) blt::profiling::printOrderedProfile(profileName, ##__VA_ARGS__); + #define BLT_START_INTERVAL(profileName, intervalName) blt::profiling::startInterval(profileName, intervalName) + #define BLT_END_INTERVAL(profileName, intervalName) blt::profiling::endInterval(profileName, intervalName) + #define BLT_POINT(profileName, pointName) blt::profiling::point(profileName, pointName) + #define BLT_PRINT_PROFILE(profileName, ...) blt::profiling::printProfile(profileName, ##__VA_ARGS__) + #define BLT_PRINT_ORDERED(profileName, ...) blt::profiling::printOrderedProfile(profileName, ##__VA_ARGS__) #endif #define BLT_INTERVAL_START(profileName, intervalName) BLT_START_INTERVAL(profileName, intervalName) diff --git a/src/blt/profiling/profiler.cpp b/src/blt/profiling/profiler.cpp index 55bf13d..522051b 100644 --- a/src/blt/profiling/profiler.cpp +++ b/src/blt/profiling/profiler.cpp @@ -16,11 +16,11 @@ namespace blt::profiling { // TODO: a better way std::mutex profileLock{}; - std::unordered_map profiles; + std::unordered_map profiles; void startInterval(const std::string& profileName, const std::string& intervalName) { std::scoped_lock lock(profileLock); - CaptureInterval interval{}; + capture_interval interval{}; interval.start = system::getCurrentTimeNanoseconds(); profiles[profileName].intervals[intervalName] = interval; } @@ -33,17 +33,17 @@ namespace blt::profiling { void point(const std::string& profileName, const std::string& pointName) { std::scoped_lock lock(profileLock); - CapturePoint point{}; + capture_point point{}; point.point = system::getCurrentTimeNanoseconds(); point.name = pointName; profiles[profileName].points.push(point); } - CaptureInterval getInterval(const std::string& profileName, const std::string& intervalName) { + capture_interval getInterval(const std::string& profileName, const std::string& intervalName) { return profiles[profileName].intervals[intervalName]; } - Profile getProfile(const std::string& profileName) { + profile getProfile(const std::string& profileName) { return profiles[profileName]; } @@ -133,7 +133,7 @@ namespace blt::profiling { profiles[profileName].points = {}; } - std::vector getAllIntervals(const std::string& profileName, const std::string& intervalName) { + std::vector getAllIntervals(const std::string& profileName, const std::string& intervalName) { return profiles[profileName].historicalIntervals[intervalName]; } } \ No newline at end of file