cmCTestMultiProcessHandler.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCTestMultiProcessHandler.h"
  4. #include "cmCTest.h"
  5. #include "cmCTestRunTest.h"
  6. #include "cmCTestScriptHandler.h"
  7. #include "cmCTestTestHandler.h"
  8. #include "cmSystemTools.h"
  9. #include "cmWorkingDirectory.h"
  10. #include "cmsys/FStream.hxx"
  11. #include "cmsys/String.hxx"
  12. #include "cmsys/SystemInformation.hxx"
  13. #include <algorithm>
  14. #include <iomanip>
  15. #include <list>
  16. #include <math.h>
  17. #include <sstream>
  18. #include <stack>
  19. #include <stdlib.h>
  20. #include <utility>
  21. class TestComparator
  22. {
  23. public:
  24. TestComparator(cmCTestMultiProcessHandler* handler)
  25. : Handler(handler)
  26. {
  27. }
  28. ~TestComparator() {}
  29. // Sorts tests in descending order of cost
  30. bool operator()(int index1, int index2) const
  31. {
  32. return Handler->Properties[index1]->Cost >
  33. Handler->Properties[index2]->Cost;
  34. }
  35. private:
  36. cmCTestMultiProcessHandler* Handler;
  37. };
  38. cmCTestMultiProcessHandler::cmCTestMultiProcessHandler()
  39. {
  40. this->ParallelLevel = 1;
  41. this->TestLoad = 0;
  42. this->Completed = 0;
  43. this->RunningCount = 0;
  44. this->StopTimePassed = false;
  45. this->HasCycles = false;
  46. this->SerialTestRunning = false;
  47. }
  48. cmCTestMultiProcessHandler::~cmCTestMultiProcessHandler()
  49. {
  50. }
  51. // Set the tests
  52. void cmCTestMultiProcessHandler::SetTests(TestMap& tests,
  53. PropertiesMap& properties)
  54. {
  55. this->Tests = tests;
  56. this->Properties = properties;
  57. this->Total = this->Tests.size();
  58. // set test run map to false for all
  59. for (auto const& t : this->Tests) {
  60. this->TestRunningMap[t.first] = false;
  61. this->TestFinishMap[t.first] = false;
  62. }
  63. if (!this->CTest->GetShowOnly()) {
  64. this->ReadCostData();
  65. this->HasCycles = !this->CheckCycles();
  66. if (this->HasCycles) {
  67. return;
  68. }
  69. this->CreateTestCostList();
  70. }
  71. }
  72. // Set the max number of tests that can be run at the same time.
  73. void cmCTestMultiProcessHandler::SetParallelLevel(size_t level)
  74. {
  75. this->ParallelLevel = level < 1 ? 1 : level;
  76. }
  77. void cmCTestMultiProcessHandler::SetTestLoad(unsigned long load)
  78. {
  79. this->TestLoad = load;
  80. }
  81. void cmCTestMultiProcessHandler::RunTests()
  82. {
  83. this->CheckResume();
  84. if (this->HasCycles) {
  85. return;
  86. }
  87. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  88. this->StartNextTests();
  89. while (!this->Tests.empty()) {
  90. if (this->StopTimePassed) {
  91. return;
  92. }
  93. this->CheckOutput();
  94. this->StartNextTests();
  95. }
  96. // let all running tests finish
  97. while (this->CheckOutput()) {
  98. }
  99. this->MarkFinished();
  100. this->UpdateCostData();
  101. }
  102. void cmCTestMultiProcessHandler::StartTestProcess(int test)
  103. {
  104. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  105. "test " << test << "\n", this->Quiet);
  106. this->TestRunningMap[test] = true; // mark the test as running
  107. // now remove the test itself
  108. this->EraseTest(test);
  109. this->RunningCount += GetProcessorsUsed(test);
  110. cmCTestRunTest* testRun = new cmCTestRunTest(this->TestHandler);
  111. if (this->CTest->GetRepeatUntilFail()) {
  112. testRun->SetRunUntilFailOn();
  113. testRun->SetNumberOfRuns(this->CTest->GetTestRepeat());
  114. }
  115. testRun->SetIndex(test);
  116. testRun->SetTestProperties(this->Properties[test]);
  117. // Find any failed dependencies for this test. We assume the more common
  118. // scenario has no failed tests, so make it the outer loop.
  119. for (std::string const& f : *this->Failed) {
  120. if (this->Properties[test]->RequireSuccessDepends.find(f) !=
  121. this->Properties[test]->RequireSuccessDepends.end()) {
  122. testRun->AddFailedDependency(f);
  123. }
  124. }
  125. cmWorkingDirectory workdir(this->Properties[test]->Directory);
  126. // Lock the resources we'll be using
  127. this->LockResources(test);
  128. if (testRun->StartTest(this->Total)) {
  129. this->RunningTests.insert(testRun);
  130. } else if (testRun->IsStopTimePassed()) {
  131. this->StopTimePassed = true;
  132. delete testRun;
  133. return;
  134. } else {
  135. for (auto& j : this->Tests) {
  136. j.second.erase(test);
  137. }
  138. this->UnlockResources(test);
  139. this->Completed++;
  140. this->TestFinishMap[test] = true;
  141. this->TestRunningMap[test] = false;
  142. this->RunningCount -= GetProcessorsUsed(test);
  143. testRun->EndTest(this->Completed, this->Total, false);
  144. if (!this->Properties[test]->Disabled) {
  145. this->Failed->push_back(this->Properties[test]->Name);
  146. }
  147. delete testRun;
  148. }
  149. }
  150. void cmCTestMultiProcessHandler::LockResources(int index)
  151. {
  152. this->LockedResources.insert(
  153. this->Properties[index]->LockedResources.begin(),
  154. this->Properties[index]->LockedResources.end());
  155. if (this->Properties[index]->RunSerial) {
  156. this->SerialTestRunning = true;
  157. }
  158. }
  159. void cmCTestMultiProcessHandler::UnlockResources(int index)
  160. {
  161. for (std::string const& i : this->Properties[index]->LockedResources) {
  162. this->LockedResources.erase(i);
  163. }
  164. if (this->Properties[index]->RunSerial) {
  165. this->SerialTestRunning = false;
  166. }
  167. }
  168. void cmCTestMultiProcessHandler::EraseTest(int test)
  169. {
  170. this->Tests.erase(test);
  171. this->SortedTests.erase(
  172. std::find(this->SortedTests.begin(), this->SortedTests.end(), test));
  173. }
  174. inline size_t cmCTestMultiProcessHandler::GetProcessorsUsed(int test)
  175. {
  176. size_t processors = static_cast<int>(this->Properties[test]->Processors);
  177. // If processors setting is set higher than the -j
  178. // setting, we default to using all of the process slots.
  179. if (processors > this->ParallelLevel) {
  180. processors = this->ParallelLevel;
  181. }
  182. return processors;
  183. }
  184. std::string cmCTestMultiProcessHandler::GetName(int test)
  185. {
  186. return this->Properties[test]->Name;
  187. }
  188. bool cmCTestMultiProcessHandler::StartTest(int test)
  189. {
  190. // Check for locked resources
  191. for (std::string const& i : this->Properties[test]->LockedResources) {
  192. if (this->LockedResources.find(i) != this->LockedResources.end()) {
  193. return false;
  194. }
  195. }
  196. // if there are no depends left then run this test
  197. if (this->Tests[test].empty()) {
  198. this->StartTestProcess(test);
  199. return true;
  200. }
  201. // This test was not able to start because it is waiting
  202. // on depends to run
  203. return false;
  204. }
  205. void cmCTestMultiProcessHandler::StartNextTests()
  206. {
  207. size_t numToStart = 0;
  208. if (this->RunningCount < this->ParallelLevel) {
  209. numToStart = this->ParallelLevel - this->RunningCount;
  210. }
  211. if (numToStart == 0) {
  212. return;
  213. }
  214. // Don't start any new tests if one with the RUN_SERIAL property
  215. // is already running.
  216. if (this->SerialTestRunning) {
  217. return;
  218. }
  219. bool allTestsFailedTestLoadCheck = false;
  220. bool usedFakeLoadForTesting = false;
  221. size_t minProcessorsRequired = this->ParallelLevel;
  222. std::string testWithMinProcessors;
  223. cmsys::SystemInformation info;
  224. unsigned long systemLoad = 0;
  225. size_t spareLoad = 0;
  226. if (this->TestLoad > 0) {
  227. // Activate possible wait.
  228. allTestsFailedTestLoadCheck = true;
  229. // Check for a fake load average value used in testing.
  230. std::string fake_load_value;
  231. if (cmSystemTools::GetEnv("__CTEST_FAKE_LOAD_AVERAGE_FOR_TESTING",
  232. fake_load_value)) {
  233. usedFakeLoadForTesting = true;
  234. if (!cmSystemTools::StringToULong(fake_load_value.c_str(),
  235. &systemLoad)) {
  236. cmSystemTools::Error("Failed to parse fake load value: ",
  237. fake_load_value.c_str());
  238. }
  239. }
  240. // If it's not set, look up the true load average.
  241. else {
  242. systemLoad = static_cast<unsigned long>(ceil(info.GetLoadAverage()));
  243. }
  244. spareLoad =
  245. (this->TestLoad > systemLoad ? this->TestLoad - systemLoad : 0);
  246. // Don't start more tests than the spare load can support.
  247. if (numToStart > spareLoad) {
  248. numToStart = spareLoad;
  249. }
  250. }
  251. TestList copy = this->SortedTests;
  252. for (auto const& test : copy) {
  253. // Take a nap if we're currently performing a RUN_SERIAL test.
  254. if (this->SerialTestRunning) {
  255. break;
  256. }
  257. // We can only start a RUN_SERIAL test if no other tests are also running.
  258. if (this->Properties[test]->RunSerial && this->RunningCount > 0) {
  259. continue;
  260. }
  261. size_t processors = GetProcessorsUsed(test);
  262. bool testLoadOk = true;
  263. if (this->TestLoad > 0) {
  264. if (processors <= spareLoad) {
  265. cmCTestLog(this->CTest, DEBUG, "OK to run "
  266. << GetName(test) << ", it requires " << processors
  267. << " procs & system load is: " << systemLoad
  268. << std::endl);
  269. allTestsFailedTestLoadCheck = false;
  270. } else {
  271. testLoadOk = false;
  272. }
  273. }
  274. if (processors <= minProcessorsRequired) {
  275. minProcessorsRequired = processors;
  276. testWithMinProcessors = GetName(test);
  277. }
  278. if (testLoadOk && processors <= numToStart && this->StartTest(test)) {
  279. if (this->StopTimePassed) {
  280. return;
  281. }
  282. numToStart -= processors;
  283. } else if (numToStart == 0) {
  284. break;
  285. }
  286. }
  287. if (allTestsFailedTestLoadCheck) {
  288. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***** WAITING, ");
  289. if (this->SerialTestRunning) {
  290. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  291. "Waiting for RUN_SERIAL test to finish.");
  292. } else {
  293. /* clang-format off */
  294. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  295. "System Load: " << systemLoad << ", "
  296. "Max Allowed Load: " << this->TestLoad << ", "
  297. "Smallest test " << testWithMinProcessors <<
  298. " requires " << minProcessorsRequired);
  299. /* clang-format on */
  300. }
  301. cmCTestLog(this->CTest, HANDLER_OUTPUT, "*****" << std::endl);
  302. if (usedFakeLoadForTesting) {
  303. // Break out of the infinite loop of waiting for our fake load
  304. // to come down.
  305. this->StopTimePassed = true;
  306. } else {
  307. // Wait between 1 and 5 seconds before trying again.
  308. cmCTestScriptHandler::SleepInSeconds(cmSystemTools::RandomSeed() % 5 +
  309. 1);
  310. }
  311. }
  312. }
  313. bool cmCTestMultiProcessHandler::CheckOutput()
  314. {
  315. // no more output we are done
  316. if (this->RunningTests.empty()) {
  317. return false;
  318. }
  319. std::vector<cmCTestRunTest*> finished;
  320. std::string out, err;
  321. for (cmCTestRunTest* p : this->RunningTests) {
  322. if (!p->CheckOutput()) {
  323. finished.push_back(p);
  324. }
  325. }
  326. for (cmCTestRunTest* p : finished) {
  327. this->Completed++;
  328. int test = p->GetIndex();
  329. bool testResult = p->EndTest(this->Completed, this->Total, true);
  330. if (p->StartAgain()) {
  331. this->Completed--; // remove the completed test because run again
  332. continue;
  333. }
  334. if (testResult) {
  335. this->Passed->push_back(p->GetTestProperties()->Name);
  336. } else {
  337. this->Failed->push_back(p->GetTestProperties()->Name);
  338. }
  339. for (auto& t : this->Tests) {
  340. t.second.erase(test);
  341. }
  342. this->TestFinishMap[test] = true;
  343. this->TestRunningMap[test] = false;
  344. this->RunningTests.erase(p);
  345. this->WriteCheckpoint(test);
  346. this->UnlockResources(test);
  347. this->RunningCount -= GetProcessorsUsed(test);
  348. delete p;
  349. }
  350. return true;
  351. }
  352. void cmCTestMultiProcessHandler::UpdateCostData()
  353. {
  354. std::string fname = this->CTest->GetCostDataFile();
  355. std::string tmpout = fname + ".tmp";
  356. cmsys::ofstream fout;
  357. fout.open(tmpout.c_str());
  358. PropertiesMap temp = this->Properties;
  359. if (cmSystemTools::FileExists(fname.c_str())) {
  360. cmsys::ifstream fin;
  361. fin.open(fname.c_str());
  362. std::string line;
  363. while (std::getline(fin, line)) {
  364. if (line == "---") {
  365. break;
  366. }
  367. std::vector<cmsys::String> parts = cmSystemTools::SplitString(line, ' ');
  368. // Format: <name> <previous_runs> <avg_cost>
  369. if (parts.size() < 3) {
  370. break;
  371. }
  372. std::string name = parts[0];
  373. int prev = atoi(parts[1].c_str());
  374. float cost = static_cast<float>(atof(parts[2].c_str()));
  375. int index = this->SearchByName(name);
  376. if (index == -1) {
  377. // This test is not in memory. We just rewrite the entry
  378. fout << name << " " << prev << " " << cost << "\n";
  379. } else {
  380. // Update with our new average cost
  381. fout << name << " " << this->Properties[index]->PreviousRuns << " "
  382. << this->Properties[index]->Cost << "\n";
  383. temp.erase(index);
  384. }
  385. }
  386. fin.close();
  387. cmSystemTools::RemoveFile(fname);
  388. }
  389. // Add all tests not previously listed in the file
  390. for (auto const& i : temp) {
  391. fout << i.second->Name << " " << i.second->PreviousRuns << " "
  392. << i.second->Cost << "\n";
  393. }
  394. // Write list of failed tests
  395. fout << "---\n";
  396. for (std::string const& f : *this->Failed) {
  397. fout << f << "\n";
  398. }
  399. fout.close();
  400. cmSystemTools::RenameFile(tmpout.c_str(), fname.c_str());
  401. }
  402. void cmCTestMultiProcessHandler::ReadCostData()
  403. {
  404. std::string fname = this->CTest->GetCostDataFile();
  405. if (cmSystemTools::FileExists(fname.c_str(), true)) {
  406. cmsys::ifstream fin;
  407. fin.open(fname.c_str());
  408. std::string line;
  409. while (std::getline(fin, line)) {
  410. if (line == "---") {
  411. break;
  412. }
  413. std::vector<cmsys::String> parts = cmSystemTools::SplitString(line, ' ');
  414. // Probably an older version of the file, will be fixed next run
  415. if (parts.size() < 3) {
  416. fin.close();
  417. return;
  418. }
  419. std::string name = parts[0];
  420. int prev = atoi(parts[1].c_str());
  421. float cost = static_cast<float>(atof(parts[2].c_str()));
  422. int index = this->SearchByName(name);
  423. if (index == -1) {
  424. continue;
  425. }
  426. this->Properties[index]->PreviousRuns = prev;
  427. // When not running in parallel mode, don't use cost data
  428. if (this->ParallelLevel > 1 && this->Properties[index] &&
  429. this->Properties[index]->Cost == 0) {
  430. this->Properties[index]->Cost = cost;
  431. }
  432. }
  433. // Next part of the file is the failed tests
  434. while (std::getline(fin, line)) {
  435. if (!line.empty()) {
  436. this->LastTestsFailed.push_back(line);
  437. }
  438. }
  439. fin.close();
  440. }
  441. }
  442. int cmCTestMultiProcessHandler::SearchByName(std::string const& name)
  443. {
  444. int index = -1;
  445. for (auto const& p : this->Properties) {
  446. if (p.second->Name == name) {
  447. index = p.first;
  448. }
  449. }
  450. return index;
  451. }
  452. void cmCTestMultiProcessHandler::CreateTestCostList()
  453. {
  454. if (this->ParallelLevel > 1) {
  455. CreateParallelTestCostList();
  456. } else {
  457. CreateSerialTestCostList();
  458. }
  459. }
  460. void cmCTestMultiProcessHandler::CreateParallelTestCostList()
  461. {
  462. TestSet alreadySortedTests;
  463. std::list<TestSet> priorityStack;
  464. priorityStack.push_back(TestSet());
  465. TestSet& topLevel = priorityStack.back();
  466. // In parallel test runs add previously failed tests to the front
  467. // of the cost list and queue other tests for further sorting
  468. for (auto const& t : this->Tests) {
  469. if (std::find(this->LastTestsFailed.begin(), this->LastTestsFailed.end(),
  470. this->Properties[t.first]->Name) !=
  471. this->LastTestsFailed.end()) {
  472. // If the test failed last time, it should be run first.
  473. this->SortedTests.push_back(t.first);
  474. alreadySortedTests.insert(t.first);
  475. } else {
  476. topLevel.insert(t.first);
  477. }
  478. }
  479. // In parallel test runs repeatedly move dependencies of the tests on
  480. // the current dependency level to the next level until no
  481. // further dependencies exist.
  482. while (!priorityStack.back().empty()) {
  483. TestSet& previousSet = priorityStack.back();
  484. priorityStack.push_back(TestSet());
  485. TestSet& currentSet = priorityStack.back();
  486. for (auto const& i : previousSet) {
  487. TestSet const& dependencies = this->Tests[i];
  488. currentSet.insert(dependencies.begin(), dependencies.end());
  489. }
  490. for (auto const& i : currentSet) {
  491. previousSet.erase(i);
  492. }
  493. }
  494. // Remove the empty dependency level
  495. priorityStack.pop_back();
  496. // Reverse iterate over the different dependency levels (deepest first).
  497. // Sort tests within each level by COST and append them to the cost list.
  498. for (std::list<TestSet>::reverse_iterator i = priorityStack.rbegin();
  499. i != priorityStack.rend(); ++i) {
  500. TestSet const& currentSet = *i;
  501. TestComparator comp(this);
  502. TestList sortedCopy;
  503. sortedCopy.insert(sortedCopy.end(), currentSet.begin(), currentSet.end());
  504. std::stable_sort(sortedCopy.begin(), sortedCopy.end(), comp);
  505. for (auto const& j : sortedCopy) {
  506. if (alreadySortedTests.find(j) == alreadySortedTests.end()) {
  507. this->SortedTests.push_back(j);
  508. alreadySortedTests.insert(j);
  509. }
  510. }
  511. }
  512. }
  513. void cmCTestMultiProcessHandler::GetAllTestDependencies(int test,
  514. TestList& dependencies)
  515. {
  516. TestSet const& dependencySet = this->Tests[test];
  517. for (int i : dependencySet) {
  518. GetAllTestDependencies(i, dependencies);
  519. dependencies.push_back(i);
  520. }
  521. }
  522. void cmCTestMultiProcessHandler::CreateSerialTestCostList()
  523. {
  524. TestList presortedList;
  525. for (auto const& i : this->Tests) {
  526. presortedList.push_back(i.first);
  527. }
  528. TestComparator comp(this);
  529. std::stable_sort(presortedList.begin(), presortedList.end(), comp);
  530. TestSet alreadySortedTests;
  531. for (int test : presortedList) {
  532. if (alreadySortedTests.find(test) != alreadySortedTests.end()) {
  533. continue;
  534. }
  535. TestList dependencies;
  536. GetAllTestDependencies(test, dependencies);
  537. for (int testDependency : dependencies) {
  538. if (alreadySortedTests.find(testDependency) ==
  539. alreadySortedTests.end()) {
  540. alreadySortedTests.insert(testDependency);
  541. this->SortedTests.push_back(testDependency);
  542. }
  543. }
  544. alreadySortedTests.insert(test);
  545. this->SortedTests.push_back(test);
  546. }
  547. }
  548. void cmCTestMultiProcessHandler::WriteCheckpoint(int index)
  549. {
  550. std::string fname =
  551. this->CTest->GetBinaryDir() + "/Testing/Temporary/CTestCheckpoint.txt";
  552. cmsys::ofstream fout;
  553. fout.open(fname.c_str(), std::ios::app);
  554. fout << index << "\n";
  555. fout.close();
  556. }
  557. void cmCTestMultiProcessHandler::MarkFinished()
  558. {
  559. std::string fname =
  560. this->CTest->GetBinaryDir() + "/Testing/Temporary/CTestCheckpoint.txt";
  561. cmSystemTools::RemoveFile(fname);
  562. }
  563. // For ShowOnly mode
  564. void cmCTestMultiProcessHandler::PrintTestList()
  565. {
  566. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  567. int count = 0;
  568. for (auto& it : this->Properties) {
  569. count++;
  570. cmCTestTestHandler::cmCTestTestProperties& p = *it.second;
  571. cmWorkingDirectory workdir(p.Directory);
  572. cmCTestRunTest testRun(this->TestHandler);
  573. testRun.SetIndex(p.Index);
  574. testRun.SetTestProperties(&p);
  575. testRun.ComputeArguments(); // logs the command in verbose mode
  576. if (!p.Labels.empty()) // print the labels
  577. {
  578. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Labels:",
  579. this->Quiet);
  580. }
  581. for (std::string const& label : p.Labels) {
  582. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " " << label,
  583. this->Quiet);
  584. }
  585. if (!p.Labels.empty()) // print the labels
  586. {
  587. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl,
  588. this->Quiet);
  589. }
  590. if (this->TestHandler->MemCheck) {
  591. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Memory Check",
  592. this->Quiet);
  593. } else {
  594. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Test", this->Quiet);
  595. }
  596. std::ostringstream indexStr;
  597. indexStr << " #" << p.Index << ":";
  598. cmCTestOptionalLog(
  599. this->CTest, HANDLER_OUTPUT,
  600. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  601. << indexStr.str(),
  602. this->Quiet);
  603. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " " << p.Name,
  604. this->Quiet);
  605. if (p.Disabled) {
  606. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " (Disabled)",
  607. this->Quiet);
  608. }
  609. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, std::endl, this->Quiet);
  610. }
  611. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, std::endl
  612. << "Total Tests: " << this->Total << std::endl,
  613. this->Quiet);
  614. }
  615. void cmCTestMultiProcessHandler::PrintLabels()
  616. {
  617. std::set<std::string> allLabels;
  618. for (auto& it : this->Properties) {
  619. cmCTestTestHandler::cmCTestTestProperties& p = *it.second;
  620. allLabels.insert(p.Labels.begin(), p.Labels.end());
  621. }
  622. if (!allLabels.empty()) {
  623. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "All Labels:" << std::endl,
  624. this->Quiet);
  625. } else {
  626. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  627. "No Labels Exist" << std::endl, this->Quiet);
  628. }
  629. for (std::string const& label : allLabels) {
  630. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " " << label << std::endl,
  631. this->Quiet);
  632. }
  633. }
  634. void cmCTestMultiProcessHandler::CheckResume()
  635. {
  636. std::string fname =
  637. this->CTest->GetBinaryDir() + "/Testing/Temporary/CTestCheckpoint.txt";
  638. if (this->CTest->GetFailover()) {
  639. if (cmSystemTools::FileExists(fname.c_str(), true)) {
  640. *this->TestHandler->LogFile
  641. << "Resuming previously interrupted test set" << std::endl
  642. << "----------------------------------------------------------"
  643. << std::endl;
  644. cmsys::ifstream fin;
  645. fin.open(fname.c_str());
  646. std::string line;
  647. while (std::getline(fin, line)) {
  648. int index = atoi(line.c_str());
  649. this->RemoveTest(index);
  650. }
  651. fin.close();
  652. }
  653. } else if (cmSystemTools::FileExists(fname.c_str(), true)) {
  654. cmSystemTools::RemoveFile(fname);
  655. }
  656. }
  657. void cmCTestMultiProcessHandler::RemoveTest(int index)
  658. {
  659. this->EraseTest(index);
  660. this->Properties.erase(index);
  661. this->TestRunningMap[index] = false;
  662. this->TestFinishMap[index] = true;
  663. this->Completed++;
  664. }
  665. int cmCTestMultiProcessHandler::FindMaxIndex()
  666. {
  667. int max = 0;
  668. for (auto const& i : this->Tests) {
  669. if (i.first > max) {
  670. max = i.first;
  671. }
  672. }
  673. return max;
  674. }
  675. // Returns true if no cycles exist in the dependency graph
  676. bool cmCTestMultiProcessHandler::CheckCycles()
  677. {
  678. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  679. "Checking test dependency graph..." << std::endl,
  680. this->Quiet);
  681. for (auto const& it : this->Tests) {
  682. // DFS from each element to itself
  683. int root = it.first;
  684. std::set<int> visited;
  685. std::stack<int> s;
  686. s.push(root);
  687. while (!s.empty()) {
  688. int test = s.top();
  689. s.pop();
  690. if (visited.insert(test).second) {
  691. for (auto const& d : this->Tests[test]) {
  692. if (d == root) {
  693. // cycle exists
  694. cmCTestLog(
  695. this->CTest, ERROR_MESSAGE,
  696. "Error: a cycle exists in the test dependency graph "
  697. "for the test \""
  698. << this->Properties[root]->Name
  699. << "\".\nPlease fix the cycle and run ctest again.\n");
  700. return false;
  701. }
  702. s.push(d);
  703. }
  704. }
  705. }
  706. }
  707. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  708. "Checking test dependency graph end" << std::endl,
  709. this->Quiet);
  710. return true;
  711. }