cmCTestMultiProcessHandler.cxx 25 KB

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