NvApplicationProfiler.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #include "NvApplicationProfiler.h"
  29. #include <fstream>
  30. #include <sstream>
  31. #include <pthread.h>
  32. #include <string.h>
  33. #include <sys/types.h>
  34. #include <unistd.h>
  35. #define GOVERNOR_SYS_FILE "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
  36. #define CPU_FREQ_FILE "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
  37. #define REQUIRED_GOVERNOR "performance"
  38. #define TIMESPEC_DIFF_USEC(timespec1, timespec2) \
  39. (timespec1.tv_sec - timespec2.tv_sec) * 1000000.0 + \
  40. (timespec1.tv_nsec - timespec2.tv_nsec) / 1000.0
  41. using namespace std;
  42. NvApplicationProfiler::NvApplicationProfiler()
  43. {
  44. char governor[64];
  45. uint64_t cpu_freq_khz;
  46. memset(&data, 0, sizeof(data));
  47. running = false;
  48. data.max_cpu_usage = 0;
  49. data.min_cpu_usage = 100;
  50. sampling_interval = DefaultSamplingInterval;
  51. profiling_thread = 0;
  52. pthread_mutex_init(&thread_lock, NULL);
  53. check_cpu_usage = true;
  54. ifstream cpu_governor_file(GOVERNOR_SYS_FILE, std::ifstream::in);
  55. cpu_governor_file >> governor;
  56. if (strcmp(governor, REQUIRED_GOVERNOR))
  57. {
  58. cerr << "Set governor to " REQUIRED_GOVERNOR " before enabling profiler"
  59. << endl;
  60. check_cpu_usage = false;
  61. }
  62. num_cpu_cores = sysconf(_SC_NPROCESSORS_ONLN);
  63. ifstream cpu_freq_file(CPU_FREQ_FILE, std::ifstream::in);
  64. cpu_freq_file >> cpu_freq_khz;
  65. cpu_freq = cpu_freq_khz / 1000;
  66. }
  67. NvApplicationProfiler&
  68. NvApplicationProfiler::getProfilerInstance()
  69. {
  70. static NvApplicationProfiler profiler;
  71. return profiler;
  72. }
  73. void
  74. NvApplicationProfiler::start(uint32_t sampling_interval_ms)
  75. {
  76. pthread_mutex_lock(&thread_lock);
  77. if (running)
  78. {
  79. pthread_mutex_unlock(&thread_lock);
  80. return;
  81. }
  82. if (!check_cpu_usage)
  83. {
  84. cerr << "Set governor to " REQUIRED_GOVERNOR " before enabling profiler"
  85. << endl;
  86. pthread_mutex_unlock(&thread_lock);
  87. return;
  88. }
  89. running = true;
  90. sampling_interval = sampling_interval_ms;
  91. memset(&data, 0, sizeof(data));
  92. gettimeofday(&data.start_time, NULL);
  93. if (check_cpu_usage)
  94. {
  95. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &data.start_proc_cpu_clock_time);
  96. clock_gettime(CLOCK_MONOTONIC, &data.start_cpu_clock_time);
  97. }
  98. pthread_create(&profiling_thread, NULL, ProfilerThread, this);
  99. pthread_setname_np(profiling_thread, "ProfilingThread");
  100. pthread_mutex_unlock(&thread_lock);
  101. }
  102. void
  103. NvApplicationProfiler::stop()
  104. {
  105. running = false;
  106. pthread_join(profiling_thread, NULL);
  107. pthread_mutex_lock(&thread_lock);
  108. gettimeofday(&data.stop_time, NULL);
  109. pthread_mutex_unlock(&thread_lock);
  110. }
  111. void
  112. NvApplicationProfiler::profile()
  113. {
  114. if (check_cpu_usage)
  115. {
  116. struct timespec cur_proc_cpu_clock_time;
  117. struct timespec cur_cpu_clock_time;
  118. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &cur_proc_cpu_clock_time);
  119. clock_gettime(CLOCK_MONOTONIC, &cur_cpu_clock_time);
  120. if (data.num_readings)
  121. {
  122. float proc_cpu_time = TIMESPEC_DIFF_USEC(cur_proc_cpu_clock_time,
  123. data.stop_proc_cpu_clock_time);
  124. float total_cpu_time = TIMESPEC_DIFF_USEC(cur_cpu_clock_time,
  125. data.stop_cpu_clock_time);
  126. float cpu_usage = proc_cpu_time * 100 / total_cpu_time;
  127. if (cpu_usage < data.min_cpu_usage && cpu_usage > 0)
  128. {
  129. data.min_cpu_usage = cpu_usage;
  130. }
  131. if (cpu_usage > data.max_cpu_usage)
  132. {
  133. data.max_cpu_usage = cpu_usage;
  134. }
  135. }
  136. data.stop_proc_cpu_clock_time = cur_proc_cpu_clock_time;
  137. data.stop_cpu_clock_time = cur_cpu_clock_time;
  138. }
  139. data.num_readings++;
  140. }
  141. void *
  142. NvApplicationProfiler::ProfilerThread(void * data)
  143. {
  144. NvApplicationProfiler *profiler = (NvApplicationProfiler *) data;
  145. struct timespec next_profile_time;
  146. struct timeval now;
  147. pthread_cond_t sleep_cond;
  148. pthread_cond_init(&sleep_cond, NULL);
  149. gettimeofday(&now, NULL);
  150. next_profile_time.tv_sec = now.tv_sec;
  151. next_profile_time.tv_nsec = now.tv_usec * 1000L;
  152. pthread_mutex_lock(&profiler->thread_lock);
  153. while (profiler->running)
  154. {
  155. pthread_cond_timedwait(&sleep_cond, &profiler->thread_lock,
  156. &next_profile_time);
  157. profiler->profile();
  158. next_profile_time.tv_sec += profiler->sampling_interval / 1000;
  159. next_profile_time.tv_nsec += (profiler->sampling_interval % 1000) * 1000000L;
  160. next_profile_time.tv_sec += next_profile_time.tv_nsec / 1000000000L;
  161. next_profile_time.tv_nsec %= 1000000000L;
  162. }
  163. pthread_mutex_unlock(&profiler->thread_lock);
  164. return NULL;
  165. }
  166. void
  167. NvApplicationProfiler::getProfilerData(NvAppProfilerData &pdata)
  168. {
  169. pthread_mutex_lock(&thread_lock);
  170. memset (&pdata, 0, sizeof(pdata));
  171. if (check_cpu_usage)
  172. {
  173. float proc_cpu_time = TIMESPEC_DIFF_USEC(data.stop_proc_cpu_clock_time,
  174. data.start_proc_cpu_clock_time);
  175. float total_cpu_time = TIMESPEC_DIFF_USEC(data.stop_cpu_clock_time,
  176. data.start_cpu_clock_time);
  177. pdata.peak_cpu_usage = data.max_cpu_usage / num_cpu_cores;
  178. pdata.avg_cpu_usage = proc_cpu_time * 100 / total_cpu_time / num_cpu_cores;
  179. pdata.total_time.tv_sec = data.stop_time.tv_sec - data.start_time.tv_sec;
  180. pdata.total_time.tv_usec = data.stop_time.tv_usec - data.start_time.tv_usec;
  181. if (pdata.total_time.tv_usec < 0)
  182. {
  183. pdata.total_time.tv_sec--;
  184. pdata.total_time.tv_usec += 1000000;
  185. }
  186. pdata.num_cpu_cores = num_cpu_cores;
  187. pdata.cpu_freq_mhz = cpu_freq;
  188. }
  189. pthread_mutex_unlock(&thread_lock);
  190. }
  191. void
  192. NvApplicationProfiler::printProfilerData(std::ostream &outstream)
  193. {
  194. NvAppProfilerData data;
  195. getProfilerData(data);
  196. outstream << "************************************" << endl;
  197. outstream << "Total Profiling Time = " <<
  198. (data.total_time.tv_sec + 0.000001 * data.total_time.tv_usec) <<
  199. " sec" << endl;
  200. if (check_cpu_usage)
  201. {
  202. outstream << "Peak CPU Usage = " << data.peak_cpu_usage << "%" << endl;
  203. outstream << "Avg CPU Usage = " << data.avg_cpu_usage << "%" << endl;
  204. outstream << "Num. of Cores = " << data.num_cpu_cores << endl;
  205. outstream << "CPU frequency = " << data.cpu_freq_mhz << "MHz" << endl;
  206. }
  207. outstream << "************************************" << endl;
  208. }