cmCTestMultiProcessHandler.cxx 25 KB

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