cmCTestMultiProcessHandler.cxx 24 KB

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