cmCTestMultiProcessHandler.cxx 12 KB

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