trace.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*************************************************************************
  2. * Copyright (C) [2020-2021] 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_TRACE_HPP_
  21. #define CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_TRACE_HPP_
  22. #include <chrono>
  23. #include <string>
  24. #include <unordered_map>
  25. #include <utility>
  26. #include <vector>
  27. /*!
  28. * @file trace.hpp
  29. *
  30. * This file contains declarations of the TraceEvent class, the TraceElem struct and the TraceElem struct.
  31. */
  32. namespace cnstream {
  33. /*!
  34. * Defines an alias for the std::chrono::steady_clock.
  35. */
  36. using Clock = std::chrono::steady_clock;
  37. /*!
  38. * Defines an alias for the std::chrono::duration<double, std::milli>.
  39. */
  40. using Duration = std::chrono::duration<double, std::milli>;
  41. /*!
  42. * Defines an alias for the std::chrono::steady_clock::timepoint.
  43. */
  44. using Time = Clock::time_point;
  45. /*!
  46. * Defines an alias for the std::pair<std::string, int64_t>. RecordKey now denotes a pair of the stream name
  47. * ``CNFrameInfo::stream_id`` and pts ``CNFrameInfo::timestamp``.
  48. */
  49. using RecordKey = std::pair<std::string, int64_t>;
  50. /*!
  51. * @class TraceEvent
  52. *
  53. * @brief TraceEvent is a class representing a trace event used by Profile.
  54. */
  55. class TraceEvent {
  56. public:
  57. RecordKey key; /*!< The unique identification of a frame. */
  58. std::string module_name; /*!< The name of a module. */
  59. std::string process_name; /*!< The name of a process. A process can be a function call or a piece of code. */
  60. Time time; /*!< The timestamp of an event. */
  61. /*!
  62. * @enum Level
  63. *
  64. * @brief Enumeration variables describing the level of an event. The default level is 0 (pipeline's event).
  65. */
  66. enum class Level {
  67. PIPELINE = 0, /*!< A event of a pipeline. */
  68. MODULE /*!< An event of a module. */
  69. } level = Level::PIPELINE;
  70. /*!
  71. * @enum Type
  72. *
  73. * @brief Enumeration variables describing the type of an event. The default type is 1 (START).
  74. */
  75. enum class Type {
  76. START = 1 << 0, /*!< A process-start event. */
  77. END = 1 << 1 /*!< A process-end event. */
  78. } type = Type::START;
  79. /*!
  80. * @brief Constructs a TraceEvent object by using default constructor.
  81. *
  82. * @return No return value.
  83. */
  84. TraceEvent() = default;
  85. /*!
  86. * @brief Constructs a TraceEvent object with a RecordKey instance.
  87. *
  88. * @param[in] key The unique identification of a frame.
  89. *
  90. * @return No return value.
  91. */
  92. explicit TraceEvent(const RecordKey& key);
  93. /*!
  94. * @brief Constructs a TraceEvent object with a RecordKey using move semantics.
  95. *
  96. * @param[in] key The unique identification of a frame.
  97. *
  98. * @return No return value.
  99. */
  100. explicit TraceEvent(RecordKey&& key);
  101. /*!
  102. * @brief Constructs a TraceEvent object with the copy of the contents of another object.
  103. *
  104. * @param[in] other Another object used to initialize an object.
  105. *
  106. * @return No return value.
  107. */
  108. TraceEvent(const TraceEvent& other) = default;
  109. /*!
  110. * @brief Replaces the contents with a copy of the contents of another TraceEvent object.
  111. *
  112. * @param[in] other Another object used to initialize the current object.
  113. *
  114. * @return Returns a lvalue reference to the current instance.
  115. */
  116. TraceEvent& operator=(const TraceEvent& other) = default;
  117. /*!
  118. * @brief Constructs a TraceEvent object with the contents of another object using move semantics.
  119. *
  120. * @param[in] other Another object used to initialize an object.
  121. *
  122. * @return No return value.
  123. */
  124. TraceEvent(TraceEvent&& other);
  125. /*!
  126. * @brief Replaces the contents with those of another TraceEvent object using move semantics.
  127. *
  128. * @param[in] other Another object used to initialize the current object.
  129. *
  130. * @return Returns a lvalue reference to the current instance.
  131. */
  132. TraceEvent& operator=(TraceEvent&& other);
  133. /*!
  134. * @brief Sets a unique identification for a frame.
  135. *
  136. * @param[in] key The unique identification of a frame.
  137. *
  138. * @return Returns a lvalue reference to the current instance.
  139. */
  140. TraceEvent& SetKey(const RecordKey& key);
  141. /*!
  142. * @brief Sets a unique identification for a frame using move semantics.
  143. *
  144. * @param[in] key The unique identification of a frame.
  145. *
  146. * @return Returns a lvalue reference to the current instance.
  147. */
  148. TraceEvent& SetKey(RecordKey&& key);
  149. /*!
  150. * @brief Sets the name of a module.
  151. *
  152. * @param[in] module_name The name of a module.
  153. *
  154. * @return Returns a lvalue reference to the current instance.
  155. */
  156. TraceEvent& SetModuleName(const std::string& module_name);
  157. /*!
  158. * @brief Sets the name of a module using move semantics.
  159. *
  160. * @param[in] module_name The name of a module.
  161. *
  162. * @return Returns a lvalue reference to the current instance.
  163. */
  164. TraceEvent& SetModuleName(std::string&& module_name);
  165. /*!
  166. * @brief Sets the name of a process.
  167. *
  168. * @param[in] process_name The name of a process.
  169. *
  170. * @return Returns a lvalue reference to the current instance.
  171. */
  172. TraceEvent& SetProcessName(const std::string& process_name);
  173. /*!
  174. * @brief Sets the name of a process using move semantics.
  175. *
  176. * @param[in] process_name The name of a process.
  177. *
  178. * @return Returns a lvalue reference to the current instance.
  179. */
  180. TraceEvent& SetProcessName(std::string&& process_name);
  181. /*!
  182. * @brief Sets the timestamp of this event.
  183. *
  184. * @param[in] time The timestamp of the event.
  185. *
  186. * @return Returns a lvalue reference to the current instance.
  187. */
  188. TraceEvent& SetTime(const Time& time);
  189. /*!
  190. * @brief Sets the timestamp of this event using move semantics.
  191. *
  192. * @param[in] time The timestamp of the event.
  193. *
  194. * @return Returns a lvalue reference to the current instance.
  195. */
  196. TraceEvent& SetTime(Time&& time);
  197. /*!
  198. * @brief Sets the level of this event.
  199. *
  200. * @param[in] level the level of the event.
  201. *
  202. * @return Returns a lvalue reference to the current instance.
  203. */
  204. TraceEvent& SetLevel(const Level& level);
  205. /*!
  206. * @brief Sets the type of this event.
  207. *
  208. * @param[in] type The type of th event.
  209. *
  210. * @return Returns a lvalue reference to the current instance.
  211. */
  212. TraceEvent& SetType(const Type& type);
  213. }; // class TraceEvent
  214. /*!
  215. * @struct TraceElem
  216. *
  217. * @brief The TraceElem is a structure describing a trace element used by profilers.
  218. */
  219. struct TraceElem {
  220. RecordKey key; /*!< The unique identification of a frame. */
  221. Time time; /*!< The timestamp of an event. */
  222. TraceEvent::Type type; /*!< The type of an event. It could be START or END. */
  223. /*!
  224. * @brief Constructs a TraceElem object by using default constructor.
  225. *
  226. * @return No return value.
  227. */
  228. TraceElem() = default;
  229. /*!
  230. * @brief Constructs a TraceElem object with the copy of the contents of another object.
  231. *
  232. * @param[in] other Another object used to initialize an object.
  233. *
  234. * @return No return value.
  235. */
  236. TraceElem(const TraceElem& other) = default;
  237. /*!
  238. * @brief Replaces the contents with a copy of the contents of another TraceElem object.
  239. *
  240. * @param[in] other Another object used to initialize the current object.
  241. *
  242. * @return Returns a lvalue reference to the current instance.
  243. */
  244. TraceElem& operator=(const TraceElem& other) = default;
  245. /*!
  246. * @brief Constructs a TraceElem object with the contents of another object using move semantics.
  247. *
  248. * @param[in] other Another object used to initialize an object.
  249. *
  250. * @return No return value.
  251. */
  252. TraceElem(TraceElem&& other);
  253. /*!
  254. * @brief Replaces the contents with those of another TraceElem object using move semantics.
  255. *
  256. * @param[in] other Another object used to initialize the current object.
  257. *
  258. * @return Returns a lvalue reference to the current instance.
  259. */
  260. TraceElem& operator=(TraceElem&& other);
  261. /*!
  262. * @brief Constructs a TraceElem object with a trace event.
  263. *
  264. * @param[in] event A specific trace event instance.
  265. *
  266. * @return No return value.
  267. */
  268. explicit TraceElem(const TraceEvent& event);
  269. /*!
  270. * @brief Constructs a TraceElem object with a trace event using move semantics.
  271. *
  272. * @param[in] event A specific trace event instance.
  273. *
  274. * @return No return value.
  275. */
  276. explicit TraceElem(TraceEvent&& event);
  277. }; // struct TraceElem
  278. /*!
  279. * Defines an alias for the std::vector<TraceElem>. ProcessTrace now denotes a vector which contains trace elements for
  280. * a process.
  281. */
  282. using ProcessTrace = std::vector<TraceElem>;
  283. /*!
  284. * Defines an alias for the std::unordered_map<std::string, ProcessTrace>. ModuleTrace now denotes an unordered map
  285. * which contains the pairs of the process name and the ProcessTrace object for a module.
  286. */
  287. using ModuleTrace = std::unordered_map<std::string, ProcessTrace>;
  288. /*!
  289. * @struct PipelineTrace
  290. *
  291. * @brief The PipelineTrace is a structure describing the trace data of a pipeline.
  292. */
  293. struct PipelineTrace {
  294. std::unordered_map<std::string, ProcessTrace> process_traces; /*!< The trace data of processes. */
  295. std::unordered_map<std::string, ModuleTrace> module_traces; /*!< The trace data of modules. */
  296. /*!
  297. * @brief Constructs a PipelineTrace object by using default constructor.
  298. *
  299. * @return No return value.
  300. */
  301. PipelineTrace() = default;
  302. /*!
  303. * @brief Constructs a PipelineTrace object with the copy of the contents of another object.
  304. *
  305. * @param[in] other Another object used to initialize an object.
  306. *
  307. * @return No return value.
  308. */
  309. PipelineTrace(const PipelineTrace& other) = default;
  310. /*!
  311. * @brief Replaces the contents with a copy of the contents of another PipelineTrace object.
  312. *
  313. * @param[in] other Another object used to initialize the current object.
  314. *
  315. * @return Returns a lvalue reference to the current instance.
  316. */
  317. PipelineTrace& operator=(const PipelineTrace& other) = default;
  318. /*!
  319. * @brief Constructs a PipelineTrace object with the contents of another object using move semantics.
  320. *
  321. * @param[in] other Another object used to initialize an object.
  322. *
  323. * @return No return value.
  324. */
  325. PipelineTrace(PipelineTrace&& other);
  326. /*!
  327. * @brief Replaces the contents with those of another PipelineTrace object using move semantics.
  328. *
  329. * @param[in] other Another object used to initialize the current object.
  330. *
  331. * @return Returns a lvalue reference to the current instance.
  332. */
  333. PipelineTrace& operator=(PipelineTrace&& other);
  334. }; // struct PipelineTrace
  335. inline TraceEvent::TraceEvent(const RecordKey& key) : key(key) {}
  336. inline TraceEvent::TraceEvent(RecordKey&& key) : key(std::forward<RecordKey>(key)) {}
  337. inline TraceEvent::TraceEvent(TraceEvent&& other) { *this = std::forward<TraceEvent>(other); }
  338. inline TraceEvent& TraceEvent::operator=(TraceEvent&& other) {
  339. key = std::move(other.key);
  340. module_name = std::move(other.module_name);
  341. process_name = std::move(other.process_name);
  342. time = std::move(other.time);
  343. level = other.level;
  344. type = other.type;
  345. return *this;
  346. }
  347. inline TraceEvent& TraceEvent::SetKey(const RecordKey& key) {
  348. this->key = key;
  349. return *this;
  350. }
  351. inline TraceEvent& TraceEvent::SetKey(RecordKey&& key) {
  352. this->key = std::forward<RecordKey>(key);
  353. return *this;
  354. }
  355. inline TraceEvent& TraceEvent::SetModuleName(const std::string& module_name) {
  356. this->module_name = module_name;
  357. return *this;
  358. }
  359. inline TraceEvent& TraceEvent::SetModuleName(std::string&& module_name) {
  360. this->module_name = std::forward<std::string>(module_name);
  361. return *this;
  362. }
  363. inline TraceEvent& TraceEvent::SetProcessName(const std::string& process_name) {
  364. this->process_name = process_name;
  365. return *this;
  366. }
  367. inline TraceEvent& TraceEvent::SetProcessName(std::string&& process_name) {
  368. this->process_name = std::forward<std::string>(process_name);
  369. return *this;
  370. }
  371. inline TraceEvent& TraceEvent::SetTime(const Time& time) {
  372. this->time = time;
  373. return *this;
  374. }
  375. inline TraceEvent& TraceEvent::SetTime(Time&& time) {
  376. this->time = std::forward<Time>(time);
  377. return *this;
  378. }
  379. inline TraceEvent& TraceEvent::SetLevel(const Level& level) {
  380. this->level = level;
  381. return *this;
  382. }
  383. inline TraceEvent& TraceEvent::SetType(const Type& type) {
  384. this->type = type;
  385. return *this;
  386. }
  387. inline TraceElem::TraceElem(TraceElem&& other) { *this = std::forward<TraceElem>(other); }
  388. inline TraceElem& TraceElem::operator=(TraceElem&& other) {
  389. key = std::move(other.key);
  390. time = std::move(other.time);
  391. type = other.type;
  392. return *this;
  393. }
  394. inline TraceElem::TraceElem(const TraceEvent& event) {
  395. key = event.key;
  396. time = event.time;
  397. type = event.type;
  398. }
  399. inline TraceElem::TraceElem(TraceEvent&& event) {
  400. key = std::move(event.key);
  401. time = std::move(event.time);
  402. type = event.type;
  403. }
  404. inline PipelineTrace::PipelineTrace(PipelineTrace&& other) { *this = std::forward<PipelineTrace>(other); }
  405. inline PipelineTrace& PipelineTrace::operator=(PipelineTrace&& other) {
  406. process_traces = std::move(other.process_traces);
  407. module_traces = std::move(other.module_traces);
  408. return *this;
  409. }
  410. } // namespace cnstream
  411. #endif // CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_TRACE_HPP_