NvElementProfiler.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of NVIDIA CORPORATION nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  24. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef __NV_ELEMENT_PROFILER_H__
  29. #define __NV_ELEMENT_PROFILER_H__
  30. #include <iostream>
  31. #include <pthread.h>
  32. #include <map>
  33. #include <stdint.h>
  34. #include <sys/time.h>
  35. /**
  36. * @file
  37. * <b>NVIDIA Multimedia API: Element Profiler API</b>
  38. *
  39. * @b Description: This file profiles the performance of individual elements.
  40. */
  41. /**
  42. *
  43. * Helper class for profiling the performance of individual elements.
  44. *
  45. * NvElementProfiler currently measures processing latencies, average processing rate, and
  46. * the number of units that arrived late at the element. Components should use this
  47. * information internally.
  48. *
  49. * If you require latency measurements,
  50. * you must call startProcessing() to indicate that a unit has been submitted
  51. * for processing and finishProcessing() to indicate that a unit has finished processing.
  52. * If you require only averaging processing rate or the number of units that
  53. * arrived late need not call startProcessing().
  54. *
  55. * You can get data from NvElementProfiler using getProfilerData(). This function
  56. * fills the [NvElementProfilerData](@ref NvElementProfiler::NvElementProfilerData)
  57. * structure. Components that do not support all
  58. * the fields available in the structure must use the variable
  59. * [valid_fields](@ref NvElementProfiler::NvElementProfilerData::valid_fields) of
  60. * type [ProfilerField](@ref NvElementProfiler::ProfilerField), which is also
  61. * included in the structure.
  62. *
  63. * @defgroup l4t_mm_nvelementprofiler_group Element Profiler API
  64. * @ingroup aa_framework_api_group
  65. * @{
  66. */
  67. class NvElementProfiler {
  68. public:
  69. /**
  70. * @defgroup Defines @c valid_field values for the #NvElementProfilerData structure.
  71. * @ingroup l4t_mm_nvelementprofiler_group
  72. * @{
  73. */
  74. typedef int ProfilerField;
  75. static const ProfilerField PROFILER_FIELD_NONE = 0;
  76. static const ProfilerField PROFILER_FIELD_TOTAL_UNITS = 1;
  77. static const ProfilerField PROFILER_FIELD_LATE_UNITS = 2;
  78. static const ProfilerField PROFILER_FIELD_LATENCIES = 4;
  79. static const ProfilerField PROFILER_FIELD_FPS = 8;
  80. static const ProfilerField PROFILER_FIELD_ALL = (PROFILER_FIELD_FPS << 1) - 1;
  81. /** @} */
  82. /**
  83. * Holds profiling data for the element.
  84. *
  85. * Some elements may not support all the fields in the structure. User must check
  86. * the @a valid_fields flag to determine which fields are valid.
  87. */
  88. typedef struct {
  89. /** Valid Fields which are supported by the element. */
  90. ProfilerField valid_fields;
  91. /** Average latency of all processed units, in microseconds. */
  92. uint64_t average_latency_usec;
  93. /** Minimum of latencies for each processed units, in microseconds. */
  94. uint64_t min_latency_usec;
  95. /** Maximum of latencies for each processed units, in microseconds. */
  96. uint64_t max_latency_usec;
  97. /** Total units processed. */
  98. uint64_t total_processed_units;
  99. /** Number of units which arrived late at the element. */
  100. uint64_t num_late_units;
  101. /** Average rate at which the units were processed. */
  102. float average_fps;
  103. /** Total profiling time. */
  104. struct timeval profiling_time;
  105. } NvElementProfilerData;
  106. /**
  107. * Gets the profiling data for the element.
  108. *
  109. * @param[out] data Reference to the NvElementProfilerData structure which should be filled.
  110. */
  111. void getProfilerData(NvElementProfilerData &data);
  112. /**
  113. * Prints the element's profiling data to an output stream.
  114. *
  115. * @param[in] out_stream Reference to a std::ostream.
  116. */
  117. void printProfilerData(std::ostream &out_stream = std::cout);
  118. /**
  119. * Informs the profiler that processing has started.
  120. *
  121. * Has no effect if profiler is disabled.
  122. *
  123. * @return ID of the unit, to be supplied with finishProcessing();.
  124. */
  125. uint64_t startProcessing();
  126. /**
  127. * Informs the profiler that processing has finished.
  128. *
  129. * Has no effect if profiler is disabled.
  130. *
  131. * @param[in] id ID of the unit whose processing is finished,
  132. * 0 if the first unit in the profiler's queue should be picked.
  133. * @param[in] is_late Should be true if the frame arrived late at the element.
  134. */
  135. void finishProcessing(uint64_t id, bool is_late);
  136. /**
  137. * Enables the profiler.
  138. *
  139. * startProcessing() and finishProcessing() are ineffective until the profiler is enabled.
  140. *
  141. * @param[in] reset_data Reset the profiled data.
  142. */
  143. void enableProfiling(bool reset_data);
  144. /**
  145. * Disables the profiler.
  146. */
  147. void disableProfiling();
  148. private:
  149. /**
  150. * Resets the profiler data.
  151. */
  152. void reset();
  153. pthread_mutex_t profiler_lock; /**< Mutex to synchronize multithreaded access to profiler data. */
  154. bool enabled; /**< Flag indicating if profiler is enabled. */
  155. const ProfilerField valid_fields; /**< Valid fields for the element. */
  156. struct NvElementProfilerDataInternal : NvElementProfilerData {
  157. /** Wall-clock time at which the first unit was processed. */
  158. struct timeval start_time;
  159. /** Wall-clock time at which the latest unit was processed. */
  160. struct timeval stop_time;
  161. /** Total accumulated time.
  162. * When performance measurement is restarted @a start_time and @a stop_time
  163. * are reset. This field is used to accumulate time before
  164. * resetting. */
  165. struct timeval accumulated_time;
  166. /** Total accumulated latency for all units, in microseconds. */
  167. uint64_t total_latency;
  168. } data_int;
  169. /** Queue used to maintain the timestamps of when the unit
  170. * processing started. Required to calculate latency. */
  171. std::map<uint64_t, struct timeval> unit_start_time_queue;
  172. uint64_t unit_id_counter; /**< Unique ID of the last unit. */
  173. /**
  174. * Constructor for NvElementProfiler.
  175. *
  176. * Initializes internal data structures. The profiler is disabled by default.
  177. * @param fields
  178. */
  179. NvElementProfiler(ProfilerField fields);
  180. /**
  181. * Disallow copy constructor.
  182. */
  183. NvElementProfiler(const NvElementProfiler& that);
  184. /**
  185. * Disallow assignment.
  186. */
  187. void operator=(NvElementProfiler const&);
  188. ~NvElementProfiler();
  189. friend class NvElement;
  190. };
  191. /** @} */
  192. #endif