stream_profiler.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_STREAM_PROFILER_HPP_
  21. #define CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_STREAM_PROFILER_HPP_
  22. #include <algorithm>
  23. #include <chrono>
  24. #include <string>
  25. #include "profiler/profile.hpp"
  26. /*!
  27. * @file stream_profiler.hpp
  28. *
  29. * This file contains a declaration of the StreamProfiler class.
  30. */
  31. namespace cnstream {
  32. /*!
  33. * @class StreamProfiler
  34. *
  35. * @brief StreamProfiler is responsible for the performance statistics of a certain processing process of a stream. It
  36. * is used by ProcessProfiler.
  37. *
  38. * @see cnstream::ProcessProfiler.
  39. */
  40. class StreamProfiler {
  41. using Duration = std::chrono::duration<double, std::milli>;
  42. public:
  43. /*!
  44. * @brief Constructs a StreamProfiler object.
  45. *
  46. * @param[in] stream_name The name of a stream.
  47. *
  48. * @return No return value.
  49. */
  50. explicit StreamProfiler(const std::string& stream_name);
  51. /*!
  52. * @brief Accumulates latency to total latency.
  53. *
  54. * @param[in] latency The latency to be added. The latency will be accumulated to total latency.
  55. *
  56. * @return Returns a lvalue reference to the current instance.
  57. */
  58. StreamProfiler& AddLatency(const Duration& latency);
  59. /*!
  60. * @brief Updates physical time that a stream costs.
  61. *
  62. * @param[in] time The physical time a stream costs.
  63. *
  64. * @return Returns a lvalue reference to the current instance.
  65. */
  66. StreamProfiler& UpdatePhysicalTime(const Duration& time);
  67. /*!
  68. * @brief Accumulates dropped frame count.
  69. *
  70. * @param[in] dropped The dropped frame count.
  71. *
  72. * @return Returns a lvalue reference to the current instance.
  73. */
  74. StreamProfiler& AddDropped(uint64_t dropped);
  75. /*!
  76. * @brief Accumulates completed frame count with 1.
  77. *
  78. * @return Returns a lvalue reference to the current instance.
  79. */
  80. StreamProfiler& AddCompleted();
  81. /*!
  82. * @brief Gets the name of the stream.
  83. *
  84. * @return Returns the name of the stream.
  85. */
  86. std::string GetName() const;
  87. /*!
  88. * @brief Gets the performance statistics for this stream.
  89. *
  90. * @return Returns the performance statistics for this stream.
  91. */
  92. StreamProfile GetProfile();
  93. private:
  94. std::string stream_name_ = "";
  95. uint64_t completed_ = 0;
  96. uint64_t latency_add_times_ = 0;
  97. uint64_t dropped_ = 0;
  98. Duration total_latency_ = Duration::zero();
  99. Duration maximum_latency_ = Duration::zero();
  100. Duration minimum_latency_ = Duration::max();
  101. Duration total_phy_time_ = Duration::zero();
  102. }; // class StreamProfiler
  103. inline StreamProfiler& StreamProfiler::AddLatency(const Duration& latency) {
  104. latency_add_times_++;
  105. total_latency_ += latency;
  106. maximum_latency_ = std::max(latency, maximum_latency_);
  107. minimum_latency_ = std::min(latency, minimum_latency_);
  108. return *this;
  109. }
  110. inline StreamProfiler& StreamProfiler::UpdatePhysicalTime(const Duration& time) {
  111. total_phy_time_ = time;
  112. return *this;
  113. }
  114. inline StreamProfiler& StreamProfiler::AddDropped(uint64_t dropped) {
  115. dropped_ += dropped;
  116. return *this;
  117. }
  118. inline StreamProfiler& StreamProfiler::AddCompleted() {
  119. completed_++;
  120. return *this;
  121. }
  122. inline std::string StreamProfiler::GetName() const {
  123. return stream_name_;
  124. }
  125. } // namespace cnstream
  126. #endif // CNSTREAM_FRAMEWORK_CORE_INCLUDE_PROFILER_STREAM_PROFILER_HPP_