trace_serialize_helper.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_TRACE_SERIALIZE_HELPER_HPP_
  21. #define CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_TRACE_SERIALIZE_HELPER_HPP_
  22. #include <rapidjson/document.h>
  23. #include <rapidjson/rapidjson.h>
  24. #include <rapidjson/stringbuffer.h>
  25. #include <rapidjson/writer.h>
  26. #include <fstream>
  27. #include <string>
  28. #include "cnstream_common.hpp"
  29. #include "cnstream_logging.hpp"
  30. #include "trace.hpp"
  31. /*!
  32. * @file trace_serialize_helper.hpp
  33. *
  34. * This file contains a declaration of the TraceSerializeHelper class.
  35. */
  36. namespace cnstream {
  37. /*!
  38. * @class TraceSerializeHelper
  39. *
  40. * @brief Serializes trace data into JSON format. You can load JSON file by chrome-tracing to show the trace data.
  41. */
  42. class TraceSerializeHelper {
  43. public:
  44. /*!
  45. * @brief Deserializes a JSON string.
  46. *
  47. * @param[in] jsonstr The JSON string.
  48. * @param[out] pout The output pointer stores the results.
  49. *
  50. * @return Returns true if the JSON string is deserialized successfully, otherwise returns false.
  51. */
  52. static bool DeserializeFromJSONStr(const std::string& jsonstr, TraceSerializeHelper* pout);
  53. /*!
  54. * @brief Deserializes a JSON file.
  55. *
  56. * @param[in] jsonstr The JSON file path.
  57. * @param[out] pout The output pointer stores the results.
  58. *
  59. * @return Returns true if the JSON string is deserialized successfully, otherwise returns false.
  60. */
  61. static bool DeserializeFromJSONFile(const std::string& filename, TraceSerializeHelper* pout);
  62. /*!
  63. * @brief Constructs a TraceSerializeHelper object.
  64. *
  65. * @return No return value.
  66. */
  67. TraceSerializeHelper();
  68. /*!
  69. * @brief Constructs a TraceSerializeHelper object with the copy of the contents of another object.
  70. *
  71. * @param[in] other Another object used to initialize an object.
  72. *
  73. * @return No return value.
  74. */
  75. TraceSerializeHelper(const TraceSerializeHelper& other);
  76. /*!
  77. * @brief Constructs a TraceSerializeHelper object with the contents of another object using move semantics.
  78. *
  79. * @param[in] other Another object used to initialize an object.
  80. *
  81. * @return No return value.
  82. */
  83. TraceSerializeHelper(TraceSerializeHelper&& other);
  84. /*!
  85. * @brief Replaces the contents with a copy of the contents of another TraceSerializeHelper object.
  86. *
  87. * @param[in] other Another object used to initialize the current object.
  88. *
  89. * @return Returns a lvalue reference to the current instance.
  90. */
  91. TraceSerializeHelper& operator=(const TraceSerializeHelper& other);
  92. /*!
  93. * @brief Replaces the contents with those of another TraceSerializeHelper object using move semantics.
  94. *
  95. * @param[in] other Another object used to initialize the current object.
  96. *
  97. * @return Returns a lvalue reference to the current instance.
  98. */
  99. TraceSerializeHelper& operator=(TraceSerializeHelper&& other);
  100. /*!
  101. * @brief Destructs a TraceSerializeHelper object by using default constructor.
  102. *
  103. * @return No return value.
  104. */
  105. ~TraceSerializeHelper() = default;
  106. /*!
  107. * @brief Serializes trace data.
  108. *
  109. * @param[in] pipeline_trace The trace data. Get it by ``pipeline.GetTracer()->GetTrace()``.
  110. *
  111. * @return No return value.
  112. */
  113. void Serialize(const PipelineTrace& pipeline_trace);
  114. /*!
  115. * @brief Merges another trace serialization helper tool data.
  116. *
  117. * @param[in] t The trace serialization helper tool to be merged.
  118. *
  119. * @return No return value.
  120. */
  121. void Merge(const TraceSerializeHelper& t);
  122. /*!
  123. * @brief Serializes to a JSON string.
  124. *
  125. * @return Returns a JSON string.
  126. */
  127. std::string ToJsonStr() const;
  128. /*!
  129. * @brief Serializes to a JSON file.
  130. *
  131. * @param[in] filename The JSON file name.
  132. *
  133. * @return Returns true if the serialization is successful, otherwise returns false.
  134. *
  135. * @note the possible reason of serialization failure is that writing to the file is not permitted.
  136. */
  137. bool ToFile(const std::string& filename) const;
  138. /*!
  139. * @brief Resets serialization helper. Clears data and frees up memory.
  140. *
  141. * @return No return value.
  142. */
  143. void Reset();
  144. private:
  145. rapidjson::Document doc_;
  146. }; // class TraceSerializeHelper
  147. inline
  148. bool TraceSerializeHelper::ToFile(const std::string& filename) const {
  149. std::ofstream ofs(filename);
  150. if (!ofs.is_open()) {
  151. LOGE(PROFILER) << "Open or create file failed. filename: " << filename;
  152. return false;
  153. }
  154. ofs << ToJsonStr();
  155. ofs.close();
  156. return true;
  157. }
  158. } // namespace cnstream
  159. #endif // CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_TRACE_SERIALIZE_HELPER_HPP_