From 7debcd60633c63264d739be3e199603e9041a620 Mon Sep 17 00:00:00 2001 From: Jihoon Lee Date: Thu, 28 Oct 2021 13:42:30 +0900 Subject: [PATCH] [Profiler] Add listener total time and percent This patch add listener total time and percent mechanism **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: Jihoon Lee --- nntrainer/utils/profiler.cpp | 25 ++++++++++++++++++------- nntrainer/utils/profiler.h | 25 +++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/nntrainer/utils/profiler.cpp b/nntrainer/utils/profiler.cpp index 9a04d5c..7287c2f 100644 --- a/nntrainer/utils/profiler.cpp +++ b/nntrainer/utils/profiler.cpp @@ -93,7 +93,7 @@ GenericProfileListener::result(const int event) { } void GenericProfileListener::report(std::ostream &out) const { - std::vector column_size = {10, 23, 23, 23, 23}; + std::vector column_size = {20, 23, 23, 23, 23, 23}; for (auto &entry : time_taken) { auto title = profiler->eventToStr(entry.first); @@ -107,13 +107,20 @@ void GenericProfileListener::report(std::ostream &out) const { out << "warm up: " << warmups << '\n'; } + auto end = std::chrono::steady_clock::now(); + auto duration = + std::chrono::duration_cast(end - start_time); + + out << "profiled for " << duration.count() << '\n'; + /// creating header // clang-format off out << std::setw(column_size[0]) << "key" << std::setw(column_size[1]) << "avg" << std::setw(column_size[2]) << "min" << std::setw(column_size[3]) << "max" - << std::setw(column_size[4]) << "sum" << '\n'; + << std::setw(column_size[4]) << "sum" + << std::setw(column_size[5]) << "pct" << '\n'; // clang-format on // seperator @@ -139,13 +146,17 @@ void GenericProfileListener::report(std::ostream &out) const { return; } + out_.setf(std::ios::fixed); + out_.precision(2); // clang-format off - out_ << std::setw(column_size[0]) << title - << std::setw(column_size[1]) << sum_.count() / (cnt_ - warmups) - << std::setw(column_size[2]) << min_.count() - << std::setw(column_size[3]) << max_.count() - << std::setw(column_size[4]) << sum_.count() << '\n'; + out_ << std::setw(column_size[0]) << title + << std::setw(column_size[1]) << sum_.count() / (cnt_ - warmups) + << std::setw(column_size[2]) << min_.count() + << std::setw(column_size[3]) << max_.count() + << std::setw(column_size[4]) << sum_.count() + << std::setw(column_size[5]) << sum_.count() / (double)duration.count() * 100 << '\n'; // clang-format on + out_.unsetf(std::ios::fixed); }; ordered_report[-entry.first] = func; } diff --git a/nntrainer/utils/profiler.h b/nntrainer/utils/profiler.h index b6217c7..e3dcb66 100644 --- a/nntrainer/utils/profiler.h +++ b/nntrainer/utils/profiler.h @@ -112,6 +112,10 @@ protected: Profiler *profiler; }; +/** + * @brief Generic Profiler Listener + * + */ class GenericProfileListener : public ProfileListener { public: /** @@ -123,6 +127,7 @@ public: GenericProfileListener(Profiler *profiler, std::vector events = {}, int warmups_ = 0) : ProfileListener(profiler, events), + start_time(std::chrono::steady_clock::now()), warmups(warmups_) { for (auto &event : events) { reset(event); @@ -158,6 +163,7 @@ public: virtual void report(std::ostream &out) const override; private: + std::chrono::time_point start_time; unsigned int warmups; static constexpr int CUR = 0; @@ -173,7 +179,11 @@ private: unsigned int /** CNT */>> time_taken; - decltype(time_taken)::iterator time_iter; /**< iterator for the time_taken */ + /** + * @brief iterator for the time_taken + * + */ + decltype(time_taken)::iterator time_iter; }; /** @@ -187,12 +197,23 @@ std::ostream &operator<<(std::ostream &out, T &l) { return out; } +/** + * @brief Profiler object + * + */ class Profiler { public: + /** + * @brief Construct a new Profiler object + * + */ Profiler() {} + /** + * @brief Deleted constructor + * + */ Profiler(const Profiler &) = delete; - Profiler &operator=(const Profiler &) = delete; /** -- 2.7.4