cmCTestMultiProcessHandler.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestMultiProcessHandler.h"
  11. #include "cmCTest.h"
  12. #include "cmCTestScriptHandler.h"
  13. #include "cmProcess.h"
  14. #include "cmStandardIncludes.h"
  15. #include "cmSystemTools.h"
  16. #include <cmsys/FStream.hxx>
  17. #include <cmsys/SystemInformation.hxx>
  18. #include <float.h>
  19. #include <list>
  20. #include <math.h>
  21. #include <stack>
  22. #include <stdlib.h>
  23. class TestComparator
  24. {
  25. public:
  26. TestComparator(cmCTestMultiProcessHandler* handler) : Handler(handler) {}
  27. ~TestComparator() {}
  28. // Sorts tests in descending order of cost
  29. bool operator() (int index1, int index2) const
  30. {
  31. return Handler->Properties[index1]->Cost >
  32. Handler->Properties[index2]->Cost;
  33. }
  34. private:
  35. cmCTestMultiProcessHandler* Handler;
  36. };
  37. cmCTestMultiProcessHandler::cmCTestMultiProcessHandler()
  38. {
  39. this->ParallelLevel = 1;
  40. this->TestLoad = 0;
  41. this->Completed = 0;
  42. this->RunningCount = 0;
  43. this->StopTimePassed = false;
  44. this->HasCycles = false;
  45. this->SerialTestRunning = false;
  46. }
  47. cmCTestMultiProcessHandler::~cmCTestMultiProcessHandler()
  48. {
  49. }
  50. // Set the tests
  51. void
  52. cmCTestMultiProcessHandler::SetTests(TestMap& tests,
  53. PropertiesMap& properties)
  54. {
  55. this->Tests = tests;
  56. this->Properties = properties;
  57. this->Total = this->Tests.size();
  58. // set test run map to false for all
  59. for(TestMap::iterator i = this->Tests.begin();
  60. i != this->Tests.end(); ++i)
  61. {
  62. this->TestRunningMap[i->first] = false;
  63. this->TestFinishMap[i->first] = false;
  64. }
  65. if(!this->CTest->GetShowOnly())
  66. {
  67. this->ReadCostData();
  68. this->HasCycles = !this->CheckCycles();
  69. if(this->HasCycles)
  70. {
  71. return;
  72. }
  73. this->CreateTestCostList();
  74. }
  75. }
  76. // Set the max number of tests that can be run at the same time.
  77. void cmCTestMultiProcessHandler::SetParallelLevel(size_t level)
  78. {
  79. this->ParallelLevel = level < 1 ? 1 : level;
  80. }
  81. void cmCTestMultiProcessHandler::SetTestLoad(unsigned long load)
  82. {
  83. this->TestLoad = load;
  84. }
  85. //---------------------------------------------------------
  86. void cmCTestMultiProcessHandler::RunTests()
  87. {
  88. this->CheckResume();
  89. if(this->HasCycles)
  90. {
  91. return;
  92. }
  93. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  94. this->StartNextTests();
  95. while(!this->Tests.empty())
  96. {
  97. if(this->StopTimePassed)
  98. {
  99. return;
  100. }
  101. this->CheckOutput();
  102. this->StartNextTests();
  103. }
  104. // let all running tests finish
  105. while(this->CheckOutput())
  106. {
  107. }
  108. this->MarkFinished();
  109. this->UpdateCostData();
  110. }
  111. //---------------------------------------------------------
  112. void cmCTestMultiProcessHandler::StartTestProcess(int test)
  113. {
  114. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  115. "test " << test << "\n", this->Quiet);
  116. this->TestRunningMap[test] = true; // mark the test as running
  117. // now remove the test itself
  118. this->EraseTest(test);
  119. this->RunningCount += GetProcessorsUsed(test);
  120. cmCTestRunTest* testRun = new cmCTestRunTest(this->TestHandler);
  121. if(this->CTest->GetRepeatUntilFail())
  122. {
  123. testRun->SetRunUntilFailOn();
  124. testRun->SetNumberOfRuns(this->CTest->GetTestRepeat());
  125. }
  126. testRun->SetIndex(test);
  127. testRun->SetTestProperties(this->Properties[test]);
  128. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  129. cmSystemTools::ChangeDirectory(this->Properties[test]->Directory);
  130. // Lock the resources we'll be using
  131. this->LockResources(test);
  132. if(testRun->StartTest(this->Total))
  133. {
  134. this->RunningTests.insert(testRun);
  135. }
  136. else if(testRun->IsStopTimePassed())
  137. {
  138. this->StopTimePassed = true;
  139. delete testRun;
  140. return;
  141. }
  142. else
  143. {
  144. for(TestMap::iterator j = this->Tests.begin();
  145. j != this->Tests.end(); ++j)
  146. {
  147. j->second.erase(test);
  148. }
  149. this->UnlockResources(test);
  150. this->Completed++;
  151. this->TestFinishMap[test] = true;
  152. this->TestRunningMap[test] = false;
  153. this->RunningCount -= GetProcessorsUsed(test);
  154. testRun->EndTest(this->Completed, this->Total, false);
  155. this->Failed->push_back(this->Properties[test]->Name);
  156. delete testRun;
  157. }
  158. cmSystemTools::ChangeDirectory(current_dir);
  159. }
  160. //---------------------------------------------------------
  161. void cmCTestMultiProcessHandler::LockResources(int index)
  162. {
  163. this->LockedResources.insert(
  164. this->Properties[index]->LockedResources.begin(),
  165. this->Properties[index]->LockedResources.end());
  166. if (this->Properties[index]->RunSerial)
  167. {
  168. this->SerialTestRunning = true;
  169. }
  170. }
  171. //---------------------------------------------------------
  172. void cmCTestMultiProcessHandler::UnlockResources(int index)
  173. {
  174. for(std::set<std::string>::iterator i =
  175. this->Properties[index]->LockedResources.begin();
  176. i != this->Properties[index]->LockedResources.end(); ++i)
  177. {
  178. this->LockedResources.erase(*i);
  179. }
  180. if (this->Properties[index]->RunSerial)
  181. {
  182. this->SerialTestRunning = false;
  183. }
  184. }
  185. //---------------------------------------------------------
  186. void cmCTestMultiProcessHandler::EraseTest(int test)
  187. {
  188. this->Tests.erase(test);
  189. this->SortedTests.erase(
  190. std::find(this->SortedTests.begin(), this->SortedTests.end(), test));
  191. }
  192. //---------------------------------------------------------
  193. inline size_t cmCTestMultiProcessHandler::GetProcessorsUsed(int test)
  194. {
  195. size_t processors =
  196. static_cast<int>(this->Properties[test]->Processors);
  197. //If processors setting is set higher than the -j
  198. //setting, we default to using all of the process slots.
  199. if (processors > this->ParallelLevel)
  200. {
  201. processors = this->ParallelLevel;
  202. }
  203. return processors;
  204. }
  205. std::string cmCTestMultiProcessHandler::GetName(int test)
  206. {
  207. return this->Properties[test]->Name;
  208. }
  209. //---------------------------------------------------------
  210. bool cmCTestMultiProcessHandler::StartTest(int test)
  211. {
  212. //Check for locked resources
  213. for(std::set<std::string>::iterator i =
  214. this->Properties[test]->LockedResources.begin();
  215. i != this->Properties[test]->LockedResources.end(); ++i)
  216. {
  217. if(this->LockedResources.find(*i) != this->LockedResources.end())
  218. {
  219. return false;
  220. }
  221. }
  222. // if there are no depends left then run this test
  223. if(this->Tests[test].empty())
  224. {
  225. this->StartTestProcess(test);
  226. return true;
  227. }
  228. // This test was not able to start because it is waiting
  229. // on depends to run
  230. return false;
  231. }
  232. //---------------------------------------------------------
  233. void cmCTestMultiProcessHandler::StartNextTests()
  234. {
  235. size_t numToStart = 0;
  236. if(this->RunningCount < this->ParallelLevel)
  237. {
  238. numToStart = this->ParallelLevel - this->RunningCount;
  239. }
  240. if(numToStart == 0)
  241. {
  242. return;
  243. }
  244. // Don't start any new tests if one with the RUN_SERIAL property
  245. // is already running.
  246. if (this->SerialTestRunning)
  247. {
  248. return;
  249. }
  250. bool allTestsFailedTestLoadCheck = false;
  251. bool usedFakeLoadForTesting = false;
  252. size_t minProcessorsRequired = this->ParallelLevel;
  253. std::string testWithMinProcessors = "";
  254. cmsys::SystemInformation info;
  255. unsigned long systemLoad = 0;
  256. size_t spareLoad = 0;
  257. if (this->TestLoad > 0)
  258. {
  259. // Activate possible wait.
  260. allTestsFailedTestLoadCheck = true;
  261. // Check for a fake load average value used in testing.
  262. if (const char* fake_load_value =
  263. cmSystemTools::GetEnv("__CTEST_FAKE_LOAD_AVERAGE_FOR_TESTING"))
  264. {
  265. usedFakeLoadForTesting = true;
  266. if (!cmSystemTools::StringToULong(fake_load_value, &systemLoad))
  267. {
  268. cmSystemTools::Error("Failed to parse fake load value: ",
  269. fake_load_value);
  270. }
  271. }
  272. // If it's not set, look up the true load average.
  273. else
  274. {
  275. systemLoad = static_cast<unsigned long>(ceil(info.GetLoadAverage()));
  276. }
  277. spareLoad = (this->TestLoad > systemLoad ?
  278. this->TestLoad - systemLoad : 0);
  279. // Don't start more tests than the spare load can support.
  280. if (numToStart > spareLoad)
  281. {
  282. numToStart = spareLoad;
  283. }
  284. }
  285. TestList copy = this->SortedTests;
  286. for(TestList::iterator test = copy.begin(); test != copy.end(); ++test)
  287. {
  288. // Take a nap if we're currently performing a RUN_SERIAL test.
  289. if (this->SerialTestRunning)
  290. {
  291. break;
  292. }
  293. // We can only start a RUN_SERIAL test if no other tests are also running.
  294. if (this->Properties[*test]->RunSerial && this->RunningCount > 0)
  295. {
  296. continue;
  297. }
  298. size_t processors = GetProcessorsUsed(*test);
  299. bool testLoadOk = true;
  300. if (this->TestLoad > 0)
  301. {
  302. if (processors <= spareLoad)
  303. {
  304. cmCTestLog(this->CTest, DEBUG,
  305. "OK to run " << GetName(*test) <<
  306. ", it requires " << processors <<
  307. " procs & system load is: " <<
  308. systemLoad << std::endl);
  309. allTestsFailedTestLoadCheck = false;
  310. }
  311. else
  312. {
  313. testLoadOk = false;
  314. }
  315. }
  316. if (processors <= minProcessorsRequired)
  317. {
  318. minProcessorsRequired = processors;
  319. testWithMinProcessors = GetName(*test);
  320. }
  321. if(testLoadOk && processors <= numToStart && this->StartTest(*test))
  322. {
  323. if(this->StopTimePassed)
  324. {
  325. return;
  326. }
  327. numToStart -= processors;
  328. }
  329. else if(numToStart == 0)
  330. {
  331. break;
  332. }
  333. }
  334. if (allTestsFailedTestLoadCheck)
  335. {
  336. cmCTestLog(this->CTest, HANDLER_OUTPUT, "***** WAITING, ");
  337. if (this->SerialTestRunning)
  338. {
  339. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  340. "Waiting for RUN_SERIAL test to finish.");
  341. }
  342. else
  343. {
  344. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  345. "System Load: " << systemLoad << ", "
  346. "Max Allowed Load: " << this->TestLoad << ", "
  347. "Smallest test " << testWithMinProcessors <<
  348. " requires " << minProcessorsRequired);
  349. }
  350. cmCTestLog(this->CTest, HANDLER_OUTPUT, "*****" << std::endl);
  351. if (usedFakeLoadForTesting)
  352. {
  353. // Break out of the infinite loop of waiting for our fake load
  354. // to come down.
  355. this->StopTimePassed = true;
  356. }
  357. else
  358. {
  359. // Wait between 1 and 5 seconds before trying again.
  360. cmCTestScriptHandler::SleepInSeconds(
  361. cmSystemTools::RandomSeed() % 5 + 1);
  362. }
  363. }
  364. }
  365. //---------------------------------------------------------
  366. bool cmCTestMultiProcessHandler::CheckOutput()
  367. {
  368. // no more output we are done
  369. if(this->RunningTests.empty())
  370. {
  371. return false;
  372. }
  373. std::vector<cmCTestRunTest*> finished;
  374. std::string out, err;
  375. for(std::set<cmCTestRunTest*>::const_iterator i = this->RunningTests.begin();
  376. i != this->RunningTests.end(); ++i)
  377. {
  378. cmCTestRunTest* p = *i;
  379. if(!p->CheckOutput())
  380. {
  381. finished.push_back(p);
  382. }
  383. }
  384. for( std::vector<cmCTestRunTest*>::iterator i = finished.begin();
  385. i != finished.end(); ++i)
  386. {
  387. this->Completed++;
  388. cmCTestRunTest* p = *i;
  389. int test = p->GetIndex();
  390. bool testResult = p->EndTest(this->Completed, this->Total, true);
  391. if(p->StartAgain())
  392. {
  393. this->Completed--; // remove the completed test because run again
  394. continue;
  395. }
  396. if(testResult)
  397. {
  398. this->Passed->push_back(p->GetTestProperties()->Name);
  399. }
  400. else
  401. {
  402. this->Failed->push_back(p->GetTestProperties()->Name);
  403. }
  404. for(TestMap::iterator j = this->Tests.begin();
  405. j != this->Tests.end(); ++j)
  406. {
  407. j->second.erase(test);
  408. }
  409. this->TestFinishMap[test] = true;
  410. this->TestRunningMap[test] = false;
  411. this->RunningTests.erase(p);
  412. this->WriteCheckpoint(test);
  413. this->UnlockResources(test);
  414. this->RunningCount -= GetProcessorsUsed(test);
  415. delete p;
  416. }
  417. return true;
  418. }
  419. //---------------------------------------------------------
  420. void cmCTestMultiProcessHandler::UpdateCostData()
  421. {
  422. std::string fname = this->CTest->GetCostDataFile();
  423. std::string tmpout = fname + ".tmp";
  424. cmsys::ofstream fout;
  425. fout.open(tmpout.c_str());
  426. PropertiesMap temp = this->Properties;
  427. if(cmSystemTools::FileExists(fname.c_str()))
  428. {
  429. cmsys::ifstream fin;
  430. fin.open(fname.c_str());
  431. std::string line;
  432. while(std::getline(fin, line))
  433. {
  434. if(line == "---") break;
  435. std::vector<cmsys::String> parts =
  436. cmSystemTools::SplitString(line, ' ');
  437. //Format: <name> <previous_runs> <avg_cost>
  438. if(parts.size() < 3) break;
  439. std::string name = parts[0];
  440. int prev = atoi(parts[1].c_str());
  441. float cost = static_cast<float>(atof(parts[2].c_str()));
  442. int index = this->SearchByName(name);
  443. if(index == -1)
  444. {
  445. // This test is not in memory. We just rewrite the entry
  446. fout << name << " " << prev << " " << cost << "\n";
  447. }
  448. else
  449. {
  450. // Update with our new average cost
  451. fout << name << " " << this->Properties[index]->PreviousRuns << " "
  452. << this->Properties[index]->Cost << "\n";
  453. temp.erase(index);
  454. }
  455. }
  456. fin.close();
  457. cmSystemTools::RemoveFile(fname);
  458. }
  459. // Add all tests not previously listed in the file
  460. for(PropertiesMap::iterator i = temp.begin(); i != temp.end(); ++i)
  461. {
  462. fout << i->second->Name << " " << i->second->PreviousRuns << " "
  463. << i->second->Cost << "\n";
  464. }
  465. // Write list of failed tests
  466. fout << "---\n";
  467. for(std::vector<std::string>::iterator i = this->Failed->begin();
  468. i != this->Failed->end(); ++i)
  469. {
  470. fout << i->c_str() << "\n";
  471. }
  472. fout.close();
  473. cmSystemTools::RenameFile(tmpout.c_str(), fname.c_str());
  474. }
  475. //---------------------------------------------------------
  476. void cmCTestMultiProcessHandler::ReadCostData()
  477. {
  478. std::string fname = this->CTest->GetCostDataFile();
  479. if(cmSystemTools::FileExists(fname.c_str(), true))
  480. {
  481. cmsys::ifstream fin;
  482. fin.open(fname.c_str());
  483. std::string line;
  484. while(std::getline(fin, line))
  485. {
  486. if(line == "---") break;
  487. std::vector<cmsys::String> parts =
  488. cmSystemTools::SplitString(line, ' ');
  489. // Probably an older version of the file, will be fixed next run
  490. if(parts.size() < 3)
  491. {
  492. fin.close();
  493. return;
  494. }
  495. std::string name = parts[0];
  496. int prev = atoi(parts[1].c_str());
  497. float cost = static_cast<float>(atof(parts[2].c_str()));
  498. int index = this->SearchByName(name);
  499. if(index == -1) continue;
  500. this->Properties[index]->PreviousRuns = prev;
  501. // When not running in parallel mode, don't use cost data
  502. if(this->ParallelLevel > 1 &&
  503. this->Properties[index] &&
  504. this->Properties[index]->Cost == 0)
  505. {
  506. this->Properties[index]->Cost = cost;
  507. }
  508. }
  509. // Next part of the file is the failed tests
  510. while(std::getline(fin, line))
  511. {
  512. if(line != "")
  513. {
  514. this->LastTestsFailed.push_back(line);
  515. }
  516. }
  517. fin.close();
  518. }
  519. }
  520. //---------------------------------------------------------
  521. int cmCTestMultiProcessHandler::SearchByName(std::string name)
  522. {
  523. int index = -1;
  524. for(PropertiesMap::iterator i = this->Properties.begin();
  525. i != this->Properties.end(); ++i)
  526. {
  527. if(i->second->Name == name)
  528. {
  529. index = i->first;
  530. }
  531. }
  532. return index;
  533. }
  534. //---------------------------------------------------------
  535. void cmCTestMultiProcessHandler::CreateTestCostList()
  536. {
  537. if(this->ParallelLevel > 1)
  538. {
  539. CreateParallelTestCostList();
  540. }
  541. else
  542. {
  543. CreateSerialTestCostList();
  544. }
  545. }
  546. //---------------------------------------------------------
  547. void cmCTestMultiProcessHandler::CreateParallelTestCostList()
  548. {
  549. TestSet alreadySortedTests;
  550. std::list<TestSet> priorityStack;
  551. priorityStack.push_back(TestSet());
  552. TestSet &topLevel = priorityStack.back();
  553. // In parallel test runs add previously failed tests to the front
  554. // of the cost list and queue other tests for further sorting
  555. for(TestMap::const_iterator i = this->Tests.begin();
  556. i != this->Tests.end(); ++i)
  557. {
  558. if(std::find(this->LastTestsFailed.begin(), this->LastTestsFailed.end(),
  559. this->Properties[i->first]->Name) != this->LastTestsFailed.end())
  560. {
  561. //If the test failed last time, it should be run first.
  562. this->SortedTests.push_back(i->first);
  563. alreadySortedTests.insert(i->first);
  564. }
  565. else
  566. {
  567. topLevel.insert(i->first);
  568. }
  569. }
  570. // In parallel test runs repeatedly move dependencies of the tests on
  571. // the current dependency level to the next level until no
  572. // further dependencies exist.
  573. while(priorityStack.back().size())
  574. {
  575. TestSet &previousSet = priorityStack.back();
  576. priorityStack.push_back(TestSet());
  577. TestSet &currentSet = priorityStack.back();
  578. for(TestSet::const_iterator i = previousSet.begin();
  579. i != previousSet.end(); ++i)
  580. {
  581. TestSet const& dependencies = this->Tests[*i];
  582. currentSet.insert(dependencies.begin(), dependencies.end());
  583. }
  584. for(TestSet::const_iterator i = currentSet.begin();
  585. i != currentSet.end(); ++i)
  586. {
  587. previousSet.erase(*i);
  588. }
  589. }
  590. // Remove the empty dependency level
  591. priorityStack.pop_back();
  592. // Reverse iterate over the different dependency levels (deepest first).
  593. // Sort tests within each level by COST and append them to the cost list.
  594. for(std::list<TestSet>::reverse_iterator i = priorityStack.rbegin();
  595. i != priorityStack.rend(); ++i)
  596. {
  597. TestSet const& currentSet = *i;
  598. TestComparator comp(this);
  599. TestList sortedCopy;
  600. sortedCopy.insert(sortedCopy.end(),
  601. currentSet.begin(), currentSet.end());
  602. std::stable_sort(sortedCopy.begin(), sortedCopy.end(), comp);
  603. for(TestList::const_iterator j = sortedCopy.begin();
  604. j != sortedCopy.end(); ++j)
  605. {
  606. if(alreadySortedTests.find(*j) == alreadySortedTests.end())
  607. {
  608. this->SortedTests.push_back(*j);
  609. alreadySortedTests.insert(*j);
  610. }
  611. }
  612. }
  613. }
  614. //---------------------------------------------------------
  615. void cmCTestMultiProcessHandler::GetAllTestDependencies(
  616. int test, TestList& dependencies)
  617. {
  618. TestSet const& dependencySet = this->Tests[test];
  619. for(TestSet::const_iterator i = dependencySet.begin();
  620. i != dependencySet.end(); ++i)
  621. {
  622. GetAllTestDependencies(*i, dependencies);
  623. dependencies.push_back(*i);
  624. }
  625. }
  626. //---------------------------------------------------------
  627. void cmCTestMultiProcessHandler::CreateSerialTestCostList()
  628. {
  629. TestList presortedList;
  630. for(TestMap::iterator i = this->Tests.begin();
  631. i != this->Tests.end(); ++i)
  632. {
  633. presortedList.push_back(i->first);
  634. }
  635. TestComparator comp(this);
  636. std::stable_sort(presortedList.begin(), presortedList.end(), comp);
  637. TestSet alreadySortedTests;
  638. for(TestList::const_iterator i = presortedList.begin();
  639. i != presortedList.end(); ++i)
  640. {
  641. int test = *i;
  642. if(alreadySortedTests.find(test) != alreadySortedTests.end())
  643. {
  644. continue;
  645. }
  646. TestList dependencies;
  647. GetAllTestDependencies(test, dependencies);
  648. for(TestList::const_iterator j = dependencies.begin();
  649. j != dependencies.end(); ++j)
  650. {
  651. int testDependency = *j;
  652. if(alreadySortedTests.find(testDependency) == alreadySortedTests.end())
  653. {
  654. alreadySortedTests.insert(testDependency);
  655. this->SortedTests.push_back(testDependency);
  656. }
  657. }
  658. alreadySortedTests.insert(test);
  659. this->SortedTests.push_back(test);
  660. }
  661. }
  662. //---------------------------------------------------------
  663. void cmCTestMultiProcessHandler::WriteCheckpoint(int index)
  664. {
  665. std::string fname = this->CTest->GetBinaryDir()
  666. + "/Testing/Temporary/CTestCheckpoint.txt";
  667. cmsys::ofstream fout;
  668. fout.open(fname.c_str(), std::ios::app);
  669. fout << index << "\n";
  670. fout.close();
  671. }
  672. //---------------------------------------------------------
  673. void cmCTestMultiProcessHandler::MarkFinished()
  674. {
  675. std::string fname = this->CTest->GetBinaryDir()
  676. + "/Testing/Temporary/CTestCheckpoint.txt";
  677. cmSystemTools::RemoveFile(fname);
  678. }
  679. //---------------------------------------------------------
  680. //For ShowOnly mode
  681. void cmCTestMultiProcessHandler::PrintTestList()
  682. {
  683. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  684. int count = 0;
  685. for (PropertiesMap::iterator it = this->Properties.begin();
  686. it != this->Properties.end(); ++it)
  687. {
  688. count++;
  689. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  690. //push working dir
  691. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  692. cmSystemTools::ChangeDirectory(p.Directory);
  693. cmCTestRunTest testRun(this->TestHandler);
  694. testRun.SetIndex(p.Index);
  695. testRun.SetTestProperties(&p);
  696. testRun.ComputeArguments(); //logs the command in verbose mode
  697. if(!p.Labels.empty()) //print the labels
  698. {
  699. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Labels:",
  700. this->Quiet);
  701. }
  702. for(std::vector<std::string>::iterator label = p.Labels.begin();
  703. label != p.Labels.end(); ++label)
  704. {
  705. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " " << *label,
  706. this->Quiet);
  707. }
  708. if(!p.Labels.empty()) //print the labels
  709. {
  710. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl,
  711. this->Quiet);
  712. }
  713. if (this->TestHandler->MemCheck)
  714. {
  715. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Memory Check",
  716. this->Quiet);
  717. }
  718. else
  719. {
  720. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " Test", this->Quiet);
  721. }
  722. std::ostringstream indexStr;
  723. indexStr << " #" << p.Index << ":";
  724. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  725. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  726. << indexStr.str(), this->Quiet);
  727. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, " ", this->Quiet);
  728. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  729. p.Name.c_str() << std::endl, this->Quiet);
  730. //pop working dir
  731. cmSystemTools::ChangeDirectory(current_dir);
  732. }
  733. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, std::endl << "Total Tests: "
  734. << this->Total << std::endl, this->Quiet);
  735. }
  736. void cmCTestMultiProcessHandler::PrintLabels()
  737. {
  738. std::set<std::string> allLabels;
  739. for (PropertiesMap::iterator it = this->Properties.begin();
  740. it != this->Properties.end(); ++it)
  741. {
  742. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  743. allLabels.insert(p.Labels.begin(), p.Labels.end());
  744. }
  745. if(!allLabels.empty())
  746. {
  747. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  748. "All Labels:" << std::endl, this->Quiet);
  749. }
  750. else
  751. {
  752. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  753. "No Labels Exist" << std::endl, this->Quiet);
  754. }
  755. for(std::set<std::string>::iterator label = allLabels.begin();
  756. label != allLabels.end(); ++label)
  757. {
  758. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  759. " " << *label << std::endl, this->Quiet);
  760. }
  761. }
  762. //---------------------------------------------------------
  763. void cmCTestMultiProcessHandler::CheckResume()
  764. {
  765. std::string fname = this->CTest->GetBinaryDir()
  766. + "/Testing/Temporary/CTestCheckpoint.txt";
  767. if(this->CTest->GetFailover())
  768. {
  769. if(cmSystemTools::FileExists(fname.c_str(), true))
  770. {
  771. *this->TestHandler->LogFile << "Resuming previously interrupted test set"
  772. << std::endl
  773. << "----------------------------------------------------------"
  774. << std::endl;
  775. cmsys::ifstream fin;
  776. fin.open(fname.c_str());
  777. std::string line;
  778. while(std::getline(fin, line))
  779. {
  780. int index = atoi(line.c_str());
  781. this->RemoveTest(index);
  782. }
  783. fin.close();
  784. }
  785. }
  786. else if(cmSystemTools::FileExists(fname.c_str(), true))
  787. {
  788. cmSystemTools::RemoveFile(fname);
  789. }
  790. }
  791. //---------------------------------------------------------
  792. void cmCTestMultiProcessHandler::RemoveTest(int index)
  793. {
  794. this->EraseTest(index);
  795. this->Properties.erase(index);
  796. this->TestRunningMap[index] = false;
  797. this->TestFinishMap[index] = true;
  798. this->Completed++;
  799. }
  800. //---------------------------------------------------------
  801. int cmCTestMultiProcessHandler::FindMaxIndex()
  802. {
  803. int max = 0;
  804. cmCTestMultiProcessHandler::TestMap::iterator i = this->Tests.begin();
  805. for(; i != this->Tests.end(); ++i)
  806. {
  807. if(i->first > max)
  808. {
  809. max = i->first;
  810. }
  811. }
  812. return max;
  813. }
  814. //Returns true if no cycles exist in the dependency graph
  815. bool cmCTestMultiProcessHandler::CheckCycles()
  816. {
  817. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  818. "Checking test dependency graph..." << std::endl, this->Quiet);
  819. for(TestMap::iterator it = this->Tests.begin();
  820. it != this->Tests.end(); ++it)
  821. {
  822. //DFS from each element to itself
  823. int root = it->first;
  824. std::set<int> visited;
  825. std::stack<int> s;
  826. s.push(root);
  827. while(!s.empty())
  828. {
  829. int test = s.top();
  830. s.pop();
  831. if(visited.insert(test).second)
  832. {
  833. for(TestSet::iterator d = this->Tests[test].begin();
  834. d != this->Tests[test].end(); ++d)
  835. {
  836. if(*d == root)
  837. {
  838. //cycle exists
  839. cmCTestLog(this->CTest, ERROR_MESSAGE,
  840. "Error: a cycle exists in the test dependency graph "
  841. "for the test \"" << this->Properties[root]->Name <<
  842. "\".\nPlease fix the cycle and run ctest again.\n");
  843. return false;
  844. }
  845. else
  846. {
  847. s.push(*d);
  848. }
  849. }
  850. }
  851. }
  852. }
  853. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  854. "Checking test dependency graph end" << std::endl, this->Quiet);
  855. return true;
  856. }