cmCTestMultiProcessHandler.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. cmCTestMultiProcessHandler::cmCTestMultiProcessHandler()
  18. {
  19. this->ParallelLevel = 1;
  20. this->ProcessId = 0;
  21. }
  22. // Set the tests
  23. void
  24. cmCTestMultiProcessHandler::SetTests(TestMap& tests,
  25. std::map<int,cmStdString>& testNames)
  26. {
  27. // set test run map to false for all
  28. for(TestMap::iterator i = this->Tests.begin();
  29. i != this->Tests.end(); ++i)
  30. {
  31. this->TestRunningMap[i->first] = false;
  32. this->TestFinishMap[i->first] = false;
  33. }
  34. this->Tests = tests;
  35. this->TestNames = testNames;
  36. }
  37. // Set the max number of tests that can be run at the same time.
  38. void cmCTestMultiProcessHandler::SetParallelLevel(size_t l)
  39. {
  40. this->ParallelLevel = l;
  41. }
  42. void cmCTestMultiProcessHandler::RunTests()
  43. {
  44. this->StartNextTests();
  45. while(this->Tests.size() != 0)
  46. {
  47. this->CheckOutput();
  48. this->StartNextTests();
  49. }
  50. // let all running tests finish
  51. while(this->CheckOutput())
  52. {
  53. }
  54. for(std::map<int, cmStdString>::iterator i =
  55. this->TestOutput.begin();
  56. i != this->TestOutput.end(); ++i)
  57. {
  58. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  59. i->second << std::endl);
  60. }
  61. }
  62. void cmCTestMultiProcessHandler::StartTestProcess(int test)
  63. {
  64. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  65. " test " << test << "\n");
  66. this->TestRunningMap[test] = true; // mark the test as running
  67. // now remove the test itself
  68. this->Tests.erase(test);
  69. // now run the test
  70. cmProcess* newp = new cmProcess;
  71. newp->SetId(this->ProcessId);
  72. newp->SetId(test);
  73. newp->SetCommand(this->CTestCommand.c_str());
  74. std::vector<std::string> args;
  75. cmOStringStream width;
  76. if(this->CTest->GetMaxTestNameWidth())
  77. {
  78. args.push_back("-W");
  79. width << this->CTest->GetMaxTestNameWidth();
  80. args.push_back(width.str().c_str());
  81. }
  82. args.push_back("-I");
  83. cmOStringStream strm;
  84. strm << test << "," << test;
  85. args.push_back(strm.str());
  86. args.push_back("--parallel-cache");
  87. args.push_back(this->CTestCacheFile.c_str());
  88. args.push_back("--internal-ctest-parallel");
  89. cmOStringStream strm2;
  90. strm2 << test;
  91. args.push_back(strm2.str());
  92. if(this->CTest->GetExtraVerbose())
  93. {
  94. args.push_back("-VV");
  95. }
  96. newp->SetCommandArguments(args);
  97. if(!newp->StartProcess())
  98. {
  99. cmCTestLog(this->CTest, ERROR_MESSAGE,
  100. "Error starting " << newp->GetCommand() << "\n");
  101. this->EndTest(newp);
  102. }
  103. else
  104. {
  105. this->RunningTests.insert(newp);
  106. }
  107. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  108. "ctest -I " << test << "\n");
  109. this->ProcessId++;
  110. }
  111. bool cmCTestMultiProcessHandler::StartTest(int test)
  112. {
  113. // copy the depend tests locally because when
  114. // a test is finished it will be removed from the depend list
  115. // and we don't want to be iterating a list while removing from it
  116. TestSet depends = this->Tests[test];
  117. size_t totalDepends = depends.size();
  118. if(totalDepends)
  119. {
  120. for(TestSet::const_iterator i = depends.begin();
  121. i != depends.end(); ++i)
  122. {
  123. // if the test is not already running then start it
  124. if(!this->TestRunningMap[*i])
  125. {
  126. // this test might be finished, but since
  127. // this is a copy of the depend map we might
  128. // still have it
  129. if(!this->TestFinishMap[*i])
  130. {
  131. // only start one test in this function
  132. return this->StartTest(*i);
  133. }
  134. else
  135. {
  136. // the depend has been and finished
  137. totalDepends--;
  138. }
  139. }
  140. }
  141. }
  142. // if there are no depends left then run this test
  143. if(totalDepends == 0)
  144. {
  145. // Start this test it has no depends
  146. this->StartTestProcess(test);
  147. return true;
  148. }
  149. // This test was not able to start because it is waiting
  150. // on depends to run
  151. return false;
  152. }
  153. void cmCTestMultiProcessHandler::StartNextTests()
  154. {
  155. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  156. << "Number of running tests : " << this->RunningTests.size()
  157. << "\n");
  158. size_t numToStart = this->ParallelLevel - this->RunningTests.size();
  159. if(numToStart == 0)
  160. {
  161. return;
  162. }
  163. TestMap tests = this->Tests;
  164. for(TestMap::iterator i = tests.begin();
  165. i != tests.end(); ++i)
  166. {
  167. // start test should start only one test
  168. if(this->StartTest(i->first))
  169. {
  170. numToStart--;
  171. }
  172. else
  173. {
  174. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  175. << "Test did not start waiting on depends to finish: "
  176. << i->first << "\n");
  177. }
  178. if(numToStart == 0 )
  179. {
  180. return;
  181. }
  182. }
  183. }
  184. bool cmCTestMultiProcessHandler::CheckOutput()
  185. {
  186. // no more output we are done
  187. if(this->RunningTests.size() == 0)
  188. {
  189. return false;
  190. }
  191. std::vector<cmProcess*> finished;
  192. std::string out, err;
  193. for(std::set<cmProcess*>::const_iterator i = this->RunningTests.begin();
  194. i != this->RunningTests.end(); ++i)
  195. {
  196. cmProcess* p = *i;
  197. int pipe = p->CheckOutput(.1, out, err);
  198. if(pipe == cmsysProcess_Pipe_STDOUT)
  199. {
  200. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  201. p->GetId() << ": " << out << std::endl);
  202. this->TestOutput[ p->GetId() ] += out;
  203. this->TestOutput[ p->GetId() ] += "\n";
  204. }
  205. else if(pipe == cmsysProcess_Pipe_STDERR)
  206. {
  207. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  208. p->GetId() << ": " << err << std::endl);
  209. this->TestOutput[ p->GetId() ] += err;
  210. this->TestOutput[ p->GetId() ] += "\n";
  211. }
  212. if(!p->IsRunning())
  213. {
  214. finished.push_back(p);
  215. }
  216. }
  217. for( std::vector<cmProcess*>::iterator i = finished.begin();
  218. i != finished.end(); ++i)
  219. {
  220. cmProcess* p = *i;
  221. this->EndTest(p);
  222. }
  223. return true;
  224. }
  225. void cmCTestMultiProcessHandler::EndTest(cmProcess* p)
  226. {
  227. int test = p->GetId();
  228. int exitVal = p->GetExitValue();
  229. cmCTestTestHandler::cmCTestTestResult cres;
  230. cres.Properties = 0;
  231. cres.ExecutionTime = 0;// ???
  232. cres.ReturnValue = exitVal;
  233. cres.Status = cmCTestTestHandler::COMPLETED;
  234. cres.TestCount = test;
  235. cres.Name = this->TestNames[test];
  236. cres.Path = "";
  237. if(exitVal)
  238. {
  239. cres.Status = cmCTestTestHandler::FAILED;
  240. this->Failed->push_back(this->TestNames[test]);
  241. }
  242. else
  243. {
  244. this->Passed->push_back(this->TestNames[test]);
  245. }
  246. this->TestResults->push_back(cres);
  247. // remove test from depend of all other tests
  248. for(TestMap::iterator i = this->Tests.begin();
  249. i!= this->Tests.end(); ++i)
  250. {
  251. i->second.erase(test);
  252. }
  253. this->TestFinishMap[test] = true;
  254. this->TestRunningMap[test] = false;
  255. this->RunningTests.erase(p);
  256. delete p;
  257. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  258. "finish test " << test << "\n");
  259. }
  260. void cmCTestMultiProcessHandler::PrintTests()
  261. {
  262. #undef cout
  263. for( TestMap::iterator i = this->Tests.begin();
  264. i!= this->Tests.end(); ++i)
  265. {
  266. std::cout << "Test " << i->first << " (";
  267. for(TestSet::iterator j = i->second.begin();
  268. j != i->second.end(); ++j)
  269. {
  270. std::cout << *j << " ";
  271. }
  272. std::cout << ")\n";
  273. }
  274. }
  275. #if 0
  276. int main()
  277. {
  278. cmCTestMultiProcessHandler h;
  279. h.SetParallelLevel(4);
  280. std::map<int, std::set<int> > tests;
  281. std::set<int> depends;
  282. for(int i =1; i < 92; i++)
  283. {
  284. tests[i] = depends;
  285. }
  286. depends.clear();
  287. depends.insert(45); subprject
  288. tests[46] = depends; subproject-stage2
  289. depends.clear();
  290. depends.insert(55); simpleinstall simpleinstall-s2
  291. tests[56] = depends;
  292. depends.clear();
  293. depends.insert(70); wrapping
  294. tests[71] = depends; qtwrapping
  295. depends.clear();
  296. depends.insert(71); qtwrapping
  297. tests[72] = depends; testdriver1
  298. depends.clear();
  299. depends.insert(72) testdriver1
  300. tests[73] = depends; testdriver2
  301. depends.clear();
  302. depends.insert(73); testdriver2
  303. tests[74] = depends; testdriver3
  304. depends.clear();
  305. depends.insert(79); linkorder1
  306. tests[80] = depends; linkorder2
  307. h.SetTests(tests);
  308. h.PrintTests();
  309. h.RunTests();
  310. }
  311. #endif