pipeline_profiler.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_PIPELINE_PROFILER_HPP_
  21. #define CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_PIPELINE_PROFILER_HPP_
  22. #include <memory>
  23. #include <string>
  24. #include <vector>
  25. #include <unordered_map>
  26. #include "cnstream_common.hpp"
  27. #include "cnstream_config.hpp"
  28. #include "profiler/process_profiler.hpp"
  29. #include "profiler/profile.hpp"
  30. #include "profiler/trace.hpp"
  31. /*!
  32. * @file pipeline_profiler.hpp
  33. *
  34. * This file contains a declaration of the PipelineProfiler class.
  35. */
  36. namespace cnstream {
  37. class Module;
  38. class ModuleProfiler;
  39. static constexpr char kOVERALL_PROCESS_NAME[] = "OVERALL";
  40. /*!
  41. * @class PipelineProfiler
  42. *
  43. * @brief PipelineProfiler is responsible for the performance statistics of a pipeline. It contains multiple
  44. * cnstream::ModuleProfiler instances to support multiple module profilings.
  45. *
  46. * By default, it will perform profiling of two processes for all modules. They are named ``kPROCESS_PROFILER_NAME``
  47. * and ``kINPUT_PROFILER_NAME``. The start of the first process is before cnstream::Module::Process being called, and
  48. * the end is before cnstream::Module::Transmit being called. The time when data is pushed into the data queue of the
  49. * module is the start of the second process and the end is when data starts to be processed by the module.
  50. *
  51. * It also does profiling of the data processing process from entering to exiting the pipeline.
  52. *
  53. * The start and end trace events of each process are recorded when the ``config.enable_tracing`` is true.
  54. *
  55. * @note This class is thread safe.
  56. */
  57. class PipelineProfiler : private NonCopyable {
  58. public:
  59. /*!
  60. * @brief Constructs a PipelineProfiler object.
  61. *
  62. * @param[in] config The configuration of the profiler.
  63. * @param[in] pipeline_name The name of the pipeline.
  64. * @param[in] modules All modules of the pipeline named ``pipeline_name``.
  65. *
  66. * @return No return value.
  67. */
  68. PipelineProfiler(const ProfilerConfig& config,
  69. const std::string& pipeline_name,
  70. const std::vector<std::shared_ptr<Module>>& modules);
  71. /*!
  72. * @brief Gets the name of the pipeline.
  73. *
  74. * @return Returns the name of the pipeline.
  75. */
  76. std::string GetName() const;
  77. /**
  78. * @brief Gets profiler configuration.
  79. *
  80. * @return Returns profiler configuration.
  81. **/
  82. ProfilerConfig GetConfig() const;
  83. /**
  84. * @brief Gets tracer.
  85. *
  86. * @return Returns the tracer of the pipeline.
  87. */
  88. PipelineTracer* GetTracer() const;
  89. /*!
  90. * @brief Gets the module profiler by the name of the module.
  91. *
  92. * @param[in] module_name The name of the module.
  93. *
  94. * @return Returns the module profiler.
  95. */
  96. ModuleProfiler* GetModuleProfiler(const std::string& module_name) const;
  97. /*!
  98. * @brief Gets profiling results of the pipeline during the execution of the program.
  99. *
  100. * @return Returns the profiling results.
  101. */
  102. PipelineProfile GetProfile();
  103. /*!
  104. * @brief Gets profiling results between the start time and the end time.
  105. *
  106. * @param[in] start The start time.
  107. * @param[in] end The end time.
  108. *
  109. * @return Returns the profiling results.
  110. */
  111. PipelineProfile GetProfile(const Time& start, const Time& end);
  112. /*!
  113. * @brief Gets profiling results during a specified period time.
  114. *
  115. * @param[in] end The end time.
  116. * @param[in] duration The duration in milliseconds. The start time is the end time minus duration.
  117. *
  118. * @return Returns the profiling results.
  119. */
  120. PipelineProfile GetProfileBefore(const Time& end, const Duration& duration);
  121. /*!
  122. * @brief Gets profiling results for a specified period time.
  123. *
  124. * @param[in] start The start time.
  125. * @param[in] duration The duration in milliseconds. The end time is the start time plus duration.
  126. *
  127. * @return Returns the profiling results.
  128. */
  129. PipelineProfile GetProfileAfter(const Time& start, const Duration& duration);
  130. /*!
  131. * @brief Records the time when the data enters the pipeline.
  132. *
  133. * @param[in] key The unique identifier of a CNFrameInfo instance.
  134. *
  135. * @return No return value.
  136. *
  137. * @see cnstream::RecordKey
  138. */
  139. void RecordInput(const RecordKey& key);
  140. /*!
  141. * @brief Records the time when the data exits the pipeline.
  142. *
  143. * @param[in] key The unique identifier of a CNFrameInfo instance.
  144. *
  145. * @return No return value.
  146. *
  147. * @see cnstream::RecordKey
  148. */
  149. void RecordOutput(const RecordKey& key);
  150. /*!
  151. * @brief Clears profiling data of the stream named by ``stream_name``, as the end of the stream is reached.
  152. *
  153. * @param[in] stream_name The name of the stream, usually the ``CNFrameInfo::stream_id``.
  154. *
  155. * @return No return value.
  156. */
  157. void OnStreamEos(const std::string& stream_name);
  158. private:
  159. ProfilerConfig config_;
  160. std::string pipeline_name_;
  161. std::unordered_map<std::string, std::unique_ptr<ModuleProfiler>> module_profilers_;
  162. std::unique_ptr<ProcessProfiler> overall_profiler_;
  163. std::unique_ptr<PipelineTracer> tracer_;
  164. }; // class PipelineProfiler
  165. inline std::string PipelineProfiler::GetName() const {
  166. return pipeline_name_;
  167. }
  168. inline ProfilerConfig PipelineProfiler::GetConfig() const {
  169. return config_;
  170. }
  171. inline PipelineTracer* PipelineProfiler::GetTracer() const {
  172. return tracer_.get();
  173. }
  174. inline PipelineProfile PipelineProfiler::GetProfileBefore(const Time& end, const Duration& duration) {
  175. return GetProfile(std::chrono::time_point_cast<Clock::duration>(end - duration), end);
  176. }
  177. inline PipelineProfile PipelineProfiler::GetProfileAfter(const Time& start, const Duration& duration) {
  178. return GetProfile(start, std::chrono::time_point_cast<Clock::duration>(start + duration));
  179. }
  180. inline void PipelineProfiler::RecordInput(const RecordKey& key) {
  181. overall_profiler_->RecordStart(key);
  182. }
  183. inline void PipelineProfiler::RecordOutput(const RecordKey& key) {
  184. overall_profiler_->RecordEnd(key);
  185. }
  186. inline void PipelineProfiler::OnStreamEos(const std::string& stream_name) {
  187. overall_profiler_->OnStreamEos(stream_name);
  188. }
  189. } // namespace cnstream
  190. #endif // CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_PIPELINE_PROFILER_HPP_