cmCTestMultiProcessHandler.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCTestMultiProcessHandler.h"
  11. #include "cmProcess.h"
  12. #include "cmStandardIncludes.h"
  13. #include "cmCTest.h"
  14. #include "cmSystemTools.h"
  15. #include <stdlib.h>
  16. #include <stack>
  17. #include <float.h>
  18. cmCTestMultiProcessHandler::cmCTestMultiProcessHandler()
  19. {
  20. this->ParallelLevel = 1;
  21. this->Completed = 0;
  22. this->RunningCount = 0;
  23. this->StopTimePassed = false;
  24. }
  25. cmCTestMultiProcessHandler::~cmCTestMultiProcessHandler()
  26. {
  27. }
  28. // Set the tests
  29. void
  30. cmCTestMultiProcessHandler::SetTests(TestMap& tests,
  31. PropertiesMap& properties)
  32. {
  33. this->Tests = tests;
  34. this->Properties = properties;
  35. this->Total = this->Tests.size();
  36. // set test run map to false for all
  37. for(TestMap::iterator i = this->Tests.begin();
  38. i != this->Tests.end(); ++i)
  39. {
  40. this->TestRunningMap[i->first] = false;
  41. this->TestFinishMap[i->first] = false;
  42. }
  43. if(!this->CTest->GetShowOnly())
  44. {
  45. this->ReadCostData();
  46. this->CreateTestCostList();
  47. }
  48. }
  49. // Set the max number of tests that can be run at the same time.
  50. void cmCTestMultiProcessHandler::SetParallelLevel(size_t level)
  51. {
  52. this->ParallelLevel = level < 1 ? 1 : level;
  53. }
  54. //---------------------------------------------------------
  55. void cmCTestMultiProcessHandler::RunTests()
  56. {
  57. this->CheckResume();
  58. if(!this->CheckCycles())
  59. {
  60. return;
  61. }
  62. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  63. this->StartNextTests();
  64. while(this->Tests.size() != 0)
  65. {
  66. if(this->StopTimePassed)
  67. {
  68. return;
  69. }
  70. this->CheckOutput();
  71. this->StartNextTests();
  72. }
  73. // let all running tests finish
  74. while(this->CheckOutput())
  75. {
  76. }
  77. this->MarkFinished();
  78. this->UpdateCostData();
  79. }
  80. //---------------------------------------------------------
  81. void cmCTestMultiProcessHandler::StartTestProcess(int test)
  82. {
  83. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "test " << test << "\n");
  84. this->TestRunningMap[test] = true; // mark the test as running
  85. // now remove the test itself
  86. this->EraseTest(test);
  87. this->RunningCount += GetProcessorsUsed(test);
  88. cmCTestRunTest* testRun = new cmCTestRunTest(this->TestHandler);
  89. testRun->SetIndex(test);
  90. testRun->SetTestProperties(this->Properties[test]);
  91. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  92. cmSystemTools::ChangeDirectory(this->Properties[test]->Directory.c_str());
  93. // Lock the resources we'll be using
  94. this->LockResources(test);
  95. if(testRun->StartTest(this->Total))
  96. {
  97. this->RunningTests.insert(testRun);
  98. }
  99. else if(testRun->IsStopTimePassed())
  100. {
  101. this->StopTimePassed = true;
  102. delete testRun;
  103. return;
  104. }
  105. else
  106. {
  107. this->UnlockResources(test);
  108. this->Completed++;
  109. this->TestFinishMap[test] = true;
  110. this->TestRunningMap[test] = false;
  111. this->RunningCount -= GetProcessorsUsed(test);
  112. testRun->EndTest(this->Completed, this->Total, false);
  113. this->Failed->push_back(this->Properties[test]->Name);
  114. delete testRun;
  115. }
  116. cmSystemTools::ChangeDirectory(current_dir.c_str());
  117. }
  118. //---------------------------------------------------------
  119. void cmCTestMultiProcessHandler::LockResources(int index)
  120. {
  121. for(std::set<std::string>::iterator i =
  122. this->Properties[index]->LockedResources.begin();
  123. i != this->Properties[index]->LockedResources.end(); ++i)
  124. {
  125. this->LockedResources.insert(*i);
  126. }
  127. }
  128. //---------------------------------------------------------
  129. void cmCTestMultiProcessHandler::UnlockResources(int index)
  130. {
  131. for(std::set<std::string>::iterator i =
  132. this->Properties[index]->LockedResources.begin();
  133. i != this->Properties[index]->LockedResources.end(); ++i)
  134. {
  135. this->LockedResources.erase(*i);
  136. }
  137. }
  138. //---------------------------------------------------------
  139. void cmCTestMultiProcessHandler::EraseTest(int test)
  140. {
  141. this->Tests.erase(test);
  142. for(TestCostMap::iterator i = this->TestCosts.begin();
  143. i != this->TestCosts.end(); ++i)
  144. {
  145. if(i->second.find(test) != i->second.end())
  146. {
  147. i->second.erase(test);
  148. return;
  149. }
  150. }
  151. }
  152. //---------------------------------------------------------
  153. inline size_t cmCTestMultiProcessHandler::GetProcessorsUsed(int test)
  154. {
  155. size_t processors =
  156. static_cast<int>(this->Properties[test]->Processors);
  157. //If this is set to run serially, it must run alone.
  158. //Also, if processors setting is set higher than the -j
  159. //setting, we default to using all of the process slots.
  160. if(this->Properties[test]->RunSerial
  161. || processors > this->ParallelLevel)
  162. {
  163. processors = this->ParallelLevel;
  164. }
  165. return processors;
  166. }
  167. //---------------------------------------------------------
  168. bool cmCTestMultiProcessHandler::StartTest(int test)
  169. {
  170. //Check for locked resources
  171. for(std::set<std::string>::iterator i =
  172. this->Properties[test]->LockedResources.begin();
  173. i != this->Properties[test]->LockedResources.end(); ++i)
  174. {
  175. if(this->LockedResources.find(*i) != this->LockedResources.end())
  176. {
  177. return false;
  178. }
  179. }
  180. // copy the depend tests locally because when
  181. // a test is finished it will be removed from the depend list
  182. // and we don't want to be iterating a list while removing from it
  183. TestSet depends = this->Tests[test];
  184. size_t totalDepends = depends.size();
  185. if(totalDepends)
  186. {
  187. for(TestSet::const_iterator i = depends.begin();
  188. i != depends.end(); ++i)
  189. {
  190. // if the test is not already running then start it
  191. if(!this->TestRunningMap[*i])
  192. {
  193. // this test might be finished, but since
  194. // this is a copy of the depend map we might
  195. // still have it
  196. if(!this->TestFinishMap[*i])
  197. {
  198. // only start one test in this function
  199. return this->StartTest(*i);
  200. }
  201. else
  202. {
  203. // the depend has been and finished
  204. totalDepends--;
  205. }
  206. }
  207. }
  208. }
  209. // if there are no depends left then run this test
  210. if(totalDepends == 0)
  211. {
  212. this->StartTestProcess(test);
  213. return true;
  214. }
  215. // This test was not able to start because it is waiting
  216. // on depends to run
  217. return false;
  218. }
  219. //---------------------------------------------------------
  220. void cmCTestMultiProcessHandler::StartNextTests()
  221. {
  222. size_t numToStart = this->ParallelLevel - this->RunningCount;
  223. if(numToStart == 0)
  224. {
  225. return;
  226. }
  227. for(TestCostMap::reverse_iterator i = this->TestCosts.rbegin();
  228. i != this->TestCosts.rend(); ++i)
  229. {
  230. TestSet tests = i->second; //copy the test set
  231. for(TestSet::iterator test = tests.begin();
  232. test != tests.end(); ++test)
  233. {
  234. //in case this test has already been started due to dependency
  235. if(this->TestRunningMap[*test] || this->TestFinishMap[*test])
  236. {
  237. continue;
  238. }
  239. size_t processors = GetProcessorsUsed(*test);
  240. if(processors > numToStart)
  241. {
  242. return;
  243. }
  244. if(this->StartTest(*test))
  245. {
  246. if(this->StopTimePassed)
  247. {
  248. return;
  249. }
  250. numToStart -= processors;
  251. }
  252. else
  253. {
  254. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl
  255. << "Test did not start waiting on depends to finish: "
  256. << *test << "\n");
  257. }
  258. if(numToStart == 0)
  259. {
  260. return;
  261. }
  262. }
  263. }
  264. }
  265. //---------------------------------------------------------
  266. bool cmCTestMultiProcessHandler::CheckOutput()
  267. {
  268. // no more output we are done
  269. if(this->RunningTests.size() == 0)
  270. {
  271. return false;
  272. }
  273. std::vector<cmCTestRunTest*> finished;
  274. std::string out, err;
  275. for(std::set<cmCTestRunTest*>::const_iterator i = this->RunningTests.begin();
  276. i != this->RunningTests.end(); ++i)
  277. {
  278. cmCTestRunTest* p = *i;
  279. if(!p->CheckOutput())
  280. {
  281. finished.push_back(p);
  282. }
  283. }
  284. for( std::vector<cmCTestRunTest*>::iterator i = finished.begin();
  285. i != finished.end(); ++i)
  286. {
  287. this->Completed++;
  288. cmCTestRunTest* p = *i;
  289. int test = p->GetIndex();
  290. if(p->EndTest(this->Completed, this->Total, true))
  291. {
  292. this->Passed->push_back(p->GetTestProperties()->Name);
  293. }
  294. else
  295. {
  296. this->Failed->push_back(p->GetTestProperties()->Name);
  297. }
  298. for(TestMap::iterator j = this->Tests.begin();
  299. j != this->Tests.end(); ++j)
  300. {
  301. j->second.erase(test);
  302. }
  303. this->TestFinishMap[test] = true;
  304. this->TestRunningMap[test] = false;
  305. this->RunningTests.erase(p);
  306. this->WriteCheckpoint(test);
  307. this->UnlockResources(test);
  308. this->RunningCount -= GetProcessorsUsed(test);
  309. delete p;
  310. }
  311. return true;
  312. }
  313. //---------------------------------------------------------
  314. void cmCTestMultiProcessHandler::UpdateCostData()
  315. {
  316. std::string fname = this->CTest->GetCostDataFile();
  317. std::string tmpout = fname + ".tmp";
  318. std::fstream fout;
  319. fout.open(tmpout.c_str(), std::ios::out);
  320. PropertiesMap temp = this->Properties;
  321. if(cmSystemTools::FileExists(fname.c_str()))
  322. {
  323. std::ifstream fin;
  324. fin.open(fname.c_str());
  325. std::string line;
  326. while(std::getline(fin, line))
  327. {
  328. if(line == "---") break;
  329. std::vector<cmsys::String> parts =
  330. cmSystemTools::SplitString(line.c_str(), ' ');
  331. //Format: <name> <previous_runs> <avg_cost>
  332. if(parts.size() < 3) break;
  333. std::string name = parts[0];
  334. int prev = atoi(parts[1].c_str());
  335. float cost = static_cast<float>(atof(parts[2].c_str()));
  336. int index = this->SearchByName(name);
  337. if(index == -1)
  338. {
  339. // This test is not in memory. We just rewrite the entry
  340. fout << name << " " << prev << " " << cost << "\n";
  341. }
  342. else
  343. {
  344. // Update with our new average cost
  345. fout << name << " " << this->Properties[index]->PreviousRuns << " "
  346. << this->Properties[index]->Cost << "\n";
  347. temp.erase(index);
  348. }
  349. }
  350. fin.close();
  351. cmSystemTools::RemoveFile(fname.c_str());
  352. }
  353. // Add all tests not previously listed in the file
  354. for(PropertiesMap::iterator i = temp.begin(); i != temp.end(); ++i)
  355. {
  356. fout << i->second->Name << " " << i->second->PreviousRuns << " "
  357. << i->second->Cost << "\n";
  358. }
  359. // Write list of failed tests
  360. fout << "---\n";
  361. for(std::vector<cmStdString>::iterator i = this->Failed->begin();
  362. i != this->Failed->end(); ++i)
  363. {
  364. fout << i->c_str() << "\n";
  365. }
  366. fout.close();
  367. cmSystemTools::RenameFile(tmpout.c_str(), fname.c_str());
  368. }
  369. //---------------------------------------------------------
  370. void cmCTestMultiProcessHandler::ReadCostData()
  371. {
  372. std::string fname = this->CTest->GetCostDataFile();
  373. if(cmSystemTools::FileExists(fname.c_str(), true))
  374. {
  375. std::ifstream fin;
  376. fin.open(fname.c_str());
  377. std::string line;
  378. while(std::getline(fin, line))
  379. {
  380. if(line == "---") break;
  381. std::vector<cmsys::String> parts =
  382. cmSystemTools::SplitString(line.c_str(), ' ');
  383. // Probably an older version of the file, will be fixed next run
  384. if(parts.size() < 3)
  385. {
  386. fin.close();
  387. return;
  388. }
  389. std::string name = parts[0];
  390. int prev = atoi(parts[1].c_str());
  391. float cost = static_cast<float>(atof(parts[2].c_str()));
  392. int index = this->SearchByName(name);
  393. if(index == -1) continue;
  394. this->Properties[index]->PreviousRuns = prev;
  395. if(this->Properties[index] && this->Properties[index]->Cost == 0)
  396. {
  397. this->Properties[index]->Cost = cost;
  398. }
  399. }
  400. // Next part of the file is the failed tests
  401. while(std::getline(fin, line))
  402. {
  403. if(line != "")
  404. {
  405. this->LastTestsFailed.push_back(line);
  406. }
  407. }
  408. fin.close();
  409. }
  410. }
  411. //---------------------------------------------------------
  412. int cmCTestMultiProcessHandler::SearchByName(std::string name)
  413. {
  414. int index = -1;
  415. for(PropertiesMap::iterator i = this->Properties.begin();
  416. i != this->Properties.end(); ++i)
  417. {
  418. if(i->second->Name == name)
  419. {
  420. index = i->first;
  421. }
  422. }
  423. return index;
  424. }
  425. //---------------------------------------------------------
  426. void cmCTestMultiProcessHandler::CreateTestCostList()
  427. {
  428. for(TestMap::iterator i = this->Tests.begin();
  429. i != this->Tests.end(); ++i)
  430. {
  431. //We only want to schedule them by cost in a parallel situation
  432. if(this->ParallelLevel > 1)
  433. {
  434. std::string name = this->Properties[i->first]->Name;
  435. if(std::find(this->LastTestsFailed.begin(), this->LastTestsFailed.end(),
  436. name) != this->LastTestsFailed.end())
  437. {
  438. this->TestCosts[FLT_MAX].insert(i->first);
  439. }
  440. else
  441. {
  442. this->TestCosts[this->Properties[i->first]->Cost].insert(i->first);
  443. }
  444. }
  445. else //we ignore their cost
  446. {
  447. size_t index = this->Tests.size()
  448. - static_cast<size_t>(this->Properties[i->first]->Index);
  449. this->TestCosts[index].insert(i->first);
  450. }
  451. }
  452. }
  453. //---------------------------------------------------------
  454. void cmCTestMultiProcessHandler::WriteCheckpoint(int index)
  455. {
  456. std::string fname = this->CTest->GetBinaryDir()
  457. + "/Testing/Temporary/CTestCheckpoint.txt";
  458. std::fstream fout;
  459. fout.open(fname.c_str(), std::ios::app | std::ios::out);
  460. fout << index << "\n";
  461. fout.close();
  462. }
  463. //---------------------------------------------------------
  464. void cmCTestMultiProcessHandler::MarkFinished()
  465. {
  466. std::string fname = this->CTest->GetBinaryDir()
  467. + "/Testing/Temporary/CTestCheckpoint.txt";
  468. cmSystemTools::RemoveFile(fname.c_str());
  469. }
  470. //---------------------------------------------------------
  471. //For ShowOnly mode
  472. void cmCTestMultiProcessHandler::PrintTestList()
  473. {
  474. this->TestHandler->SetMaxIndex(this->FindMaxIndex());
  475. int count = 0;
  476. for (PropertiesMap::iterator it = this->Properties.begin();
  477. it != this->Properties.end(); ++it)
  478. {
  479. count++;
  480. cmCTestTestHandler::cmCTestTestProperties& p = *it->second;
  481. //push working dir
  482. std::string current_dir = cmSystemTools::GetCurrentWorkingDirectory();
  483. cmSystemTools::ChangeDirectory(p.Directory.c_str());
  484. cmCTestRunTest testRun(this->TestHandler);
  485. testRun.SetIndex(p.Index);
  486. testRun.SetTestProperties(&p);
  487. testRun.ComputeArguments(); //logs the command in verbose mode
  488. if (this->TestHandler->MemCheck)
  489. {
  490. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Memory Check");
  491. }
  492. else
  493. {
  494. cmCTestLog(this->CTest, HANDLER_OUTPUT, " Test");
  495. }
  496. cmOStringStream indexStr;
  497. indexStr << " #" << p.Index << ":";
  498. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  499. std::setw(3 + getNumWidth(this->TestHandler->GetMaxIndex()))
  500. << indexStr.str().c_str());
  501. cmCTestLog(this->CTest, HANDLER_OUTPUT, " ");
  502. cmCTestLog(this->CTest, HANDLER_OUTPUT, p.Name.c_str() << std::endl);
  503. //pop working dir
  504. cmSystemTools::ChangeDirectory(current_dir.c_str());
  505. }
  506. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl << "Total Tests: "
  507. << this->Total << std::endl);
  508. }
  509. //---------------------------------------------------------
  510. void cmCTestMultiProcessHandler::CheckResume()
  511. {
  512. std::string fname = this->CTest->GetBinaryDir()
  513. + "/Testing/Temporary/CTestCheckpoint.txt";
  514. if(this->CTest->GetFailover())
  515. {
  516. if(cmSystemTools::FileExists(fname.c_str(), true))
  517. {
  518. *this->TestHandler->LogFile << "Resuming previously interrupted test set"
  519. << std::endl
  520. << "----------------------------------------------------------"
  521. << std::endl;
  522. std::ifstream fin;
  523. fin.open(fname.c_str());
  524. std::string line;
  525. while(std::getline(fin, line))
  526. {
  527. int index = atoi(line.c_str());
  528. this->RemoveTest(index);
  529. }
  530. fin.close();
  531. }
  532. }
  533. else if(cmSystemTools::FileExists(fname.c_str(), true))
  534. {
  535. cmSystemTools::RemoveFile(fname.c_str());
  536. }
  537. }
  538. //---------------------------------------------------------
  539. void cmCTestMultiProcessHandler::RemoveTest(int index)
  540. {
  541. this->EraseTest(index);
  542. this->Properties.erase(index);
  543. this->TestRunningMap[index] = false;
  544. this->TestFinishMap[index] = true;
  545. this->Completed++;
  546. }
  547. //---------------------------------------------------------
  548. int cmCTestMultiProcessHandler::FindMaxIndex()
  549. {
  550. int max = 0;
  551. cmCTestMultiProcessHandler::TestMap::iterator i = this->Tests.begin();
  552. for(; i != this->Tests.end(); ++i)
  553. {
  554. if(i->first > max)
  555. {
  556. max = i->first;
  557. }
  558. }
  559. return max;
  560. }
  561. //Returns true if no cycles exist in the dependency graph
  562. bool cmCTestMultiProcessHandler::CheckCycles()
  563. {
  564. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  565. "Checking test dependency graph..." << std::endl);
  566. for(TestMap::iterator it = this->Tests.begin();
  567. it != this->Tests.end(); ++it)
  568. {
  569. //DFS from each element to itself
  570. std::stack<int> s;
  571. std::vector<int> visited;
  572. s.push(it->first);
  573. while(!s.empty())
  574. {
  575. int test = s.top();
  576. s.pop();
  577. for(TestSet::iterator d = this->Tests[test].begin();
  578. d != this->Tests[test].end(); ++d)
  579. {
  580. if(std::find(visited.begin(), visited.end(), *d) != visited.end())
  581. {
  582. //cycle exists
  583. cmCTestLog(this->CTest, ERROR_MESSAGE, "Error: a cycle exists in "
  584. "the test dependency graph for the test \""
  585. << this->Properties[it->first]->Name << "\"." << std::endl
  586. << "Please fix the cycle and run ctest again." << std::endl);
  587. return false;
  588. }
  589. s.push(*d);
  590. }
  591. visited.push_back(test);
  592. }
  593. }
  594. return true;
  595. }