1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-16 22:37:30 +08:00

cmCTest: Move timing functions from cmCTestScriptHandler to cmCTest

This commit is contained in:
Daniel Pfeifer
2024-10-23 14:16:10 +02:00
parent 063e98eb4b
commit 83845184db
5 changed files with 29 additions and 43 deletions

View File

@@ -200,6 +200,9 @@ bool cmCTestHandlerCommand::InitialPass(std::vector<std::string> const& args,
return false; 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(); int res = handler->ProcessHandler();
if (!this->ReturnValue.empty()) { if (!this->ReturnValue.empty()) {
this->Makefile->AddDefinition(this->ReturnValue, std::to_string(res)); this->Makefile->AddDefinition(this->ReturnValue, std::to_string(res));

View File

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

View File

@@ -4,13 +4,11 @@
#include "cmConfigure.h" // IWYU pragma: keep #include "cmConfigure.h" // IWYU pragma: keep
#include <chrono>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "cmCTestGenericHandler.h" #include "cmCTestGenericHandler.h"
#include "cmDuration.h"
class cmCTest; class cmCTest;
class cmCTestCommand; class cmCTestCommand;
@@ -48,13 +46,6 @@ public:
*/ */
void UpdateElapsedTime(); 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();
cmCTestScriptHandler(const cmCTestScriptHandler&) = delete; cmCTestScriptHandler(const cmCTestScriptHandler&) = delete;
const cmCTestScriptHandler& operator=(const cmCTestScriptHandler&) = delete; const cmCTestScriptHandler& operator=(const cmCTestScriptHandler&) = delete;
@@ -80,10 +71,6 @@ private:
std::vector<std::string> ConfigurationScripts; std::vector<std::string> ConfigurationScripts;
std::vector<bool> ScriptProcessScope; 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; std::unique_ptr<cmMakefile> Makefile;
cmMakefile* ParentMakefile = nullptr; cmMakefile* ParentMakefile = nullptr;
std::unique_ptr<cmGlobalGenerator> GlobalGenerator; std::unique_ptr<cmGlobalGenerator> GlobalGenerator;

View File

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

View File

@@ -30,6 +30,7 @@ class cmCTestSubmitHandler;
class cmCTestUploadHandler; class cmCTestUploadHandler;
class cmGeneratedFileStream; class cmGeneratedFileStream;
class cmMakefile; class cmMakefile;
class cmValue;
class cmXMLWriter; class cmXMLWriter;
/** \class cmCTest /** \class cmCTest
@@ -166,12 +167,15 @@ public:
/** base64 encode a file */ /** base64 encode a file */
std::string Base64EncodeFile(std::string const& 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 * 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 * seconds if the user has set the variable CTEST_TIME_LIMIT. If that has
* not been set it returns a very large duration. * not been set it returns a very large duration.
*/ */
cmDuration GetRemainingTimeAllowed(); cmDuration GetRemainingTimeAllowed() const;
static cmDuration MaxDuration(); static cmDuration MaxDuration();