cmCTestMultiProcessHandler.cxx 24 KB

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