cmCTestMultiProcessHandler.cxx 12 KB

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