cmCTestMultiProcessHandler.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 LockResources(int index);
  124. void UnlockResources(int index);
  125. enum class ResourceAvailabilityError
  126. {
  127. NoResourceType,
  128. InsufficientResources,
  129. };
  130. bool Complete();
  131. bool AllocateResources(int index);
  132. bool TryAllocateResources(
  133. int index,
  134. std::map<std::string, std::vector<cmCTestBinPackerAllocation>>&
  135. allocations,
  136. std::map<std::string, ResourceAvailabilityError>* errors = nullptr);
  137. void DeallocateResources(int index);
  138. bool AllResourcesAvailable();
  139. bool InitResourceAllocator(std::string& error);
  140. bool CheckGeneratedResourceSpec();
  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;
  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;
  153. // Number of tests that are complete
  154. size_t Completed;
  155. size_t RunningCount;
  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. size_t ParallelLevel; // max number of process that can be run at once
  174. unsigned long TestLoad;
  175. unsigned long FakeLoadForTesting;
  176. cm::uv_loop_ptr Loop;
  177. cm::uv_timer_ptr TestLoadRetryTimer;
  178. cmCTestTestHandler* TestHandler;
  179. cmCTest* CTest;
  180. bool HasCycles;
  181. cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
  182. int RepeatCount = 1;
  183. bool Quiet;
  184. bool SerialTestRunning;
  185. };