cmCTestMultiProcessHandler.cxx 24 KB

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