NvApplicationProfiler.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /**
  29. * @file
  30. * <b>NVIDIA Multimedia API: Application Resource Profiling API</b>
  31. *
  32. * @b Description: This file declares the NvApplicationProfiler API.
  33. */
  34. #ifndef __NV_PROFILER_H__
  35. #define __NV_PROFILER_H__
  36. #include <iostream>
  37. #include <pthread.h>
  38. #include <stdint.h>
  39. #include <sys/time.h>
  40. #include <time.h>
  41. /**
  42. *
  43. * Helper class for profiling the overall application
  44. * resource usage.
  45. *
  46. * NvApplicationProfiler spawns a background thread which periodically measures resource
  47. * usage. This sampling interval can be configured. Smaller sampling intervals
  48. * may lead to more accurate results but the background thread itself will
  49. * have a higher CPU usage.
  50. *
  51. * Only one instance of NvApplicationProfiler object gets created for the application.
  52. * It can be accessed using getProfilerInstance().
  53. *
  54. * NvApplicationProfiler currently samples CPU usage and provides peak and average CPU
  55. * usage during the profiling duration. It requires that the CPU frequency be
  56. * constant over the entire duration. To force this, NvApplicationProfiler will start only
  57. * when the CPU governor is set to @b performance.
  58. *
  59. * @defgroup l4t_mm_nvapplicationprofiler_group Application Resource Profiler API
  60. * @ingroup aa_framework_api_group
  61. * @{
  62. */
  63. class NvApplicationProfiler
  64. {
  65. public:
  66. /**
  67. * Holds the profiling data.
  68. */
  69. typedef struct
  70. {
  71. /** Total time for which the profiler ran or is running. */
  72. struct timeval total_time;
  73. /** Peak CPU usage during the profiling time. */
  74. float peak_cpu_usage;
  75. /** Average CPU usage over the entire profiling duration. */
  76. float avg_cpu_usage;
  77. /** Number of cpu cores. */
  78. uint32_t num_cpu_cores;
  79. /** Operating frequency of the cpu in MHz. */
  80. uint32_t cpu_freq_mhz;
  81. } NvAppProfilerData;
  82. static const uint64_t DefaultSamplingInterval = 100;
  83. /**
  84. * Gets a reference to the global #NvApplicationProfiler instance.
  85. *
  86. * @return A reference to the global NvApplicationProfiler instance.
  87. */
  88. static NvApplicationProfiler& getProfilerInstance();
  89. /**
  90. * Starts the profiler with the specified sampling interval.
  91. *
  92. * This method resets the internal profiler data measurements.
  93. *
  94. * Starting an already started profiler does nothing.
  95. *
  96. * @param[in] sampling_interval_ms Sampling interval in milliseconds.
  97. */
  98. void start(uint32_t sampling_interval_ms);
  99. /**
  100. * Stops the profiler.
  101. */
  102. void stop();
  103. /**
  104. * Prints the profiler data to an output stream.
  105. *
  106. * @param[in] outstream A reference to an output stream of type std::ostream.
  107. * std::cout if not specified.
  108. */
  109. void printProfilerData(std::ostream &outstream = std::cout);
  110. /**
  111. * Gets the profiler data.
  112. *
  113. * @param[out] data Pointer to the ProfilerData structure to be filled.
  114. */
  115. void getProfilerData(NvAppProfilerData &data);
  116. private:
  117. /**
  118. * Method run by the background profiling thread.
  119. *
  120. * Waits on a condition till the next periodic sampling time and calls
  121. * profile() for measurement.
  122. *
  123. * Runs in an infinite loop till signaled to stop.
  124. */
  125. static void * ProfilerThread(void *);
  126. bool running; /**< Boolean flag indicating if profiling thread is running. */
  127. uint32_t sampling_interval; /**< Interval between two measurements,
  128. in milliseconds. */
  129. pthread_mutex_t thread_lock; /**< Lock for synchronized multithreaded
  130. access to NvApplicationProfiler::data */
  131. pthread_t profiling_thread; /** ID of the profiling thread running in
  132. background */
  133. uint32_t num_cpu_cores; /**< Number of CPU cores. */
  134. uint32_t cpu_freq; /**< Operating frequency of CPU cores in MHz. */
  135. bool check_cpu_usage; /**< Flag indicating if cpu usage should be checked. */
  136. /**
  137. * Holds resource usage readings (internal use only).
  138. */
  139. struct ProfilerDataInternal
  140. {
  141. /** Wall-clock time at which profiler was started. */
  142. struct timeval start_time;
  143. /** Wall-clock time at which profiler was stopped. */
  144. struct timeval stop_time;
  145. /** CPU clock time occupied by the process when the
  146. * profiler was started. */
  147. struct timespec start_proc_cpu_clock_time;
  148. /** Total CPU clock time when the profiler was started. */
  149. struct timespec start_cpu_clock_time;
  150. /** CPU clock time occupied by the process when the
  151. * latest readings were taken. */
  152. struct timespec stop_proc_cpu_clock_time;
  153. /** Total CPU clock time when the latest readings were taken. */
  154. struct timespec stop_cpu_clock_time;
  155. /** Maximum of CPU usages of all sampled periods. */
  156. float max_cpu_usage;
  157. /** Minimum of CPU usages of all sampled periods. */
  158. float min_cpu_usage;
  159. /** Average CPU usage over the entire profiling duration. */
  160. float avg_cpu_usage;
  161. /** Number of readings taken. */
  162. uint64_t num_readings;
  163. } data; /**< Internal structure to hold intermediate measurements. */
  164. /**
  165. * Measures resource usage parameters.
  166. */
  167. void profile();
  168. /**
  169. * Default constructor used by getProfilerInstance.
  170. */
  171. NvApplicationProfiler();
  172. /**
  173. * Disallows copy constructor.
  174. */
  175. NvApplicationProfiler(const NvApplicationProfiler& that);
  176. /**
  177. * Disallows assignment.
  178. */
  179. void operator=(NvApplicationProfiler const&);
  180. };
  181. /** @} */
  182. #endif