cmCTestMultiProcessHandler.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <map>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include <cm/optional>
  12. #include <cm3p/uv.h>
  13. #include "cmCTest.h"
  14. #include "cmCTestResourceAllocator.h"
  15. #include "cmCTestResourceSpec.h"
  16. #include "cmCTestTestHandler.h"
  17. #include "cmUVHandlePtr.h"
  18. struct cmCTestBinPackerAllocation;
  19. class cmCTestRunTest;
  20. /** \class cmCTestMultiProcessHandler
  21. * \brief run parallel ctest
  22. *
  23. * cmCTestMultiProcessHandler
  24. */
  25. class cmCTestMultiProcessHandler
  26. {
  27. friend class TestComparator;
  28. friend class cmCTestRunTest;
  29. public:
  30. struct TestSet : public std::set<int>
  31. {
  32. };
  33. struct TestMap : public std::map<int, TestSet>
  34. {
  35. };
  36. struct TestList : public std::vector<int>
  37. {
  38. };
  39. struct PropertiesMap
  40. : public std::map<int, cmCTestTestHandler::cmCTestTestProperties*>
  41. {
  42. };
  43. struct ResourceAllocation
  44. {
  45. std::string Id;
  46. unsigned int Slots;
  47. };
  48. cmCTestMultiProcessHandler();
  49. virtual ~cmCTestMultiProcessHandler();
  50. // Set the tests
  51. void SetTests(TestMap& tests, PropertiesMap& properties);
  52. // Set the max number of tests that can be run at the same time.
  53. void SetParallelLevel(size_t);
  54. void SetTestLoad(unsigned long load);
  55. virtual void RunTests();
  56. void PrintOutputAsJson();
  57. void PrintTestList();
  58. void PrintLabels();
  59. void SetPassFailVectors(std::vector<std::string>* passed,
  60. std::vector<std::string>* failed)
  61. {
  62. this->Passed = passed;
  63. this->Failed = failed;
  64. }
  65. void SetTestResults(std::vector<cmCTestTestHandler::cmCTestTestResult>* r)
  66. {
  67. this->TestResults = r;
  68. }
  69. void SetCTest(cmCTest* ctest) { this->CTest = ctest; }
  70. void SetTestHandler(cmCTestTestHandler* handler)
  71. {
  72. this->TestHandler = handler;
  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 CheckResourcesAvailable();
  86. protected:
  87. // Start the next test or tests as many as are allowed by
  88. // ParallelLevel
  89. void StartNextTests();
  90. bool StartTestProcess(int test);
  91. bool 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 EraseTest(int index);
  105. void FinishTestProcess(std::unique_ptr<cmCTestRunTest> runner, bool started);
  106. static void OnTestLoadRetryCB(uv_timer_t* timer);
  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 LockResources(int index);
  119. void UnlockResources(int index);
  120. enum class ResourceAllocationError
  121. {
  122. NoResourceType,
  123. InsufficientResources,
  124. };
  125. bool Complete();
  126. bool AllocateResources(int index);
  127. bool TryAllocateResources(
  128. int index,
  129. std::map<std::string, std::vector<cmCTestBinPackerAllocation>>&
  130. allocations,
  131. std::map<std::string, ResourceAllocationError>* errors = nullptr);
  132. void DeallocateResources(int index);
  133. bool AllResourcesAvailable();
  134. bool InitResourceAllocator(std::string& error);
  135. bool CheckGeneratedResourceSpec();
  136. bool UseResourceSpec = false;
  137. cmCTestResourceSpec ResourceSpec;
  138. std::string ResourceSpecFile;
  139. std::string ResourceSpecSetupFixture;
  140. cm::optional<std::size_t> ResourceSpecSetupTest;
  141. bool HasInvalidGeneratedResourceSpec;
  142. // map from test number to set of depend tests
  143. TestMap Tests;
  144. TestList SortedTests;
  145. // Total number of tests we'll be running
  146. size_t Total;
  147. // Number of tests that are complete
  148. size_t Completed;
  149. size_t RunningCount;
  150. std::set<size_t> ProcessorsAvailable;
  151. size_t HaveAffinity;
  152. bool StopTimePassed = false;
  153. // list of test properties (indices concurrent to the test map)
  154. PropertiesMap Properties;
  155. std::map<int, std::string> TestOutput;
  156. std::vector<std::string>* Passed;
  157. std::vector<std::string>* Failed;
  158. std::vector<std::string> LastTestsFailed;
  159. std::set<std::string> LockedResources;
  160. std::map<int,
  161. std::vector<std::map<std::string, std::vector<ResourceAllocation>>>>
  162. AllocatedResources;
  163. std::map<int, std::map<std::string, ResourceAllocationError>>
  164. ResourceAllocationErrors;
  165. cmCTestResourceAllocator ResourceAllocator;
  166. std::vector<cmCTestTestHandler::cmCTestTestResult>* TestResults;
  167. size_t ParallelLevel; // max number of process that can be run at once
  168. unsigned long TestLoad;
  169. unsigned long FakeLoadForTesting;
  170. cm::uv_loop_ptr Loop;
  171. cm::uv_timer_ptr TestLoadRetryTimer;
  172. cmCTestTestHandler* TestHandler;
  173. cmCTest* CTest;
  174. bool HasCycles;
  175. cmCTest::Repeat RepeatMode = cmCTest::Repeat::Never;
  176. int RepeatCount = 1;
  177. bool Quiet;
  178. bool SerialTestRunning;
  179. };