cmake/Source/CTest/cmCTestMultiProcessHandler.cxx

1443 lines
42 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2009-10-04 10:30:41 +03:00
#include "cmCTestMultiProcessHandler.h"
2016-07-09 11:21:54 +02:00
2016-10-30 18:24:19 +01:00
#include <algorithm>
2020-02-01 23:06:01 +01:00
#include <cassert>
2018-04-23 21:13:27 +02:00
#include <chrono>
2020-02-01 23:06:01 +01:00
#include <cmath>
2020-08-30 11:54:41 +02:00
#include <cstddef> // IWYU pragma: keep
2020-02-01 23:06:01 +01:00
#include <cstdlib>
2018-08-09 18:06:22 +02:00
#include <cstring>
2016-10-30 18:24:19 +01:00
#include <iomanip>
2019-11-11 23:01:05 +01:00
#include <iostream>
2016-07-09 11:21:54 +02:00
#include <list>
2016-10-30 18:24:19 +01:00
#include <sstream>
2016-07-09 11:21:54 +02:00
#include <stack>
2019-11-11 23:01:05 +01:00
#include <unordered_map>
2016-10-30 18:24:19 +01:00
#include <utility>
2020-02-01 23:06:01 +01:00
#include <vector>
2020-08-30 11:54:41 +02:00
#include <cm/memory>
#include <cmext/algorithm>
#include <cm3p/json/value.h>
#include <cm3p/json/writer.h>
#include <cm3p/uv.h>
2020-02-01 23:06:01 +01:00
#include "cmsys/FStream.hxx"
#include "cmsys/SystemInformation.hxx"
#include "cmAffinity.h"
#include "cmCTest.h"
#include "cmCTestBinPacker.h"
#include "cmCTestRunTest.h"
#include "cmCTestTestHandler.h"
#include "cmDuration.h"
#include "cmListFileCache.h"
#include "cmRange.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmUVSignalHackRAII.h" // IWYU pragma: keep
#include "cmWorkingDirectory.h"
2009-10-04 10:30:41 +03:00
2019-11-11 23:01:05 +01:00
namespace cmsys {
class RegularExpression;
}
2010-11-13 01:00:53 +02:00
class TestComparator
{
public:
2016-07-09 11:21:54 +02:00
TestComparator(cmCTestMultiProcessHandler* handler)
: Handler(handler)
{
}
2010-11-13 01:00:53 +02:00
// Sorts tests in descending order of cost
2016-07-09 11:21:54 +02:00
bool operator()(int index1, int index2) const
{
2021-09-14 00:13:48 +02:00
return this->Handler->Properties[index1]->Cost >
this->Handler->Properties[index2]->Cost;
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
private:
cmCTestMultiProcessHandler* Handler;
};
2009-10-04 10:30:41 +03:00
cmCTestMultiProcessHandler::cmCTestMultiProcessHandler()
{
this->ParallelLevel = 1;
2015-11-17 17:22:37 +01:00
this->TestLoad = 0;
2018-10-07 12:27:06 +02:00
this->FakeLoadForTesting = 0;
2009-10-04 10:30:41 +03:00
this->Completed = 0;
this->RunningCount = 0;
2018-08-09 18:06:22 +02:00
this->ProcessorsAvailable = cmAffinity::GetProcessorsAvailable();
this->HaveAffinity = this->ProcessorsAvailable.size();
2014-08-03 19:52:23 +02:00
this->HasCycles = false;
2015-11-17 17:22:37 +01:00
this->SerialTestRunning = false;
2009-10-04 10:30:41 +03:00
}
2019-11-11 23:01:05 +01:00
cmCTestMultiProcessHandler::~cmCTestMultiProcessHandler() = default;
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
// Set the tests
void cmCTestMultiProcessHandler::SetTests(TestMap& tests,
PropertiesMap& properties)
2009-10-04 10:30:41 +03:00
{
this->Tests = tests;
this->Properties = properties;
this->Total = this->Tests.size();
// set test run map to false for all
2018-01-26 17:06:56 +01:00
for (auto const& t : this->Tests) {
this->TestRunningMap[t.first] = false;
this->TestFinishMap[t.first] = false;
2016-07-09 11:21:54 +02:00
}
if (!this->CTest->GetShowOnly()) {
2010-03-17 14:00:29 +02:00
this->ReadCostData();
2014-08-03 19:52:23 +02:00
this->HasCycles = !this->CheckCycles();
2016-07-09 11:21:54 +02:00
if (this->HasCycles) {
2014-08-03 19:52:23 +02:00
return;
2010-03-17 14:00:29 +02:00
}
2016-07-09 11:21:54 +02:00
this->CreateTestCostList();
}
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
// Set the max number of tests that can be run at the same time.
2009-10-04 10:30:41 +03:00
void cmCTestMultiProcessHandler::SetParallelLevel(size_t level)
{
this->ParallelLevel = level < 1 ? 1 : level;
}
2015-11-17 17:22:37 +01:00
void cmCTestMultiProcessHandler::SetTestLoad(unsigned long load)
{
this->TestLoad = load;
2018-10-07 12:27:06 +02:00
std::string fake_load_value;
if (cmSystemTools::GetEnv("__CTEST_FAKE_LOAD_AVERAGE_FOR_TESTING",
fake_load_value)) {
2020-02-01 23:06:01 +01:00
if (!cmStrToULong(fake_load_value, &this->FakeLoadForTesting)) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Failed to parse fake load value: " +
fake_load_value);
2018-10-07 12:27:06 +02:00
}
}
2015-11-17 17:22:37 +01:00
}
2009-10-04 10:30:41 +03:00
void cmCTestMultiProcessHandler::RunTests()
{
this->CheckResume();
2016-07-09 11:21:54 +02:00
if (this->HasCycles) {
2009-11-06 22:07:41 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2018-04-23 21:13:27 +02:00
#ifdef CMAKE_UV_SIGNAL_HACK
cmUVSignalHackRAII hackRAII;
#endif
2009-10-04 10:30:41 +03:00
this->TestHandler->SetMaxIndex(this->FindMaxIndex());
2018-04-23 21:13:27 +02:00
uv_loop_init(&this->Loop);
2009-10-04 10:30:41 +03:00
this->StartNextTests();
2018-04-23 21:13:27 +02:00
uv_run(&this->Loop, UV_RUN_DEFAULT);
uv_loop_close(&this->Loop);
2020-08-30 11:54:41 +02:00
if (!this->StopTimePassed && !this->CheckStopOnFailure()) {
2020-02-01 23:06:01 +01:00
assert(this->Completed == this->Total);
assert(this->Tests.empty());
}
assert(this->AllResourcesAvailable());
2009-10-04 10:30:41 +03:00
this->MarkFinished();
2010-06-23 01:18:35 +03:00
this->UpdateCostData();
2009-10-04 10:30:41 +03:00
}
2018-04-23 21:13:27 +02:00
bool cmCTestMultiProcessHandler::StartTestProcess(int test)
2009-10-04 10:30:41 +03:00
{
2018-08-09 18:06:22 +02:00
if (this->HaveAffinity && this->Properties[test]->WantAffinity) {
size_t needProcessors = this->GetProcessorsUsed(test);
if (needProcessors > this->ProcessorsAvailable.size()) {
return false;
}
std::vector<size_t> affinity;
affinity.reserve(needProcessors);
for (size_t i = 0; i < needProcessors; ++i) {
auto p = this->ProcessorsAvailable.begin();
affinity.push_back(*p);
this->ProcessorsAvailable.erase(p);
}
this->Properties[test]->Affinity = std::move(affinity);
}
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
2016-07-09 11:21:54 +02:00
"test " << test << "\n", this->Quiet);
2009-10-04 10:30:41 +03:00
this->TestRunningMap[test] = true; // mark the test as running
// now remove the test itself
this->EraseTest(test);
2021-09-14 00:13:48 +02:00
this->RunningCount += this->GetProcessorsUsed(test);
2009-10-04 10:30:41 +03:00
2020-08-30 11:54:41 +02:00
auto testRun = cm::make_unique<cmCTestRunTest>(*this);
if (this->RepeatMode != cmCTest::Repeat::Never) {
testRun->SetRepeatMode(this->RepeatMode);
testRun->SetNumberOfRuns(this->RepeatCount);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
testRun->SetIndex(test);
testRun->SetTestProperties(this->Properties[test]);
2020-02-01 23:06:01 +01:00
if (this->TestHandler->UseResourceSpec) {
testRun->SetUseAllocatedResources(true);
testRun->SetAllocatedResources(this->AllocatedResources[test]);
}
2009-11-06 22:07:41 +02:00
2016-10-30 18:24:19 +01:00
// Find any failed dependencies for this test. We assume the more common
// scenario has no failed tests, so make it the outer loop.
2018-01-26 17:06:56 +01:00
for (std::string const& f : *this->Failed) {
2020-08-30 11:54:41 +02:00
if (cm::contains(this->Properties[test]->RequireSuccessDepends, f)) {
2018-01-26 17:06:56 +01:00
testRun->AddFailedDependency(f);
2016-10-30 18:24:19 +01:00
}
}
2018-08-09 18:06:22 +02:00
// Always lock the resources we'll be using, even if we fail to set the
// working directory because FinishTestProcess() will try to unlock them
2010-06-23 01:18:35 +03:00
this->LockResources(test);
2020-08-30 11:54:41 +02:00
if (!this->ResourceAllocationErrors[test].empty()) {
std::ostringstream e;
e << "Insufficient resources for test " << this->Properties[test]->Name
<< ":\n\n";
for (auto const& it : this->ResourceAllocationErrors[test]) {
switch (it.second) {
case ResourceAllocationError::NoResourceType:
e << " Test requested resources of type '" << it.first
<< "' which does not exist\n";
break;
case ResourceAllocationError::InsufficientResources:
e << " Test requested resources of type '" << it.first
<< "' in the following amounts:\n";
for (auto const& group : this->Properties[test]->ResourceGroups) {
for (auto const& requirement : group) {
if (requirement.ResourceType == it.first) {
e << " " << requirement.SlotsNeeded
<< (requirement.SlotsNeeded == 1 ? " slot\n" : " slots\n");
}
}
}
e << " but only the following units were available:\n";
for (auto const& res :
this->ResourceAllocator.GetResources().at(it.first)) {
e << " '" << res.first << "': " << res.second.Total
<< (res.second.Total == 1 ? " slot\n" : " slots\n");
}
break;
}
e << "\n";
}
e << "Resource spec file:\n\n " << this->TestHandler->ResourceSpecFile;
cmCTestRunTest::StartFailure(std::move(testRun), e.str(),
"Insufficient resources");
2020-02-01 23:06:01 +01:00
return false;
}
2018-08-09 18:06:22 +02:00
cmWorkingDirectory workdir(this->Properties[test]->Directory);
if (workdir.Failed()) {
2020-08-30 11:54:41 +02:00
cmCTestRunTest::StartFailure(std::move(testRun),
"Failed to change working directory to " +
this->Properties[test]->Directory + " : " +
std::strerror(workdir.GetLastResult()),
"Failed to change working directory");
return false;
2016-07-09 11:21:54 +02:00
}
2018-04-23 21:13:27 +02:00
2020-08-30 11:54:41 +02:00
// Ownership of 'testRun' has moved to another structure.
// When the test finishes, FinishTestProcess will be called.
return cmCTestRunTest::StartTest(std::move(testRun), this->Completed,
this->Total);
2009-10-04 10:30:41 +03:00
}
2020-02-01 23:06:01 +01:00
bool cmCTestMultiProcessHandler::AllocateResources(int index)
{
if (!this->TestHandler->UseResourceSpec) {
return true;
}
std::map<std::string, std::vector<cmCTestBinPackerAllocation>> allocations;
if (!this->TryAllocateResources(index, allocations)) {
return false;
}
auto& allocatedResources = this->AllocatedResources[index];
allocatedResources.resize(this->Properties[index]->ResourceGroups.size());
for (auto const& it : allocations) {
for (auto const& alloc : it.second) {
bool result = this->ResourceAllocator.AllocateResource(
it.first, alloc.Id, alloc.SlotsNeeded);
(void)result;
assert(result);
allocatedResources[alloc.ProcessIndex][it.first].push_back(
{ alloc.Id, static_cast<unsigned int>(alloc.SlotsNeeded) });
}
}
return true;
}
bool cmCTestMultiProcessHandler::TryAllocateResources(
int index,
2020-08-30 11:54:41 +02:00
std::map<std::string, std::vector<cmCTestBinPackerAllocation>>& allocations,
std::map<std::string, ResourceAllocationError>* errors)
2020-02-01 23:06:01 +01:00
{
allocations.clear();
std::size_t processIndex = 0;
for (auto const& process : this->Properties[index]->ResourceGroups) {
for (auto const& requirement : process) {
for (int i = 0; i < requirement.UnitsNeeded; ++i) {
allocations[requirement.ResourceType].push_back(
{ processIndex, requirement.SlotsNeeded, "" });
}
}
++processIndex;
}
2020-08-30 11:54:41 +02:00
bool result = true;
2020-02-01 23:06:01 +01:00
auto const& availableResources = this->ResourceAllocator.GetResources();
for (auto& it : allocations) {
if (!availableResources.count(it.first)) {
2020-08-30 11:54:41 +02:00
if (errors) {
(*errors)[it.first] = ResourceAllocationError::NoResourceType;
result = false;
} else {
return false;
}
} else if (!cmAllocateCTestResourcesRoundRobin(
availableResources.at(it.first), it.second)) {
if (errors) {
(*errors)[it.first] = ResourceAllocationError::InsufficientResources;
result = false;
} else {
return false;
}
2020-02-01 23:06:01 +01:00
}
}
2020-08-30 11:54:41 +02:00
return result;
2020-02-01 23:06:01 +01:00
}
void cmCTestMultiProcessHandler::DeallocateResources(int index)
{
if (!this->TestHandler->UseResourceSpec) {
return;
}
{
auto& allocatedResources = this->AllocatedResources[index];
for (auto const& processAlloc : allocatedResources) {
for (auto const& it : processAlloc) {
auto resourceType = it.first;
for (auto const& it2 : it.second) {
bool success = this->ResourceAllocator.DeallocateResource(
resourceType, it2.Id, it2.Slots);
(void)success;
assert(success);
}
}
}
}
this->AllocatedResources.erase(index);
}
bool cmCTestMultiProcessHandler::AllResourcesAvailable()
{
for (auto const& it : this->ResourceAllocator.GetResources()) {
for (auto const& it2 : it.second) {
if (it2.second.Locked != 0) {
return false;
}
}
}
return true;
}
void cmCTestMultiProcessHandler::CheckResourcesAvailable()
{
2020-08-30 11:54:41 +02:00
if (this->TestHandler->UseResourceSpec) {
for (auto test : this->SortedTests) {
std::map<std::string, std::vector<cmCTestBinPackerAllocation>>
allocations;
this->TryAllocateResources(test, allocations,
&this->ResourceAllocationErrors[test]);
}
2020-02-01 23:06:01 +01:00
}
}
2020-08-30 11:54:41 +02:00
bool cmCTestMultiProcessHandler::CheckStopOnFailure()
{
return this->CTest->GetStopOnFailure();
}
2018-10-28 12:09:07 +01:00
bool cmCTestMultiProcessHandler::CheckStopTimePassed()
{
if (!this->StopTimePassed) {
std::chrono::system_clock::time_point stop_time =
this->CTest->GetStopTime();
if (stop_time != std::chrono::system_clock::time_point() &&
stop_time <= std::chrono::system_clock::now()) {
this->SetStopTimePassed();
}
}
return this->StopTimePassed;
}
void cmCTestMultiProcessHandler::SetStopTimePassed()
{
if (!this->StopTimePassed) {
cmCTestLog(this->CTest, ERROR_MESSAGE,
"The stop time has been passed. "
"Stopping all tests."
<< std::endl);
this->StopTimePassed = true;
}
}
2010-06-23 01:18:35 +03:00
void cmCTestMultiProcessHandler::LockResources(int index)
{
2015-04-27 22:25:09 +02:00
this->LockedResources.insert(
2016-07-09 11:21:54 +02:00
this->Properties[index]->LockedResources.begin(),
this->Properties[index]->LockedResources.end());
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
if (this->Properties[index]->RunSerial) {
2015-11-17 17:22:37 +01:00
this->SerialTestRunning = true;
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
}
void cmCTestMultiProcessHandler::UnlockResources(int index)
{
2018-01-26 17:06:56 +01:00
for (std::string const& i : this->Properties[index]->LockedResources) {
this->LockedResources.erase(i);
2016-07-09 11:21:54 +02:00
}
if (this->Properties[index]->RunSerial) {
2015-11-17 17:22:37 +01:00
this->SerialTestRunning = false;
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
}
2009-10-04 10:30:41 +03:00
void cmCTestMultiProcessHandler::EraseTest(int test)
{
this->Tests.erase(test);
2010-11-13 01:00:53 +02:00
this->SortedTests.erase(
std::find(this->SortedTests.begin(), this->SortedTests.end(), test));
2009-10-04 10:30:41 +03:00
}
inline size_t cmCTestMultiProcessHandler::GetProcessorsUsed(int test)
{
2016-07-09 11:21:54 +02:00
size_t processors = static_cast<int>(this->Properties[test]->Processors);
// If processors setting is set higher than the -j
// setting, we default to using all of the process slots.
if (processors > this->ParallelLevel) {
2009-10-04 10:30:41 +03:00
processors = this->ParallelLevel;
2016-07-09 11:21:54 +02:00
}
2018-08-09 18:06:22 +02:00
// Cap tests that want affinity to the maximum affinity available.
if (this->HaveAffinity && processors > this->HaveAffinity &&
this->Properties[test]->WantAffinity) {
processors = this->HaveAffinity;
}
2009-10-04 10:30:41 +03:00
return processors;
}
2015-11-17 17:22:37 +01:00
std::string cmCTestMultiProcessHandler::GetName(int test)
{
return this->Properties[test]->Name;
}
2009-10-04 10:30:41 +03:00
bool cmCTestMultiProcessHandler::StartTest(int test)
{
2016-07-09 11:21:54 +02:00
// Check for locked resources
2018-01-26 17:06:56 +01:00
for (std::string const& i : this->Properties[test]->LockedResources) {
2020-08-30 11:54:41 +02:00
if (cm::contains(this->LockedResources, i)) {
2010-06-23 01:18:35 +03:00
return false;
}
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
2020-02-01 23:06:01 +01:00
// Allocate resources
2020-08-30 11:54:41 +02:00
if (this->ResourceAllocationErrors[test].empty() &&
2020-02-01 23:06:01 +01:00
!this->AllocateResources(test)) {
this->DeallocateResources(test);
return false;
}
2009-10-04 10:30:41 +03:00
// if there are no depends left then run this test
2016-07-09 11:21:54 +02:00
if (this->Tests[test].empty()) {
2018-04-23 21:13:27 +02:00
return this->StartTestProcess(test);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// This test was not able to start because it is waiting
2009-10-04 10:30:41 +03:00
// on depends to run
2020-02-01 23:06:01 +01:00
this->DeallocateResources(test);
2009-10-04 10:30:41 +03:00
return false;
}
void cmCTestMultiProcessHandler::StartNextTests()
{
2018-10-07 12:27:06 +02:00
if (this->TestLoadRetryTimer.get() != nullptr) {
// This timer may be waiting to call StartNextTests again.
// Since we have been called it is no longer needed.
uv_timer_stop(this->TestLoadRetryTimer);
}
2018-04-23 21:13:27 +02:00
if (this->Tests.empty()) {
2018-10-07 12:27:06 +02:00
this->TestLoadRetryTimer.reset();
2018-04-23 21:13:27 +02:00
return;
}
2018-10-28 12:09:07 +01:00
if (this->CheckStopTimePassed()) {
return;
}
2020-08-30 11:54:41 +02:00
if (this->CheckStopOnFailure() && !this->Failed->empty()) {
return;
}
2018-10-07 12:27:06 +02:00
size_t numToStart = 0;
2016-07-09 11:21:54 +02:00
if (this->RunningCount < this->ParallelLevel) {
2013-03-16 19:13:01 +02:00
numToStart = this->ParallelLevel - this->RunningCount;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (numToStart == 0) {
2009-10-04 10:30:41 +03:00
return;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2015-11-17 17:22:37 +01:00
// Don't start any new tests if one with the RUN_SERIAL property
// is already running.
2016-07-09 11:21:54 +02:00
if (this->SerialTestRunning) {
2015-11-17 17:22:37 +01:00
return;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
bool allTestsFailedTestLoadCheck = false;
size_t minProcessorsRequired = this->ParallelLevel;
2017-04-14 19:02:05 +02:00
std::string testWithMinProcessors;
2015-11-17 17:22:37 +01:00
cmsys::SystemInformation info;
unsigned long systemLoad = 0;
size_t spareLoad = 0;
2016-07-09 11:21:54 +02:00
if (this->TestLoad > 0) {
2015-11-17 17:22:37 +01:00
// Activate possible wait.
allTestsFailedTestLoadCheck = true;
// Check for a fake load average value used in testing.
2018-10-07 12:27:06 +02:00
if (this->FakeLoadForTesting > 0) {
systemLoad = this->FakeLoadForTesting;
// Drop the fake load for the next iteration to a value low enough
// that the next iteration will start tests.
this->FakeLoadForTesting = 1;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// If it's not set, look up the true load average.
2016-07-09 11:21:54 +02:00
else {
2015-11-17 17:22:37 +01:00
systemLoad = static_cast<unsigned long>(ceil(info.GetLoadAverage()));
2016-07-09 11:21:54 +02:00
}
spareLoad =
(this->TestLoad > systemLoad ? this->TestLoad - systemLoad : 0);
2015-11-17 17:22:37 +01:00
// Don't start more tests than the spare load can support.
2016-07-09 11:21:54 +02:00
if (numToStart > spareLoad) {
2015-11-17 17:22:37 +01:00
numToStart = spareLoad;
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2010-11-13 01:00:53 +02:00
TestList copy = this->SortedTests;
2018-01-26 17:06:56 +01:00
for (auto const& test : copy) {
2015-11-17 17:22:37 +01:00
// Take a nap if we're currently performing a RUN_SERIAL test.
2016-07-09 11:21:54 +02:00
if (this->SerialTestRunning) {
2015-11-17 17:22:37 +01:00
break;
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
// We can only start a RUN_SERIAL test if no other tests are also
// running.
2018-01-26 17:06:56 +01:00
if (this->Properties[test]->RunSerial && this->RunningCount > 0) {
2015-11-17 17:22:37 +01:00
continue;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2021-09-14 00:13:48 +02:00
size_t processors = this->GetProcessorsUsed(test);
2015-11-17 17:22:37 +01:00
bool testLoadOk = true;
2016-07-09 11:21:54 +02:00
if (this->TestLoad > 0) {
if (processors <= spareLoad) {
2018-08-09 18:06:22 +02:00
cmCTestLog(this->CTest, DEBUG,
2021-09-14 00:13:48 +02:00
"OK to run " << this->GetName(test) << ", it requires "
2018-08-09 18:06:22 +02:00
<< processors << " procs & system load is: "
<< systemLoad << std::endl);
2015-11-17 17:22:37 +01:00
allTestsFailedTestLoadCheck = false;
2016-07-09 11:21:54 +02:00
} else {
2015-11-17 17:22:37 +01:00
testLoadOk = false;
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
if (processors <= minProcessorsRequired) {
2015-11-17 17:22:37 +01:00
minProcessorsRequired = processors;
2021-09-14 00:13:48 +02:00
testWithMinProcessors = this->GetName(test);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
if (testLoadOk && processors <= numToStart && this->StartTest(test)) {
2015-11-17 17:22:37 +01:00
numToStart -= processors;
2016-07-09 11:21:54 +02:00
} else if (numToStart == 0) {
2015-11-17 17:22:37 +01:00
break;
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
if (allTestsFailedTestLoadCheck) {
2018-04-23 21:13:27 +02:00
// Find out whether there are any non RUN_SERIAL tests left, so that the
// correct warning may be displayed.
bool onlyRunSerialTestsLeft = true;
for (auto const& test : copy) {
if (!this->Properties[test]->RunSerial) {
onlyRunSerialTestsLeft = false;
}
}
2021-09-14 00:13:48 +02:00
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "***** WAITING, ");
2018-04-23 21:13:27 +02:00
2016-07-09 11:21:54 +02:00
if (this->SerialTestRunning) {
2021-09-14 00:13:48 +02:00
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
2015-11-17 17:22:37 +01:00
"Waiting for RUN_SERIAL test to finish.");
2018-04-23 21:13:27 +02:00
} else if (onlyRunSerialTestsLeft) {
2021-09-14 00:13:48 +02:00
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
2018-04-23 21:13:27 +02:00
"Only RUN_SERIAL tests remain, awaiting available slot.");
2016-07-09 11:21:54 +02:00
} else {
/* clang-format off */
2021-09-14 00:13:48 +02:00
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
2015-11-17 17:22:37 +01:00
"System Load: " << systemLoad << ", "
"Max Allowed Load: " << this->TestLoad << ", "
"Smallest test " << testWithMinProcessors <<
" requires " << minProcessorsRequired);
2016-07-09 11:21:54 +02:00
/* clang-format on */
}
2021-09-14 00:13:48 +02:00
cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "*****" << std::endl);
2015-11-17 17:22:37 +01:00
2018-10-07 12:27:06 +02:00
// Wait between 1 and 5 seconds before trying again.
unsigned int milliseconds = (cmSystemTools::RandomSeed() % 5 + 1) * 1000;
if (this->FakeLoadForTesting) {
milliseconds = 10;
}
if (this->TestLoadRetryTimer.get() == nullptr) {
this->TestLoadRetryTimer.init(this->Loop, this);
2009-10-04 10:30:41 +03:00
}
2018-10-07 12:27:06 +02:00
this->TestLoadRetryTimer.start(
&cmCTestMultiProcessHandler::OnTestLoadRetryCB, milliseconds, 0);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
2018-10-07 12:27:06 +02:00
void cmCTestMultiProcessHandler::OnTestLoadRetryCB(uv_timer_t* timer)
{
2021-09-14 00:13:48 +02:00
auto* self = static_cast<cmCTestMultiProcessHandler*>(timer->data);
2018-10-07 12:27:06 +02:00
self->StartNextTests();
}
2020-08-30 11:54:41 +02:00
void cmCTestMultiProcessHandler::FinishTestProcess(
std::unique_ptr<cmCTestRunTest> runner, bool started)
2009-10-04 10:30:41 +03:00
{
2018-04-23 21:13:27 +02:00
this->Completed++;
int test = runner->GetIndex();
2021-09-14 00:13:48 +02:00
auto* properties = runner->GetTestProperties();
2009-10-04 10:30:41 +03:00
2018-04-23 21:13:27 +02:00
bool testResult = runner->EndTest(this->Completed, this->Total, started);
2018-10-28 12:09:07 +01:00
if (runner->TimedOutForStopTime()) {
this->SetStopTimePassed();
}
2018-04-23 21:13:27 +02:00
if (started) {
2020-08-30 11:54:41 +02:00
if (!this->StopTimePassed &&
cmCTestRunTest::StartAgain(std::move(runner), this->Completed)) {
2015-08-17 11:37:30 +02:00
this->Completed--; // remove the completed test because run again
2018-04-23 21:13:27 +02:00
return;
2016-07-09 11:21:54 +02:00
}
}
2018-04-23 21:13:27 +02:00
if (testResult) {
this->Passed->push_back(properties->Name);
} else if (!properties->Disabled) {
this->Failed->push_back(properties->Name);
}
for (auto& t : this->Tests) {
t.second.erase(test);
}
this->TestFinishMap[test] = true;
this->TestRunningMap[test] = false;
this->WriteCheckpoint(test);
2020-02-01 23:06:01 +01:00
this->DeallocateResources(test);
2018-04-23 21:13:27 +02:00
this->UnlockResources(test);
2021-09-14 00:13:48 +02:00
this->RunningCount -= this->GetProcessorsUsed(test);
2018-04-23 21:13:27 +02:00
2018-08-09 18:06:22 +02:00
for (auto p : properties->Affinity) {
this->ProcessorsAvailable.insert(p);
}
properties->Affinity.clear();
2020-08-30 11:54:41 +02:00
runner.reset();
2018-04-23 21:13:27 +02:00
if (started) {
this->StartNextTests();
}
2009-10-04 10:30:41 +03:00
}
2010-06-23 01:18:35 +03:00
void cmCTestMultiProcessHandler::UpdateCostData()
2009-10-04 10:30:41 +03:00
{
2010-06-23 01:18:35 +03:00
std::string fname = this->CTest->GetCostDataFile();
std::string tmpout = fname + ".tmp";
2015-04-27 22:25:09 +02:00
cmsys::ofstream fout;
fout.open(tmpout.c_str());
2010-06-23 01:18:35 +03:00
PropertiesMap temp = this->Properties;
2009-10-04 10:30:41 +03:00
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(fname)) {
2014-08-03 19:52:23 +02:00
cmsys::ifstream fin;
2009-10-04 10:30:41 +03:00
fin.open(fname.c_str());
2010-06-23 01:18:35 +03:00
2009-10-04 10:30:41 +03:00
std::string line;
2016-07-09 11:21:54 +02:00
while (std::getline(fin, line)) {
2016-10-30 18:24:19 +01:00
if (line == "---") {
2016-07-09 11:21:54 +02:00
break;
2016-10-30 18:24:19 +01:00
}
2018-10-28 12:09:07 +01:00
std::vector<std::string> parts = cmSystemTools::SplitString(line, ' ');
2016-07-09 11:21:54 +02:00
// Format: <name> <previous_runs> <avg_cost>
2016-10-30 18:24:19 +01:00
if (parts.size() < 3) {
2016-07-09 11:21:54 +02:00
break;
2016-10-30 18:24:19 +01:00
}
2010-06-23 01:18:35 +03:00
std::string name = parts[0];
int prev = atoi(parts[1].c_str());
float cost = static_cast<float>(atof(parts[2].c_str()));
2009-10-04 10:30:41 +03:00
2010-06-23 01:18:35 +03:00
int index = this->SearchByName(name);
2016-07-09 11:21:54 +02:00
if (index == -1) {
2010-06-23 01:18:35 +03:00
// This test is not in memory. We just rewrite the entry
fout << name << " " << prev << " " << cost << "\n";
2016-07-09 11:21:54 +02:00
} else {
2010-06-23 01:18:35 +03:00
// Update with our new average cost
fout << name << " " << this->Properties[index]->PreviousRuns << " "
2016-07-09 11:21:54 +02:00
<< this->Properties[index]->Cost << "\n";
2010-06-23 01:18:35 +03:00
temp.erase(index);
}
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
fin.close();
2015-04-27 22:25:09 +02:00
cmSystemTools::RemoveFile(fname);
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
// Add all tests not previously listed in the file
2018-01-26 17:06:56 +01:00
for (auto const& i : temp) {
fout << i.second->Name << " " << i.second->PreviousRuns << " "
<< i.second->Cost << "\n";
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
// Write list of failed tests
fout << "---\n";
2018-01-26 17:06:56 +01:00
for (std::string const& f : *this->Failed) {
fout << f << "\n";
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
fout.close();
2019-11-11 23:01:05 +01:00
cmSystemTools::RenameFile(tmpout, fname);
2010-06-23 01:18:35 +03:00
}
void cmCTestMultiProcessHandler::ReadCostData()
{
std::string fname = this->CTest->GetCostDataFile();
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(fname, true)) {
2014-08-03 19:52:23 +02:00
cmsys::ifstream fin;
2010-06-23 01:18:35 +03:00
fin.open(fname.c_str());
std::string line;
2016-07-09 11:21:54 +02:00
while (std::getline(fin, line)) {
2016-10-30 18:24:19 +01:00
if (line == "---") {
2016-07-09 11:21:54 +02:00
break;
2016-10-30 18:24:19 +01:00
}
2010-06-23 01:18:35 +03:00
2018-10-28 12:09:07 +01:00
std::vector<std::string> parts = cmSystemTools::SplitString(line, ' ');
2010-06-23 01:18:35 +03:00
// Probably an older version of the file, will be fixed next run
2016-07-09 11:21:54 +02:00
if (parts.size() < 3) {
2010-06-23 01:18:35 +03:00
fin.close();
return;
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
std::string name = parts[0];
int prev = atoi(parts[1].c_str());
float cost = static_cast<float>(atof(parts[2].c_str()));
int index = this->SearchByName(name);
2016-10-30 18:24:19 +01:00
if (index == -1) {
2016-07-09 11:21:54 +02:00
continue;
2016-10-30 18:24:19 +01:00
}
2010-06-23 01:18:35 +03:00
this->Properties[index]->PreviousRuns = prev;
2011-01-16 11:35:12 +01:00
// When not running in parallel mode, don't use cost data
2016-07-09 11:21:54 +02:00
if (this->ParallelLevel > 1 && this->Properties[index] &&
this->Properties[index]->Cost == 0) {
2009-10-04 10:30:41 +03:00
this->Properties[index]->Cost = cost;
}
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
// Next part of the file is the failed tests
2016-07-09 11:21:54 +02:00
while (std::getline(fin, line)) {
2018-01-26 17:06:56 +01:00
if (!line.empty()) {
2010-06-23 01:18:35 +03:00
this->LastTestsFailed.push_back(line);
}
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
fin.close();
}
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
int cmCTestMultiProcessHandler::SearchByName(std::string const& name)
2009-10-04 10:30:41 +03:00
{
2010-06-23 01:18:35 +03:00
int index = -1;
2018-01-26 17:06:56 +01:00
for (auto const& p : this->Properties) {
if (p.second->Name == name) {
index = p.first;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2010-06-23 01:18:35 +03:00
return index;
2009-10-04 10:30:41 +03:00
}
2010-06-23 01:18:35 +03:00
void cmCTestMultiProcessHandler::CreateTestCostList()
2009-10-04 10:30:41 +03:00
{
2016-07-09 11:21:54 +02:00
if (this->ParallelLevel > 1) {
2021-09-14 00:13:48 +02:00
this->CreateParallelTestCostList();
2016-07-09 11:21:54 +02:00
} else {
2021-09-14 00:13:48 +02:00
this->CreateSerialTestCostList();
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
2010-11-13 01:00:53 +02:00
2014-08-03 19:52:23 +02:00
void cmCTestMultiProcessHandler::CreateParallelTestCostList()
{
TestSet alreadySortedTests;
std::list<TestSet> priorityStack;
2018-04-23 21:13:27 +02:00
priorityStack.emplace_back();
2016-07-09 11:21:54 +02:00
TestSet& topLevel = priorityStack.back();
2014-08-03 19:52:23 +02:00
// In parallel test runs add previously failed tests to the front
// of the cost list and queue other tests for further sorting
2018-01-26 17:06:56 +01:00
for (auto const& t : this->Tests) {
2020-08-30 11:54:41 +02:00
if (cm::contains(this->LastTestsFailed, this->Properties[t.first]->Name)) {
2016-07-09 11:21:54 +02:00
// If the test failed last time, it should be run first.
2018-01-26 17:06:56 +01:00
this->SortedTests.push_back(t.first);
alreadySortedTests.insert(t.first);
2016-07-09 11:21:54 +02:00
} else {
2018-01-26 17:06:56 +01:00
topLevel.insert(t.first);
2010-06-23 01:18:35 +03:00
}
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2014-08-03 19:52:23 +02:00
// In parallel test runs repeatedly move dependencies of the tests on
// the current dependency level to the next level until no
// further dependencies exist.
2017-07-20 19:35:53 +02:00
while (!priorityStack.back().empty()) {
2016-07-09 11:21:54 +02:00
TestSet& previousSet = priorityStack.back();
2018-04-23 21:13:27 +02:00
priorityStack.emplace_back();
2016-07-09 11:21:54 +02:00
TestSet& currentSet = priorityStack.back();
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
for (auto const& i : previousSet) {
TestSet const& dependencies = this->Tests[i];
2015-04-27 22:25:09 +02:00
currentSet.insert(dependencies.begin(), dependencies.end());
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
for (auto const& i : currentSet) {
previousSet.erase(i);
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Remove the empty dependency level
priorityStack.pop_back();
// Reverse iterate over the different dependency levels (deepest first).
// Sort tests within each level by COST and append them to the cost list.
2019-11-11 23:01:05 +01:00
for (TestSet const& currentSet : cmReverseRange(priorityStack)) {
2014-08-03 19:52:23 +02:00
TestList sortedCopy;
2020-08-30 11:54:41 +02:00
cm::append(sortedCopy, currentSet);
2019-11-11 23:01:05 +01:00
std::stable_sort(sortedCopy.begin(), sortedCopy.end(),
TestComparator(this));
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
for (auto const& j : sortedCopy) {
2020-08-30 11:54:41 +02:00
if (!cm::contains(alreadySortedTests, j)) {
2018-01-26 17:06:56 +01:00
this->SortedTests.push_back(j);
alreadySortedTests.insert(j);
2014-08-03 19:52:23 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
void cmCTestMultiProcessHandler::GetAllTestDependencies(int test,
TestList& dependencies)
2014-08-03 19:52:23 +02:00
{
TestSet const& dependencySet = this->Tests[test];
2018-01-26 17:06:56 +01:00
for (int i : dependencySet) {
2021-09-14 00:13:48 +02:00
this->GetAllTestDependencies(i, dependencies);
2018-01-26 17:06:56 +01:00
dependencies.push_back(i);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
void cmCTestMultiProcessHandler::CreateSerialTestCostList()
{
TestList presortedList;
2018-01-26 17:06:56 +01:00
for (auto const& i : this->Tests) {
presortedList.push_back(i.first);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2019-11-11 23:01:05 +01:00
std::stable_sort(presortedList.begin(), presortedList.end(),
TestComparator(this));
2014-08-03 19:52:23 +02:00
TestSet alreadySortedTests;
2018-01-26 17:06:56 +01:00
for (int test : presortedList) {
2020-08-30 11:54:41 +02:00
if (cm::contains(alreadySortedTests, test)) {
2016-07-09 11:21:54 +02:00
continue;
}
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
TestList dependencies;
2021-09-14 00:13:48 +02:00
this->GetAllTestDependencies(test, dependencies);
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
for (int testDependency : dependencies) {
2020-08-30 11:54:41 +02:00
if (!cm::contains(alreadySortedTests, testDependency)) {
2016-07-09 11:21:54 +02:00
alreadySortedTests.insert(testDependency);
this->SortedTests.push_back(testDependency);
}
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
alreadySortedTests.insert(test);
this->SortedTests.push_back(test);
}
2009-10-04 10:30:41 +03:00
}
void cmCTestMultiProcessHandler::WriteCheckpoint(int index)
{
2016-07-09 11:21:54 +02:00
std::string fname =
this->CTest->GetBinaryDir() + "/Testing/Temporary/CTestCheckpoint.txt";
2015-04-27 22:25:09 +02:00
cmsys::ofstream fout;
fout.open(fname.c_str(), std::ios::app);
2009-10-04 10:30:41 +03:00
fout << index << "\n";
fout.close();
}
void cmCTestMultiProcessHandler::MarkFinished()
{
2016-07-09 11:21:54 +02:00
std::string fname =
this->CTest->GetBinaryDir() + "/Testing/Temporary/CTestCheckpoint.txt";
2015-04-27 22:25:09 +02:00
cmSystemTools::RemoveFile(fname);
2009-10-04 10:30:41 +03:00
}
2019-11-11 23:01:05 +01:00
static Json::Value DumpToJsonArray(const std::set<std::string>& values)
{
Json::Value jsonArray = Json::arrayValue;
2021-09-14 00:13:48 +02:00
for (const auto& it : values) {
2019-11-11 23:01:05 +01:00
jsonArray.append(it);
}
return jsonArray;
}
static Json::Value DumpToJsonArray(const std::vector<std::string>& values)
{
Json::Value jsonArray = Json::arrayValue;
2021-09-14 00:13:48 +02:00
for (const auto& it : values) {
2019-11-11 23:01:05 +01:00
jsonArray.append(it);
}
return jsonArray;
}
static Json::Value DumpRegExToJsonArray(
const std::vector<std::pair<cmsys::RegularExpression, std::string>>& values)
{
Json::Value jsonArray = Json::arrayValue;
2021-09-14 00:13:48 +02:00
for (const auto& it : values) {
2019-11-11 23:01:05 +01:00
jsonArray.append(it.second);
}
return jsonArray;
}
static Json::Value DumpMeasurementToJsonArray(
const std::map<std::string, std::string>& values)
{
Json::Value jsonArray = Json::arrayValue;
2021-09-14 00:13:48 +02:00
for (const auto& it : values) {
2019-11-11 23:01:05 +01:00
Json::Value measurement = Json::objectValue;
measurement["measurement"] = it.first;
measurement["value"] = it.second;
jsonArray.append(measurement);
}
return jsonArray;
}
static Json::Value DumpTimeoutAfterMatch(
cmCTestTestHandler::cmCTestTestProperties& testProperties)
{
Json::Value timeoutAfterMatch = Json::objectValue;
timeoutAfterMatch["timeout"] = testProperties.AlternateTimeout.count();
timeoutAfterMatch["regex"] =
DumpRegExToJsonArray(testProperties.TimeoutRegularExpressions);
return timeoutAfterMatch;
}
2020-02-01 23:06:01 +01:00
static Json::Value DumpResourceGroupsToJsonArray(
const std::vector<
std::vector<cmCTestTestHandler::cmCTestTestResourceRequirement>>&
resourceGroups)
{
Json::Value jsonResourceGroups = Json::arrayValue;
for (auto const& it : resourceGroups) {
Json::Value jsonResourceGroup = Json::objectValue;
Json::Value requirements = Json::arrayValue;
for (auto const& it2 : it) {
Json::Value res = Json::objectValue;
res[".type"] = it2.ResourceType;
// res[".units"] = it2.UnitsNeeded; // Intentionally commented out
res["slots"] = it2.SlotsNeeded;
requirements.append(res);
}
jsonResourceGroup["requirements"] = requirements;
jsonResourceGroups.append(jsonResourceGroup);
}
return jsonResourceGroups;
}
2019-11-11 23:01:05 +01:00
static Json::Value DumpCTestProperty(std::string const& name,
Json::Value value)
{
Json::Value property = Json::objectValue;
property["name"] = name;
property["value"] = std::move(value);
return property;
}
static Json::Value DumpCTestProperties(
cmCTestTestHandler::cmCTestTestProperties& testProperties)
{
Json::Value properties = Json::arrayValue;
if (!testProperties.AttachOnFail.empty()) {
properties.append(DumpCTestProperty(
"ATTACHED_FILES_ON_FAIL", DumpToJsonArray(testProperties.AttachOnFail)));
}
if (!testProperties.AttachedFiles.empty()) {
properties.append(DumpCTestProperty(
"ATTACHED_FILES", DumpToJsonArray(testProperties.AttachedFiles)));
}
if (testProperties.Cost != 0.0f) {
properties.append(
DumpCTestProperty("COST", static_cast<double>(testProperties.Cost)));
}
if (!testProperties.Depends.empty()) {
properties.append(
DumpCTestProperty("DEPENDS", DumpToJsonArray(testProperties.Depends)));
}
if (testProperties.Disabled) {
properties.append(DumpCTestProperty("DISABLED", testProperties.Disabled));
}
if (!testProperties.Environment.empty()) {
properties.append(DumpCTestProperty(
"ENVIRONMENT", DumpToJsonArray(testProperties.Environment)));
}
if (!testProperties.ErrorRegularExpressions.empty()) {
properties.append(DumpCTestProperty(
"FAIL_REGULAR_EXPRESSION",
DumpRegExToJsonArray(testProperties.ErrorRegularExpressions)));
}
2020-02-01 23:06:01 +01:00
if (!testProperties.SkipRegularExpressions.empty()) {
properties.append(DumpCTestProperty(
"SKIP_REGULAR_EXPRESSION",
DumpRegExToJsonArray(testProperties.SkipRegularExpressions)));
}
2019-11-11 23:01:05 +01:00
if (!testProperties.FixturesCleanup.empty()) {
properties.append(DumpCTestProperty(
"FIXTURES_CLEANUP", DumpToJsonArray(testProperties.FixturesCleanup)));
}
if (!testProperties.FixturesRequired.empty()) {
properties.append(DumpCTestProperty(
"FIXTURES_REQUIRED", DumpToJsonArray(testProperties.FixturesRequired)));
}
if (!testProperties.FixturesSetup.empty()) {
properties.append(DumpCTestProperty(
"FIXTURES_SETUP", DumpToJsonArray(testProperties.FixturesSetup)));
}
if (!testProperties.Labels.empty()) {
properties.append(
DumpCTestProperty("LABELS", DumpToJsonArray(testProperties.Labels)));
}
if (!testProperties.Measurements.empty()) {
properties.append(DumpCTestProperty(
"MEASUREMENT", DumpMeasurementToJsonArray(testProperties.Measurements)));
}
if (!testProperties.RequiredRegularExpressions.empty()) {
properties.append(DumpCTestProperty(
"PASS_REGULAR_EXPRESSION",
DumpRegExToJsonArray(testProperties.RequiredRegularExpressions)));
}
2020-02-01 23:06:01 +01:00
if (!testProperties.ResourceGroups.empty()) {
properties.append(DumpCTestProperty(
"RESOURCE_GROUPS",
DumpResourceGroupsToJsonArray(testProperties.ResourceGroups)));
}
2019-11-11 23:01:05 +01:00
if (testProperties.WantAffinity) {
properties.append(
DumpCTestProperty("PROCESSOR_AFFINITY", testProperties.WantAffinity));
}
if (testProperties.Processors != 1) {
properties.append(
DumpCTestProperty("PROCESSORS", testProperties.Processors));
}
if (!testProperties.RequiredFiles.empty()) {
properties.append(DumpCTestProperty(
"REQUIRED_FILES", DumpToJsonArray(testProperties.RequiredFiles)));
}
if (!testProperties.LockedResources.empty()) {
properties.append(DumpCTestProperty(
"RESOURCE_LOCK", DumpToJsonArray(testProperties.LockedResources)));
}
if (testProperties.RunSerial) {
properties.append(
DumpCTestProperty("RUN_SERIAL", testProperties.RunSerial));
}
if (testProperties.SkipReturnCode != -1) {
properties.append(
DumpCTestProperty("SKIP_RETURN_CODE", testProperties.SkipReturnCode));
}
if (testProperties.ExplicitTimeout) {
properties.append(
DumpCTestProperty("TIMEOUT", testProperties.Timeout.count()));
}
if (!testProperties.TimeoutRegularExpressions.empty()) {
properties.append(DumpCTestProperty(
"TIMEOUT_AFTER_MATCH", DumpTimeoutAfterMatch(testProperties)));
}
if (testProperties.WillFail) {
properties.append(DumpCTestProperty("WILL_FAIL", testProperties.WillFail));
}
if (!testProperties.Directory.empty()) {
properties.append(
DumpCTestProperty("WORKING_DIRECTORY", testProperties.Directory));
}
return properties;
}
class BacktraceData
{
std::unordered_map<std::string, Json::ArrayIndex> CommandMap;
std::unordered_map<std::string, Json::ArrayIndex> FileMap;
std::unordered_map<cmListFileContext const*, Json::ArrayIndex> NodeMap;
Json::Value Commands = Json::arrayValue;
Json::Value Files = Json::arrayValue;
Json::Value Nodes = Json::arrayValue;
Json::ArrayIndex AddCommand(std::string const& command)
{
auto i = this->CommandMap.find(command);
if (i == this->CommandMap.end()) {
i = this->CommandMap.emplace(command, this->Commands.size()).first;
this->Commands.append(command);
}
return i->second;
}
Json::ArrayIndex AddFile(std::string const& file)
{
auto i = this->FileMap.find(file);
if (i == this->FileMap.end()) {
i = this->FileMap.emplace(file, this->Files.size()).first;
this->Files.append(file);
}
return i->second;
}
public:
bool Add(cmListFileBacktrace const& bt, Json::ArrayIndex& index);
Json::Value Dump();
};
bool BacktraceData::Add(cmListFileBacktrace const& bt, Json::ArrayIndex& index)
{
if (bt.Empty()) {
return false;
}
cmListFileContext const* top = &bt.Top();
auto found = this->NodeMap.find(top);
if (found != this->NodeMap.end()) {
index = found->second;
return true;
}
Json::Value entry = Json::objectValue;
entry["file"] = this->AddFile(top->FilePath);
if (top->Line) {
entry["line"] = static_cast<int>(top->Line);
}
if (!top->Name.empty()) {
entry["command"] = this->AddCommand(top->Name);
}
Json::ArrayIndex parent;
if (this->Add(bt.Pop(), parent)) {
entry["parent"] = parent;
}
index = this->NodeMap[top] = this->Nodes.size();
this->Nodes.append(std::move(entry)); // NOLINT(*)
return true;
}
Json::Value BacktraceData::Dump()
{
Json::Value backtraceGraph;
this->CommandMap.clear();
this->FileMap.clear();
this->NodeMap.clear();
backtraceGraph["commands"] = std::move(this->Commands);
backtraceGraph["files"] = std::move(this->Files);
backtraceGraph["nodes"] = std::move(this->Nodes);
return backtraceGraph;
}
static void AddBacktrace(BacktraceData& backtraceGraph, Json::Value& object,
cmListFileBacktrace const& bt)
{
Json::ArrayIndex backtrace;
if (backtraceGraph.Add(bt, backtrace)) {
object["backtrace"] = backtrace;
}
}
static Json::Value DumpCTestInfo(
cmCTestRunTest& testRun,
cmCTestTestHandler::cmCTestTestProperties& testProperties,
BacktraceData& backtraceGraph)
{
Json::Value testInfo = Json::objectValue;
// test name should always be present
testInfo["name"] = testProperties.Name;
std::string const& config = testRun.GetCTest()->GetConfigType();
if (!config.empty()) {
testInfo["config"] = config;
}
std::string const& command = testRun.GetActualCommand();
if (!command.empty()) {
std::vector<std::string> commandAndArgs;
commandAndArgs.push_back(command);
const std::vector<std::string>& args = testRun.GetArguments();
if (!args.empty()) {
commandAndArgs.reserve(args.size() + 1);
2020-08-30 11:54:41 +02:00
cm::append(commandAndArgs, args);
2019-11-11 23:01:05 +01:00
}
testInfo["command"] = DumpToJsonArray(commandAndArgs);
}
Json::Value properties = DumpCTestProperties(testProperties);
if (!properties.empty()) {
testInfo["properties"] = properties;
}
if (!testProperties.Backtrace.Empty()) {
AddBacktrace(backtraceGraph, testInfo, testProperties.Backtrace);
}
return testInfo;
}
static Json::Value DumpVersion(int major, int minor)
{
Json::Value version = Json::objectValue;
version["major"] = major;
version["minor"] = minor;
return version;
}
void cmCTestMultiProcessHandler::PrintOutputAsJson()
{
this->TestHandler->SetMaxIndex(this->FindMaxIndex());
Json::Value result = Json::objectValue;
result["kind"] = "ctestInfo";
result["version"] = DumpVersion(1, 0);
BacktraceData backtraceGraph;
Json::Value tests = Json::arrayValue;
for (auto& it : this->Properties) {
cmCTestTestHandler::cmCTestTestProperties& p = *it.second;
// Don't worry if this fails, we are only showing the test list, not
// running the tests
cmWorkingDirectory workdir(p.Directory);
cmCTestRunTest testRun(*this);
testRun.SetIndex(p.Index);
testRun.SetTestProperties(&p);
testRun.ComputeArguments();
// Skip tests not available in this configuration.
if (p.Args.size() >= 2 && p.Args[1] == "NOT_AVAILABLE") {
continue;
}
Json::Value testInfo = DumpCTestInfo(testRun, p, backtraceGraph);
tests.append(testInfo);
}
result["backtraceGraph"] = backtraceGraph.Dump();
result["tests"] = std::move(tests);
Json::StreamWriterBuilder builder;
builder["indentation"] = " ";
std::unique_ptr<Json::StreamWriter> jout(builder.newStreamWriter());
jout->write(result, &std::cout);
}
2016-07-09 11:21:54 +02:00
// For ShowOnly mode
2009-10-04 10:30:41 +03:00
void cmCTestMultiProcessHandler::PrintTestList()
{
2019-11-11 23:01:05 +01:00
if (this->CTest->GetOutputAsJson()) {
2021-09-14 00:13:48 +02:00
this->PrintOutputAsJson();
2019-11-11 23:01:05 +01:00
return;
}
2009-10-04 10:30:41 +03:00
this->TestHandler->SetMaxIndex(this->FindMaxIndex());
int count = 0;
2010-11-13 01:00:53 +02:00
2018-01-26 17:06:56 +01:00
for (auto& it : this->Properties) {
2009-10-04 10:30:41 +03:00
count++;
2018-01-26 17:06:56 +01:00
cmCTestTestHandler::cmCTestTestProperties& p = *it.second;
2010-11-13 01:00:53 +02:00
2018-08-09 18:06:22 +02:00
// Don't worry if this fails, we are only showing the test list, not
// running the tests
2017-07-20 19:35:53 +02:00
cmWorkingDirectory workdir(p.Directory);
2009-10-04 10:30:41 +03:00
2018-04-23 21:13:27 +02:00
cmCTestRunTest testRun(*this);
2009-10-04 10:30:41 +03:00
testRun.SetIndex(p.Index);
testRun.SetTestProperties(&p);
2016-07-09 11:21:54 +02:00
testRun.ComputeArguments(); // logs the command in verbose mode
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
if (!p.Labels.empty()) // print the labels
{
2018-08-09 18:06:22 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"Labels:", this->Quiet);
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
for (std::string const& label : p.Labels) {
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " " << label,
2016-07-09 11:21:54 +02:00
this->Quiet);
}
if (!p.Labels.empty()) // print the labels
{
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl,
2016-07-09 11:21:54 +02:00
this->Quiet);
}
2010-11-13 01:00:53 +02:00
2016-07-09 11:21:54 +02:00
if (this->TestHandler->MemCheck) {
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Memory Check",
2016-07-09 11:21:54 +02:00
this->Quiet);
} else {
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Test", this->Quiet);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::ostringstream indexStr;
2009-10-04 10:30:41 +03:00
indexStr << " #" << p.Index << ":";
2016-07-09 11:21:54 +02:00
cmCTestOptionalLog(
this->CTest, HANDLER_OUTPUT,
2009-10-04 10:30:41 +03:00
std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
2016-07-09 11:21:54 +02:00
<< indexStr.str(),
this->Quiet);
2017-07-20 19:35:53 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " " << p.Name,
2016-10-30 18:24:19 +01:00
this->Quiet);
2017-07-20 19:35:53 +02:00
if (p.Disabled) {
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " (Disabled)",
this->Quiet);
}
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, std::endl, this->Quiet);
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
2018-08-09 18:06:22 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
std::endl
2016-07-09 11:21:54 +02:00
<< "Total Tests: " << this->Total << std::endl,
this->Quiet);
2009-10-04 10:30:41 +03:00
}
2010-11-13 01:00:53 +02:00
void cmCTestMultiProcessHandler::PrintLabels()
{
std::set<std::string> allLabels;
2018-01-26 17:06:56 +01:00
for (auto& it : this->Properties) {
cmCTestTestHandler::cmCTestTestProperties& p = *it.second;
2010-11-13 01:00:53 +02:00
allLabels.insert(p.Labels.begin(), p.Labels.end());
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
2016-07-09 11:21:54 +02:00
if (!allLabels.empty()) {
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "All Labels:" << std::endl,
this->Quiet);
} else {
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
2016-07-09 11:21:54 +02:00
"No Labels Exist" << std::endl, this->Quiet);
}
2018-01-26 17:06:56 +01:00
for (std::string const& label : allLabels) {
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " " << label << std::endl,
this->Quiet);
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
}
2009-10-04 10:30:41 +03:00
void cmCTestMultiProcessHandler::CheckResume()
{
2016-07-09 11:21:54 +02:00
std::string fname =
this->CTest->GetBinaryDir() + "/Testing/Temporary/CTestCheckpoint.txt";
if (this->CTest->GetFailover()) {
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(fname, true)) {
2016-07-09 11:21:54 +02:00
*this->TestHandler->LogFile
<< "Resuming previously interrupted test set" << std::endl
2009-10-04 10:30:41 +03:00
<< "----------------------------------------------------------"
<< std::endl;
2013-03-16 19:13:01 +02:00
2014-08-03 19:52:23 +02:00
cmsys::ifstream fin;
2009-10-04 10:30:41 +03:00
fin.open(fname.c_str());
std::string line;
2016-07-09 11:21:54 +02:00
while (std::getline(fin, line)) {
2009-10-04 10:30:41 +03:00
int index = atoi(line.c_str());
this->RemoveTest(index);
}
2016-07-09 11:21:54 +02:00
fin.close();
2009-10-04 10:30:41 +03:00
}
2018-04-23 21:13:27 +02:00
} else if (cmSystemTools::FileExists(fname, true)) {
2015-04-27 22:25:09 +02:00
cmSystemTools::RemoveFile(fname);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
void cmCTestMultiProcessHandler::RemoveTest(int index)
{
this->EraseTest(index);
this->Properties.erase(index);
this->TestRunningMap[index] = false;
this->TestFinishMap[index] = true;
this->Completed++;
}
int cmCTestMultiProcessHandler::FindMaxIndex()
{
int max = 0;
2018-01-26 17:06:56 +01:00
for (auto const& i : this->Tests) {
if (i.first > max) {
max = i.first;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return max;
}
2009-11-06 22:07:41 +02:00
2016-07-09 11:21:54 +02:00
// Returns true if no cycles exist in the dependency graph
2009-11-06 22:07:41 +02:00
bool cmCTestMultiProcessHandler::CheckCycles()
{
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
2016-07-09 11:21:54 +02:00
"Checking test dependency graph..." << std::endl,
this->Quiet);
2018-01-26 17:06:56 +01:00
for (auto const& it : this->Tests) {
2016-07-09 11:21:54 +02:00
// DFS from each element to itself
2018-01-26 17:06:56 +01:00
int root = it.first;
2011-01-16 11:35:12 +01:00
std::set<int> visited;
2009-11-06 22:07:41 +02:00
std::stack<int> s;
2011-01-16 11:35:12 +01:00
s.push(root);
2016-07-09 11:21:54 +02:00
while (!s.empty()) {
2009-11-06 22:07:41 +02:00
int test = s.top();
s.pop();
2016-07-09 11:21:54 +02:00
if (visited.insert(test).second) {
2018-01-26 17:06:56 +01:00
for (auto const& d : this->Tests[test]) {
if (d == root) {
2016-07-09 11:21:54 +02:00
// cycle exists
cmCTestLog(
this->CTest, ERROR_MESSAGE,
"Error: a cycle exists in the test dependency graph "
"for the test \""
<< this->Properties[root]->Name
<< "\".\nPlease fix the cycle and run ctest again.\n");
2011-01-16 11:35:12 +01:00
return false;
2009-11-06 22:07:41 +02:00
}
2018-01-26 17:06:56 +01:00
s.push(d);
2009-11-06 22:07:41 +02:00
}
}
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
2016-07-09 11:21:54 +02:00
"Checking test dependency graph end" << std::endl,
this->Quiet);
2009-11-06 22:07:41 +02:00
return true;
}