cmCTestMultiProcessHandler.cxx 24 KB

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