cmCTestMultiProcessHandler.cxx 14 KB

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