cmCTestMultiProcessHandler.h 5.3 KB

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