cmCTestMultiProcessHandler.cxx 22 KB

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