cmCTestMultiProcessHandler.cxx 12 KB

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