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

@ -23,41 +23,41 @@ namespace blt
static inline constexpr std::uint32_t PRINT_WALL = 0x4; static inline constexpr std::uint32_t PRINT_WALL = 0x4;
// print out the thread CPU time // print out the thread CPU time
static inline constexpr std::uint32_t PRINT_THREAD = 0x8; static inline constexpr std::uint32_t PRINT_THREAD = 0x8;
enum class sort_by enum class sort_by
{ {
CYCLES, CYCLES,
WALL, WALL,
THREAD THREAD
}; };
// 32 bit currently not supported // 32 bit currently not supported
typedef std::int64_t pf_time_t; typedef std::int64_t pf_time_t;
typedef std::uint64_t pf_cycle_t; typedef std::uint64_t pf_cycle_t;
struct interval_t struct interval_t
{ {
pf_time_t wall_start = 0; pf_time_t wall_start = 0;
pf_time_t wall_end = 0; pf_time_t wall_end = 0;
pf_time_t wall_total = 0; pf_time_t wall_total = 0;
pf_time_t thread_start = 0; pf_time_t thread_start = 0;
pf_time_t thread_end = 0; pf_time_t thread_end = 0;
pf_time_t thread_total = 0; pf_time_t thread_total = 0;
pf_cycle_t cycles_start = 0; pf_cycle_t cycles_start = 0;
pf_cycle_t cycles_end = 0; pf_cycle_t cycles_end = 0;
pf_cycle_t cycles_total = 0; pf_cycle_t cycles_total = 0;
std::uint64_t count = 0; std::uint64_t count = 0;
std::string interval_name; std::string interval_name;
interval_t() = default; interval_t() = default;
interval_t(pf_time_t wallStart, pf_time_t wallEnd, pf_time_t wallTotal, pf_time_t threadStart, pf_time_t threadEnd, pf_time_t threadTotal, interval_t(pf_time_t wallStart, pf_time_t wallEnd, pf_time_t wallTotal, pf_time_t threadStart, pf_time_t threadEnd, pf_time_t threadTotal,
pf_cycle_t cyclesStart, pf_cycle_t cyclesEnd, pf_cycle_t cyclesTotal, uint64_t count, std::string intervalName); pf_cycle_t cyclesStart, pf_cycle_t cyclesEnd, pf_cycle_t cyclesTotal, uint64_t count, std::string intervalName);
}; };
struct cycle_interval_t struct cycle_interval_t
{ {
pf_cycle_t cycles_start = 0; pf_cycle_t cycles_start = 0;
@ -66,81 +66,86 @@ namespace blt
std::uint64_t count = 0; std::uint64_t count = 0;
std::string interval_name; std::string interval_name;
}; };
struct profile_t struct profile_t
{ {
std::vector<interval_t*> intervals; std::vector<interval_t*> intervals;
std::vector<cycle_interval_t*> cycle_intervals; std::vector<cycle_interval_t*> cycle_intervals;
std::string name; std::string name;
explicit profile_t(std::string name): name(std::move(name)) explicit profile_t(std::string name): name(std::move(name))
{} {
}
profile_t(const profile_t& p) = delete; profile_t(const profile_t& p) = delete;
profile_t& operator=(const profile_t& p) = delete; profile_t& operator=(const profile_t& p) = delete;
~profile_t(); ~profile_t();
}; };
interval_t* createInterval(profile_t& profiler, std::string interval_name); interval_t* createInterval(profile_t& profiler, std::string interval_name);
void startInterval(interval_t* interval); void startInterval(interval_t* interval);
inline interval_t* startInterval(profile_t& profiler, std::string interval_name) inline interval_t* startInterval(profile_t& profiler, std::string interval_name)
{ {
auto* p = createInterval(profiler, std::move(interval_name)); auto* p = createInterval(profiler, std::move(interval_name));
startInterval(p); startInterval(p);
return p; return p;
} }
void endInterval(interval_t* interval); void endInterval(interval_t* interval);
void printProfile(profile_t& profiler, std::uint32_t flags = AVERAGE_HISTORY | PRINT_CYCLES | PRINT_THREAD | PRINT_WALL, 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); 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); void clearProfile(profile_t& profiler);
namespace _internal namespace _internal
{ {
void startInterval(const std::string& profile_name, const std::string& interval_name); void startInterval(const std::string& profile_name, const std::string& interval_name);
void endInterval(const std::string& profile_name, const std::string& interval_name); void endInterval(const std::string& profile_name, const std::string& interval_name);
void printProfile(const std::string& profile_name, std::uint32_t flags = AVERAGE_HISTORY | PRINT_CYCLES | PRINT_THREAD | PRINT_WALL, 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); 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 class auto_interval
{ {
private: private:
interval_t* iv; interval_t* iv;
public:
auto_interval(std::string interval_name, profile_t& profiler) public:
{ auto_interval(std::string interval_name, profile_t& profiler)
iv = blt::createInterval(profiler, std::move(interval_name)); {
blt::startInterval(iv); iv = blt::createInterval(profiler, std::move(interval_name));
} blt::startInterval(iv);
}
explicit auto_interval(interval_t* iv): iv(iv)
{ explicit auto_interval(interval_t* iv): iv(iv)
blt::startInterval(iv); {
} blt::startInterval(iv);
}
auto_interval(const auto_interval& i) = delete;
auto_interval(const auto_interval& i) = delete;
auto_interval& operator=(const auto_interval& i) = delete;
auto_interval& operator=(const auto_interval& i) = delete;
~auto_interval()
{ ~auto_interval()
blt::endInterval(iv); {
} blt::endInterval(iv);
}
}; };
} }
#ifdef BLT_DISABLE_PROFILING #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. * 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. * 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. * Prints the profile order from least time to most time.
* @param profileName the profile to print * @param profileName the profile to print
* @param loggingLevel blt::logging::LOG_LEVEL to log with (default: BLT_NONE) * @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) * @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. * 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
#endif //BLT_PROFILER_V2_H #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(); 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) 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; std::function<bool(const interval_t* a, const interval_t* b)> sort_func;
@ -136,27 +131,22 @@ namespace blt
container.thread = unit::S; container.thread = unit::S;
return container; return container;
} }
inline void println(const std::vector<std::string>&& lines, logging::log_level level) { void writeProfile(std::ostream& stream, profile_t& profiler, std::uint32_t flags, sort_by sort)
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)
{ {
bool printHistory = flags & AVERAGE_HISTORY; bool printHistory = flags & AVERAGE_HISTORY;
bool printCycles = flags & PRINT_CYCLES; bool printCycles = flags & PRINT_CYCLES;
bool printThread = flags & PRINT_THREAD; bool printThread = flags & PRINT_THREAD;
bool printWall = flags & PRINT_WALL; bool printWall = flags & PRINT_WALL;
sort_intervals(profiler.intervals, sort, printHistory); sort_intervals(profiler.intervals, sort, printHistory);
auto units = determine_max_unit(profiler.intervals, printHistory); auto units = determine_max_unit(profiler.intervals, printHistory);
std::string thread_unit_string = units.thread == unit::MS ? "ms" : units.thread == unit::NS ? "ns" : "s"; std::string thread_unit_string = units.thread == unit::MS ? "ms" : units.thread == unit::NS ? "ns" : "s";
std::string wall_unit_string = units.wall == unit::MS ? "ms" : units.wall == unit::NS ? "ns" : "s"; std::string wall_unit_string = units.wall == unit::MS ? "ms" : units.wall == unit::NS ? "ns" : "s";
auto thread_unit_divide = units.thread == unit::MS ? 1e6 : units.thread == unit::NS ? 1 : 1e9; auto thread_unit_divide = units.thread == unit::MS ? 1e6 : units.thread == unit::NS ? 1 : 1e9;
auto wall_unit_divide = units.wall == unit::MS ? 1e6 : units.wall == unit::NS ? 1 : 1e9; auto wall_unit_divide = units.wall == unit::MS ? 1e6 : units.wall == unit::NS ? 1 : 1e9;
string::TableFormatter formatter{profiler.name}; string::TableFormatter formatter{profiler.name};
formatter.addColumn("Order"); formatter.addColumn("Order");
if (printHistory) if (printHistory)
@ -168,17 +158,17 @@ namespace blt
formatter.addColumn("CPU Time (" + thread_unit_string += ")"); formatter.addColumn("CPU Time (" + thread_unit_string += ")");
if (printWall) if (printWall)
formatter.addColumn("Wall Time (" + wall_unit_string += ")"); formatter.addColumn("Wall Time (" + wall_unit_string += ")");
for (size_t i = 0; i < profiler.intervals.size(); i++) for (size_t i = 0; i < profiler.intervals.size(); i++)
{ {
blt::string::TableRow row; blt::string::TableRow row;
auto interval = profiler.intervals[i]; auto interval = profiler.intervals[i];
if (interval->count == 0) if (interval->count == 0)
continue; continue;
INTERVAL_DIFFERENCE_MACRO(printHistory, interval); INTERVAL_DIFFERENCE_MACRO(printHistory, interval);
row.rowValues.push_back(std::to_string(i + 1)); row.rowValues.push_back(std::to_string(i + 1));
if (printHistory) if (printHistory)
row.rowValues.push_back(std::to_string(interval->count)); row.rowValues.push_back(std::to_string(interval->count));
@ -191,8 +181,17 @@ namespace blt
row.rowValues.push_back(std::to_string(wall / static_cast<double>(wall_unit_divide))); row.rowValues.push_back(std::to_string(wall / static_cast<double>(wall_unit_divide)));
formatter.addRow(row); 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() profile_t::~profile_t()
@ -226,7 +225,7 @@ namespace blt
blt::endInterval(profiles[profile_name].at(interval_name)); 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()) if (profiles.find(profile_name) == profiles.end())
return; return;
@ -234,7 +233,7 @@ namespace blt
profile_t profile{profile_name}; profile_t profile{profile_name};
for (const auto& i : pref) for (const auto& i : pref)
profile.intervals.push_back(i.second); profile.intervals.push_back(i.second);
blt::writeProfile(stream, profile); blt::writeProfile(stream, profile, flags, sort);
profiles.erase(profile_name); profiles.erase(profile_name);
} }