cmCTestMultiProcessHandler.cxx 22 KB

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