cmCTestMultiProcessHandler.cxx 24 KB

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