cmCTestMultiProcessHandler.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestMultiProcessHandler.h"
  11. #include "cmProcess.h"
  12. #include "cmStandardIncludes.h"
  13. #include "cmCTest.h"
  14. #include "cmSystemTools.h"
  15. #include <stdlib.h>
  16. #include <stack>
  17. #include <float.h>
  18. class TestComparator
  19. {
  20. public:
  21. TestComparator(cmCTestMultiProcessHandler* handler) : Handler(handler) {}
  22. ~TestComparator() {}
  23. // Sorts tests in descending order of cost
  24. bool operator() (int index1, int index2) const
  25. {
  26. return Handler->Properties[index1]->Cost >
  27. Handler->Properties[index2]->Cost;
  28. }
  29. private:
  30. cmCTestMultiProcessHandler* Handler;
  31. };
  32. cmCTestMultiProcessHandler::cmCTestMultiProcessHandler()
  33. {
  34. this->ParallelLevel = 1;
  35. this->Completed = 0;
  36. this->RunningCount = 0;
  37. this->StopTimePassed = false;
  38. }
  39. cmCTestMultiProcessHandler::~cmCTestMultiProcessHandler()
  40. {
  41. }
  42. // Set the tests
  43. void
  44. cmCTestMultiProcessHandler::SetTests(TestMap& tests,
  45. PropertiesMap& properties)
  46. {
  47. this->Tests = tests;
  48. this->Properties = properties;
  49. this->Total = this->Tests.size();
  50. // set test run map to false for all
  51. for(TestMap::iterator i = this->Tests.begin();
  52. i != this->Tests.end(); ++i)
  53. {
  54. this->TestRunningMap[i->first] = false;
  55. this->TestFinishMap[i->first] = false;
  56. }
  57. if(!this->CTest->GetShowOnly())
  58. {
  59. this->ReadCostData();
  60. this->CreateTestCostList();
  61. }
  62. }
  63. // Set the max number of tests that can be run at the same time.
  64. void cmCTestMultiProcessHandler::SetParallelLevel(size_t level)
  65. {
  66. this->ParallelLevel = level < 1 ? 1 : level;
  67. }
  68. //---------------------------------------------------------
  69. void cmCTestMultiProcessHandler::RunTests()
  70. {
  71. this->CheckResume();
  72. if(!this->CheckCycles())
  73. {
  74. return;
  75. }
  76. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  77. this->StartNextTests();
  78. while(this->Tests.size() != 0)
  79. {
  80. if(this->StopTimePassed)
  81. {
  82. return;
  83. }
  84. this->CheckOutput();
  85. this->StartNextTests();
  86. }
  87. // let all running tests finish
  88. while(this->CheckOutput())
  89. {
  90. }
  91. this->MarkFinished();
  92. this->UpdateCostData();
  93. }
  94. //---------------------------------------------------------
  95. void cmCTestMultiProcessHandler::StartTestProcess(int test)
  96. {
  97. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "test " << test << "\n");
  98. this->TestRunningMap[test] = true; // mark the test as running
  99. // now remove the test itself
  100. this->EraseTest(test);
  101. this->RunningCount += GetProcessorsUsed(test);
  102. cmCTestRunTest* testRun = new cmCTestRunTest(this->TestHandler);
  103. testRun->SetIndex(test);
  104. testRun->SetTestProperties(this->Properties[test]);
  105. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  106. cmSystemTools::ChangeDirectory(this->Properties[test]->Directory.c_str());
  107. // Lock the resources we'll be using
  108. this->LockResources(test);
  109. if(testRun->StartTest(this->Total))
  110. {
  111. this->RunningTests.insert(testRun);
  112. }
  113. else if(testRun->IsStopTimePassed())
  114. {
  115. this->StopTimePassed = true;
  116. delete testRun;
  117. return;
  118. }
  119. else
  120. {
  121. this->UnlockResources(test);
  122. this->Completed++;
  123. this->TestFinishMap[test] = true;
  124. this->TestRunningMap[test] = false;
  125. this->RunningCount -= GetProcessorsUsed(test);
  126. testRun->EndTest(this->Completed, this->Total, false);
  127. this->Failed->push_back(this->Properties[test]->Name);
  128. delete testRun;
  129. }
  130. cmSystemTools::ChangeDirectory(current_dir.c_str());
  131. }
  132. //---------------------------------------------------------
  133. void cmCTestMultiProcessHandler::LockResources(int index)
  134. {
  135. for(std::set<std::string>::iterator i =
  136. this->Properties[index]->LockedResources.begin();
  137. i != this->Properties[index]->LockedResources.end(); ++i)
  138. {
  139. this->LockedResources.insert(*i);
  140. }
  141. }
  142. //---------------------------------------------------------
  143. void cmCTestMultiProcessHandler::UnlockResources(int index)
  144. {
  145. for(std::set<std::string>::iterator i =
  146. this->Properties[index]->LockedResources.begin();
  147. i != this->Properties[index]->LockedResources.end(); ++i)
  148. {
  149. this->LockedResources.erase(*i);
  150. }
  151. }
  152. //---------------------------------------------------------
  153. void cmCTestMultiProcessHandler::EraseTest(int test)
  154. {
  155. this->Tests.erase(test);
  156. this->SortedTests.erase(
  157. std::find(this->SortedTests.begin(), this->SortedTests.end(), test));
  158. }
  159. //---------------------------------------------------------
  160. inline size_t cmCTestMultiProcessHandler::GetProcessorsUsed(int test)
  161. {
  162. size_t processors =
  163. static_cast<int>(this->Properties[test]->Processors);
  164. //If this is set to run serially, it must run alone.
  165. //Also, if processors setting is set higher than the -j
  166. //setting, we default to using all of the process slots.
  167. if(this->Properties[test]->RunSerial
  168. || processors > this->ParallelLevel)
  169. {
  170. processors = this->ParallelLevel;
  171. }
  172. return processors;
  173. }
  174. //---------------------------------------------------------
  175. bool cmCTestMultiProcessHandler::StartTest(int test)
  176. {
  177. //Check for locked resources
  178. for(std::set<std::string>::iterator i =
  179. this->Properties[test]->LockedResources.begin();
  180. i != this->Properties[test]->LockedResources.end(); ++i)
  181. {
  182. if(this->LockedResources.find(*i) != this->LockedResources.end())
  183. {
  184. return false;
  185. }
  186. }
  187. // copy the depend tests locally because when
  188. // a test is finished it will be removed from the depend list
  189. // and we don't want to be iterating a list while removing from it
  190. TestSet depends = this->Tests[test];
  191. size_t totalDepends = depends.size();
  192. // if there are no depends left then run this test
  193. if(totalDepends == 0)
  194. {
  195. this->StartTestProcess(test);
  196. return true;
  197. }
  198. // This test was not able to start because it is waiting
  199. // on depends to run
  200. return false;
  201. }
  202. //---------------------------------------------------------
  203. void cmCTestMultiProcessHandler::StartNextTests()
  204. {
  205. size_t numToStart = 0;
  206. if(this->RunningCount < this->ParallelLevel)
  207. {
  208. numToStart = this->ParallelLevel - this->RunningCount;
  209. }
  210. if(numToStart == 0)
  211. {
  212. return;
  213. }
  214. TestList copy = this->SortedTests;
  215. for(TestList::iterator test = copy.begin(); test != copy.end(); ++test)
  216. {
  217. size_t processors = GetProcessorsUsed(*test);
  218. if(processors <= numToStart && this->StartTest(*test))
  219. {
  220. if(this->StopTimePassed)
  221. {
  222. return;
  223. }
  224. numToStart -= processors;
  225. }
  226. else if(numToStart == 0)
  227. {
  228. return;
  229. }
  230. }
  231. }
  232. //---------------------------------------------------------
  233. bool cmCTestMultiProcessHandler::CheckOutput()
  234. {
  235. // no more output we are done
  236. if(this->RunningTests.size() == 0)
  237. {
  238. return false;
  239. }
  240. std::vector<cmCTestRunTest*> finished;
  241. std::string out, err;
  242. for(std::set<cmCTestRunTest*>::const_iterator i = this->RunningTests.begin();
  243. i != this->RunningTests.end(); ++i)
  244. {
  245. cmCTestRunTest* p = *i;
  246. if(!p->CheckOutput())
  247. {
  248. finished.push_back(p);
  249. }
  250. }
  251. for( std::vector<cmCTestRunTest*>::iterator i = finished.begin();
  252. i != finished.end(); ++i)
  253. {
  254. this->Completed++;
  255. cmCTestRunTest* p = *i;
  256. int test = p->GetIndex();
  257. if(p->EndTest(this->Completed, this->Total, true))
  258. {
  259. this->Passed->push_back(p->GetTestProperties()->Name);
  260. }
  261. else
  262. {
  263. this->Failed->push_back(p->GetTestProperties()->Name);
  264. }
  265. for(TestMap::iterator j = this->Tests.begin();
  266. j != this->Tests.end(); ++j)
  267. {
  268. j->second.erase(test);
  269. }
  270. this->TestFinishMap[test] = true;
  271. this->TestRunningMap[test] = false;
  272. this->RunningTests.erase(p);
  273. this->WriteCheckpoint(test);
  274. this->UnlockResources(test);
  275. this->RunningCount -= GetProcessorsUsed(test);
  276. delete p;
  277. }
  278. return true;
  279. }
  280. //---------------------------------------------------------
  281. void cmCTestMultiProcessHandler::UpdateCostData()
  282. {
  283. std::string fname = this->CTest->GetCostDataFile();
  284. std::string tmpout = fname + ".tmp";
  285. std::fstream fout;
  286. fout.open(tmpout.c_str(), std::ios::out);
  287. PropertiesMap temp = this->Properties;
  288. if(cmSystemTools::FileExists(fname.c_str()))
  289. {
  290. std::ifstream fin;
  291. fin.open(fname.c_str());
  292. std::string line;
  293. while(std::getline(fin, line))
  294. {
  295. if(line == "---") break;
  296. std::vector<cmsys::String> parts =
  297. cmSystemTools::SplitString(line.c_str(), ' ');
  298. //Format: <name> <previous_runs> <avg_cost>
  299. if(parts.size() < 3) break;
  300. std::string name = parts[0];
  301. int prev = atoi(parts[1].c_str());
  302. float cost = static_cast<float>(atof(parts[2].c_str()));
  303. int index = this->SearchByName(name);
  304. if(index == -1)
  305. {
  306. // This test is not in memory. We just rewrite the entry
  307. fout << name << " " << prev << " " << cost << "\n";
  308. }
  309. else
  310. {
  311. // Update with our new average cost
  312. fout << name << " " << this->Properties[index]->PreviousRuns << " "
  313. << this->Properties[index]->Cost << "\n";
  314. temp.erase(index);
  315. }
  316. }
  317. fin.close();
  318. cmSystemTools::RemoveFile(fname.c_str());
  319. }
  320. // Add all tests not previously listed in the file
  321. for(PropertiesMap::iterator i = temp.begin(); i != temp.end(); ++i)
  322. {
  323. fout << i->second->Name << " " << i->second->PreviousRuns << " "
  324. << i->second->Cost << "\n";
  325. }
  326. // Write list of failed tests
  327. fout << "---\n";
  328. for(std::vector<cmStdString>::iterator i = this->Failed->begin();
  329. i != this->Failed->end(); ++i)
  330. {
  331. fout << i->c_str() << "\n";
  332. }
  333. fout.close();
  334. cmSystemTools::RenameFile(tmpout.c_str(), fname.c_str());
  335. }
  336. //---------------------------------------------------------
  337. void cmCTestMultiProcessHandler::ReadCostData()
  338. {
  339. std::string fname = this->CTest->GetCostDataFile();
  340. if(cmSystemTools::FileExists(fname.c_str(), true))
  341. {
  342. std::ifstream fin;
  343. fin.open(fname.c_str());
  344. std::string line;
  345. while(std::getline(fin, line))
  346. {
  347. if(line == "---") break;
  348. std::vector<cmsys::String> parts =
  349. cmSystemTools::SplitString(line.c_str(), ' ');
  350. // Probably an older version of the file, will be fixed next run
  351. if(parts.size() < 3)
  352. {
  353. fin.close();
  354. return;
  355. }
  356. std::string name = parts[0];
  357. int prev = atoi(parts[1].c_str());
  358. float cost = static_cast<float>(atof(parts[2].c_str()));
  359. int index = this->SearchByName(name);
  360. if(index == -1) continue;
  361. this->Properties[index]->PreviousRuns = prev;
  362. // When not running in parallel mode, don't use cost data
  363. if(this->ParallelLevel > 1 &&
  364. this->Properties[index] &&
  365. this->Properties[index]->Cost == 0)
  366. {
  367. this->Properties[index]->Cost = cost;
  368. }
  369. }
  370. // Next part of the file is the failed tests
  371. while(std::getline(fin, line))
  372. {
  373. if(line != "")
  374. {
  375. this->LastTestsFailed.push_back(line);
  376. }
  377. }
  378. fin.close();
  379. }
  380. }
  381. //---------------------------------------------------------
  382. int cmCTestMultiProcessHandler::SearchByName(std::string name)
  383. {
  384. int index = -1;
  385. for(PropertiesMap::iterator i = this->Properties.begin();
  386. i != this->Properties.end(); ++i)
  387. {
  388. if(i->second->Name == name)
  389. {
  390. index = i->first;
  391. }
  392. }
  393. return index;
  394. }
  395. //---------------------------------------------------------
  396. void cmCTestMultiProcessHandler::CreateTestCostList()
  397. {
  398. for(TestMap::iterator i = this->Tests.begin();
  399. i != this->Tests.end(); ++i)
  400. {
  401. SortedTests.push_back(i->first);
  402. //If the test failed last time, it should be run first, so max the cost.
  403. //Only do this for parallel runs; in non-parallel runs, avoid clobbering
  404. //the test's explicitly set cost.
  405. if(this->ParallelLevel > 1 &&
  406. std::find(this->LastTestsFailed.begin(), this->LastTestsFailed.end(),
  407. this->Properties[i->first]->Name) != this->LastTestsFailed.end())
  408. {
  409. this->Properties[i->first]->Cost = FLT_MAX;
  410. }
  411. }
  412. TestComparator comp(this);
  413. std::stable_sort(SortedTests.begin(), SortedTests.end(), comp);
  414. }
  415. //---------------------------------------------------------
  416. void cmCTestMultiProcessHandler::WriteCheckpoint(int index)
  417. {
  418. std::string fname = this->CTest->GetBinaryDir()
  419. + "/Testing/Temporary/CTestCheckpoint.txt";
  420. std::fstream fout;
  421. fout.open(fname.c_str(), std::ios::app | std::ios::out);
  422. fout << index << "\n";
  423. fout.close();
  424. }
  425. //---------------------------------------------------------
  426. void cmCTestMultiProcessHandler::MarkFinished()
  427. {
  428. std::string fname = this->CTest->GetBinaryDir()
  429. + "/Testing/Temporary/CTestCheckpoint.txt";
  430. cmSystemTools::RemoveFile(fname.c_str());
  431. }
  432. //---------------------------------------------------------
  433. //For ShowOnly mode
  434. void cmCTestMultiProcessHandler::PrintTestList()
  435. {
  436. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  437. int count = 0;
  438. for (PropertiesMap::iterator it = this->Properties.begin();
  439. it != this->Properties.end(); ++it)
  440. {
  441. count++;
  442. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  443. //push working dir
  444. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  445. cmSystemTools::ChangeDirectory(p.Directory.c_str());
  446. cmCTestRunTest testRun(this->TestHandler);
  447. testRun.SetIndex(p.Index);
  448. testRun.SetTestProperties(&p);
  449. testRun.ComputeArguments(); //logs the command in verbose mode
  450. if(p.Labels.size()) //print the labels
  451. {
  452. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Labels:");
  453. }
  454. for(std::vector<std::string>::iterator label = p.Labels.begin();
  455. label != p.Labels.end(); ++label)
  456. {
  457. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " " << *label);
  458. }
  459. if(p.Labels.size()) //print the labels
  460. {
  461. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl);
  462. }
  463. if (this->TestHandler->MemCheck)
  464. {
  465. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Memory Check");
  466. }
  467. else
  468. {
  469. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Test");
  470. }
  471. cmOStringStream indexStr;
  472. indexStr << " #" << p.Index << ":";
  473. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  474. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  475. << indexStr.str().c_str());
  476. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  477. cmCTestLog(this->CTest, HANDLER_OUTPUT, p.Name.c_str() << std::endl);
  478. //pop working dir
  479. cmSystemTools::ChangeDirectory(current_dir.c_str());
  480. }
  481. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl << "Total Tests: "
  482. << this->Total << std::endl);
  483. }
  484. void cmCTestMultiProcessHandler::PrintLabels()
  485. {
  486. std::set<std::string> allLabels;
  487. for (PropertiesMap::iterator it = this->Properties.begin();
  488. it != this->Properties.end(); ++it)
  489. {
  490. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  491. allLabels.insert(p.Labels.begin(), p.Labels.end());
  492. }
  493. if(allLabels.size())
  494. {
  495. cmCTestLog(this->CTest, HANDLER_OUTPUT, "All Labels:" << std::endl);
  496. }
  497. else
  498. {
  499. cmCTestLog(this->CTest, HANDLER_OUTPUT, "No Labels Exist" << std::endl);
  500. }
  501. for(std::set<std::string>::iterator label = allLabels.begin();
  502. label != allLabels.end(); ++label)
  503. {
  504. cmCTestLog(this->CTest, HANDLER_OUTPUT, " " << *label << std::endl);
  505. }
  506. }
  507. //---------------------------------------------------------
  508. void cmCTestMultiProcessHandler::CheckResume()
  509. {
  510. std::string fname = this->CTest->GetBinaryDir()
  511. + "/Testing/Temporary/CTestCheckpoint.txt";
  512. if(this->CTest->GetFailover())
  513. {
  514. if(cmSystemTools::FileExists(fname.c_str(), true))
  515. {
  516. *this->TestHandler->LogFile << "Resuming previously interrupted test set"
  517. << std::endl
  518. << "----------------------------------------------------------"
  519. << std::endl;
  520. std::ifstream fin;
  521. fin.open(fname.c_str());
  522. std::string line;
  523. while(std::getline(fin, line))
  524. {
  525. int index = atoi(line.c_str());
  526. this->RemoveTest(index);
  527. }
  528. fin.close();
  529. }
  530. }
  531. else if(cmSystemTools::FileExists(fname.c_str(), true))
  532. {
  533. cmSystemTools::RemoveFile(fname.c_str());
  534. }
  535. }
  536. //---------------------------------------------------------
  537. void cmCTestMultiProcessHandler::RemoveTest(int index)
  538. {
  539. this->EraseTest(index);
  540. this->Properties.erase(index);
  541. this->TestRunningMap[index] = false;
  542. this->TestFinishMap[index] = true;
  543. this->Completed++;
  544. }
  545. //---------------------------------------------------------
  546. int cmCTestMultiProcessHandler::FindMaxIndex()
  547. {
  548. int max = 0;
  549. cmCTestMultiProcessHandler::TestMap::iterator i = this->Tests.begin();
  550. for(; i != this->Tests.end(); ++i)
  551. {
  552. if(i->first > max)
  553. {
  554. max = i->first;
  555. }
  556. }
  557. return max;
  558. }
  559. //Returns true if no cycles exist in the dependency graph
  560. bool cmCTestMultiProcessHandler::CheckCycles()
  561. {
  562. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  563. "Checking test dependency graph..." << std::endl);
  564. for(TestMap::iterator it = this->Tests.begin();
  565. it != this->Tests.end(); ++it)
  566. {
  567. //DFS from each element to itself
  568. int root = it->first;
  569. std::set<int> visited;
  570. std::stack<int> s;
  571. s.push(root);
  572. while(!s.empty())
  573. {
  574. int test = s.top();
  575. s.pop();
  576. if(visited.insert(test).second)
  577. {
  578. for(TestSet::iterator d = this->Tests[test].begin();
  579. d != this->Tests[test].end(); ++d)
  580. {
  581. if(*d == root)
  582. {
  583. //cycle exists
  584. cmCTestLog(this->CTest, ERROR_MESSAGE,
  585. "Error: a cycle exists in the test dependency graph "
  586. "for the test \"" << this->Properties[root]->Name <<
  587. "\".\nPlease fix the cycle and run ctest again.\n");
  588. return false;
  589. }
  590. else
  591. {
  592. s.push(*d);
  593. }
  594. }
  595. }
  596. }
  597. }
  598. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  599. "Checking test dependency graph end" << std::endl);
  600. return true;
  601. }