cmCTestMultiProcessHandler.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmCTestMultiProcessHandler_h
  4. #define cmCTestMultiProcessHandler_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include <stddef.h>
  11. #include "cm_uv.h"
  12. #include "cmCTestResourceAllocator.h"
  13. #include "cmCTestTestHandler.h"
  14. #include "cmUVHandlePtr.h"
  15. class cmCTest;
  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 SetQuiet(bool b) { this->Quiet = b; }
  75. void InitResourceAllocator(const cmCTestResourceSpec& spec)
  76. {
  77. this->ResourceAllocator.InitializeFromResourceSpec(spec);
  78. }
  79. void CheckResourcesAvailable();
  80. protected:
  81. // Start the next test or tests as many as are allowed by
  82. // ParallelLevel
  83. void StartNextTests();
  84. bool StartTestProcess(int test);
  85. bool StartTest(int test);
  86. // Mark the checkpoint for the given test
  87. void WriteCheckpoint(int index);
  88. void UpdateCostData();
  89. void ReadCostData();
  90. // Return index of a test based on its name
  91. int SearchByName(std::string const& name);
  92. void CreateTestCostList();
  93. void GetAllTestDependencies(int test, TestList& dependencies);
  94. void CreateSerialTestCostList();
  95. void CreateParallelTestCostList();
  96. // Removes the checkpoint file
  97. void MarkFinished();
  98. void EraseTest(int index);
  99. void FinishTestProcess(cmCTestRunTest* runner, bool started);
  100. static void OnTestLoadRetryCB(uv_timer_t* timer);
  101. void RemoveTest(int index);
  102. // Check if we need to resume an interrupted test set
  103. void CheckResume();
  104. // Check if there are any circular dependencies
  105. bool CheckCycles();
  106. int FindMaxIndex();
  107. inline size_t GetProcessorsUsed(int index);
  108. std::string GetName(int index);
  109. bool CheckStopTimePassed();
  110. void SetStopTimePassed();
  111. void LockResources(int index);
  112. void UnlockResources(int index);
  113. bool AllocateResources(int index);
  114. bool TryAllocateResources(
  115. int index,
  116. std::map<std::string, std::vector<cmCTestBinPackerAllocation>>&
  117. allocations);
  118. void DeallocateResources(int index);
  119. bool AllResourcesAvailable();
  120. // map from test number to set of depend tests
  121. TestMap Tests;
  122. TestList SortedTests;
  123. // Total number of tests we'll be running
  124. size_t Total;
  125. // Number of tests that are complete
  126. size_t Completed;
  127. size_t RunningCount;
  128. std::set<size_t> ProcessorsAvailable;
  129. size_t HaveAffinity;
  130. bool StopTimePassed = false;
  131. // list of test properties (indices concurrent to the test map)
  132. PropertiesMap Properties;
  133. std::map<int, bool> TestRunningMap;
  134. std::map<int, bool> TestFinishMap;
  135. std::map<int, std::string> TestOutput;
  136. std::vector<std::string>* Passed;
  137. std::vector<std::string>* Failed;
  138. std::vector<std::string> LastTestsFailed;
  139. std::set<std::string> LockedResources;
  140. std::map<int,
  141. std::vector<std::map<std::string, std::vector<ResourceAllocation>>>>
  142. AllocatedResources;
  143. std::map<int, bool> TestsHaveSufficientResources;
  144. cmCTestResourceAllocator ResourceAllocator;
  145. std::vector<cmCTestTestHandler::cmCTestTestResult>* TestResults;
  146. size_t ParallelLevel; // max number of process that can be run at once
  147. unsigned long TestLoad;
  148. unsigned long FakeLoadForTesting;
  149. uv_loop_t Loop;
  150. cm::uv_timer_ptr TestLoadRetryTimer;
  151. cmCTestTestHandler* TestHandler;
  152. cmCTest* CTest;
  153. bool HasCycles;
  154. bool Quiet;
  155. bool SerialTestRunning;
  156. };
  157. #endif