cmCTestMultiProcessHandler.cxx 22 KB

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