From 09c4660f2d54684c8419227356e65ed9fd5d6f78 Mon Sep 17 00:00:00 2001 From: matekelemen Date: Sun, 15 Dec 2024 13:07:45 +0100 Subject: [PATCH 1/5] attempt to fix symbol exports for Profiler --- kratos/sources/profiler.cpp | 18 +++++++++--------- kratos/utilities/profiler.h | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/kratos/sources/profiler.cpp b/kratos/sources/profiler.cpp index c5cd54acb277..02f89fb99e41 100644 --- a/kratos/sources/profiler.cpp +++ b/kratos/sources/profiler.cpp @@ -267,19 +267,19 @@ template std::mutex ProfilerSingleton::mMutex; -template class Profiler; -template class ProfilerSingleton; -template std::ostream& operator<<(std::ostream&, const Profiler&); +template class KRATOS_API(KRATOS_CORE) Profiler; +template class KRATOS_API(KRATOS_CORE) ProfilerSingleton; +template KRATOS_API(KRATOS_CORE) std::ostream& operator<<(std::ostream&, const Profiler&); -template class Profiler; -template class ProfilerSingleton; -template std::ostream& operator<<(std::ostream&, const Profiler&); +template class KRATOS_API(KRATOS_CORE) Profiler; +template class KRATOS_API(KRATOS_CORE) ProfilerSingleton; +template KRATOS_API(KRATOS_CORE) std::ostream& operator<<(std::ostream&, const Profiler&); -template class Profiler; -template class ProfilerSingleton; -template std::ostream& operator<<(std::ostream&, const Profiler&); +template class KRATOS_API(KRATOS_CORE) Profiler; +template class KRATOS_API(KRATOS_CORE) ProfilerSingleton; +template KRATOS_API(KRATOS_CORE) std::ostream& operator<<(std::ostream&, const Profiler&); } // namespace cie::utils diff --git a/kratos/utilities/profiler.h b/kratos/utilities/profiler.h index 8dc50e078b52..b2a889db5c1e 100644 --- a/kratos/utilities/profiler.h +++ b/kratos/utilities/profiler.h @@ -29,7 +29,7 @@ namespace Kratos::Internals { template -class Profiler +class KRATOS_API(KRATOS_CORE) Profiler { private: using TimeUnit = TTimeUnit; @@ -180,11 +180,11 @@ class Profiler template -std::ostream& operator<<(std::ostream& rStream, const Profiler& rProfiler); +KRATOS_API(KRATOS_CORE) std::ostream& operator<<(std::ostream& rStream, const Profiler& rProfiler); template -class ProfilerSingleton +class KRATOS_API(KRATOS_CORE) ProfilerSingleton { public: static Profiler& Get() noexcept; From 415df9ae9bdc69b1c2a3ca3cc68a843cee06f83a Mon Sep 17 00:00:00 2001 From: matekelemen Date: Sun, 15 Dec 2024 13:09:51 +0100 Subject: [PATCH 2/5] prevent profilers with different time units of overwriting each others' output files --- kratos/sources/profiler.cpp | 59 ++++++++++++++++++------------------- kratos/utilities/profiler.h | 2 +- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/kratos/sources/profiler.cpp b/kratos/sources/profiler.cpp index 02f89fb99e41..44ecd99c3714 100644 --- a/kratos/sources/profiler.cpp +++ b/kratos/sources/profiler.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -30,6 +29,34 @@ namespace Kratos::Internals { +namespace { +template +std::string GetTimeUnit() +{ + KRATOS_ERROR << "Unsupported time unit"; +} + +template <> +std::string GetTimeUnit() +{ + return "ms"; +} + +template <> +std::string GetTimeUnit() +{ + return "us"; +} + +template <> + +std::string GetTimeUnit() +{ + return "ns"; +} +} // unnamed namespace + + template Profiler::Item::Item(CodeLocation&& rLocation) : Item(0, @@ -70,7 +97,7 @@ typename Profiler::Item& Profiler::Item::operator+=(const Item& rOther) template Profiler::Profiler() - : Profiler("kratos_profiler_output.json") + : Profiler("kratos_profiler_output_" + GetTimeUnit() + ".json") { } @@ -112,34 +139,6 @@ typename Profiler::Item& Profiler::Create(CodeLocation&& r_item) } -namespace { -template -std::string GetTimeUnit() -{ - KRATOS_ERROR << "Unknown time unit"; -} - -template <> -std::string GetTimeUnit() -{ - return "ms"; -} - -template <> -std::string GetTimeUnit() -{ - return "us"; -} - -template <> - -std::string GetTimeUnit() -{ - return "ns"; -} -} // unnamed namespace - - template typename Profiler::ItemMap Profiler::Aggregate() const { diff --git a/kratos/utilities/profiler.h b/kratos/utilities/profiler.h index b2a889db5c1e..238f65928775 100644 --- a/kratos/utilities/profiler.h +++ b/kratos/utilities/profiler.h @@ -42,7 +42,7 @@ class KRATOS_API(KRATOS_CORE) Profiler class Item { public: - Item(CodeLocation&& rLocation); + explicit Item(CodeLocation&& rLocation); private: Item(std::size_t CallCount, From f04db9e3941504ceaaf1a2e8086654cd13411abe Mon Sep 17 00:00:00 2001 From: matekelemen Date: Sun, 15 Dec 2024 13:12:41 +0100 Subject: [PATCH 3/5] distinguish profiled items of different instantiations of the same template --- kratos/sources/profiler.cpp | 3 ++- kratos/utilities/profiler.h | 13 ++++++------- kratos/utilities/profiler_impl.h | 1 - 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/kratos/sources/profiler.cpp b/kratos/sources/profiler.cpp index 44ecd99c3714..a2d9dc35b7b5 100644 --- a/kratos/sources/profiler.cpp +++ b/kratos/sources/profiler.cpp @@ -207,7 +207,8 @@ void Profiler::Write(std::ostream& rStream) const const auto& r_location = p_item->mLocation; result.AddString("file", std::string(r_location.GetFileName())); result.AddInt("line", int(r_location.GetLineNumber())); - result.AddString("function", std::string(r_location.GetFunctionName())); + result.AddString("signature", std::string(r_location.GetFunctionName())); + result.AddString("function", std::string(r_location.CleanFunctionName())); result.AddInt("callCount", p_item->mCallCount); std::stringstream stream; diff --git a/kratos/utilities/profiler.h b/kratos/utilities/profiler.h index 238f65928775..55f2aca084ea 100644 --- a/kratos/utilities/profiler.h +++ b/kratos/utilities/profiler.h @@ -71,20 +71,19 @@ class KRATOS_API(KRATOS_CORE) Profiler struct SourceLocationHash { - std::size_t operator()(const CodeLocation& r_argument) const + std::size_t operator()(const CodeLocation& rArgument) const { - std::string string(r_argument.GetFileName()); - string.append(std::to_string(r_argument.GetLineNumber())); - return std::hash()(string); + return std::hash()(rArgument.GetFileName() + rArgument.GetFunctionName()); } }; struct SourceLocationEquality { - bool operator()(const CodeLocation& r_lhs, - const CodeLocation& r_rhs) const + bool operator()(const CodeLocation& rLhs, + const CodeLocation& rRhs) const { - return (std::string(r_lhs.GetFileName()) == std::string(r_rhs.GetFileName())) && (r_lhs.GetLineNumber() == r_rhs.GetLineNumber()); + return (rLhs.GetFileName() == rRhs.GetFileName()) + && (rLhs.GetFunctionName() == rRhs.GetFunctionName()); } }; diff --git a/kratos/utilities/profiler_impl.h b/kratos/utilities/profiler_impl.h index c52b8d7721a8..4b2745867272 100644 --- a/kratos/utilities/profiler_impl.h +++ b/kratos/utilities/profiler_impl.h @@ -16,7 +16,6 @@ #include "utilities/profiler.h" // <== help the language server // System includes -#include #include From 013e2360ff72e42bb4f49e747f78d593c45f02c0 Mon Sep 17 00:00:00 2001 From: matekelemen Date: Sun, 15 Dec 2024 13:13:17 +0100 Subject: [PATCH 4/5] fix minimum duration tracking --- kratos/sources/profiler.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/kratos/sources/profiler.cpp b/kratos/sources/profiler.cpp index a2d9dc35b7b5..5e56c2d01dc7 100644 --- a/kratos/sources/profiler.cpp +++ b/kratos/sources/profiler.cpp @@ -24,6 +24,7 @@ #include #include #include +#include // std::numeric_limits namespace Kratos::Internals { @@ -59,11 +60,11 @@ std::string GetTimeUnit() template Profiler::Item::Item(CodeLocation&& rLocation) - : Item(0, - Duration(0), - Duration(0), - Duration(0), - std::move(rLocation)) + : Item(0, // <== .mCallCount + Duration(0), // <== .mCumulative + Duration(std::numeric_limits::max()), // <== .mMin + Duration(0), // <== .mMax + std::move(rLocation)) // <== .mLocation { } From f72a6bfd9ff7162ed3a14fb138620acea2178055 Mon Sep 17 00:00:00 2001 From: matekelemen Date: Sun, 15 Dec 2024 13:13:41 +0100 Subject: [PATCH 5/5] add some missing annotations --- kratos/utilities/profiler.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/kratos/utilities/profiler.h b/kratos/utilities/profiler.h index 55f2aca084ea..14cfa194e1b8 100644 --- a/kratos/utilities/profiler.h +++ b/kratos/utilities/profiler.h @@ -32,10 +32,13 @@ template class KRATOS_API(KRATOS_CORE) Profiler { private: + /// @brief Absolute time type. using TimeUnit = TTimeUnit; + /// @brief Relative time type. using Duration = TimeUnit; + /// @brief Clock type used for measuring durations. using Clock = std::chrono::high_resolution_clock; /// @brief Class for identifying a profiled scope and aggregating its stats. @@ -51,21 +54,31 @@ class KRATOS_API(KRATOS_CORE) Profiler Duration MaxDuration, CodeLocation&& rLocation); + /// @brief Aggregate profiled data from another @ref Item in the same scope. Item& operator+=(const Item& rOther); private: friend class Profiler; + /// @brief Counter for keeping track of recursive calls. + /// @details Recursive function calls are aggregated onto the top + /// level call. To do that, the @ref Item must keep track + /// of its recursion depth. unsigned mRecursionLevel; + /// @brief Counter tracking total number of calls to a function during the program's entire execution time. std::size_t mCallCount; + /// @brief Counter summing the duration of each call to the profiled scope. Duration mCumulative; + /// @brief Minimum time spent in the profiled scope. Duration mMin; + /// @brief Maximum time spent in the profiled scope. Duration mMax; + /// @brief Source information about the profiled scope. CodeLocation mLocation; }; // class Item