cmCTestMultiProcessHandler.cxx 17 KB

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