cmCTestMultiProcessHandler.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. // When not running in parallel mode, don't use cost data
  392. if(this->ParallelLevel > 1 &&
  393. this->Properties[index] &&
  394. this->Properties[index]->Cost == 0)
  395. {
  396. this->Properties[index]->Cost = cost;
  397. }
  398. }
  399. // Next part of the file is the failed tests
  400. while(std::getline(fin, line))
  401. {
  402. if(line != "")
  403. {
  404. this->LastTestsFailed.push_back(line);
  405. }
  406. }
  407. fin.close();
  408. }
  409. }
  410. //---------------------------------------------------------
  411. int cmCTestMultiProcessHandler::SearchByName(std::string name)
  412. {
  413. int index = -1;
  414. for(PropertiesMap::iterator i = this->Properties.begin();
  415. i != this->Properties.end(); ++i)
  416. {
  417. if(i->second->Name == name)
  418. {
  419. index = i->first;
  420. }
  421. }
  422. return index;
  423. }
  424. //---------------------------------------------------------
  425. void cmCTestMultiProcessHandler::CreateTestCostList()
  426. {
  427. for(TestMap::iterator i = this->Tests.begin();
  428. i != this->Tests.end(); ++i)
  429. {
  430. SortedTests.push_back(i->first);
  431. //If the test failed last time, it should be run first, so max the cost.
  432. //Only do this for parallel runs; in non-parallel runs, avoid clobbering
  433. //the test's explicitly set cost.
  434. if(this->ParallelLevel > 1 &&
  435. std::find(this->LastTestsFailed.begin(), this->LastTestsFailed.end(),
  436. this->Properties[i->first]->Name) != this->LastTestsFailed.end())
  437. {
  438. this->Properties[i->first]->Cost = FLT_MAX;
  439. }
  440. }
  441. TestComparator comp(this);
  442. std::stable_sort(SortedTests.begin(), SortedTests.end(), comp);
  443. }
  444. //---------------------------------------------------------
  445. void cmCTestMultiProcessHandler::WriteCheckpoint(int index)
  446. {
  447. std::string fname = this->CTest->GetBinaryDir()
  448. + "/Testing/Temporary/CTestCheckpoint.txt";
  449. std::fstream fout;
  450. fout.open(fname.c_str(), std::ios::app | std::ios::out);
  451. fout << index << "\n";
  452. fout.close();
  453. }
  454. //---------------------------------------------------------
  455. void cmCTestMultiProcessHandler::MarkFinished()
  456. {
  457. std::string fname = this->CTest->GetBinaryDir()
  458. + "/Testing/Temporary/CTestCheckpoint.txt";
  459. cmSystemTools::RemoveFile(fname.c_str());
  460. }
  461. //---------------------------------------------------------
  462. //For ShowOnly mode
  463. void cmCTestMultiProcessHandler::PrintTestList()
  464. {
  465. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  466. int count = 0;
  467. for (PropertiesMap::iterator it = this->Properties.begin();
  468. it != this->Properties.end(); ++it)
  469. {
  470. count++;
  471. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  472. //push working dir
  473. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  474. cmSystemTools::ChangeDirectory(p.Directory.c_str());
  475. cmCTestRunTest testRun(this->TestHandler);
  476. testRun.SetIndex(p.Index);
  477. testRun.SetTestProperties(&p);
  478. testRun.ComputeArguments(); //logs the command in verbose mode
  479. if(p.Labels.size()) //print the labels
  480. {
  481. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Labels:");
  482. }
  483. for(std::vector<std::string>::iterator label = p.Labels.begin();
  484. label != p.Labels.end(); ++label)
  485. {
  486. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, " " << *label);
  487. }
  488. if(p.Labels.size()) //print the labels
  489. {
  490. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl);
  491. }
  492. if (this->TestHandler->MemCheck)
  493. {
  494. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Memory Check");
  495. }
  496. else
  497. {
  498. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Test");
  499. }
  500. cmOStringStream indexStr;
  501. indexStr << " #" << p.Index << ":";
  502. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  503. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  504. << indexStr.str().c_str());
  505. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  506. cmCTestLog(this->CTest, HANDLER_OUTPUT, p.Name.c_str() << std::endl);
  507. //pop working dir
  508. cmSystemTools::ChangeDirectory(current_dir.c_str());
  509. }
  510. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl << "Total Tests: "
  511. << this->Total << std::endl);
  512. }
  513. void cmCTestMultiProcessHandler::PrintLabels()
  514. {
  515. std::set<std::string> allLabels;
  516. for (PropertiesMap::iterator it = this->Properties.begin();
  517. it != this->Properties.end(); ++it)
  518. {
  519. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  520. allLabels.insert(p.Labels.begin(), p.Labels.end());
  521. }
  522. if(allLabels.size())
  523. {
  524. cmCTestLog(this->CTest, HANDLER_OUTPUT, "All Labels:" << std::endl);
  525. }
  526. else
  527. {
  528. cmCTestLog(this->CTest, HANDLER_OUTPUT, "No Labels Exist" << std::endl);
  529. }
  530. for(std::set<std::string>::iterator label = allLabels.begin();
  531. label != allLabels.end(); ++label)
  532. {
  533. cmCTestLog(this->CTest, HANDLER_OUTPUT, " " << *label << std::endl);
  534. }
  535. }
  536. //---------------------------------------------------------
  537. void cmCTestMultiProcessHandler::CheckResume()
  538. {
  539. std::string fname = this->CTest->GetBinaryDir()
  540. + "/Testing/Temporary/CTestCheckpoint.txt";
  541. if(this->CTest->GetFailover())
  542. {
  543. if(cmSystemTools::FileExists(fname.c_str(), true))
  544. {
  545. *this->TestHandler->LogFile << "Resuming previously interrupted test set"
  546. << std::endl
  547. << "----------------------------------------------------------"
  548. << std::endl;
  549. std::ifstream fin;
  550. fin.open(fname.c_str());
  551. std::string line;
  552. while(std::getline(fin, line))
  553. {
  554. int index = atoi(line.c_str());
  555. this->RemoveTest(index);
  556. }
  557. fin.close();
  558. }
  559. }
  560. else if(cmSystemTools::FileExists(fname.c_str(), true))
  561. {
  562. cmSystemTools::RemoveFile(fname.c_str());
  563. }
  564. }
  565. //---------------------------------------------------------
  566. void cmCTestMultiProcessHandler::RemoveTest(int index)
  567. {
  568. this->EraseTest(index);
  569. this->Properties.erase(index);
  570. this->TestRunningMap[index] = false;
  571. this->TestFinishMap[index] = true;
  572. this->Completed++;
  573. }
  574. //---------------------------------------------------------
  575. int cmCTestMultiProcessHandler::FindMaxIndex()
  576. {
  577. int max = 0;
  578. cmCTestMultiProcessHandler::TestMap::iterator i = this->Tests.begin();
  579. for(; i != this->Tests.end(); ++i)
  580. {
  581. if(i->first > max)
  582. {
  583. max = i->first;
  584. }
  585. }
  586. return max;
  587. }
  588. //Returns true if no cycles exist in the dependency graph
  589. bool cmCTestMultiProcessHandler::CheckCycles()
  590. {
  591. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  592. "Checking test dependency graph..." << std::endl);
  593. for(TestMap::iterator it = this->Tests.begin();
  594. it != this->Tests.end(); ++it)
  595. {
  596. //DFS from each element to itself
  597. int root = it->first;
  598. std::set<int> visited;
  599. std::stack<int> s;
  600. s.push(root);
  601. while(!s.empty())
  602. {
  603. int test = s.top();
  604. s.pop();
  605. if(visited.insert(test).second)
  606. {
  607. for(TestSet::iterator d = this->Tests[test].begin();
  608. d != this->Tests[test].end(); ++d)
  609. {
  610. if(*d == root)
  611. {
  612. //cycle exists
  613. cmCTestLog(this->CTest, ERROR_MESSAGE,
  614. "Error: a cycle exists in the test dependency graph "
  615. "for the test \"" << this->Properties[root]->Name <<
  616. "\".\nPlease fix the cycle and run ctest again.\n");
  617. return false;
  618. }
  619. else
  620. {
  621. s.push(*d);
  622. }
  623. }
  624. }
  625. }
  626. }
  627. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  628. "Checking test dependency graph end" << std::endl);
  629. return true;
  630. }