process_profiler.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_PROCESS_PROFILER_HPP_
  21. #define CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_PROCESS_PROFILER_HPP_
  22. #include <algorithm>
  23. #include <string>
  24. #include <unordered_map>
  25. #include <vector>
  26. #include "cnstream_common.hpp"
  27. #include "cnstream_config.hpp"
  28. #include "profiler/pipeline_tracer.hpp"
  29. #include "profiler/profile.hpp"
  30. #include "profiler/stream_profiler.hpp"
  31. #include "profiler/trace.hpp"
  32. /*!
  33. * @file process_profiler.hpp
  34. *
  35. * This file contains a declaration of the ProcessProfiler class.
  36. */
  37. namespace cnstream {
  38. class RecordPolicy;
  39. /*!
  40. * @class ProcessProfiler
  41. *
  42. * @brief ProcessProfiler is the profiler for a process. A process can be a function call or a piece of code.
  43. *
  44. * @note This class is thread safe.
  45. */
  46. class ProcessProfiler : private NonCopyable {
  47. public:
  48. /*!
  49. * @brief Constructs a ProcessProfiler object.
  50. *
  51. * @param[in] config The configuration of the profiler.
  52. * @param[in] process_name The name of the process.
  53. * @param[in] tracer The tracer for tracing events.
  54. *
  55. * @return No return value.
  56. */
  57. explicit ProcessProfiler(const ProfilerConfig& config,
  58. const std::string& process_name,
  59. PipelineTracer* tracer);
  60. /*!
  61. * @brief Destructs a ProcessProfiler object.
  62. *
  63. * @return No return value.
  64. */
  65. ~ProcessProfiler();
  66. /*!
  67. * @brief Sets the module name to identify which module this profiler belongs to.
  68. * The module name takes effect when the trace level is TraceEvent::MODULE.
  69. * The trace level can be set by cnstream::ProcessProfiler::SetTraceLevel.
  70. *
  71. * @param[in] module_name The name of the module.
  72. *
  73. * @return Returns this profiler itself.
  74. */
  75. ProcessProfiler& SetModuleName(const std::string& module_name);
  76. /*!
  77. * @brief Set the trace level for this profiler.
  78. * Trace level identifies whether this profiler belongs to a module or a pipeline.
  79. *
  80. * @param[in] level Trace level.
  81. *
  82. * @return Returns the ProcessProfiler object itself.
  83. *
  84. * @see cnstream::TraceEvent::Level.
  85. */
  86. ProcessProfiler& SetTraceLevel(const TraceEvent::Level& level);
  87. /*!
  88. * @brief Records the start of the process.
  89. *
  90. * @param[in] key The unique identifier of a CNFrameInfo instance.
  91. *
  92. * @return No return value.
  93. *
  94. * @see cnstream::RecordKey.
  95. */
  96. void RecordStart(const RecordKey& key);
  97. /*!
  98. * @brief Records the end of the process.
  99. *
  100. * @param[in] key The unique identifier of a CNFrameInfo instance.
  101. *
  102. * @return No return value.
  103. *
  104. * @see cnstream::RecordKey.
  105. */
  106. void RecordEnd(const RecordKey& key);
  107. /*!
  108. * @brief Gets the name of the process.
  109. *
  110. * @return The name of the process.
  111. */
  112. std::string GetName() const;
  113. /*!
  114. * @brief Gets profiling results of the process during the execution of the program.
  115. *
  116. * @return Returns the profiling results.
  117. */
  118. ProcessProfile GetProfile();
  119. /*!
  120. * @brief Gets profiling results according to the trace data.
  121. *
  122. * @param[in] trace The trace data of the process.
  123. *
  124. * @return Returns the profiling results.
  125. */
  126. ProcessProfile GetProfile(const ProcessTrace& trace) const;
  127. /*!
  128. * @brief Clears profiling data of the stream named by ``stream_name``, as the end of the stream is reached.
  129. *
  130. * @param[in] stream_name The name of the stream, usually the ``CNFrameInfo::stream_id``.
  131. *
  132. * @return No return value.
  133. */
  134. void OnStreamEos(const std::string& stream_name);
  135. private:
  136. // Records start time, called by RecordStart(const RecordKey&).
  137. void RecordStart(const RecordKey& key, const Time& time);
  138. // Records end time, called by RecordEnd(const RecordKey&).
  139. void RecordEnd(const RecordKey& key, const Time& time);
  140. // Increases the physical time used by the process named by ``process_name``.
  141. void AddPhysicalTime(const Time& now);
  142. // Statistics latency during profiling.
  143. void AddLatency(const std::string& stream_name, const Duration& latency);
  144. // Statistics the number of dropped datas during profiling.
  145. void AddDropped(const std::string& stream_name, uint64_t dropped);
  146. // Tell this profiler the stream named by ``stream_name`` is going to be profiled.
  147. // Prepares resources that needed by profiler for profiling the stream named by ``stream_name``.
  148. // Called when the first record of the stream named by ``stream_name`` arrives.
  149. void OnStreamStart(const std::string& stream_name);
  150. // Gets profiling results for streams.
  151. std::vector<StreamProfiler> GetStreamProfilers();
  152. // Tracing. Called by RecordStart and RecordEnd when config_.enable_tracing is true.
  153. void Tracing(const RecordKey& key, const Time& time, const TraceEvent::Type& type);
  154. private:
  155. ProfilerConfig config_;
  156. std::mutex lk_;
  157. // Processing data counter.
  158. // The data that only records the start time but not the end time is called an ongoing-data.
  159. uint64_t ongoing_ = 0;
  160. // Dropped frame counter.
  161. uint64_t dropped_ = 0;
  162. // Completed frame counter. It is incremented by 1 when an end time is recorded.
  163. uint64_t completed_ = 0;
  164. // The number of latencies counted.
  165. // The latency will be be counted only when the start and end times of the data are recorded.
  166. uint64_t latency_add_times_ = 0;
  167. // The last time recorded by RecordStart or RecordEnd.
  168. Time last_record_time_ = Time::min();
  169. Duration total_latency_ = Duration::zero();
  170. Duration maximum_latency_ = Duration::zero();
  171. Duration minimum_latency_ = Duration::max();
  172. // Physical time used for the process named by ``process_name``.
  173. Duration total_phy_time_ = Duration::zero();
  174. std::string module_name_ = "";
  175. std::string process_name_ = "";
  176. PipelineTracer* tracer_ = nullptr;
  177. // Start time record tool.
  178. RecordPolicy* record_policy_ = nullptr;
  179. TraceEvent::Level trace_level_;
  180. // Stream profilers for each stream.
  181. std::unordered_map<std::string, StreamProfiler> stream_profilers_;
  182. }; // class ProcessProfiler
  183. inline ProcessProfiler& ProcessProfiler::SetModuleName(const std::string& module_name) {
  184. module_name_ = module_name;
  185. return *this;
  186. }
  187. inline ProcessProfiler& ProcessProfiler::SetTraceLevel(const TraceEvent::Level& level) {
  188. trace_level_ = level;
  189. return *this;
  190. }
  191. inline std::string ProcessProfiler::GetName() const {
  192. return process_name_;
  193. }
  194. inline void ProcessProfiler::AddLatency(const std::string& stream_name, const Duration& latency) {
  195. total_latency_ += latency;
  196. maximum_latency_ = std::max(latency, maximum_latency_);
  197. minimum_latency_ = std::min(latency, minimum_latency_);
  198. latency_add_times_++;
  199. stream_profilers_.find(stream_name)->second.AddLatency(latency);
  200. }
  201. inline void ProcessProfiler::AddDropped(const std::string& stream_name, uint64_t dropped) {
  202. dropped_ += dropped;
  203. stream_profilers_.find(stream_name)->second.AddDropped(dropped);
  204. }
  205. inline void ProcessProfiler::Tracing(const RecordKey& key, const Time& time, const TraceEvent::Type& type) {
  206. tracer_->RecordEvent(TraceEvent(key).SetModuleName(module_name_)
  207. .SetProcessName(process_name_)
  208. .SetLevel(trace_level_)
  209. .SetTime(time)
  210. .SetType(type));
  211. }
  212. } // namespace cnstream
  213. #endif // CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_PROCESS_PROFILER_HPP_