cmCTestMultiProcessHandler.cxx 23 KB

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