Add total to profiler

v1
Brett 2023-03-07 18:02:19 -05:00
parent b62d4bfe78
commit 13b7c3905b
1 changed files with 25 additions and 12 deletions

View File

@ -21,9 +21,13 @@ namespace blt::profiling {
struct IntervalComparable { struct IntervalComparable {
long difference; long difference;
std::string name; std::string name;
std::uint64_t total;
IntervalComparable(long difference, std::string name): IntervalComparable(long difference, std::string name):
difference(difference), name(std::move(name)) {} difference(difference), name(std::move(name)), total(difference) {}
IntervalComparable(long difference, std::string name, std::uint64_t total):
difference(difference), name(std::move(name)), total(total) {}
}; };
inline void println(const std::vector<std::string>&& lines, logging::LOG_LEVEL level) { inline void println(const std::vector<std::string>&& lines, logging::LOG_LEVEL level) {
@ -37,11 +41,15 @@ namespace blt::profiling {
*/ */
inline void orderIntervals( inline void orderIntervals(
const std::unordered_map<std::string, capture_interval>& unordered, const std::unordered_map<std::string, capture_interval>& unordered,
std::vector<IntervalComparable>& ordered std::vector<IntervalComparable>& ordered, bool history
) { ) {
// copy // copy
for (const auto& i : unordered) for (const auto& i : unordered) {
ordered.emplace_back(i.second.end - i.second.start, i.first); if (history)
ordered.emplace_back(i.second.end, i.first, i.second.start);
else
ordered.emplace_back(i.second.end - i.second.start, i.first);
}
// sort // sort
std::sort( std::sort(
@ -63,7 +71,9 @@ namespace blt::profiling {
// we can exploit how the order func works by supplying the difference into end, // we can exploit how the order func works by supplying the difference into end,
// which sorts correctly despite not being a true interval. // which sorts correctly despite not being a true interval.
averagedIntervals.insert({name, capture_interval{0, (long)(interval_vec.total / interval_vec.count)}}); averagedIntervals.insert(
{name, capture_interval{(long)interval_vec.total, (long) (interval_vec.total / interval_vec.count)}}
);
} }
} }
@ -79,16 +89,17 @@ namespace blt::profiling {
if (averageHistory) if (averageHistory)
averageIntervals(intervals_total, averaged_intervals); averageIntervals(intervals_total, averaged_intervals);
orderIntervals(averageHistory ? averaged_intervals : intervals, order_rows); orderIntervals(averageHistory ? averaged_intervals : intervals, order_rows, averageHistory);
out << "Order,Count,Interval,Time (ms),Time (ns)\n"; out << "Order,Count,Interval,Time (ms),Time (ns),Total (ms)\n";
int index = 1; int index = 1;
for (const auto& row : order_rows) { for (const auto& row : order_rows) {
out << std::to_string(index++) << "," out << std::to_string(index++) << ","
<< std::to_string(averageHistory ? intervals_total.at(row.name).count : 1) << "," << std::to_string(averageHistory ? intervals_total.at(row.name).count : 1) << ","
<< row.name << "," << row.name << ","
<< std::to_string((double) row.difference / 1000000.0) << "," << std::to_string((double) row.difference / 1000000.0) << ","
<< std::to_string(row.difference) << "\n"; << std::to_string(row.difference) << ","
<< std::to_string(row.total) << "\n";
} }
out.flush(); out.flush();
} }
@ -107,7 +118,7 @@ namespace blt::profiling {
if (averageHistory) if (averageHistory)
averageIntervals(intervals_total, averaged_intervals); averageIntervals(intervals_total, averaged_intervals);
orderIntervals(averageHistory ? averaged_intervals : intervals, ordered_rows); orderIntervals(averageHistory ? averaged_intervals : intervals, ordered_rows, averageHistory);
string::TableFormatter formatter{profileName}; string::TableFormatter formatter{profileName};
formatter.addColumn({"Order"}); formatter.addColumn({"Order"});
@ -115,6 +126,7 @@ namespace blt::profiling {
formatter.addColumn({"Interval"}); formatter.addColumn({"Interval"});
formatter.addColumn({"Time (ms)"}); formatter.addColumn({"Time (ms)"});
formatter.addColumn({"Time (ns)"}); formatter.addColumn({"Time (ns)"});
formatter.addColumn({"Total (ms)"});
int index = 1; int index = 1;
for (const auto& row : ordered_rows) { for (const auto& row : ordered_rows) {
@ -123,7 +135,8 @@ namespace blt::profiling {
std::to_string(averageHistory ? intervals_total.at(row.name).count : 1), std::to_string(averageHistory ? intervals_total.at(row.name).count : 1),
row.name, row.name,
std::to_string((double) row.difference / 1000000.0), std::to_string((double) row.difference / 1000000.0),
std::to_string(row.difference)} std::to_string(row.difference),
std::to_string(row.total)}
); );
} }
@ -143,12 +156,12 @@ namespace blt::profiling {
std::scoped_lock lock(profileLock); std::scoped_lock lock(profileLock);
auto& prof = profiles[profileName]; auto& prof = profiles[profileName];
auto& ref = prof.intervals[intervalName]; auto& ref = prof.intervals[intervalName];
ref.end = system::getCurrentTimeNanoseconds(); ref.end = system::getCurrentTimeNanoseconds();
auto difference = ref.end - ref.start; auto difference = ref.end - ref.start;
auto& href = prof.intervals_total[intervalName]; auto& href = prof.intervals_total[intervalName];
href.total += difference; href.total += difference;
href.count++; href.count++;
} }