cmCTestMultiProcessHandler.cxx 9.1 KB

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