cmCTestMultiProcessHandler.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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 <cm/string_view>
  14. #include "cmCTest.h"
  15. #include "cmCTestResourceAllocator.h"
  16. #include "cmCTestResourceSpec.h"
  17. #include "cmCTestTestHandler.h"
  18. #include "cmUVHandlePtr.h"
  19. #include "cmUVJobServerClient.h"
  20. struct cmCTestBinPackerAllocation;
  21. class cmCTestRunTest;
  22. /** \class cmCTestMultiProcessHandler
  23. * \brief run parallel ctest
  24. *
  25. * cmCTestMultiProcessHandler
  26. */
  27. class cmCTestMultiProcessHandler
  28. {
  29. friend class TestComparator;
  30. friend class cmCTestRunTest;
  31. public:
  32. struct TestSet : public std::set<int>
  33. {
  34. };
  35. struct TestInfo
  36. {
  37. TestSet Depends;
  38. };
  39. struct TestMap : public std::map<int, TestInfo>
  40. {
  41. };
  42. struct TestList : public std::vector<int>
  43. {
  44. };
  45. struct PropertiesMap
  46. : public std::map<int, cmCTestTestHandler::cmCTestTestProperties*>
  47. {
  48. };
  49. struct ResourceAllocation
  50. {
  51. std::string Id;
  52. unsigned int Slots;
  53. };
  54. cmCTestMultiProcessHandler(cmCTest* ctest, cmCTestTestHandler* handler);
  55. virtual ~cmCTestMultiProcessHandler();
  56. // Set the tests
  57. bool SetTests(TestMap tests, PropertiesMap properties);
  58. // Set the max number of tests that can be run at the same time.
  59. void SetParallelLevel(cm::optional<size_t> level);
  60. void SetTestLoad(unsigned long load);
  61. virtual void RunTests();
  62. void PrintOutputAsJson();
  63. void PrintTestList();
  64. void PrintLabels();
  65. void SetPassFailVectors(std::vector<std::string>* passed,
  66. std::vector<std::string>* failed)
  67. {
  68. this->Passed = passed;
  69. this->Failed = failed;
  70. }
  71. void SetTestResults(std::vector<cmCTestTestHandler::cmCTestTestResult>* r)
  72. {
  73. this->TestResults = r;
  74. }
  75. cmCTestTestHandler* GetTestHandler() { return this->TestHandler; }
  76. void SetRepeatMode(cmCTest::Repeat mode, int count)
  77. {
  78. this->RepeatMode = mode;
  79. this->RepeatCount = count;
  80. }
  81. void SetResourceSpecFile(std::string const& resourceSpecFile)
  82. {
  83. this->ResourceSpecFile = resourceSpecFile;
  84. }
  85. void SetQuiet(bool b) { this->Quiet = b; }
  86. void CheckResourceAvailability();
  87. protected:
  88. // Start the next test or tests as many as are allowed by
  89. // ParallelLevel
  90. void StartNextTests();
  91. void StartTestProcess(int test);
  92. void StartTest(int test);
  93. // Mark the checkpoint for the given test
  94. void WriteCheckpoint(int index);
  95. void UpdateCostData();
  96. void ReadCostData();
  97. // Return index of a test based on its name
  98. int SearchByName(cm::string_view name);
  99. void CreateTestCostList();
  100. void GetAllTestDependencies(int test, TestList& dependencies);
  101. void CreateSerialTestCostList();
  102. void CreateParallelTestCostList();
  103. // Removes the checkpoint file
  104. void MarkFinished();
  105. void FinishTestProcess(std::unique_ptr<cmCTestRunTest> runner, bool started);
  106. void StartNextTestsOnIdle();
  107. void StartNextTestsOnTimer();
  108. void RemoveTest(int index);
  109. // Check if we need to resume an interrupted test set
  110. void CheckResume();
  111. // Check if there are any circular dependencies
  112. bool CheckCycles();
  113. int FindMaxIndex();
  114. inline size_t GetProcessorsUsed(int index);
  115. std::string GetName(int index);
  116. bool CheckStopOnFailure();
  117. bool CheckStopTimePassed();
  118. void SetStopTimePassed();
  119. void InitializeLoop();
  120. void FinalizeLoop();
  121. bool ResourceLocksAvailable(int test);
  122. void LockResources(int index);
  123. void UnlockResources(int index);
  124. enum class ResourceAvailabilityError
  125. {
  126. NoResourceType,
  127. InsufficientResources,
  128. };
  129. bool Complete();
  130. bool AllocateResources(int index);
  131. bool TryAllocateResources(
  132. int index,
  133. std::map<std::string, std::vector<cmCTestBinPackerAllocation>>&
  134. allocations,
  135. std::map<std::string, ResourceAvailabilityError>* errors = nullptr);
  136. void DeallocateResources(int index);
  137. bool AllResourcesAvailable();
  138. bool InitResourceAllocator(std::string& error);
  139. bool CheckGeneratedResourceSpec();
  140. private:
  141. cmCTest* CTest;
  142. cmCTestTestHandler* TestHandler;
  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 = false;
  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 = 0;
  155. // Number of tests that are complete
  156. size_t Completed = 0;
  157. size_t RunningCount = 0;
  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. // Get the maximum number of processors that may be used at once.
  176. size_t GetParallelLevel() const;
  177. // With no '-j' option, default to serial testing.
  178. cm::optional<size_t> ParallelLevel = 1;
  179. // Fallback parallelism limit when '-j' is given with no value.
  180. size_t ParallelLevelDefault;
  181. // 'make' jobserver client. If connected, we acquire a token
  182. // for each test before running its process.
  183. cm::optional<cmUVJobServerClient> JobServerClient;
  184. // List of tests that are queued to run when a token is available.
  185. std::list<int> JobServerQueuedTests;
  186. // Callback invoked when a token is received.
  187. void JobServerReceivedToken();
  188. unsigned long TestLoad = 0;
  189. unsigned long FakeLoadForTesting = 0;
  190. cm::uv_loop_ptr Loop;
  191. cm::uv_idle_ptr StartNextTestsOnIdle_;
  192. cm::uv_timer_ptr StartNextTestsOnTimer_;
  193. bool HasCycles = false;
  194. cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
  195. int RepeatCount = 1;
  196. bool Quiet = false;
  197. bool SerialTestRunning = false;
  198. };