cmCTestMultiProcessHandler.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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(totalDepends)
  193. {
  194. for(TestSet::const_iterator i = depends.begin();
  195. i != depends.end(); ++i)
  196. {
  197. // if the test is not already running then start it
  198. if(!this->TestRunningMap[*i])
  199. {
  200. // this test might be finished, but since
  201. // this is a copy of the depend map we might
  202. // still have it
  203. if(!this->TestFinishMap[*i])
  204. {
  205. // only start one test in this function
  206. return this->StartTest(*i);
  207. }
  208. else
  209. {
  210. // the depend has been and finished
  211. totalDepends--;
  212. }
  213. }
  214. }
  215. }
  216. // if there are no depends left then run this test
  217. if(totalDepends == 0)
  218. {
  219. this->StartTestProcess(test);
  220. return true;
  221. }
  222. // This test was not able to start because it is waiting
  223. // on depends to run
  224. return false;
  225. }
  226. //---------------------------------------------------------
  227. void cmCTestMultiProcessHandler::StartNextTests()
  228. {
  229. size_t numToStart = this->ParallelLevel - this->RunningCount;
  230. if(numToStart == 0)
  231. {
  232. return;
  233. }
  234. TestList copy = this->SortedTests;
  235. for(TestList::iterator test = copy.begin(); test != copy.end(); ++test)
  236. {
  237. //in case this test has already been started due to dependency
  238. if(this->TestRunningMap[*test] || this->TestFinishMap[*test])
  239. {
  240. continue;
  241. }
  242. size_t processors = GetProcessorsUsed(*test);
  243. if(processors > numToStart)
  244. {
  245. return;
  246. }
  247. if(this->StartTest(*test))
  248. {
  249. if(this->StopTimePassed)
  250. {
  251. return;
  252. }
  253. numToStart -= processors;
  254. }
  255. if(numToStart == 0)
  256. {
  257. return;
  258. }
  259. }
  260. }
  261. //---------------------------------------------------------
  262. bool cmCTestMultiProcessHandler::CheckOutput()
  263. {
  264. // no more output we are done
  265. if(this->RunningTests.size() == 0)
  266. {
  267. return false;
  268. }
  269. std::vector<cmCTestRunTest*> finished;
  270. std::string out, err;
  271. for(std::set<cmCTestRunTest*>::const_iterator i = this->RunningTests.begin();
  272. i != this->RunningTests.end(); ++i)
  273. {
  274. cmCTestRunTest* p = *i;
  275. if(!p->CheckOutput())
  276. {
  277. finished.push_back(p);
  278. }
  279. }
  280. for( std::vector<cmCTestRunTest*>::iterator i = finished.begin();
  281. i != finished.end(); ++i)
  282. {
  283. this->Completed++;
  284. cmCTestRunTest* p = *i;
  285. int test = p->GetIndex();
  286. if(p->EndTest(this->Completed, this->Total, true))
  287. {
  288. this->Passed->push_back(p->GetTestProperties()->Name);
  289. }
  290. else
  291. {
  292. this->Failed->push_back(p->GetTestProperties()->Name);
  293. }
  294. for(TestMap::iterator j = this->Tests.begin();
  295. j != this->Tests.end(); ++j)
  296. {
  297. j->second.erase(test);
  298. }
  299. this->TestFinishMap[test] = true;
  300. this->TestRunningMap[test] = false;
  301. this->RunningTests.erase(p);
  302. this->WriteCheckpoint(test);
  303. this->UnlockResources(test);
  304. this->RunningCount -= GetProcessorsUsed(test);
  305. delete p;
  306. }
  307. return true;
  308. }
  309. //---------------------------------------------------------
  310. void cmCTestMultiProcessHandler::UpdateCostData()
  311. {
  312. std::string fname = this->CTest->GetCostDataFile();
  313. std::string tmpout = fname + ".tmp";
  314. std::fstream fout;
  315. fout.open(tmpout.c_str(), std::ios::out);
  316. PropertiesMap temp = this->Properties;
  317. if(cmSystemTools::FileExists(fname.c_str()))
  318. {
  319. std::ifstream fin;
  320. fin.open(fname.c_str());
  321. std::string line;
  322. while(std::getline(fin, line))
  323. {
  324. if(line == "---") break;
  325. std::vector<cmsys::String> parts =
  326. cmSystemTools::SplitString(line.c_str(), ' ');
  327. //Format: <name> <previous_runs> <avg_cost>
  328. if(parts.size() < 3) break;
  329. std::string name = parts[0];
  330. int prev = atoi(parts[1].c_str());
  331. float cost = static_cast<float>(atof(parts[2].c_str()));
  332. int index = this->SearchByName(name);
  333. if(index == -1)
  334. {
  335. // This test is not in memory. We just rewrite the entry
  336. fout << name << " " << prev << " " << cost << "\n";
  337. }
  338. else
  339. {
  340. // Update with our new average cost
  341. fout << name << " " << this->Properties[index]->PreviousRuns << " "
  342. << this->Properties[index]->Cost << "\n";
  343. temp.erase(index);
  344. }
  345. }
  346. fin.close();
  347. cmSystemTools::RemoveFile(fname.c_str());
  348. }
  349. // Add all tests not previously listed in the file
  350. for(PropertiesMap::iterator i = temp.begin(); i != temp.end(); ++i)
  351. {
  352. fout << i->second->Name << " " << i->second->PreviousRuns << " "
  353. << i->second->Cost << "\n";
  354. }
  355. // Write list of failed tests
  356. fout << "---\n";
  357. for(std::vector<cmStdString>::iterator i = this->Failed->begin();
  358. i != this->Failed->end(); ++i)
  359. {
  360. fout << i->c_str() << "\n";
  361. }
  362. fout.close();
  363. cmSystemTools::RenameFile(tmpout.c_str(), fname.c_str());
  364. }
  365. //---------------------------------------------------------
  366. void cmCTestMultiProcessHandler::ReadCostData()
  367. {
  368. std::string fname = this->CTest->GetCostDataFile();
  369. if(cmSystemTools::FileExists(fname.c_str(), true))
  370. {
  371. std::ifstream fin;
  372. fin.open(fname.c_str());
  373. std::string line;
  374. while(std::getline(fin, line))
  375. {
  376. if(line == "---") break;
  377. std::vector<cmsys::String> parts =
  378. cmSystemTools::SplitString(line.c_str(), ' ');
  379. // Probably an older version of the file, will be fixed next run
  380. if(parts.size() < 3)
  381. {
  382. fin.close();
  383. return;
  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) continue;
  390. this->Properties[index]->PreviousRuns = prev;
  391. if(this->Properties[index] && this->Properties[index]->Cost == 0)
  392. {
  393. this->Properties[index]->Cost = cost;
  394. }
  395. }
  396. // Next part of the file is the failed tests
  397. while(std::getline(fin, line))
  398. {
  399. if(line != "")
  400. {
  401. this->LastTestsFailed.push_back(line);
  402. }
  403. }
  404. fin.close();
  405. }
  406. }
  407. //---------------------------------------------------------
  408. int cmCTestMultiProcessHandler::SearchByName(std::string name)
  409. {
  410. int index = -1;
  411. for(PropertiesMap::iterator i = this->Properties.begin();
  412. i != this->Properties.end(); ++i)
  413. {
  414. if(i->second->Name == name)
  415. {
  416. index = i->first;
  417. }
  418. }
  419. return index;
  420. }
  421. //---------------------------------------------------------
  422. void cmCTestMultiProcessHandler::CreateTestCostList()
  423. {
  424. for(TestMap::iterator i = this->Tests.begin();
  425. i != this->Tests.end(); ++i)
  426. {
  427. SortedTests.push_back(i->first);
  428. //If the test failed last time, it should be run first, so max the cost
  429. if(std::find(this->LastTestsFailed.begin(),
  430. this->LastTestsFailed.end(),
  431. this->Properties[i->first]->Name)
  432. != this->LastTestsFailed.end())
  433. {
  434. this->Properties[i->first]->Cost = FLT_MAX;
  435. }
  436. }
  437. if(this->ParallelLevel > 1)
  438. {
  439. TestComparator comp(this);
  440. std::sort(SortedTests.begin(), SortedTests.end(), comp);
  441. }
  442. }
  443. //---------------------------------------------------------
  444. void cmCTestMultiProcessHandler::WriteCheckpoint(int index)
  445. {
  446. std::string fname = this->CTest->GetBinaryDir()
  447. + "/Testing/Temporary/CTestCheckpoint.txt";
  448. std::fstream fout;
  449. fout.open(fname.c_str(), std::ios::app | std::ios::out);
  450. fout << index << "\n";
  451. fout.close();
  452. }
  453. //---------------------------------------------------------
  454. void cmCTestMultiProcessHandler::MarkFinished()
  455. {
  456. std::string fname = this->CTest->GetBinaryDir()
  457. + "/Testing/Temporary/CTestCheckpoint.txt";
  458. cmSystemTools::RemoveFile(fname.c_str());
  459. }
  460. //---------------------------------------------------------
  461. //For ShowOnly mode
  462. void cmCTestMultiProcessHandler::PrintTestList()
  463. {
  464. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  465. int count = 0;
  466. for (PropertiesMap::iterator it = this->Properties.begin();
  467. it != this->Properties.end(); ++it)
  468. {
  469. count++;
  470. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  471. //push working dir
  472. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  473. cmSystemTools::ChangeDirectory(p.Directory.c_str());
  474. cmCTestRunTest testRun(this->TestHandler);
  475. testRun.SetIndex(p.Index);
  476. testRun.SetTestProperties(&p);
  477. testRun.ComputeArguments(); //logs the command in verbose mode
  478. if(p.Labels.size()) //print the labels
  479. {
  480. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Labels:");
  481. }
  482. for(std::vector<std::string>::iterator label = p.Labels.begin();
  483. label != p.Labels.end(); ++label)
  484. {
  485. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " " << *label);
  486. }
  487. if(p.Labels.size()) //print the labels
  488. {
  489. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl);
  490. }
  491. if (this->TestHandler->MemCheck)
  492. {
  493. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Memory Check");
  494. }
  495. else
  496. {
  497. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Test");
  498. }
  499. cmOStringStream indexStr;
  500. indexStr << " #" << p.Index << ":";
  501. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  502. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  503. << indexStr.str().c_str());
  504. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  505. cmCTestLog(this->CTest, HANDLER_OUTPUT, p.Name.c_str() << std::endl);
  506. //pop working dir
  507. cmSystemTools::ChangeDirectory(current_dir.c_str());
  508. }
  509. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl << "Total Tests: "
  510. << this->Total << std::endl);
  511. }
  512. void cmCTestMultiProcessHandler::PrintLabels()
  513. {
  514. std::set<std::string> allLabels;
  515. for (PropertiesMap::iterator it = this->Properties.begin();
  516. it != this->Properties.end(); ++it)
  517. {
  518. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  519. allLabels.insert(p.Labels.begin(), p.Labels.end());
  520. }
  521. if(allLabels.size())
  522. {
  523. cmCTestLog(this->CTest, HANDLER_OUTPUT, "All Labels:" << std::endl);
  524. }
  525. else
  526. {
  527. cmCTestLog(this->CTest, HANDLER_OUTPUT, "No Labels Exist" << std::endl);
  528. }
  529. for(std::set<std::string>::iterator label = allLabels.begin();
  530. label != allLabels.end(); ++label)
  531. {
  532. cmCTestLog(this->CTest, HANDLER_OUTPUT, " " << *label << std::endl);
  533. }
  534. }
  535. //---------------------------------------------------------
  536. void cmCTestMultiProcessHandler::CheckResume()
  537. {
  538. std::string fname = this->CTest->GetBinaryDir()
  539. + "/Testing/Temporary/CTestCheckpoint.txt";
  540. if(this->CTest->GetFailover())
  541. {
  542. if(cmSystemTools::FileExists(fname.c_str(), true))
  543. {
  544. *this->TestHandler->LogFile << "Resuming previously interrupted test set"
  545. << std::endl
  546. << "----------------------------------------------------------"
  547. << std::endl;
  548. std::ifstream fin;
  549. fin.open(fname.c_str());
  550. std::string line;
  551. while(std::getline(fin, line))
  552. {
  553. int index = atoi(line.c_str());
  554. this->RemoveTest(index);
  555. }
  556. fin.close();
  557. }
  558. }
  559. else if(cmSystemTools::FileExists(fname.c_str(), true))
  560. {
  561. cmSystemTools::RemoveFile(fname.c_str());
  562. }
  563. }
  564. //---------------------------------------------------------
  565. void cmCTestMultiProcessHandler::RemoveTest(int index)
  566. {
  567. this->EraseTest(index);
  568. this->Properties.erase(index);
  569. this->TestRunningMap[index] = false;
  570. this->TestFinishMap[index] = true;
  571. this->Completed++;
  572. }
  573. //---------------------------------------------------------
  574. int cmCTestMultiProcessHandler::FindMaxIndex()
  575. {
  576. int max = 0;
  577. cmCTestMultiProcessHandler::TestMap::iterator i = this->Tests.begin();
  578. for(; i != this->Tests.end(); ++i)
  579. {
  580. if(i->first > max)
  581. {
  582. max = i->first;
  583. }
  584. }
  585. return max;
  586. }
  587. //Returns true if no cycles exist in the dependency graph
  588. bool cmCTestMultiProcessHandler::CheckCycles()
  589. {
  590. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  591. "Checking test dependency graph..." << std::endl);
  592. for(TestMap::iterator it = this->Tests.begin();
  593. it != this->Tests.end(); ++it)
  594. {
  595. //DFS from each element to itself
  596. std::stack<int> s;
  597. std::vector<int> visited;
  598. s.push(it->first);
  599. while(!s.empty())
  600. {
  601. int test = s.top();
  602. s.pop();
  603. for(TestSet::iterator d = this->Tests[test].begin();
  604. d != this->Tests[test].end(); ++d)
  605. {
  606. if(std::find(visited.begin(), visited.end(), *d) != visited.end())
  607. {
  608. //cycle exists
  609. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error: a cycle exists in "
  610. "the test dependency graph for the test \""
  611. << this->Properties[it->first]->Name << "\"." << std::endl
  612. << "Please fix the cycle and run ctest again." << std::endl);
  613. return false;
  614. }
  615. s.push(*d);
  616. }
  617. visited.push_back(test);
  618. }
  619. }
  620. return true;
  621. }