cmCTestMultiProcessHandler.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "cmCTest.h"
  14. #include "cmCTestResourceAllocator.h"
  15. #include "cmCTestResourceSpec.h"
  16. #include "cmCTestTestHandler.h"
  17. #include "cmUVHandlePtr.h"
  18. #include "cmUVJobServerClient.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(cmCTest* ctest, cmCTestTestHandler* handler);
  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(cm::optional<size_t> level);
  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. cmCTestTestHandler* GetTestHandler() { return this->TestHandler; }
  75. void SetRepeatMode(cmCTest::Repeat mode, int count)
  76. {
  77. this->RepeatMode = mode;
  78. this->RepeatCount = count;
  79. }
  80. void SetResourceSpecFile(const std::string& resourceSpecFile)
  81. {
  82. this->ResourceSpecFile = resourceSpecFile;
  83. }
  84. void SetQuiet(bool b) { this->Quiet = b; }
  85. void CheckResourceAvailability();
  86. protected:
  87. // Start the next test or tests as many as are allowed by
  88. // ParallelLevel
  89. void StartNextTests();
  90. void StartTestProcess(int test);
  91. void StartTest(int test);
  92. // Mark the checkpoint for the given test
  93. void WriteCheckpoint(int index);
  94. void UpdateCostData();
  95. void ReadCostData();
  96. // Return index of a test based on its name
  97. int SearchByName(std::string const& name);
  98. void CreateTestCostList();
  99. void GetAllTestDependencies(int test, TestList& dependencies);
  100. void CreateSerialTestCostList();
  101. void CreateParallelTestCostList();
  102. // Removes the checkpoint file
  103. void MarkFinished();
  104. void FinishTestProcess(std::unique_ptr<cmCTestRunTest> runner, bool started);
  105. void StartNextTestsOnIdle();
  106. void StartNextTestsOnTimer();
  107. void RemoveTest(int index);
  108. // Check if we need to resume an interrupted test set
  109. void CheckResume();
  110. // Check if there are any circular dependencies
  111. bool CheckCycles();
  112. int FindMaxIndex();
  113. inline size_t GetProcessorsUsed(int index);
  114. std::string GetName(int index);
  115. bool CheckStopOnFailure();
  116. bool CheckStopTimePassed();
  117. void SetStopTimePassed();
  118. void InitializeLoop();
  119. void FinalizeLoop();
  120. void LockResources(int index);
  121. void UnlockResources(int index);
  122. enum class ResourceAvailabilityError
  123. {
  124. NoResourceType,
  125. InsufficientResources,
  126. };
  127. bool Complete();
  128. bool AllocateResources(int index);
  129. bool TryAllocateResources(
  130. int index,
  131. std::map<std::string, std::vector<cmCTestBinPackerAllocation>>&
  132. allocations,
  133. std::map<std::string, ResourceAvailabilityError>* errors = nullptr);
  134. void DeallocateResources(int index);
  135. bool AllResourcesAvailable();
  136. bool InitResourceAllocator(std::string& error);
  137. bool CheckGeneratedResourceSpec();
  138. private:
  139. cmCTest* CTest;
  140. cmCTestTestHandler* TestHandler;
  141. bool UseResourceSpec = false;
  142. cmCTestResourceSpec ResourceSpec;
  143. std::string ResourceSpecFile;
  144. std::string ResourceSpecSetupFixture;
  145. cm::optional<std::size_t> ResourceSpecSetupTest;
  146. bool HasInvalidGeneratedResourceSpec = false;
  147. // Tests pending selection to start. They may have dependencies.
  148. TestMap PendingTests;
  149. // List of pending test indexes, ordered by cost.
  150. std::list<int> OrderedTests;
  151. // Total number of tests we'll be running
  152. size_t Total = 0;
  153. // Number of tests that are complete
  154. size_t Completed = 0;
  155. size_t RunningCount = 0;
  156. std::set<size_t> ProcessorsAvailable;
  157. size_t HaveAffinity;
  158. bool StopTimePassed = false;
  159. // list of test properties (indices concurrent to the test map)
  160. PropertiesMap Properties;
  161. std::map<int, std::string> TestOutput;
  162. std::vector<std::string>* Passed;
  163. std::vector<std::string>* Failed;
  164. std::vector<std::string> LastTestsFailed;
  165. std::set<std::string> ProjectResourcesLocked;
  166. std::map<int,
  167. std::vector<std::map<std::string, std::vector<ResourceAllocation>>>>
  168. AllocatedResources;
  169. std::map<int, std::map<std::string, ResourceAvailabilityError>>
  170. ResourceAvailabilityErrors;
  171. cmCTestResourceAllocator ResourceAllocator;
  172. std::vector<cmCTestTestHandler::cmCTestTestResult>* TestResults;
  173. // Get the maximum number of processors that may be used at once.
  174. size_t GetParallelLevel() const;
  175. // With no '-j' option, default to serial testing.
  176. cm::optional<size_t> ParallelLevel = 1;
  177. // Fallback parallelism limit when '-j' is given with no value.
  178. size_t ParallelLevelDefault;
  179. // 'make' jobserver client. If connected, we acquire a token
  180. // for each test before running its process.
  181. cm::optional<cmUVJobServerClient> JobServerClient;
  182. // List of tests that are queued to run when a token is available.
  183. std::list<int> JobServerQueuedTests;
  184. // Callback invoked when a token is received.
  185. void JobServerReceivedToken();
  186. unsigned long TestLoad = 0;
  187. unsigned long FakeLoadForTesting = 0;
  188. cm::uv_loop_ptr Loop;
  189. cm::uv_idle_ptr StartNextTestsOnIdle_;
  190. cm::uv_timer_ptr StartNextTestsOnTimer_;
  191. bool HasCycles = false;
  192. cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
  193. int RepeatCount = 1;
  194. bool Quiet = false;
  195. bool SerialTestRunning = false;
  196. };