From 3d6a8d7eac588e42ae60231409fc763a6bc41e9b Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Tue, 25 Oct 2022 14:30:25 -0400 Subject: cmMakefileProfilingData: Refactor to use RAII And add a more generic StartEntry() method. --- Source/cmMakefile.cxx | 13 ++++----- Source/cmMakefileProfilingData.cxx | 60 +++++++++++++++++++++++++++++++------- Source/cmMakefileProfilingData.h | 30 +++++++++++++++++++ Source/cmake.h | 15 ++++++++-- 4 files changed, 97 insertions(+), 21 deletions(-) diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 90a38be..2091f27 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -375,19 +375,15 @@ public: ++this->Makefile->RecursionDepth; this->Makefile->ExecutionStatusStack.push_back(&status); #if !defined(CMAKE_BOOTSTRAP) - if (this->Makefile->GetCMakeInstance()->IsProfilingEnabled()) { - this->Makefile->GetCMakeInstance()->GetProfilingOutput().StartEntry(lff, - lfc); - } + this->ProfilingDataRAII = + this->Makefile->GetCMakeInstance()->CreateProfilingEntry(lff, lfc); #endif } ~cmMakefileCall() { #if !defined(CMAKE_BOOTSTRAP) - if (this->Makefile->GetCMakeInstance()->IsProfilingEnabled()) { - this->Makefile->GetCMakeInstance()->GetProfilingOutput().StopEntry(); - } + this->ProfilingDataRAII.reset(); #endif this->Makefile->ExecutionStatusStack.pop_back(); --this->Makefile->RecursionDepth; @@ -399,6 +395,9 @@ public: private: cmMakefile* Makefile; +#if !defined(CMAKE_BOOTSTRAP) + cm::optional ProfilingDataRAII; +#endif }; void cmMakefile::OnExecuteCommand(std::function callback) diff --git a/Source/cmMakefileProfilingData.cxx b/Source/cmMakefileProfilingData.cxx index 1cd97c9..dc974b1 100644 --- a/Source/cmMakefileProfilingData.cxx +++ b/Source/cmMakefileProfilingData.cxx @@ -4,8 +4,12 @@ #include #include +#include +#include #include +#include + #include #include @@ -46,6 +50,23 @@ cmMakefileProfilingData::~cmMakefileProfilingData() noexcept void cmMakefileProfilingData::StartEntry(const cmListFileFunction& lff, cmListFileContext const& lfc) { + cm::optional argsValue(cm::in_place, Json::objectValue); + if (!lff.Arguments().empty()) { + std::string args; + for (auto const& a : lff.Arguments()) { + args = cmStrCat(args, args.empty() ? "" : " ", a.Value); + } + (*argsValue)["functionArgs"] = args; + } + (*argsValue)["location"] = + cmStrCat(lfc.FilePath, ':', std::to_string(lfc.Line)); + this->StartEntry("cmake", lff.LowerCaseName(), std::move(argsValue)); +} + +void cmMakefileProfilingData::StartEntry(const std::string& category, + const std::string& name, + cm::optional args) +{ /* Do not try again if we previously failed to write to output. */ if (!this->ProfileStream.good()) { return; @@ -58,24 +79,17 @@ void cmMakefileProfilingData::StartEntry(const cmListFileFunction& lff, cmsys::SystemInformation info; Json::Value v; v["ph"] = "B"; - v["name"] = lff.LowerCaseName(); - v["cat"] = "cmake"; + v["name"] = name; + v["cat"] = category; v["ts"] = static_cast( std::chrono::duration_cast( std::chrono::steady_clock::now().time_since_epoch()) .count()); v["pid"] = static_cast(info.GetProcessId()); v["tid"] = 0; - Json::Value argsValue; - if (!lff.Arguments().empty()) { - std::string args; - for (auto const& a : lff.Arguments()) { - args += (args.empty() ? "" : " ") + a.Value; - } - argsValue["functionArgs"] = args; + if (args) { + v["args"] = *std::move(args); } - argsValue["location"] = lfc.FilePath + ":" + std::to_string(lfc.Line); - v["args"] = argsValue; this->JsonWriter->write(v, &this->ProfileStream); } catch (std::ios_base::failure& fail) { @@ -112,3 +126,27 @@ void cmMakefileProfilingData::StopEntry() cmSystemTools::Error("Error writing profiling output!"); } } + +cmMakefileProfilingData::RAII::RAII(RAII&& other) noexcept + : Data(other.Data) +{ + other.Data = nullptr; +} + +cmMakefileProfilingData::RAII::~RAII() +{ + if (this->Data) { + this->Data->StopEntry(); + } +} + +cmMakefileProfilingData::RAII& cmMakefileProfilingData::RAII::operator=( + RAII&& other) noexcept +{ + if (this->Data) { + this->Data->StopEntry(); + } + this->Data = other.Data; + other.Data = nullptr; + return *this; +} diff --git a/Source/cmMakefileProfilingData.h b/Source/cmMakefileProfilingData.h index a86764a..e8d7dfa 100644 --- a/Source/cmMakefileProfilingData.h +++ b/Source/cmMakefileProfilingData.h @@ -3,6 +3,11 @@ #pragma once #include #include +#include + +#include + +#include // IWYU pragma: keep #include "cmsys/FStream.hxx" @@ -19,8 +24,33 @@ public: cmMakefileProfilingData(const std::string&); ~cmMakefileProfilingData() noexcept; void StartEntry(const cmListFileFunction& lff, cmListFileContext const& lfc); + void StartEntry(const std::string& category, const std::string& name, + cm::optional args = cm::nullopt); void StopEntry(); + class RAII + { + public: + RAII() = delete; + RAII(const RAII&) = delete; + RAII(RAII&&) noexcept; + + template + RAII(cmMakefileProfilingData& data, Args&&... args) + : Data(&data) + { + this->Data->StartEntry(std::forward(args)...); + } + + ~RAII(); + + RAII& operator=(const RAII&) = delete; + RAII& operator=(RAII&&) noexcept; + + private: + cmMakefileProfilingData* Data = nullptr; + }; + private: cmsys::ofstream ProfileStream; std::unique_ptr JsonWriter; diff --git a/Source/cmake.h b/Source/cmake.h index 5b5425c..a9a8bf2 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -33,6 +33,7 @@ # include # include "cmCMakePresetsGraph.h" +# include "cmMakefileProfilingData.h" #endif class cmExternalMakefileProjectGeneratorFactory; @@ -41,9 +42,6 @@ class cmFileTimeCache; class cmGlobalGenerator; class cmGlobalGeneratorFactory; class cmMakefile; -#if !defined(CMAKE_BOOTSTRAP) -class cmMakefileProfilingData; -#endif class cmMessenger; class cmVariableWatch; struct cmBuildOptions; @@ -628,6 +626,17 @@ public: #if !defined(CMAKE_BOOTSTRAP) cmMakefileProfilingData& GetProfilingOutput(); bool IsProfilingEnabled() const; + + template + cm::optional CreateProfilingEntry( + Args&&... args) + { + if (this->IsProfilingEnabled()) { + return cm::make_optional( + this->GetProfilingOutput(), std::forward(args)...); + } + return cm::nullopt; + } #endif protected: -- cgit v0.12 From 0fe7aae91f6709c17d2d1b005f7fcac67930ff48 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Tue, 25 Oct 2022 14:38:35 -0400 Subject: cmake: Add profiling events for configure and generate --- Source/cmMakefileProfilingData.cxx | 2 +- Source/cmake.cxx | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Source/cmMakefileProfilingData.cxx b/Source/cmMakefileProfilingData.cxx index dc974b1..e4844f3 100644 --- a/Source/cmMakefileProfilingData.cxx +++ b/Source/cmMakefileProfilingData.cxx @@ -60,7 +60,7 @@ void cmMakefileProfilingData::StartEntry(const cmListFileFunction& lff, } (*argsValue)["location"] = cmStrCat(lfc.FilePath, ':', std::to_string(lfc.Line)); - this->StartEntry("cmake", lff.LowerCaseName(), std::move(argsValue)); + this->StartEntry("script", lff.LowerCaseName(), std::move(argsValue)); } void cmMakefileProfilingData::StartEntry(const std::string& category, diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 73b8e88..1a37ca2 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -2070,6 +2070,10 @@ int cmake::HandleDeleteCacheVariables(const std::string& var) int cmake::Configure() { +#if !defined(CMAKE_BOOTSTRAP) + auto profilingRAII = this->CreateProfilingEntry("project", "configure"); +#endif + DiagLevel diagLevel; if (this->DiagLevels.count("deprecated") == 1) { @@ -2582,6 +2586,11 @@ int cmake::Generate() if (!this->GlobalGenerator) { return -1; } + +#if !defined(CMAKE_BOOTSTRAP) + auto profilingRAII = this->CreateProfilingEntry("project", "generate"); +#endif + if (!this->GlobalGenerator->Compute()) { return -1; } -- cgit v0.12