cmCTestMultiProcessHandler.cxx 25 KB

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