cmCTestMultiProcessHandler.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 "cmsys/FStream.hxx"
  12. #include "cmsys/String.hxx"
  13. #include "cmsys/SystemInformation.hxx"
  14. #include <algorithm>
  15. #include <chrono>
  16. #include <iomanip>
  17. #include <list>
  18. #include <math.h>
  19. #include <sstream>
  20. #include <stack>
  21. #include <stdlib.h>
  22. #include <utility>
  23. #if defined(CMAKE_USE_SYSTEM_LIBUV) && !defined(_WIN32) && \
  24. UV_VERSION_MAJOR == 1 && UV_VERSION_MINOR < 19
  25. #define CMAKE_UV_SIGNAL_HACK
  26. /*
  27. libuv does not use SA_RESTART on its signal handler, but C++ streams
  28. depend on it for reliable i/o operations. This RAII helper convinces
  29. libuv to install its handler, and then revises the handler to add the
  30. SA_RESTART flag. We use a distinct uv loop that never runs to avoid
  31. ever really getting a callback. libuv may fill the hack loop's signal
  32. pipe and then stop writing, but that won't break any real loops.
  33. */
  34. class cmUVSignalHackRAII
  35. {
  36. uv_loop_t HackLoop;
  37. cm::uv_signal_ptr HackSignal;
  38. static void HackCB(uv_signal_t*, int) {}
  39. public:
  40. cmUVSignalHackRAII()
  41. {
  42. uv_loop_init(&this->HackLoop);
  43. this->HackSignal.init(this->HackLoop);
  44. this->HackSignal.start(HackCB, SIGCHLD);
  45. struct sigaction hack_sa;
  46. sigaction(SIGCHLD, NULL, &hack_sa);
  47. if (!(hack_sa.sa_flags & SA_RESTART)) {
  48. hack_sa.sa_flags |= SA_RESTART;
  49. sigaction(SIGCHLD, &hack_sa, NULL);
  50. }
  51. }
  52. ~cmUVSignalHackRAII()
  53. {
  54. this->HackSignal.stop();
  55. uv_loop_close(&this->HackLoop);
  56. }
  57. };
  58. #endif
  59. class TestComparator
  60. {
  61. public:
  62. TestComparator(cmCTestMultiProcessHandler* handler)
  63. : Handler(handler)
  64. {
  65. }
  66. ~TestComparator() {}
  67. // Sorts tests in descending order of cost
  68. bool operator()(int index1, int index2) const
  69. {
  70. return Handler->Properties[index1]->Cost >
  71. Handler->Properties[index2]->Cost;
  72. }
  73. private:
  74. cmCTestMultiProcessHandler* Handler;
  75. };
  76. cmCTestMultiProcessHandler::cmCTestMultiProcessHandler()
  77. {
  78. this->ParallelLevel = 1;
  79. this->TestLoad = 0;
  80. this->Completed = 0;
  81. this->RunningCount = 0;
  82. this->StopTimePassed = false;
  83. this->HasCycles = false;
  84. this->SerialTestRunning = false;
  85. }
  86. cmCTestMultiProcessHandler::~cmCTestMultiProcessHandler()
  87. {
  88. }
  89. // Set the tests
  90. void cmCTestMultiProcessHandler::SetTests(TestMap& tests,
  91. PropertiesMap& properties)
  92. {
  93. this->Tests = tests;
  94. this->Properties = properties;
  95. this->Total = this->Tests.size();
  96. // set test run map to false for all
  97. for (auto const& t : this->Tests) {
  98. this->TestRunningMap[t.first] = false;
  99. this->TestFinishMap[t.first] = false;
  100. }
  101. if (!this->CTest->GetShowOnly()) {
  102. this->ReadCostData();
  103. this->HasCycles = !this->CheckCycles();
  104. if (this->HasCycles) {
  105. return;
  106. }
  107. this->CreateTestCostList();
  108. }
  109. }
  110. // Set the max number of tests that can be run at the same time.
  111. void cmCTestMultiProcessHandler::SetParallelLevel(size_t level)
  112. {
  113. this->ParallelLevel = level < 1 ? 1 : level;
  114. }
  115. void cmCTestMultiProcessHandler::SetTestLoad(unsigned long load)
  116. {
  117. this->TestLoad = load;
  118. }
  119. void cmCTestMultiProcessHandler::RunTests()
  120. {
  121. this->CheckResume();
  122. if (this->HasCycles) {
  123. return;
  124. }
  125. #ifdef CMAKE_UV_SIGNAL_HACK
  126. cmUVSignalHackRAII hackRAII;
  127. #endif
  128. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  129. uv_loop_init(&this->Loop);
  130. this->StartNextTests();
  131. uv_run(&this->Loop, UV_RUN_DEFAULT);
  132. uv_loop_close(&this->Loop);
  133. this->MarkFinished();
  134. this->UpdateCostData();
  135. }
  136. bool cmCTestMultiProcessHandler::StartTestProcess(int test)
  137. {
  138. std::chrono::system_clock::time_point stop_time = this->CTest->GetStopTime();
  139. if (stop_time != std::chrono::system_clock::time_point() &&
  140. stop_time <= std::chrono::system_clock::now()) {
  141. cmCTestLog(this->CTest, ERROR_MESSAGE, "The stop time has been passed. "
  142. "Stopping all tests."
  143. << std::endl);
  144. this->StopTimePassed = true;
  145. return false;
  146. }
  147. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  148. "test " << test << "\n", this->Quiet);
  149. this->TestRunningMap[test] = true; // mark the test as running
  150. // now remove the test itself
  151. this->EraseTest(test);
  152. this->RunningCount += GetProcessorsUsed(test);
  153. cmCTestRunTest* testRun = new cmCTestRunTest(*this);
  154. if (this->CTest->GetRepeatUntilFail()) {
  155. testRun->SetRunUntilFailOn();
  156. testRun->SetNumberOfRuns(this->CTest->GetTestRepeat());
  157. }
  158. testRun->SetIndex(test);
  159. testRun->SetTestProperties(this->Properties[test]);
  160. // Find any failed dependencies for this test. We assume the more common
  161. // scenario has no failed tests, so make it the outer loop.
  162. for (std::string const& f : *this->Failed) {
  163. if (this->Properties[test]->RequireSuccessDepends.find(f) !=
  164. this->Properties[test]->RequireSuccessDepends.end()) {
  165. testRun->AddFailedDependency(f);
  166. }
  167. }
  168. cmWorkingDirectory workdir(this->Properties[test]->Directory);
  169. // Lock the resources we'll be using
  170. this->LockResources(test);
  171. if (testRun->StartTest(this->Total)) {
  172. return true;
  173. }
  174. this->FinishTestProcess(testRun, false);
  175. return false;
  176. }
  177. void cmCTestMultiProcessHandler::LockResources(int index)
  178. {
  179. this->LockedResources.insert(
  180. this->Properties[index]->LockedResources.begin(),
  181. this->Properties[index]->LockedResources.end());
  182. if (this->Properties[index]->RunSerial) {
  183. this->SerialTestRunning = true;
  184. }
  185. }
  186. void cmCTestMultiProcessHandler::UnlockResources(int index)
  187. {
  188. for (std::string const& i : this->Properties[index]->LockedResources) {
  189. this->LockedResources.erase(i);
  190. }
  191. if (this->Properties[index]->RunSerial) {
  192. this->SerialTestRunning = false;
  193. }
  194. }
  195. void cmCTestMultiProcessHandler::EraseTest(int test)
  196. {
  197. this->Tests.erase(test);
  198. this->SortedTests.erase(
  199. std::find(this->SortedTests.begin(), this->SortedTests.end(), test));
  200. }
  201. inline size_t cmCTestMultiProcessHandler::GetProcessorsUsed(int test)
  202. {
  203. size_t processors = static_cast<int>(this->Properties[test]->Processors);
  204. // If processors setting is set higher than the -j
  205. // setting, we default to using all of the process slots.
  206. if (processors > this->ParallelLevel) {
  207. processors = this->ParallelLevel;
  208. }
  209. return processors;
  210. }
  211. std::string cmCTestMultiProcessHandler::GetName(int test)
  212. {
  213. return this->Properties[test]->Name;
  214. }
  215. bool cmCTestMultiProcessHandler::StartTest(int test)
  216. {
  217. // Check for locked resources
  218. for (std::string const& i : this->Properties[test]->LockedResources) {
  219. if (this->LockedResources.find(i) != this->LockedResources.end()) {
  220. return false;
  221. }
  222. }
  223. // if there are no depends left then run this test
  224. if (this->Tests[test].empty()) {
  225. return this->StartTestProcess(test);
  226. }
  227. // This test was not able to start because it is waiting
  228. // on depends to run
  229. return false;
  230. }
  231. void cmCTestMultiProcessHandler::StartNextTests()
  232. {
  233. size_t numToStart = 0;
  234. if (this->Tests.empty()) {
  235. return;
  236. }
  237. if (this->RunningCount < this->ParallelLevel) {
  238. numToStart = this->ParallelLevel - this->RunningCount;
  239. }
  240. if (numToStart == 0) {
  241. return;
  242. }
  243. // Don't start any new tests if one with the RUN_SERIAL property
  244. // is already running.
  245. if (this->SerialTestRunning) {
  246. return;
  247. }
  248. bool allTestsFailedTestLoadCheck = false;
  249. bool usedFakeLoadForTesting = false;
  250. size_t minProcessorsRequired = this->ParallelLevel;
  251. std::string testWithMinProcessors;
  252. cmsys::SystemInformation info;
  253. unsigned long systemLoad = 0;
  254. size_t spareLoad = 0;
  255. if (this->TestLoad > 0) {
  256. // Activate possible wait.
  257. allTestsFailedTestLoadCheck = true;
  258. // Check for a fake load average value used in testing.
  259. std::string fake_load_value;
  260. if (cmSystemTools::GetEnv("__CTEST_FAKE_LOAD_AVERAGE_FOR_TESTING",
  261. fake_load_value)) {
  262. usedFakeLoadForTesting = true;
  263. if (!cmSystemTools::StringToULong(fake_load_value.c_str(),
  264. &systemLoad)) {
  265. cmSystemTools::Error("Failed to parse fake load value: ",
  266. fake_load_value.c_str());
  267. }
  268. }
  269. // If it's not set, look up the true load average.
  270. else {
  271. systemLoad = static_cast<unsigned long>(ceil(info.GetLoadAverage()));
  272. }
  273. spareLoad =
  274. (this->TestLoad > systemLoad ? this->TestLoad - systemLoad : 0);
  275. // Don't start more tests than the spare load can support.
  276. if (numToStart > spareLoad) {
  277. numToStart = spareLoad;
  278. }
  279. }
  280. TestList copy = this->SortedTests;
  281. for (auto const& test : copy) {
  282. // Take a nap if we're currently performing a RUN_SERIAL test.
  283. if (this->SerialTestRunning) {
  284. break;
  285. }
  286. // We can only start a RUN_SERIAL test if no other tests are also running.
  287. if (this->Properties[test]->RunSerial && this->RunningCount > 0) {
  288. continue;
  289. }
  290. size_t processors = GetProcessorsUsed(test);
  291. bool testLoadOk = true;
  292. if (this->TestLoad > 0) {
  293. if (processors <= spareLoad) {
  294. cmCTestLog(this->CTest, DEBUG, "OK to run "
  295. << GetName(test) << ", it requires " << processors
  296. << " procs & system load is: " << systemLoad
  297. << std::endl);
  298. allTestsFailedTestLoadCheck = false;
  299. } else {
  300. testLoadOk = false;
  301. }
  302. }
  303. if (processors <= minProcessorsRequired) {
  304. minProcessorsRequired = processors;
  305. testWithMinProcessors = GetName(test);
  306. }
  307. if (testLoadOk && processors <= numToStart && this->StartTest(test)) {
  308. if (this->StopTimePassed) {
  309. return;
  310. }
  311. numToStart -= processors;
  312. } else if (numToStart == 0) {
  313. break;
  314. }
  315. }
  316. if (allTestsFailedTestLoadCheck) {
  317. // Find out whether there are any non RUN_SERIAL tests left, so that the
  318. // correct warning may be displayed.
  319. bool onlyRunSerialTestsLeft = true;
  320. for (auto const& test : copy) {
  321. if (!this->Properties[test]->RunSerial) {
  322. onlyRunSerialTestsLeft = false;
  323. }
  324. }
  325. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***** WAITING, ");
  326. if (this->SerialTestRunning) {
  327. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  328. "Waiting for RUN_SERIAL test to finish.");
  329. } else if (onlyRunSerialTestsLeft) {
  330. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  331. "Only RUN_SERIAL tests remain, awaiting available slot.");
  332. } else {
  333. /* clang-format off */
  334. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  335. "System Load: " << systemLoad << ", "
  336. "Max Allowed Load: " << this->TestLoad << ", "
  337. "Smallest test " << testWithMinProcessors <<
  338. " requires " << minProcessorsRequired);
  339. /* clang-format on */
  340. }
  341. cmCTestLog(this->CTest, HANDLER_OUTPUT, "*****" << std::endl);
  342. if (usedFakeLoadForTesting) {
  343. // Break out of the infinite loop of waiting for our fake load
  344. // to come down.
  345. this->StopTimePassed = true;
  346. } else {
  347. // Wait between 1 and 5 seconds before trying again.
  348. cmCTestScriptHandler::SleepInSeconds(cmSystemTools::RandomSeed() % 5 +
  349. 1);
  350. }
  351. }
  352. }
  353. void cmCTestMultiProcessHandler::FinishTestProcess(cmCTestRunTest* runner,
  354. bool started)
  355. {
  356. this->Completed++;
  357. int test = runner->GetIndex();
  358. auto properties = runner->GetTestProperties();
  359. bool testResult = runner->EndTest(this->Completed, this->Total, started);
  360. if (started) {
  361. if (runner->StartAgain()) {
  362. this->Completed--; // remove the completed test because run again
  363. return;
  364. }
  365. }
  366. if (testResult) {
  367. this->Passed->push_back(properties->Name);
  368. } else if (!properties->Disabled) {
  369. this->Failed->push_back(properties->Name);
  370. }
  371. for (auto& t : this->Tests) {
  372. t.second.erase(test);
  373. }
  374. this->TestFinishMap[test] = true;
  375. this->TestRunningMap[test] = false;
  376. this->WriteCheckpoint(test);
  377. this->UnlockResources(test);
  378. this->RunningCount -= GetProcessorsUsed(test);
  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.c_str())) {
  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.c_str(), 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. cmWorkingDirectory workdir(p.Directory);
  604. cmCTestRunTest testRun(*this);
  605. testRun.SetIndex(p.Index);
  606. testRun.SetTestProperties(&p);
  607. testRun.ComputeArguments(); // logs the command in verbose mode
  608. if (!p.Labels.empty()) // print the labels
  609. {
  610. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Labels:",
  611. this->Quiet);
  612. }
  613. for (std::string const& label : p.Labels) {
  614. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " " << label,
  615. this->Quiet);
  616. }
  617. if (!p.Labels.empty()) // print the labels
  618. {
  619. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl,
  620. this->Quiet);
  621. }
  622. if (this->TestHandler->MemCheck) {
  623. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Memory Check",
  624. this->Quiet);
  625. } else {
  626. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Test", this->Quiet);
  627. }
  628. std::ostringstream indexStr;
  629. indexStr << " #" << p.Index << ":";
  630. cmCTestOptionalLog(
  631. this->CTest, HANDLER_OUTPUT,
  632. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  633. << indexStr.str(),
  634. this->Quiet);
  635. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " " << p.Name,
  636. this->Quiet);
  637. if (p.Disabled) {
  638. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " (Disabled)",
  639. this->Quiet);
  640. }
  641. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, std::endl, this->Quiet);
  642. }
  643. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, std::endl
  644. << "Total Tests: " << this->Total << std::endl,
  645. this->Quiet);
  646. }
  647. void cmCTestMultiProcessHandler::PrintLabels()
  648. {
  649. std::set<std::string> allLabels;
  650. for (auto& it : this->Properties) {
  651. cmCTestTestHandler::cmCTestTestProperties& p = *it.second;
  652. allLabels.insert(p.Labels.begin(), p.Labels.end());
  653. }
  654. if (!allLabels.empty()) {
  655. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "All Labels:" << std::endl,
  656. this->Quiet);
  657. } else {
  658. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  659. "No Labels Exist" << std::endl, this->Quiet);
  660. }
  661. for (std::string const& label : allLabels) {
  662. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " " << label << std::endl,
  663. this->Quiet);
  664. }
  665. }
  666. void cmCTestMultiProcessHandler::CheckResume()
  667. {
  668. std::string fname =
  669. this->CTest->GetBinaryDir() + "/Testing/Temporary/CTestCheckpoint.txt";
  670. if (this->CTest->GetFailover()) {
  671. if (cmSystemTools::FileExists(fname.c_str(), true)) {
  672. *this->TestHandler->LogFile
  673. << "Resuming previously interrupted test set" << std::endl
  674. << "----------------------------------------------------------"
  675. << std::endl;
  676. cmsys::ifstream fin;
  677. fin.open(fname.c_str());
  678. std::string line;
  679. while (std::getline(fin, line)) {
  680. int index = atoi(line.c_str());
  681. this->RemoveTest(index);
  682. }
  683. fin.close();
  684. }
  685. } else if (cmSystemTools::FileExists(fname.c_str(), true)) {
  686. cmSystemTools::RemoveFile(fname);
  687. }
  688. }
  689. void cmCTestMultiProcessHandler::RemoveTest(int index)
  690. {
  691. this->EraseTest(index);
  692. this->Properties.erase(index);
  693. this->TestRunningMap[index] = false;
  694. this->TestFinishMap[index] = true;
  695. this->Completed++;
  696. }
  697. int cmCTestMultiProcessHandler::FindMaxIndex()
  698. {
  699. int max = 0;
  700. for (auto const& i : this->Tests) {
  701. if (i.first > max) {
  702. max = i.first;
  703. }
  704. }
  705. return max;
  706. }
  707. // Returns true if no cycles exist in the dependency graph
  708. bool cmCTestMultiProcessHandler::CheckCycles()
  709. {
  710. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  711. "Checking test dependency graph..." << std::endl,
  712. this->Quiet);
  713. for (auto const& it : this->Tests) {
  714. // DFS from each element to itself
  715. int root = it.first;
  716. std::set<int> visited;
  717. std::stack<int> s;
  718. s.push(root);
  719. while (!s.empty()) {
  720. int test = s.top();
  721. s.pop();
  722. if (visited.insert(test).second) {
  723. for (auto const& d : this->Tests[test]) {
  724. if (d == root) {
  725. // cycle exists
  726. cmCTestLog(
  727. this->CTest, ERROR_MESSAGE,
  728. "Error: a cycle exists in the test dependency graph "
  729. "for the test \""
  730. << this->Properties[root]->Name
  731. << "\".\nPlease fix the cycle and run ctest again.\n");
  732. return false;
  733. }
  734. s.push(d);
  735. }
  736. }
  737. }
  738. }
  739. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  740. "Checking test dependency graph end" << std::endl,
  741. this->Quiet);
  742. return true;
  743. }