2022-12-23 13:50:27 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett on 23/12/22.
|
|
|
|
* Licensed under GNU General Public License V3.0
|
|
|
|
* See LICENSE file for license detail
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_PROFILER_H
|
|
|
|
#define BLT_PROFILER_H
|
|
|
|
|
2022-12-25 23:19:44 -05:00
|
|
|
#include <string>
|
2023-01-10 10:45:11 -05:00
|
|
|
#include <blt/std/queue.h>
|
2023-02-14 22:06:30 -05:00
|
|
|
#include <vector>
|
2023-01-26 00:59:36 -05:00
|
|
|
#include <unordered_map>
|
2023-07-21 03:32:42 -04:00
|
|
|
#include <blt/std/logging.h>
|
2023-03-04 11:42:24 -05:00
|
|
|
#include <fstream>
|
2023-03-06 21:29:04 -05:00
|
|
|
#include <cstdint>
|
|
|
|
|
2022-12-23 13:50:27 -05:00
|
|
|
|
2023-01-26 00:59:36 -05:00
|
|
|
/**
|
2023-03-04 11:42:24 -05:00
|
|
|
* Defines several disableable macros (#define BLT_DISABLE_PROFILING). If you do not use these macros profiling cannot be disabled!
|
2023-01-26 00:59:36 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace blt::profiling {
|
2023-02-14 22:22:48 -05:00
|
|
|
struct capture_point {
|
2023-01-26 00:59:36 -05:00
|
|
|
std::string name;
|
2023-02-14 22:32:23 -05:00
|
|
|
long point{};
|
2022-12-26 00:31:00 -05:00
|
|
|
};
|
|
|
|
|
2023-03-30 21:48:31 -04:00
|
|
|
|
2023-02-14 22:22:48 -05:00
|
|
|
struct capture_interval {
|
2023-01-05 01:52:56 -05:00
|
|
|
long start;
|
|
|
|
long end;
|
2022-12-26 00:31:00 -05:00
|
|
|
};
|
|
|
|
|
2023-03-06 21:29:04 -05:00
|
|
|
struct capture_history {
|
|
|
|
std::uint64_t count;
|
|
|
|
std::uint64_t total;
|
|
|
|
};
|
|
|
|
|
2023-02-14 22:22:48 -05:00
|
|
|
struct profile {
|
|
|
|
std::unordered_map<std::string, capture_interval> intervals;
|
2023-03-06 21:29:04 -05:00
|
|
|
std::unordered_map<std::string, capture_history> intervals_total;
|
2023-02-14 22:22:48 -05:00
|
|
|
blt::flat_queue<capture_point> points;
|
2023-01-12 23:11:17 -05:00
|
|
|
};
|
|
|
|
|
2023-01-26 00:59:36 -05:00
|
|
|
void startInterval(const std::string& profileName, const std::string& intervalName);
|
2023-02-14 22:32:23 -05:00
|
|
|
|
2023-01-26 00:59:36 -05:00
|
|
|
void endInterval(const std::string& profileName, const std::string& intervalName);
|
|
|
|
|
|
|
|
void point(const std::string& profileName, const std::string& pointName);
|
|
|
|
|
2023-02-14 22:22:48 -05:00
|
|
|
capture_interval getInterval(const std::string& profileName, const std::string& intervalName);
|
2023-02-14 22:32:23 -05:00
|
|
|
|
2023-02-14 22:22:48 -05:00
|
|
|
profile getProfile(const std::string& profileName);
|
2023-01-26 00:59:36 -05:00
|
|
|
|
2023-02-14 22:32:23 -05:00
|
|
|
void printProfile(
|
2023-07-20 22:38:17 -04:00
|
|
|
const std::string& profileName, logging::log_level loggingLevel = logging::log_level::NONE,
|
2023-03-04 11:46:37 -05:00
|
|
|
bool averageHistory = false
|
2023-02-14 22:32:23 -05:00
|
|
|
);
|
|
|
|
|
2023-03-04 11:52:54 -05:00
|
|
|
void writeProfile(std::ofstream& out, const std::string& profileName, bool averageHistory = false);
|
2023-01-26 00:59:36 -05:00
|
|
|
|
|
|
|
void discardProfiles();
|
2023-02-14 22:32:23 -05:00
|
|
|
|
2023-01-26 00:59:36 -05:00
|
|
|
void discardIntervals(const std::string& profileName);
|
2023-02-14 22:32:23 -05:00
|
|
|
|
2023-01-26 00:59:36 -05:00
|
|
|
void discardPoints(const std::string& profileName);
|
2023-02-14 22:22:48 -05:00
|
|
|
|
|
|
|
class scoped_interval {
|
|
|
|
private:
|
|
|
|
std::string m_Profile;
|
|
|
|
std::string m_Interval;
|
|
|
|
public:
|
2023-03-04 11:42:24 -05:00
|
|
|
scoped_interval(std::string profile, std::string interval):
|
|
|
|
m_Profile(std::move(profile)), m_Interval(std::move(interval)) {
|
2023-02-14 22:22:48 -05:00
|
|
|
blt::profiling::startInterval(m_Profile, m_Interval);
|
|
|
|
}
|
2023-02-14 22:32:23 -05:00
|
|
|
|
2023-02-14 22:22:48 -05:00
|
|
|
~scoped_interval() {
|
|
|
|
blt::profiling::endInterval(m_Profile, m_Interval);
|
|
|
|
}
|
|
|
|
};
|
2022-12-26 00:31:00 -05:00
|
|
|
}
|
2022-12-23 13:50:27 -05:00
|
|
|
|
|
|
|
#endif //BLT_PROFILER_H
|
2023-01-26 00:59:36 -05:00
|
|
|
|
|
|
|
#ifdef BLT_DISABLE_PROFILING
|
|
|
|
#define BLT_START_INTERVAL(profileName, intervalName)
|
|
|
|
#define BLT_END_INTERVAL(profileName, intervalName)
|
|
|
|
#define BLT_POINT(profileName, pointName)
|
2023-10-05 01:31:54 -04:00
|
|
|
#define BLT_PRINT_PROFILE(profileName, ...)
|
|
|
|
#define BLT_WRITE_PROFILE(stream, profileName)
|
2023-01-26 00:59:36 -05:00
|
|
|
#else
|
2023-03-04 11:42:24 -05:00
|
|
|
/**
|
|
|
|
* Starts an interval to be measured, when ended the row will be added to the specified profile.
|
|
|
|
*/
|
2023-02-14 22:22:48 -05:00
|
|
|
#define BLT_START_INTERVAL(profileName, intervalName) blt::profiling::startInterval(profileName, intervalName)
|
2023-03-04 11:42:24 -05:00
|
|
|
/**
|
|
|
|
* Ends an interval, adds the interval to the profile.
|
|
|
|
*/
|
2023-02-14 22:22:48 -05:00
|
|
|
#define BLT_END_INTERVAL(profileName, intervalName) blt::profiling::endInterval(profileName, intervalName)
|
2023-03-04 11:42:24 -05:00
|
|
|
/**
|
|
|
|
* Measures this exact point in time.
|
|
|
|
*/
|
2023-02-14 22:22:48 -05:00
|
|
|
#define BLT_POINT(profileName, pointName) blt::profiling::point(profileName, pointName)
|
2023-03-04 11:42:24 -05:00
|
|
|
/**
|
|
|
|
* Prints the profile order from least time to most time.
|
|
|
|
* @param profileName the profile to print
|
2023-04-08 12:44:31 -04:00
|
|
|
* @param loggingLevel blt::logging::LOG_LEVEL to log with (default: BLT_NONE)
|
2023-03-04 11:42:24 -05:00
|
|
|
* @param averageHistory use the historical collection of interval rows in an average or just the latest? (default: false)
|
|
|
|
*/
|
2023-02-14 22:22:48 -05:00
|
|
|
#define BLT_PRINT_PROFILE(profileName, ...) blt::profiling::printProfile(profileName, ##__VA_ARGS__)
|
2023-03-04 11:42:24 -05:00
|
|
|
/**
|
|
|
|
* writes the profile to an output stream, ordered from least time to most time, in CSV format.
|
|
|
|
*/
|
|
|
|
#define BLT_WRITE_PROFILE(stream, profileName) blt::profiling::writeProfile(stream, profileName, true)
|
|
|
|
#endif
|