cmCTestMultiProcessHandler.cxx 25 KB

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