cmCTestMultiProcessHandler.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <cstddef>
  6. #include <list>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include <vector>
  12. #include <cm/optional>
  13. #include <cm3p/uv.h>
  14. #include "cmCTest.h"
  15. #include "cmCTestResourceAllocator.h"
  16. #include "cmCTestResourceSpec.h"
  17. #include "cmCTestTestHandler.h"
  18. #include "cmUVHandlePtr.h"
  19. struct cmCTestBinPackerAllocation;
  20. class cmCTestRunTest;
  21. /** \class cmCTestMultiProcessHandler
  22. * \brief run parallel ctest
  23. *
  24. * cmCTestMultiProcessHandler
  25. */
  26. class cmCTestMultiProcessHandler
  27. {
  28. friend class TestComparator;
  29. friend class cmCTestRunTest;
  30. public:
  31. struct TestSet : public std::set<int>
  32. {
  33. };
  34. struct TestInfo
  35. {
  36. TestSet Depends;
  37. };
  38. struct TestMap : public std::map<int, TestInfo>
  39. {
  40. };
  41. struct TestList : public std::vector<int>
  42. {
  43. };
  44. struct PropertiesMap
  45. : public std::map<int, cmCTestTestHandler::cmCTestTestProperties*>
  46. {
  47. };
  48. struct ResourceAllocation
  49. {
  50. std::string Id;
  51. unsigned int Slots;
  52. };
  53. cmCTestMultiProcessHandler();
  54. virtual ~cmCTestMultiProcessHandler();
  55. // Set the tests
  56. void SetTests(TestMap tests, PropertiesMap properties);
  57. // Set the max number of tests that can be run at the same time.
  58. void SetParallelLevel(size_t);
  59. void SetTestLoad(unsigned long load);
  60. virtual void RunTests();
  61. void PrintOutputAsJson();
  62. void PrintTestList();
  63. void PrintLabels();
  64. void SetPassFailVectors(std::vector<std::string>* passed,
  65. std::vector<std::string>* failed)
  66. {
  67. this->Passed = passed;
  68. this->Failed = failed;
  69. }
  70. void SetTestResults(std::vector<cmCTestTestHandler::cmCTestTestResult>* r)
  71. {
  72. this->TestResults = r;
  73. }
  74. void SetCTest(cmCTest* ctest) { this->CTest = ctest; }
  75. void SetTestHandler(cmCTestTestHandler* handler)
  76. {
  77. this->TestHandler = handler;
  78. }
  79. cmCTestTestHandler* GetTestHandler() { return this->TestHandler; }
  80. void SetRepeatMode(cmCTest::Repeat mode, int count)
  81. {
  82. this->RepeatMode = mode;
  83. this->RepeatCount = count;
  84. }
  85. void SetResourceSpecFile(const std::string& resourceSpecFile)
  86. {
  87. this->ResourceSpecFile = resourceSpecFile;
  88. }
  89. void SetQuiet(bool b) { this->Quiet = b; }
  90. void CheckResourceAvailability();
  91. protected:
  92. // Start the next test or tests as many as are allowed by
  93. // ParallelLevel
  94. void StartNextTests();
  95. bool StartTestProcess(int test);
  96. bool StartTest(int test);
  97. // Mark the checkpoint for the given test
  98. void WriteCheckpoint(int index);
  99. void UpdateCostData();
  100. void ReadCostData();
  101. // Return index of a test based on its name
  102. int SearchByName(std::string const& name);
  103. void CreateTestCostList();
  104. void GetAllTestDependencies(int test, TestList& dependencies);
  105. void CreateSerialTestCostList();
  106. void CreateParallelTestCostList();
  107. // Removes the checkpoint file
  108. void MarkFinished();
  109. void ErasePendingTest(int index);
  110. void FinishTestProcess(std::unique_ptr<cmCTestRunTest> runner, bool started);
  111. static void OnTestLoadRetryCB(uv_timer_t* timer);
  112. void RemoveTest(int index);
  113. // Check if we need to resume an interrupted test set
  114. void CheckResume();
  115. // Check if there are any circular dependencies
  116. bool CheckCycles();
  117. int FindMaxIndex();
  118. inline size_t GetProcessorsUsed(int index);
  119. std::string GetName(int index);
  120. bool CheckStopOnFailure();
  121. bool CheckStopTimePassed();
  122. void SetStopTimePassed();
  123. void InitializeLoop();
  124. void FinalizeLoop();
  125. void LockResources(int index);
  126. void UnlockResources(int index);
  127. enum class ResourceAvailabilityError
  128. {
  129. NoResourceType,
  130. InsufficientResources,
  131. };
  132. bool Complete();
  133. bool AllocateResources(int index);
  134. bool TryAllocateResources(
  135. int index,
  136. std::map<std::string, std::vector<cmCTestBinPackerAllocation>>&
  137. allocations,
  138. std::map<std::string, ResourceAvailabilityError>* errors = nullptr);
  139. void DeallocateResources(int index);
  140. bool AllResourcesAvailable();
  141. bool InitResourceAllocator(std::string& error);
  142. bool CheckGeneratedResourceSpec();
  143. bool UseResourceSpec = false;
  144. cmCTestResourceSpec ResourceSpec;
  145. std::string ResourceSpecFile;
  146. std::string ResourceSpecSetupFixture;
  147. cm::optional<std::size_t> ResourceSpecSetupTest;
  148. bool HasInvalidGeneratedResourceSpec;
  149. // Tests pending selection to start. They may have dependencies.
  150. TestMap PendingTests;
  151. // List of pending test indexes, ordered by cost.
  152. std::list<int> OrderedTests;
  153. // Total number of tests we'll be running
  154. size_t Total;
  155. // Number of tests that are complete
  156. size_t Completed;
  157. size_t RunningCount;
  158. std::set<size_t> ProcessorsAvailable;
  159. size_t HaveAffinity;
  160. bool StopTimePassed = false;
  161. // list of test properties (indices concurrent to the test map)
  162. PropertiesMap Properties;
  163. std::map<int, std::string> TestOutput;
  164. std::vector<std::string>* Passed;
  165. std::vector<std::string>* Failed;
  166. std::vector<std::string> LastTestsFailed;
  167. std::set<std::string> ProjectResourcesLocked;
  168. std::map<int,
  169. std::vector<std::map<std::string, std::vector<ResourceAllocation>>>>
  170. AllocatedResources;
  171. std::map<int, std::map<std::string, ResourceAvailabilityError>>
  172. ResourceAvailabilityErrors;
  173. cmCTestResourceAllocator ResourceAllocator;
  174. std::vector<cmCTestTestHandler::cmCTestTestResult>* TestResults;
  175. size_t ParallelLevel; // max number of process that can be run at once
  176. unsigned long TestLoad;
  177. unsigned long FakeLoadForTesting;
  178. cm::uv_loop_ptr Loop;
  179. cm::uv_timer_ptr TestLoadRetryTimer;
  180. cmCTestTestHandler* TestHandler;
  181. cmCTest* CTest;
  182. bool HasCycles;
  183. cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
  184. int RepeatCount = 1;
  185. bool Quiet;
  186. bool SerialTestRunning;
  187. };