module_profiler.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_MODULE_PROFILER_HPP_
  21. #define CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_MODULE_PROFILER_HPP_
  22. #include <memory>
  23. #include <string>
  24. #include <unordered_map>
  25. #include "cnstream_common.hpp"
  26. #include "cnstream_config.hpp"
  27. #include "profiler/process_profiler.hpp"
  28. #include "profiler/profile.hpp"
  29. #include "profiler/trace.hpp"
  30. /*!
  31. * @file module_profiler.hpp
  32. *
  33. * This file contains a declaration of the ModuleProfiler class.
  34. */
  35. namespace cnstream {
  36. static constexpr char kPROCESS_PROFILER_NAME[] = "PROCESS";
  37. static constexpr char kINPUT_PROFILER_NAME[] = "INPUT_QUEUE";
  38. class PipelineTracer;
  39. /*!
  40. * @class ModuleProfiler
  41. *
  42. * @brief ModuleProfiler is a class of the performance statistics of a module. It contains multiple
  43. * cnstream::ProcessProfiler instances to support multiple process profilings.
  44. *
  45. * The trace events of each process will be recorded when ProfilerConfig::enable_tracing is true. Profiling and tracing of customized process is supported. See ModuleProfiler::RegisterProcessName for details.
  46. *
  47. * @note This class is thread safe.
  48. */
  49. class ModuleProfiler : private NonCopyable {
  50. public:
  51. /*!
  52. * @brief Constructs a ModuleProfiler object.
  53. *
  54. * @param[in] config The configuration of the profiler.
  55. * @param[in] module_name The name of the module.
  56. * @param[in] tracer The tracer for tracing events.
  57. *
  58. * @return No return value.
  59. */
  60. explicit ModuleProfiler(const ProfilerConfig& config,
  61. const std::string& module_name,
  62. PipelineTracer* tracer);
  63. /*!
  64. * @brief Registers process named by ``process_name`` for this profiler.
  65. *
  66. * @param[in] process_name The process name is the unique identification of a function or a piece of code that needs
  67. * to do profiling.
  68. *
  69. * @return Returns true if the registration is successful. Returns false if the process name has been registered.
  70. */
  71. bool RegisterProcessName(const std::string& process_name);
  72. /*!
  73. * @brief Records the start of a process named ``process_name``.
  74. *
  75. * @param[in] process_name The name of the process. It should be registed by ``RegisterProcessName``.
  76. * @param[in] key The unique identifier of a CNFrameInfo instance.
  77. *
  78. * @return Returns true if recording is successful. Returns false if the process named by ``process_name`` is not
  79. * registered by ``RegisterProcessName``.
  80. *
  81. * @see cnstream::ModuleProfiler::RegisterProcessName
  82. * @see cnstream::ModuleProfiler::RecordKey
  83. */
  84. bool RecordProcessStart(const std::string& process_name, const RecordKey& key);
  85. /*!
  86. * @brief Records the end of a process named ``process_name``.
  87. *
  88. * @param[in] process_name The name of the process. It should be registed by ``RegisterProcessName``.
  89. * @param[in] key The unique identifier of a CNFrameInfo instance.
  90. *
  91. * @return Returns true if record successfully. Returns false if the process named by ``process_name`` has not been
  92. * registered by ``RegisterProcessName``.
  93. *
  94. * @see cnstream::ModuleProfiler::RegisterProcessName
  95. * @see cnstream::ModuleProfiler::RecordKey
  96. */
  97. bool RecordProcessEnd(const std::string& process_name, const RecordKey& key);
  98. /*!
  99. * @brief Clears profiling data of the stream named by ``stream_name``, as the end of the stream is reached.
  100. *
  101. * @param[in] stream_name The name of the stream, usually the ``CNFrameInfo::stream_id``.
  102. *
  103. * @return No return value.
  104. */
  105. void OnStreamEos(const std::string& stream_name);
  106. /*!
  107. * @brief Gets the name of the module.
  108. *
  109. * @return Returns the name of the module.
  110. */
  111. std::string GetName() const;
  112. /*!
  113. * @brief Gets profiling results of the module during the execution of the program.
  114. *
  115. * @return Returns the profiling results.
  116. */
  117. ModuleProfile GetProfile();
  118. /*!
  119. * @brief Gets profiling results according to the trace data.
  120. *
  121. * @param[in] trace Gets profiling results according to the trace data.
  122. *
  123. * @return Returns the profiling results.
  124. */
  125. ModuleProfile GetProfile(const ModuleTrace& trace);
  126. private:
  127. // Gets process profiler by ``process_name``.
  128. ProcessProfiler* GetProcessProfiler(const std::string& process_name);
  129. private:
  130. ProfilerConfig config_;
  131. std::string module_name_ = "";
  132. PipelineTracer* tracer_ = nullptr;
  133. std::unordered_map<std::string, std::unique_ptr<ProcessProfiler>> process_profilers_;
  134. }; // class ModuleProfiler
  135. inline std::string ModuleProfiler::GetName() const {
  136. return module_name_;
  137. }
  138. } // namespace cnstream
  139. #endif // CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_MODULE_PROFILER_HPP_