Переглянути джерело

instrumentation: Write index files to data/index/ subdirectory

Martin Duffy 1 місяць тому
батько
коміт
4683db44a1

+ 6 - 2
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 <v1 Index File_>`_.
+
 ``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-<timestamp>.json`` will be generated in
-``<build>/.cmake/instrumentation/v1/data`` containing a list of data snippet
-files created since the previous indexing. The commands
+``<build>/.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-<timestamp>.json`` and
 ``/usr/bin/cmake -P callback.cmake arg index-<timestamp>.json`` will be executed
 in that order. The index file will contain the ``staticSystemInformation`` data

+ 15 - 22
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::string, std::string>;
   std::vector<snippet> 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) {

+ 2 - 1
Source/cmInstrumentation.h

@@ -63,7 +63,6 @@ public:
                       std::vector<std::vector<std::string>> 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<uint64_t>& 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;

+ 1 - 1
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()

+ 0 - 1
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}")