cmCTestMultiProcessHandler.cxx 24 KB

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