pipeline_tracer.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_TRACER_HPP_
  21. #define CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_PIPELINE_TRACER_HPP_
  22. #include <chrono>
  23. #include <memory>
  24. #include <string>
  25. #include "cnstream_common.hpp"
  26. #include "profiler/trace.hpp"
  27. /*!
  28. * @file pipeline_tracer.hpp
  29. *
  30. * This file contains a declaration of the PipelineTracer class.
  31. */
  32. namespace cnstream {
  33. template<typename T>
  34. class CircularBuffer;
  35. /*!
  36. * @class PipelineTracer
  37. *
  38. * @brief PipelineTracer is a class for recording trace events of the pipeline.
  39. */
  40. class PipelineTracer : private NonCopyable {
  41. public:
  42. /*!
  43. * @brief Constructs a PipelineTracer object.
  44. *
  45. * @param[in] capacity The capacity to store trace events.
  46. *
  47. * @return No return value.
  48. */
  49. explicit PipelineTracer(size_t capacity = 100000);
  50. /*!
  51. * @brief Destructs a PipelineTracer object.
  52. *
  53. * @return No return value.
  54. */
  55. ~PipelineTracer();
  56. /*!
  57. * @brief Records a trace event using value reference semantics.
  58. *
  59. * @param[in] event The trace event.
  60. *
  61. * @return No return value.
  62. */
  63. void RecordEvent(const TraceEvent& event);
  64. /*!
  65. * @brief Records a trace event using move semantics.
  66. *
  67. * @param[in] event The trace event.
  68. *
  69. * @return No return value.
  70. */
  71. void RecordEvent(TraceEvent&& event);
  72. /*!
  73. * @brief Gets the trace data of the pipeline for a specified period of time.
  74. *
  75. * @param[in] start The start time.
  76. * @param[in] end The end time.
  77. *
  78. * @return Returns the trace data of the pipeline.
  79. */
  80. PipelineTrace GetTrace(const Time& start, const Time& end) const;
  81. /*!
  82. * @brief Gets the trace data of the pipeline for a specified period of time.
  83. *
  84. * @param[in] end The end time
  85. * @param[in] duration The duration in milliseconds. The start time is the end time minus duration.
  86. *
  87. * @return Returns the trace data of the pipeline.
  88. */
  89. PipelineTrace GetTraceBefore(const Time& end, const Duration& duration) const;
  90. /*!
  91. * @brief Gets the trace data of the pipeline for a specified period of time.
  92. *
  93. * @param[in] start The start time.
  94. * @param[in] duration The duration in milliseconds. The end time is the start time plus duration.
  95. *
  96. * @return Returns the trace data of the pipeline.
  97. */
  98. PipelineTrace GetTraceAfter(const Time& start, const Duration& duration) const;
  99. private:
  100. CircularBuffer<TraceEvent>* buffer_ = nullptr;
  101. }; // class PipelineTracer
  102. inline PipelineTrace PipelineTracer::GetTraceBefore(const Time& end, const Duration& duration) const {
  103. return GetTrace(std::chrono::time_point_cast<Clock::duration>(end - duration), end);
  104. }
  105. inline PipelineTrace PipelineTracer::GetTraceAfter(const Time& start, const Duration& duration) const {
  106. return GetTrace(start, std::chrono::time_point_cast<Clock::duration>(start + duration));
  107. }
  108. } // namespace cnstream
  109. #endif // CNSTREAM_FRAMEWORK_CORE_INCLUDE_TRACER_PIPELINE_TRACER_HPP_