cmCTestMultiProcessHandler.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. cmCTestMultiProcessHandler::cmCTestMultiProcessHandler()
  18. {
  19. this->ParallelLevel = 1;
  20. this->Completed = 0;
  21. this->RunningCount = 0;
  22. }
  23. cmCTestMultiProcessHandler::~cmCTestMultiProcessHandler()
  24. {
  25. }
  26. // Set the tests
  27. void
  28. cmCTestMultiProcessHandler::SetTests(TestMap& tests,
  29. PropertiesMap& properties)
  30. {
  31. this->Tests = tests;
  32. this->Properties = properties;
  33. this->Total = this->Tests.size();
  34. // set test run map to false for all
  35. for(TestMap::iterator i = this->Tests.begin();
  36. i != this->Tests.end(); ++i)
  37. {
  38. this->TestRunningMap[i->first] = false;
  39. this->TestFinishMap[i->first] = false;
  40. }
  41. this->ReadCostData();
  42. this->CreateTestCostList();
  43. }
  44. // Set the max number of tests that can be run at the same time.
  45. void cmCTestMultiProcessHandler::SetParallelLevel(size_t level)
  46. {
  47. this->ParallelLevel = level < 1 ? 1 : level;
  48. }
  49. //---------------------------------------------------------
  50. void cmCTestMultiProcessHandler::RunTests()
  51. {
  52. this->CheckResume();
  53. if(!this->CheckCycles())
  54. {
  55. return;
  56. }
  57. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  58. this->StartNextTests();
  59. while(this->Tests.size() != 0)
  60. {
  61. this->CheckOutput();
  62. this->StartNextTests();
  63. }
  64. // let all running tests finish
  65. while(this->CheckOutput())
  66. {
  67. }
  68. this->MarkFinished();
  69. }
  70. //---------------------------------------------------------
  71. void cmCTestMultiProcessHandler::StartTestProcess(int test)
  72. {
  73. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "test " << test << "\n");
  74. this->TestRunningMap[test] = true; // mark the test as running
  75. // now remove the test itself
  76. this->EraseTest(test);
  77. cmCTestRunTest* testRun = new cmCTestRunTest(this->TestHandler);
  78. testRun->SetIndex(test);
  79. testRun->SetTestProperties(this->Properties[test]);
  80. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  81. cmSystemTools::ChangeDirectory(this->Properties[test]->Directory.c_str());
  82. if(testRun->StartTest(this->Total))
  83. {
  84. this->RunningTests.insert(testRun);
  85. }
  86. else
  87. {
  88. this->Completed++;
  89. this->TestFinishMap[test] = true;
  90. this->TestRunningMap[test] = false;
  91. this->RunningCount -= GetProcessorsUsed(test);
  92. testRun->EndTest(this->Completed, this->Total, false);
  93. this->Failed->push_back(this->Properties[test]->Name);
  94. }
  95. cmSystemTools::ChangeDirectory(current_dir.c_str());
  96. }
  97. //---------------------------------------------------------
  98. void cmCTestMultiProcessHandler::EraseTest(int test)
  99. {
  100. this->Tests.erase(test);
  101. for(TestCostMap::iterator i = this->TestCosts.begin();
  102. i != this->TestCosts.end(); ++i)
  103. {
  104. if(i->second.find(test) != i->second.end())
  105. {
  106. i->second.erase(test);
  107. return;
  108. }
  109. }
  110. }
  111. //---------------------------------------------------------
  112. inline size_t cmCTestMultiProcessHandler::GetProcessorsUsed(int test)
  113. {
  114. size_t processors =
  115. static_cast<int>(this->Properties[test]->Processors);
  116. //If this is set to run serially, it must run alone.
  117. //Also, if processors setting is set higher than the -j
  118. //setting, we default to using all of the process slots.
  119. if(this->Properties[test]->RunSerial
  120. || processors > this->ParallelLevel)
  121. {
  122. processors = this->ParallelLevel;
  123. }
  124. return processors;
  125. }
  126. //---------------------------------------------------------
  127. bool cmCTestMultiProcessHandler::StartTest(int test)
  128. {
  129. // copy the depend tests locally because when
  130. // a test is finished it will be removed from the depend list
  131. // and we don't want to be iterating a list while removing from it
  132. TestSet depends = this->Tests[test];
  133. size_t totalDepends = depends.size();
  134. if(totalDepends)
  135. {
  136. for(TestSet::const_iterator i = depends.begin();
  137. i != depends.end(); ++i)
  138. {
  139. // if the test is not already running then start it
  140. if(!this->TestRunningMap[*i])
  141. {
  142. // this test might be finished, but since
  143. // this is a copy of the depend map we might
  144. // still have it
  145. if(!this->TestFinishMap[*i])
  146. {
  147. // only start one test in this function
  148. return this->StartTest(*i);
  149. }
  150. else
  151. {
  152. // the depend has been and finished
  153. totalDepends--;
  154. }
  155. }
  156. }
  157. }
  158. // if there are no depends left then run this test
  159. if(totalDepends == 0)
  160. {
  161. this->StartTestProcess(test);
  162. return true;
  163. }
  164. // This test was not able to start because it is waiting
  165. // on depends to run
  166. return false;
  167. }
  168. //---------------------------------------------------------
  169. void cmCTestMultiProcessHandler::StartNextTests()
  170. {
  171. size_t numToStart = this->ParallelLevel - this->RunningCount;
  172. if(numToStart == 0)
  173. {
  174. return;
  175. }
  176. for(TestCostMap::reverse_iterator i = this->TestCosts.rbegin();
  177. i != this->TestCosts.rend(); ++i)
  178. {
  179. TestSet tests = i->second; //copy the test set
  180. for(TestSet::iterator test = tests.begin();
  181. test != tests.end(); ++test)
  182. {
  183. size_t processors = GetProcessorsUsed(*test);
  184. if(processors > numToStart)
  185. {
  186. return;
  187. }
  188. if(this->StartTest(*test))
  189. {
  190. numToStart -= processors;
  191. this->RunningCount += processors;
  192. }
  193. else
  194. {
  195. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  196. << "Test did not start waiting on depends to finish: "
  197. << *test << "\n");
  198. }
  199. if(numToStart == 0)
  200. {
  201. return;
  202. }
  203. }
  204. }
  205. }
  206. //---------------------------------------------------------
  207. bool cmCTestMultiProcessHandler::CheckOutput()
  208. {
  209. // no more output we are done
  210. if(this->RunningTests.size() == 0)
  211. {
  212. return false;
  213. }
  214. std::vector<cmCTestRunTest*> finished;
  215. std::string out, err;
  216. for(std::set<cmCTestRunTest*>::const_iterator i = this->RunningTests.begin();
  217. i != this->RunningTests.end(); ++i)
  218. {
  219. cmCTestRunTest* p = *i;
  220. if(!p->CheckOutput())
  221. {
  222. finished.push_back(p);
  223. }
  224. }
  225. for( std::vector<cmCTestRunTest*>::iterator i = finished.begin();
  226. i != finished.end(); ++i)
  227. {
  228. this->Completed++;
  229. cmCTestRunTest* p = *i;
  230. int test = p->GetIndex();
  231. if(p->EndTest(this->Completed, this->Total, true))
  232. {
  233. this->Passed->push_back(p->GetTestProperties()->Name);
  234. }
  235. else
  236. {
  237. this->Failed->push_back(p->GetTestProperties()->Name);
  238. }
  239. for(TestMap::iterator j = this->Tests.begin();
  240. j != this->Tests.end(); ++j)
  241. {
  242. j->second.erase(test);
  243. }
  244. this->TestFinishMap[test] = true;
  245. this->TestRunningMap[test] = false;
  246. this->RunningTests.erase(p);
  247. this->WriteCheckpoint(test);
  248. this->WriteCostData(test, static_cast<float>(
  249. p->GetTestResults().ExecutionTime));
  250. this->RunningCount -= GetProcessorsUsed(test);
  251. delete p;
  252. }
  253. return true;
  254. }
  255. //---------------------------------------------------------
  256. void cmCTestMultiProcessHandler::ReadCostData()
  257. {
  258. std::string fname = this->CTest->GetBinaryDir()
  259. + "/Testing/Temporary/CTestCostData.txt";
  260. if(cmSystemTools::FileExists(fname.c_str(), true)
  261. && this->ParallelLevel > 1)
  262. {
  263. std::ifstream fin;
  264. fin.open(fname.c_str());
  265. std::string line;
  266. while(std::getline(fin, line))
  267. {
  268. std::vector<cmsys::String> parts =
  269. cmSystemTools::SplitString(line.c_str(), ' ');
  270. int index = atoi(parts[0].c_str());
  271. float cost = static_cast<float>(atof(parts[1].c_str()));
  272. if(this->Properties[index] && this->Properties[index]->Cost == 0)
  273. {
  274. this->Properties[index]->Cost = cost;
  275. }
  276. }
  277. fin.close();
  278. }
  279. cmSystemTools::RemoveFile(fname.c_str());
  280. }
  281. //---------------------------------------------------------
  282. void cmCTestMultiProcessHandler::CreateTestCostList()
  283. {
  284. for(TestMap::iterator i = this->Tests.begin();
  285. i != this->Tests.end(); ++i)
  286. {
  287. this->TestCosts[this->Properties[i->first]->Cost].insert(i->first);
  288. }
  289. }
  290. //---------------------------------------------------------
  291. void cmCTestMultiProcessHandler::WriteCostData(int index, float cost)
  292. {
  293. std::string fname = this->CTest->GetBinaryDir()
  294. + "/Testing/Temporary/CTestCostData.txt";
  295. std::fstream fout;
  296. fout.open(fname.c_str(), std::ios::app);
  297. fout << index << " " << cost << "\n";
  298. fout.close();
  299. }
  300. //---------------------------------------------------------
  301. void cmCTestMultiProcessHandler::WriteCheckpoint(int index)
  302. {
  303. std::string fname = this->CTest->GetBinaryDir()
  304. + "/Testing/Temporary/CTestCheckpoint.txt";
  305. std::fstream fout;
  306. fout.open(fname.c_str(), std::ios::app);
  307. fout << index << "\n";
  308. fout.close();
  309. }
  310. //---------------------------------------------------------
  311. void cmCTestMultiProcessHandler::MarkFinished()
  312. {
  313. std::string fname = this->CTest->GetBinaryDir()
  314. + "/Testing/Temporary/CTestCheckpoint.txt";
  315. cmSystemTools::RemoveFile(fname.c_str());
  316. }
  317. //---------------------------------------------------------
  318. //For ShowOnly mode
  319. void cmCTestMultiProcessHandler::PrintTestList()
  320. {
  321. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  322. int count = 0;
  323. for (PropertiesMap::iterator it = this->Properties.begin();
  324. it != this->Properties.end(); ++it)
  325. {
  326. count++;
  327. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  328. //push working dir
  329. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  330. cmSystemTools::ChangeDirectory(p.Directory.c_str());
  331. cmCTestRunTest testRun(this->TestHandler);
  332. testRun.SetIndex(p.Index);
  333. testRun.SetTestProperties(&p);
  334. testRun.ComputeArguments(); //logs the command in verbose mode
  335. if (this->TestHandler->MemCheck)
  336. {
  337. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Memory Check");
  338. }
  339. else
  340. {
  341. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Test");
  342. }
  343. cmOStringStream indexStr;
  344. indexStr << " #" << p.Index << ":";
  345. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  346. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  347. << indexStr.str().c_str());
  348. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  349. cmCTestLog(this->CTest, HANDLER_OUTPUT, p.Name.c_str() << std::endl);
  350. //pop working dir
  351. cmSystemTools::ChangeDirectory(current_dir.c_str());
  352. }
  353. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl << "Total Tests: "
  354. << this->Total << std::endl);
  355. }
  356. //---------------------------------------------------------
  357. void cmCTestMultiProcessHandler::CheckResume()
  358. {
  359. std::string fname = this->CTest->GetBinaryDir()
  360. + "/Testing/Temporary/CTestCheckpoint.txt";
  361. if(this->CTest->GetFailover())
  362. {
  363. if(cmSystemTools::FileExists(fname.c_str(), true))
  364. {
  365. *this->TestHandler->LogFile << "Resuming previously interrupted test set"
  366. << std::endl
  367. << "----------------------------------------------------------"
  368. << std::endl;
  369. std::ifstream fin;
  370. fin.open(fname.c_str());
  371. std::string line;
  372. while(std::getline(fin, line))
  373. {
  374. int index = atoi(line.c_str());
  375. this->RemoveTest(index);
  376. }
  377. fin.close();
  378. }
  379. }
  380. else if(cmSystemTools::FileExists(fname.c_str(), true))
  381. {
  382. cmSystemTools::RemoveFile(fname.c_str());
  383. }
  384. }
  385. //---------------------------------------------------------
  386. void cmCTestMultiProcessHandler::RemoveTest(int index)
  387. {
  388. this->EraseTest(index);
  389. this->Properties.erase(index);
  390. this->TestRunningMap[index] = false;
  391. this->TestFinishMap[index] = true;
  392. this->Completed++;
  393. }
  394. //---------------------------------------------------------
  395. int cmCTestMultiProcessHandler::FindMaxIndex()
  396. {
  397. int max = 0;
  398. cmCTestMultiProcessHandler::TestMap::iterator i = this->Tests.begin();
  399. for(; i != this->Tests.end(); ++i)
  400. {
  401. if(i->first > max)
  402. {
  403. max = i->first;
  404. }
  405. }
  406. return max;
  407. }
  408. //Returns true if no cycles exist in the dependency graph
  409. bool cmCTestMultiProcessHandler::CheckCycles()
  410. {
  411. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  412. "Checking test dependency graph..." << std::endl);
  413. for(TestMap::iterator it = this->Tests.begin();
  414. it != this->Tests.end(); ++it)
  415. {
  416. //DFS from each element to itself
  417. std::stack<int> s;
  418. std::vector<int> visited;
  419. s.push(it->first);
  420. visited.push_back(it->first);
  421. while(!s.empty())
  422. {
  423. int test = s.top();
  424. s.pop();
  425. for(TestSet::iterator d = this->Tests[test].begin();
  426. d != this->Tests[test].end(); ++d)
  427. {
  428. s.push(*d);
  429. for(std::vector<int>::iterator v = visited.begin();
  430. v != visited.end(); ++v)
  431. {
  432. if(*v == *d)
  433. {
  434. //cycle exists
  435. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error: a cycle exists in "
  436. "the test dependency graph for the test \""
  437. << this->Properties[*d]->Name << "\"." << std::endl
  438. << "Please fix the cycle and run ctest again." << std::endl);
  439. return false;
  440. }
  441. }
  442. visited.push_back(*d);
  443. }
  444. visited.pop_back();
  445. }
  446. }
  447. return true;
  448. }