cmCTestMultiProcessHandler.cxx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. }
  24. // Set the tests
  25. void
  26. cmCTestMultiProcessHandler::SetTests(TestMap& tests,
  27. TestMap& expensiveTests,
  28. PropertiesMap& properties)
  29. {
  30. // set test run map to false for all
  31. for(TestMap::iterator i = this->Tests.begin();
  32. i != this->Tests.end(); ++i)
  33. {
  34. this->TestRunningMap[i->first] = false;
  35. this->TestFinishMap[i->first] = false;
  36. if(this->Properties[i->first]->Expensive)
  37. {
  38. this->ExpensiveTests[i->first] = i->second;
  39. }
  40. }
  41. this->Tests = tests;
  42. this->ExpensiveTests = expensiveTests;
  43. this->Properties = properties;
  44. this->Total = this->Tests.size();
  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. void cmCTestMultiProcessHandler::RunTests()
  52. {
  53. if(this->CTest->GetBatchJobs())
  54. {
  55. this->SubmitBatchTests();
  56. return;
  57. }
  58. this->CheckResume();
  59. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  60. this->StartNextTests();
  61. while(this->Tests.size() != 0 || this->ExpensiveTests.size() != 0)
  62. {
  63. this->CheckOutput();
  64. this->StartNextTests();
  65. }
  66. // let all running tests finish
  67. while(this->CheckOutput())
  68. {
  69. }
  70. this->MarkFinished();
  71. }
  72. void cmCTestMultiProcessHandler::SubmitBatchTests()
  73. {
  74. for(cmCTest::CTestConfigurationMap::iterator i =
  75. this->CTest->CTestConfiguration.begin();
  76. i != this->CTest->CTestConfiguration.end(); ++i)
  77. {
  78. cmCTestLog(this->CTest, HANDLER_OUTPUT, i->first
  79. << " = " << i->second << std::endl);
  80. }
  81. }
  82. void cmCTestMultiProcessHandler::StartTestProcess(int test)
  83. {
  84. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, test << ": "
  85. << " test " << test << "\n");
  86. this->TestRunningMap[test] = true; // mark the test as running
  87. // now remove the test itself
  88. if(this->ExpensiveTests.size() > 0)
  89. {
  90. this->ExpensiveTests.erase(test);
  91. }
  92. this->Tests.erase(test);
  93. cmCTestRunTest* testRun = new cmCTestRunTest;
  94. testRun->SetCTest(this->CTest);
  95. testRun->SetTestHandler(this->TestHandler);
  96. testRun->SetIndex(test);
  97. testRun->SetTestProperties(this->Properties[test]);
  98. if(testRun->StartTest())
  99. {
  100. this->RunningTests.insert(testRun);
  101. }
  102. else
  103. {
  104. this->Completed++;
  105. testRun->EndTest(this->Completed, this->Total, false);
  106. }
  107. }
  108. bool cmCTestMultiProcessHandler::StartTest(int test)
  109. {
  110. // copy the depend tests locally because when
  111. // a test is finished it will be removed from the depend list
  112. // and we don't want to be iterating a list while removing from it
  113. TestSet depends = this->Tests[test];
  114. size_t totalDepends = depends.size();
  115. if(totalDepends)
  116. {
  117. for(TestSet::const_iterator i = depends.begin();
  118. i != depends.end(); ++i)
  119. {
  120. // if the test is not already running then start it
  121. if(!this->TestRunningMap[*i])
  122. {
  123. // this test might be finished, but since
  124. // this is a copy of the depend map we might
  125. // still have it
  126. if(!this->TestFinishMap[*i])
  127. {
  128. // only start one test in this function
  129. return this->StartTest(*i);
  130. }
  131. else
  132. {
  133. // the depend has been and finished
  134. totalDepends--;
  135. }
  136. }
  137. }
  138. }
  139. // if there are no depends left then run this test
  140. if(totalDepends == 0)
  141. {
  142. this->StartTestProcess(test);
  143. return true;
  144. }
  145. // This test was not able to start because it is waiting
  146. // on depends to run
  147. return false;
  148. }
  149. void cmCTestMultiProcessHandler::StartNextTests()
  150. {
  151. size_t numToStart = this->ParallelLevel - this->RunningTests.size();
  152. if(numToStart == 0)
  153. {
  154. return;
  155. }
  156. TestMap tests = this->ExpensiveTests.size() > 0 ?
  157. this->ExpensiveTests : this->Tests;
  158. for(TestMap::iterator i = tests.begin();
  159. i != tests.end(); ++i)
  160. {
  161. //int processors = this->Properties[i->first]->Processors;
  162. // if(processors > )
  163. // start test should start only one test
  164. if(this->StartTest(i->first))
  165. {
  166. numToStart--;
  167. }
  168. else
  169. {
  170. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  171. << "Test did not start waiting on depends to finish: "
  172. << i->first << "\n");
  173. }
  174. if(numToStart == 0 )
  175. {
  176. return;
  177. }
  178. }
  179. }
  180. bool cmCTestMultiProcessHandler::CheckOutput()
  181. {
  182. // no more output we are done
  183. if(this->RunningTests.size() == 0)
  184. {
  185. return false;
  186. }
  187. std::vector<cmCTestRunTest*> finished;
  188. std::string out, err;
  189. for(std::set<cmCTestRunTest*>::const_iterator i = this->RunningTests.begin();
  190. i != this->RunningTests.end(); ++i)
  191. {
  192. cmCTestRunTest* p = *i;
  193. p->CheckOutput(); //reads and stores the process output
  194. if(!p->IsRunning())
  195. {
  196. finished.push_back(p);
  197. }
  198. }
  199. for( std::vector<cmCTestRunTest*>::iterator i = finished.begin();
  200. i != finished.end(); ++i)
  201. {
  202. this->Completed++;
  203. cmCTestRunTest* p = *i;
  204. int test = p->GetIndex();
  205. if(p->EndTest(this->Completed, this->Total, true))
  206. {
  207. this->Passed->push_back(p->GetTestProperties()->Name);
  208. }
  209. else
  210. {
  211. this->Failed->push_back(p->GetTestProperties()->Name);
  212. }
  213. for(TestMap::iterator j = this->ExpensiveTests.begin();
  214. j != this->ExpensiveTests.end(); ++j)
  215. {
  216. j->second.erase(test);
  217. }
  218. for(TestMap::iterator j = this->Tests.begin();
  219. j != this->Tests.end(); ++j)
  220. {
  221. j->second.erase(test);
  222. }
  223. this->TestFinishMap[test] = true;
  224. this->TestRunningMap[test] = false;
  225. this->RunningTests.erase(p);
  226. this->WriteCheckpoint(test);
  227. delete p;
  228. }
  229. return true;
  230. }
  231. void cmCTestMultiProcessHandler::WriteCheckpoint(int index)
  232. {
  233. std::string fname = this->CTest->GetBinaryDir()
  234. + "/Testing/Temporary/CTestCheckpoint.txt";
  235. std::fstream fout;
  236. fout.open(fname.c_str(), std::ios::app);
  237. fout << index << "\n";
  238. fout.close();
  239. }
  240. void cmCTestMultiProcessHandler::MarkFinished()
  241. {
  242. std::string fname = this->CTest->GetBinaryDir()
  243. + "/Testing/Temporary/CTestCheckpoint.txt";
  244. cmSystemTools::RemoveFile(fname.c_str());
  245. }
  246. //---------------------------------------------------------------------
  247. //For ShowOnly mode
  248. void cmCTestMultiProcessHandler::PrintTestList()
  249. {
  250. int count = 0;
  251. for (PropertiesMap::iterator it = this->Properties.begin();
  252. it != this->Properties.end(); it ++ )
  253. {
  254. count++;
  255. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  256. cmCTestRunTest testRun;
  257. testRun.SetCTest(this->CTest);
  258. testRun.SetTestHandler(this->TestHandler);
  259. testRun.SetIndex(p.Index);
  260. testRun.SetTestProperties(&p);
  261. testRun.ComputeArguments(); //logs the command in verbose mode
  262. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3)
  263. << count << "/");
  264. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::setw(3)
  265. << this->Total << " ");
  266. if (this->TestHandler->MemCheck)
  267. {
  268. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory Check");
  269. }
  270. else
  271. {
  272. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Testing");
  273. }
  274. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  275. cmCTestLog(this->CTest, HANDLER_OUTPUT, p.Name.c_str() << std::endl);
  276. }
  277. }
  278. //----------------------------------------------------------------
  279. void cmCTestMultiProcessHandler::CheckResume()
  280. {
  281. std::string fname = this->CTest->GetBinaryDir()
  282. + "/Testing/Temporary/CTestCheckpoint.txt";
  283. if(this->CTest->GetFailover())
  284. {
  285. if(cmSystemTools::FileExists(fname.c_str(), true))
  286. {
  287. *this->TestHandler->LogFile << "Resuming previously interrupted test set"
  288. << std::endl
  289. << "----------------------------------------------------------"
  290. << std::endl;
  291. std::ifstream fin;
  292. fin.open(fname.c_str());
  293. std::string line;
  294. while(std::getline(fin, line))
  295. {
  296. int index = atoi(line.c_str());
  297. this->RemoveTest(index);
  298. }
  299. fin.close();
  300. }
  301. }
  302. else
  303. {
  304. if(cmSystemTools::FileExists(fname.c_str(), true))
  305. {
  306. cmSystemTools::RemoveFile(fname.c_str());
  307. }
  308. }
  309. }
  310. void cmCTestMultiProcessHandler::RemoveTest(int index)
  311. {
  312. this->Tests.erase(index);
  313. this->Properties.erase(index);
  314. this->ExpensiveTests.erase(index);
  315. this->TestRunningMap[index] = false;
  316. this->TestFinishMap[index] = true;
  317. this->Completed++;
  318. }
  319. int cmCTestMultiProcessHandler::FindMaxIndex()
  320. {
  321. int max = 0;
  322. cmCTestMultiProcessHandler::TestMap::iterator i = this->Tests.begin();
  323. for(; i != this->Tests.end(); ++i)
  324. {
  325. if(i->first > max)
  326. {
  327. max = i->first;
  328. }
  329. }
  330. return max;
  331. }