From 4683db44a1c5efc5ad4f2fe8dae63637aadcf16f Mon Sep 17 00:00:00 2001 From: Martin Duffy Date: Fri, 19 Sep 2025 10:24:30 -0400 Subject: [PATCH] instrumentation: Write index files to data/index/ subdirectory --- Help/manual/cmake-instrumentation.7.rst | 8 +++- Source/cmInstrumentation.cxx | 37 ++++++++----------- Source/cmInstrumentation.h | 3 +- .../check-make-program-hooks.cmake | 2 +- Tests/RunCMake/Instrumentation/hook.cmake | 1 - 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/Help/manual/cmake-instrumentation.7.rst b/Help/manual/cmake-instrumentation.7.rst index 69d4f089f7..a551212375 100644 --- a/Help/manual/cmake-instrumentation.7.rst +++ b/Help/manual/cmake-instrumentation.7.rst @@ -158,6 +158,10 @@ subdirectories: files, they should never be removed by other processes. Data collected here remains until after `Indexing`_ occurs and all `Callbacks`_ are executed. +``data/index/`` + A subset of the collected data, containing any + `v1 Index Files `_. + ``data/content/`` A subset of the collected data, containing any :ref:`cmake_instrumentation Configure Content` files. @@ -276,8 +280,8 @@ Example: In this example, after every ``cmake --build`` or ``cmake --install`` invocation, an index file ``index-.json`` will be generated in -``/.cmake/instrumentation/v1/data`` containing a list of data snippet -files created since the previous indexing. The commands +``/.cmake/instrumentation/v1/data/index`` containing a list of data +snippet files created since the previous indexing. The commands ``/usr/bin/python callback.py index-.json`` and ``/usr/bin/cmake -P callback.cmake arg index-.json`` will be executed in that order. The index file will contain the ``staticSystemInformation`` data diff --git a/Source/cmInstrumentation.cxx b/Source/cmInstrumentation.cxx index fdcf4a0d01..3a3b64dc24 100644 --- a/Source/cmInstrumentation.cxx +++ b/Source/cmInstrumentation.cxx @@ -229,7 +229,8 @@ void cmInstrumentation::WriteCustomContent() } } -std::string cmInstrumentation::GetLatestFile(std::string const& dataSubdir) +std::string cmInstrumentation::GetLatestFile(std::string const& dataSubdir, + std::string const& exclude) { std::string fullDir = cmStrCat(this->timingDirv1, "/data/", dataSubdir); std::string latestFile; @@ -238,7 +239,8 @@ std::string cmInstrumentation::GetLatestFile(std::string const& dataSubdir) if (d.Load(fullDir)) { for (unsigned int i = 0; i < d.GetNumberOfFiles(); i++) { std::string fname = d.GetFileName(i); - if (fname != "." && fname != ".." && fname > latestFile) { + if (fname != "." && fname != ".." && fname != exclude && + fname > latestFile) { latestFile = fname; } } @@ -267,8 +269,9 @@ void cmInstrumentation::RemoveOldFiles(std::string const& dataSubdir) std::string json = ".json"; std::string timestamp = fname.substr( index.size(), fname.size() - index.size() - json.size() - 1); - if (cmSystemTools::FileExists(cmStrCat( - this->timingDirv1, "/data/index-", timestamp, ".json"))) { + if (cmSystemTools::FileExists(cmStrCat(this->timingDirv1, + "/data/index/index-", + timestamp, ".json"))) { continue; } } @@ -319,33 +322,21 @@ int cmInstrumentation::CollectTimingData(cmInstrumentationQuery::Hook hook) std::string const& directory = cmStrCat(this->timingDirv1, "/data"); std::string suffix_time = ComputeSuffixTime(); std::string const& index_name = cmStrCat("index-", suffix_time, ".json"); - std::string index_path = cmStrCat(directory, '/', index_name); + std::string index_path = cmStrCat(directory, "/index/", index_name); cmSystemTools::Touch(index_path, true); // Gather Snippets using snippet = std::pair; std::vector files; cmsys::Directory d; - std::string last_index; + std::string last_index_name = this->GetLatestFile("index", index_name); if (d.Load(directory)) { for (unsigned int i = 0; i < d.GetNumberOfFiles(); i++) { std::string fpath = d.GetFilePath(i); std::string fname = d.GetFile(i); - if (fname.rfind('.', 0) == 0 || fname == index_name || - d.FileIsDirectory(i)) { + if (fname.rfind('.', 0) == 0 || d.FileIsDirectory(i)) { continue; } - if (fname.rfind("index-", 0) == 0) { - if (last_index.empty()) { - last_index = fpath; - } else { - int compare; - cmSystemTools::FileTimeCompare(fpath, last_index, &compare); - if (compare == 1) { - last_index = fpath; - } - } - } files.push_back(snippet(std::move(fname), std::move(fpath))); } } @@ -362,11 +353,13 @@ int cmInstrumentation::CollectTimingData(cmInstrumentationQuery::Hook hook) this->InsertStaticSystemInformation(index); } for (auto const& file : files) { - if (last_index.empty()) { + if (last_index_name.empty()) { index["snippets"].append(file.first); } else { int compare; - cmSystemTools::FileTimeCompare(file.second, last_index, &compare); + std::string last_index_path = + cmStrCat(directory, "/index/", last_index_name); + cmSystemTools::FileTimeCompare(file.second, last_index_path, &compare); if (compare == 1) { index["snippets"].append(file.first); } @@ -381,7 +374,7 @@ int cmInstrumentation::CollectTimingData(cmInstrumentationQuery::Hook hook) } // Write index file - this->WriteInstrumentationJson(index, "data", index_name); + this->WriteInstrumentationJson(index, "data/index", index_name); // Execute callbacks for (auto& cb : this->callbacks) { diff --git a/Source/cmInstrumentation.h b/Source/cmInstrumentation.h index 3b0d28d9ba..46beca6f93 100644 --- a/Source/cmInstrumentation.h +++ b/Source/cmInstrumentation.h @@ -63,7 +63,6 @@ public: std::vector> const& callback); void AddCustomContent(std::string const& name, Json::Value const& contents); void WriteCustomContent(); - std::string GetLatestFile(std::string const& dataSubdir); void ClearGeneratedQueries(); int CollectTimingData(cmInstrumentationQuery::Hook hook); int SpawnBuildDaemon(); @@ -99,6 +98,8 @@ private: Json::Value const& snippetData); size_t AssignTargetToTraceThread(std::vector& workers, uint64_t timeStart, uint64_t duration); + std::string GetLatestFile(std::string const& dataSubdir, + std::string const& exclude = ""); std::string binaryDir; std::string timingDirv1; std::string userTimingDirv1; diff --git a/Tests/RunCMake/Instrumentation/check-make-program-hooks.cmake b/Tests/RunCMake/Instrumentation/check-make-program-hooks.cmake index 66ff80acc8..01336e70d3 100644 --- a/Tests/RunCMake/Instrumentation/check-make-program-hooks.cmake +++ b/Tests/RunCMake/Instrumentation/check-make-program-hooks.cmake @@ -15,7 +15,7 @@ macro(hasPostBuildArtifacts) set(postBuildRan 1) endif() if (NOT dataDirClean) - file(GLOB data "${v1}/data/*") + file(GLOB data "${v1}/data/*json") if ("${data}" STREQUAL "") set(dataDirClean 1) endif() diff --git a/Tests/RunCMake/Instrumentation/hook.cmake b/Tests/RunCMake/Instrumentation/hook.cmake index 7d6f4fd33a..84e040edc6 100644 --- a/Tests/RunCMake/Instrumentation/hook.cmake +++ b/Tests/RunCMake/Instrumentation/hook.cmake @@ -110,7 +110,6 @@ if (NOT hasStaticInfo STREQUAL UNEXPECTED) json_has_key("${index}" "${staticSystemInformation}" vendorString ${hasStaticInfo}) endif() -get_filename_component(dataDir ${index} DIRECTORY) get_filename_component(v1 ${dataDir} DIRECTORY) if (EXISTS ${v1}/${hook}.hook) add_error("Received multiple triggers of the same hook: ${hook}")