1
0

cmCTestMultiProcessHandler.cxx 23 KB

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