cmCTestMultiProcessHandler.cxx 10 KB

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