From f26f127183f6e9e5d6b86f5f8dc9420e514ada6c Mon Sep 17 00:00:00 2001 From: Martin Duffy Date: Mon, 9 Jun 2025 11:06:02 -0400 Subject: instrumentation: Reuse single cmsys::SystemInformation --- Source/cmInstrumentation.cxx | 49 ++++++++++++++++++++++++++------------------ Source/cmInstrumentation.h | 24 +++++++++++++++------- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/Source/cmInstrumentation.cxx b/Source/cmInstrumentation.cxx index 3d743dc..5ff709d 100644 --- a/Source/cmInstrumentation.cxx +++ b/Source/cmInstrumentation.cxx @@ -3,11 +3,11 @@ #include #include #include -#include #include #include #include +#include #include #include @@ -126,6 +126,14 @@ void cmInstrumentation::LoadQueries() } } +cmsys::SystemInformation& cmInstrumentation::GetSystemInformation() +{ + if (!this->systemInformation) { + this->systemInformation = cm::make_unique(); + } + return *this->systemInformation; +} + bool cmInstrumentation::ReadJSONQueries(std::string const& directory) { cmsys::Directory d; @@ -210,8 +218,7 @@ bool cmInstrumentation::HasPreOrPostBuildHook() const int cmInstrumentation::CollectTimingData(cmInstrumentationQuery::Hook hook) { // Don't run collection if hook is disabled - if (hook != cmInstrumentationQuery::Hook::Manual && - this->hooks.find(hook) == this->hooks.end()) { + if (hook != cmInstrumentationQuery::Hook::Manual && !this->HasHook(hook)) { return 0; } @@ -299,36 +306,38 @@ int cmInstrumentation::CollectTimingData(cmInstrumentationQuery::Hook hook) void cmInstrumentation::InsertDynamicSystemInformation( Json::Value& root, std::string const& prefix) { - cmsys::SystemInformation info; Json::Value data; - info.RunCPUCheck(); - info.RunMemoryCheck(); + double memory; + double load; + this->GetDynamicSystemInformation(memory, load); if (!root.isMember("dynamicSystemInformation")) { root["dynamicSystemInformation"] = Json::objectValue; } root["dynamicSystemInformation"][cmStrCat(prefix, "HostMemoryUsed")] = - (double)info.GetHostMemoryUsed(); - root["dynamicSystemInformation"][cmStrCat(prefix, "CPULoadAverage")] = - info.GetLoadAverage(); + memory; + root["dynamicSystemInformation"][cmStrCat(prefix, "CPULoadAverage")] = load; } void cmInstrumentation::GetDynamicSystemInformation(double& memory, double& load) { - cmsys::SystemInformation info; - Json::Value data; - info.RunCPUCheck(); - info.RunMemoryCheck(); + cmsys::SystemInformation& info = this->GetSystemInformation(); + if (!this->ranSystemChecks) { + info.RunCPUCheck(); + info.RunMemoryCheck(); + this->ranSystemChecks = true; + } memory = (double)info.GetHostMemoryUsed(); load = info.GetLoadAverage(); } void cmInstrumentation::InsertStaticSystemInformation(Json::Value& root) { - cmsys::SystemInformation info; - info.RunCPUCheck(); - info.RunOSCheck(); - info.RunMemoryCheck(); + cmsys::SystemInformation& info = this->GetSystemInformation(); + if (!this->ranOSCheck) { + info.RunOSCheck(); + this->ranOSCheck = true; + } Json::Value infoRoot; infoRoot["familyId"] = info.GetFamilyID(); infoRoot["hostname"] = info.GetHostname(); @@ -408,7 +417,7 @@ std::string cmInstrumentation::InstrumentTest( this->InsertDynamicSystemInformation(root, "after"); } - cmsys::SystemInformation info; + cmsys::SystemInformation& info = this->GetSystemInformation(); std::string file_name = cmStrCat( "test-", this->ComputeSuffixHash(cmStrCat(command_str, info.GetProcessId())), @@ -468,7 +477,7 @@ int cmInstrumentation::InstrumentCommand( // Exit early if configure didn't generate a query if (reloadQueriesAfterCommand) { this->LoadQueries(); - if (!this->hasQuery) { + if (!this->HasQuery()) { return ret; } if (this->HasQuery( @@ -530,7 +539,7 @@ int cmInstrumentation::InstrumentCommand( root["workingDir"] = cmSystemTools::GetLogicalWorkingDirectory(); // Write Json - cmsys::SystemInformation info; + cmsys::SystemInformation& info = this->GetSystemInformation(); std::string const& file_name = cmStrCat( command_type, "-", this->ComputeSuffixHash(cmStrCat(command_str, info.GetProcessId())), diff --git a/Source/cmInstrumentation.h b/Source/cmInstrumentation.h index b044885..aeec33b 100644 --- a/Source/cmInstrumentation.h +++ b/Source/cmInstrumentation.h @@ -11,9 +11,13 @@ #include #include +#include #include #include +#ifndef CMAKE_BOOTSTRAP +# include +#endif #include #include "cmInstrumentationQuery.h" @@ -60,13 +64,13 @@ private: void WriteInstrumentationJson(Json::Value& index, std::string const& directory, std::string const& file_name); - static void InsertStaticSystemInformation(Json::Value& index); - static void GetDynamicSystemInformation(double& memory, double& load); - static void InsertDynamicSystemInformation(Json::Value& index, - std::string const& instant); - static void InsertTimingData( - Json::Value& root, std::chrono::steady_clock::time_point steadyStart, - std::chrono::system_clock::time_point systemStart); + void InsertStaticSystemInformation(Json::Value& index); + void GetDynamicSystemInformation(double& memory, double& load); + void InsertDynamicSystemInformation(Json::Value& index, + std::string const& instant); + void InsertTimingData(Json::Value& root, + std::chrono::steady_clock::time_point steadyStart, + std::chrono::system_clock::time_point systemStart); bool HasQueryFile(std::string const& file); static std::string GetCommandStr(std::vector const& args); static std::string ComputeSuffixHash(std::string const& command_str); @@ -84,4 +88,10 @@ private: std::map cdashSnippetsMap; Json::Value preTestStats; bool hasQuery = false; + bool ranSystemChecks = false; + bool ranOSCheck = false; +#ifndef CMAKE_BOOTSTRAP + std::unique_ptr systemInformation; + cmsys::SystemInformation& GetSystemInformation(); +#endif }; -- cgit v0.12