cmCTestMultiProcessHandler.cxx 8.5 KB

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