make write profile work

v2
Brett 2025-01-11 17:58:23 -05:00
parent 9860845831
commit 8133553ed8
3 changed files with 84 additions and 80 deletions

View File

@ -74,7 +74,8 @@ namespace blt
std::string name;
explicit profile_t(std::string name): name(std::move(name))
{}
{
}
profile_t(const profile_t& p) = delete;
@ -99,7 +100,9 @@ namespace blt
void printProfile(profile_t& profiler, std::uint32_t flags = AVERAGE_HISTORY | PRINT_CYCLES | PRINT_THREAD | PRINT_WALL,
sort_by sort = sort_by::CYCLES, blt::logging::log_level log_level = blt::logging::log_level::NONE);
void writeProfile(std::ifstream& stream, const profile_t& profiler);
void writeProfile(std::ostream& stream, profile_t& profiler,
std::uint32_t flags = AVERAGE_HISTORY | PRINT_CYCLES | PRINT_THREAD | PRINT_WALL,
sort_by sort = sort_by::CYCLES);
void clearProfile(profile_t& profiler);
@ -112,35 +115,37 @@ namespace blt
void printProfile(const std::string& profile_name, std::uint32_t flags = AVERAGE_HISTORY | PRINT_CYCLES | PRINT_THREAD | PRINT_WALL,
sort_by sort = sort_by::CYCLES, blt::logging::log_level log_level = blt::logging::log_level::NONE);
void writeProfile(std::ifstream& stream, const std::string& profile_name);
void writeProfile(std::ostream& stream, const std::string& profile_name,
std::uint32_t flags = AVERAGE_HISTORY | PRINT_CYCLES | PRINT_THREAD | PRINT_WALL,
sort_by sort = sort_by::CYCLES);
}
class auto_interval
{
private:
interval_t* iv;
public:
auto_interval(std::string interval_name, profile_t& profiler)
{
iv = blt::createInterval(profiler, std::move(interval_name));
blt::startInterval(iv);
}
private:
interval_t* iv;
explicit auto_interval(interval_t* iv): iv(iv)
{
blt::startInterval(iv);
}
public:
auto_interval(std::string interval_name, profile_t& profiler)
{
iv = blt::createInterval(profiler, std::move(interval_name));
blt::startInterval(iv);
}
auto_interval(const auto_interval& i) = delete;
explicit auto_interval(interval_t* iv): iv(iv)
{
blt::startInterval(iv);
}
auto_interval& operator=(const auto_interval& i) = delete;
auto_interval(const auto_interval& i) = delete;
~auto_interval()
{
blt::endInterval(iv);
}
auto_interval& operator=(const auto_interval& i) = delete;
~auto_interval()
{
blt::endInterval(iv);
}
};
}
#ifdef BLT_DISABLE_PROFILING
@ -152,22 +157,22 @@ namespace blt
/**
* Starts an interval to be measured, when ended the row will be added to the specified profile.
*/
#define BLT_START_INTERVAL(profileName, intervalName) blt::_internal::startInterval(profileName, intervalName)
#define BLT_START_INTERVAL(profileName, intervalName) blt::_internal::startInterval(profileName, intervalName)
/**
* Ends an interval, adds the interval to the profile.
*/
#define BLT_END_INTERVAL(profileName, intervalName) blt::_internal::endInterval(profileName, intervalName)
#define BLT_END_INTERVAL(profileName, intervalName) blt::_internal::endInterval(profileName, intervalName)
/**
* Prints the profile order from least time to most time.
* @param profileName the profile to print
* @param loggingLevel blt::logging::LOG_LEVEL to log with (default: BLT_NONE)
* @param averageHistory use the historical collection of interval rows in an average or just the latest? (default: false)
*/
#define BLT_PRINT_PROFILE(profileName, ...) blt::_internal::printProfile(profileName, ##__VA_ARGS__)
#define BLT_PRINT_PROFILE(profileName, ...) blt::_internal::printProfile(profileName, ##__VA_ARGS__)
/**
* writes the profile to an output stream, ordered from least time to most time, in CSV format.
*/
#define BLT_WRITE_PROFILE(stream, profileName) blt::_internal::writeProfile(stream, profileName)
#define BLT_WRITE_PROFILE(stream, profileName) blt::_internal::writeProfile(stream, profileName)
#endif
#endif //BLT_PROFILER_V2_H

@ -1 +1 @@
Subproject commit 0bad0987f1989294e73cdb1ebf41c75f49cdb0c1
Subproject commit d88c5e15079047777b418132ece5879e7c9aaa2b

View File

@ -89,11 +89,6 @@ namespace blt
profiler.cycle_intervals.clear();
}
void writeProfile(std::ifstream&, const profile_t&)
{
BLT_WARN("Write profile for V2 is currently a TODO");
}
void sort_intervals(std::vector<interval_t*>& intervals, sort_by sort, bool)
{
std::function<bool(const interval_t* a, const interval_t* b)> sort_func;
@ -137,12 +132,7 @@ namespace blt
return container;
}
inline void println(const std::vector<std::string>&& lines, logging::log_level level) {
for (const auto& line : lines)
BLT_LOG_STREAM(level) << line << "\n";
}
void printProfile(profile_t& profiler, std::uint32_t flags, sort_by sort, blt::logging::log_level log_level)
void writeProfile(std::ostream& stream, profile_t& profiler, std::uint32_t flags, sort_by sort)
{
bool printHistory = flags & AVERAGE_HISTORY;
bool printCycles = flags & PRINT_CYCLES;
@ -192,7 +182,16 @@ namespace blt
formatter.addRow(row);
}
println(formatter.createTable(true, true), log_level);
auto lines = formatter.createTable(true, true);
for (const auto& line : lines)
stream << line << "\n";
}
void printProfile(profile_t& profiler, const std::uint32_t flags, sort_by sort, blt::logging::log_level log_level)
{
std::stringstream stream;
writeProfile(stream, profiler, flags, sort);
BLT_LOG_STREAM(log_level) << stream.str();
}
profile_t::~profile_t()
@ -226,7 +225,7 @@ namespace blt
blt::endInterval(profiles[profile_name].at(interval_name));
}
void _internal::writeProfile(std::ifstream& stream, const std::string& profile_name)
void _internal::writeProfile(std::ostream& stream, const std::string& profile_name, std::uint32_t flags, sort_by sort)
{
if (profiles.find(profile_name) == profiles.end())
return;
@ -234,7 +233,7 @@ namespace blt
profile_t profile{profile_name};
for (const auto& i : pref)
profile.intervals.push_back(i.second);
blt::writeProfile(stream, profile);
blt::writeProfile(stream, profile, flags, sort);
profiles.erase(profile_name);
}