Bladeren bron

Merge topic 'ctest-timing'

83845184db cmCTest: Move timing functions from cmCTestScriptHandler to cmCTest

Acked-by: Kitware Robot <[email protected]>
Tested-by: buildbot <[email protected]>
Merge-request: !9936
Brad King 1 jaar geleden
bovenliggende
commit
65c1147e6c

+ 3 - 0
Source/CTest/cmCTestHandlerCommand.cxx

@@ -200,6 +200,9 @@ bool cmCTestHandlerCommand::InitialPass(std::vector<std::string> const& args,
     return false;
   }
 
+  // reread time limit, as the variable may have been modified.
+  this->CTest->SetTimeLimit(this->Makefile->GetDefinition("CTEST_TIME_LIMIT"));
+
   int res = handler->ProcessHandler();
   if (!this->ReturnValue.empty()) {
     this->Makefile->AddDefinition(this->ReturnValue, std::to_string(res));

+ 2 - 27
Source/CTest/cmCTestScriptHandler.cxx

@@ -2,6 +2,7 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmCTestScriptHandler.h"
 
+#include <chrono>
 #include <cstdlib>
 #include <map>
 #include <ratio>
@@ -37,7 +38,6 @@
 #include "cmSystemTools.h"
 #include "cmUVHandlePtr.h"
 #include "cmUVProcessChain.h"
-#include "cmValue.h"
 #include "cmake.h"
 
 cmCTestScriptHandler::cmCTestScriptHandler() = default;
@@ -46,9 +46,6 @@ void cmCTestScriptHandler::Initialize(cmCTest* ctest)
 {
   this->Superclass::Initialize(ctest);
 
-  // what time in seconds did this script start running
-  this->ScriptStartTime = std::chrono::steady_clock::time_point();
-
   this->Makefile.reset();
   this->ParentMakefile = nullptr;
 
@@ -87,8 +84,7 @@ void cmCTestScriptHandler::UpdateElapsedTime()
 {
   if (this->Makefile) {
     // set the current elapsed time
-    auto itime = cmDurationTo<unsigned int>(std::chrono::steady_clock::now() -
-                                            this->ScriptStartTime);
+    auto itime = cmDurationTo<unsigned int>(this->CTest->GetElapsedTime());
     auto timeString = std::to_string(itime);
     this->Makefile->AddDefinition("CTEST_ELAPSED_TIME", timeString);
   }
@@ -341,8 +337,6 @@ int cmCTestScriptHandler::RunConfigurationScript(
 
   int result;
 
-  this->ScriptStartTime = std::chrono::steady_clock::now();
-
   // read in the script
   if (pscope) {
     cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
@@ -371,22 +365,3 @@ bool cmCTestScriptHandler::RunScript(cmCTest* ctest, cmMakefile* mf,
   }
   return true;
 }
-
-cmDuration cmCTestScriptHandler::GetRemainingTimeAllowed()
-{
-  if (!this->Makefile) {
-    return cmCTest::MaxDuration();
-  }
-
-  cmValue timelimitS = this->Makefile->GetDefinition("CTEST_TIME_LIMIT");
-
-  if (!timelimitS) {
-    return cmCTest::MaxDuration();
-  }
-
-  auto timelimit = cmDuration(atof(timelimitS->c_str()));
-
-  auto duration = std::chrono::duration_cast<cmDuration>(
-    std::chrono::steady_clock::now() - this->ScriptStartTime);
-  return (timelimit - duration);
-}

+ 0 - 13
Source/CTest/cmCTestScriptHandler.h

@@ -4,13 +4,11 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
-#include <chrono>
 #include <memory>
 #include <string>
 #include <vector>
 
 #include "cmCTestGenericHandler.h"
-#include "cmDuration.h"
 
 class cmCTest;
 class cmCTestCommand;
@@ -48,13 +46,6 @@ public:
    */
   void UpdateElapsedTime();
 
-  /**
-   * Return the time remaianing that the script is allowed to run in
-   * seconds if the user has set the variable CTEST_TIME_LIMIT. If that has
-   * not been set it returns a very large value.
-   */
-  cmDuration GetRemainingTimeAllowed();
-
   cmCTestScriptHandler();
   cmCTestScriptHandler(const cmCTestScriptHandler&) = delete;
   const cmCTestScriptHandler& operator=(const cmCTestScriptHandler&) = delete;
@@ -80,10 +71,6 @@ private:
   std::vector<std::string> ConfigurationScripts;
   std::vector<bool> ScriptProcessScope;
 
-  // what time in seconds did this script start running
-  std::chrono::steady_clock::time_point ScriptStartTime =
-    std::chrono::steady_clock::time_point();
-
   std::unique_ptr<cmMakefile> Makefile;
   cmMakefile* ParentMakefile = nullptr;
   std::unique_ptr<cmGlobalGenerator> GlobalGenerator;

+ 19 - 2
Source/cmCTest.cxx

@@ -167,6 +167,10 @@ struct cmCTest::Private
 
   cmDuration GlobalTimeout = cmDuration::zero();
 
+  std::chrono::steady_clock::time_point StartTime =
+    std::chrono::steady_clock::now();
+  cmDuration TimeLimit = cmCTest::MaxDuration();
+
   int MaxTestNameWidth = 30;
 
   cm::optional<size_t> ParallelLevel = 1;
@@ -796,6 +800,7 @@ int cmCTest::ProcessSteps()
   script.CreateCMake();
   cmMakefile& mf = *script.GetMakefile();
   this->ReadCustomConfigurationFileTree(this->Impl->BinaryDir, &mf);
+  this->SetTimeLimit(mf.GetDefinition("CTEST_TIME_LIMIT"));
   this->SetCMakeVariables(mf);
   std::vector<cmListFileArgument> args{
     cmListFileArgument("RETURN_VALUE", cmListFileArgument::Unquoted, 0),
@@ -3652,9 +3657,21 @@ std::string cmCTest::GetColorCode(Color color) const
   return "";
 }
 
-cmDuration cmCTest::GetRemainingTimeAllowed()
+void cmCTest::SetTimeLimit(cmValue val)
+{
+  this->Impl->TimeLimit =
+    val ? cmDuration(atof(val->c_str())) : cmCTest::MaxDuration();
+}
+
+cmDuration cmCTest::GetElapsedTime() const
+{
+  return std::chrono::duration_cast<cmDuration>(
+    std::chrono::steady_clock::now() - this->Impl->StartTime);
+}
+
+cmDuration cmCTest::GetRemainingTimeAllowed() const
 {
-  return this->GetScriptHandler()->GetRemainingTimeAllowed();
+  return this->Impl->TimeLimit - this->GetElapsedTime();
 }
 
 cmDuration cmCTest::MaxDuration()

+ 5 - 1
Source/cmCTest.h

@@ -30,6 +30,7 @@ class cmCTestSubmitHandler;
 class cmCTestUploadHandler;
 class cmGeneratedFileStream;
 class cmMakefile;
+class cmValue;
 class cmXMLWriter;
 
 /** \class cmCTest
@@ -161,12 +162,15 @@ public:
   /** base64 encode a file */
   std::string Base64EncodeFile(std::string const& file);
 
+  void SetTimeLimit(cmValue val);
+  cmDuration GetElapsedTime() const;
+
   /**
    * Return the time remaining that the script is allowed to run in
    * seconds if the user has set the variable CTEST_TIME_LIMIT. If that has
    * not been set it returns a very large duration.
    */
-  cmDuration GetRemainingTimeAllowed();
+  cmDuration GetRemainingTimeAllowed() const;
 
   static cmDuration MaxDuration();