cmCTestMultiProcessHandler.cxx 9.1 KB

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