Introduce scoped intervals

v1
Brett 2023-02-14 22:22:48 -05:00
parent 001b6ae46a
commit ed3dfb3dc3
2 changed files with 33 additions and 20 deletions

View File

@ -18,20 +18,20 @@
*/ */
namespace blt::profiling { namespace blt::profiling {
struct CapturePoint { struct capture_point {
std::string name; std::string name;
long point {}; long point {};
}; };
struct CaptureInterval { struct capture_interval {
long start; long start;
long end; long end;
}; };
struct Profile { struct profile {
std::unordered_map<std::string, CaptureInterval> intervals; std::unordered_map<std::string, capture_interval> intervals;
std::unordered_map<std::string, std::vector<CaptureInterval>> historicalIntervals; std::unordered_map<std::string, std::vector<capture_interval>> historicalIntervals;
blt::flat_queue<CapturePoint> points; blt::flat_queue<capture_point> points;
}; };
void startInterval(const std::string& profileName, const std::string& intervalName); 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); void point(const std::string& profileName, const std::string& pointName);
CaptureInterval getInterval(const std::string& profileName, const std::string& intervalName); capture_interval getInterval(const std::string& profileName, const std::string& intervalName);
std::vector<CaptureInterval> getAllIntervals(const std::string& profileName, const std::string& intervalName); std::vector<capture_interval> getAllIntervals(const std::string& profileName, const std::string& intervalName);
Profile getProfile(const std::string& profileName); profile getProfile(const std::string& profileName);
void printProfile(const std::string& profileName, blt::logging::LOG_LEVEL loggingLevel = logging::NONE); 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); void printOrderedProfile(const std::string& profileName, logging::LOG_LEVEL loggingLevel = logging::NONE);
@ -49,6 +49,19 @@ namespace blt::profiling {
void discardProfiles(); void discardProfiles();
void discardIntervals(const std::string& profileName); void discardIntervals(const std::string& profileName);
void discardPoints(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 #endif //BLT_PROFILER_H
@ -60,11 +73,11 @@ namespace blt::profiling {
#define BLT_PRINT_PROFILE(profileName, ...) #define BLT_PRINT_PROFILE(profileName, ...)
#define BLT_PRINT_ORDERED(profileName, ...) #define BLT_PRINT_ORDERED(profileName, ...)
#else #else
#define BLT_START_INTERVAL(profileName, intervalName) blt::profiling::startInterval(profileName, intervalName); #define BLT_START_INTERVAL(profileName, intervalName) blt::profiling::startInterval(profileName, intervalName)
#define BLT_END_INTERVAL(profileName, intervalName) blt::profiling::endInterval(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_POINT(profileName, pointName) blt::profiling::point(profileName, pointName)
#define BLT_PRINT_PROFILE(profileName, ...) blt::profiling::printProfile(profileName, ##__VA_ARGS__); #define BLT_PRINT_PROFILE(profileName, ...) blt::profiling::printProfile(profileName, ##__VA_ARGS__)
#define BLT_PRINT_ORDERED(profileName, ...) blt::profiling::printOrderedProfile(profileName, ##__VA_ARGS__); #define BLT_PRINT_ORDERED(profileName, ...) blt::profiling::printOrderedProfile(profileName, ##__VA_ARGS__)
#endif #endif
#define BLT_INTERVAL_START(profileName, intervalName) BLT_START_INTERVAL(profileName, intervalName) #define BLT_INTERVAL_START(profileName, intervalName) BLT_START_INTERVAL(profileName, intervalName)

View File

@ -16,11 +16,11 @@ namespace blt::profiling {
// TODO: a better way // TODO: a better way
std::mutex profileLock{}; std::mutex profileLock{};
std::unordered_map<std::string, Profile> profiles; std::unordered_map<std::string, profile> profiles;
void startInterval(const std::string& profileName, const std::string& intervalName) { void startInterval(const std::string& profileName, const std::string& intervalName) {
std::scoped_lock lock(profileLock); std::scoped_lock lock(profileLock);
CaptureInterval interval{}; capture_interval interval{};
interval.start = system::getCurrentTimeNanoseconds(); interval.start = system::getCurrentTimeNanoseconds();
profiles[profileName].intervals[intervalName] = interval; profiles[profileName].intervals[intervalName] = interval;
} }
@ -33,17 +33,17 @@ namespace blt::profiling {
void point(const std::string& profileName, const std::string& pointName) { void point(const std::string& profileName, const std::string& pointName) {
std::scoped_lock lock(profileLock); std::scoped_lock lock(profileLock);
CapturePoint point{}; capture_point point{};
point.point = system::getCurrentTimeNanoseconds(); point.point = system::getCurrentTimeNanoseconds();
point.name = pointName; point.name = pointName;
profiles[profileName].points.push(point); 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]; return profiles[profileName].intervals[intervalName];
} }
Profile getProfile(const std::string& profileName) { profile getProfile(const std::string& profileName) {
return profiles[profileName]; return profiles[profileName];
} }
@ -133,7 +133,7 @@ namespace blt::profiling {
profiles[profileName].points = {}; profiles[profileName].points = {};
} }
std::vector<CaptureInterval> getAllIntervals(const std::string& profileName, const std::string& intervalName) { std::vector<capture_interval> getAllIntervals(const std::string& profileName, const std::string& intervalName) {
return profiles[profileName].historicalIntervals[intervalName]; return profiles[profileName].historicalIntervals[intervalName];
} }
} }