cmCTestMultiProcessHandler.cxx 22 KB

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