helper_timer.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions
  5. * are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of NVIDIA CORPORATION nor the names of its
  12. * contributors may be used to endorse or promote products derived
  13. * from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // Helper Timing Functions
  28. #ifndef COMMON_HELPER_TIMER_H_
  29. #define COMMON_HELPER_TIMER_H_
  30. #ifndef EXIT_WAIVED
  31. #define EXIT_WAIVED 2
  32. #endif
  33. // includes, system
  34. #include <vector>
  35. // includes, project
  36. #include <exception.h>
  37. // Definition of the StopWatch Interface, this is used if we don't want to use
  38. // the CUT functions But rather in a self contained class interface
  39. class StopWatchInterface {
  40. public:
  41. StopWatchInterface() {}
  42. virtual ~StopWatchInterface() {}
  43. public:
  44. //! Start time measurement
  45. virtual void start() = 0;
  46. //! Stop time measurement
  47. virtual void stop() = 0;
  48. //! Reset time counters to zero
  49. virtual void reset() = 0;
  50. //! Time in msec. after start. If the stop watch is still running (i.e. there
  51. //! was no call to stop()) then the elapsed time is returned, otherwise the
  52. //! time between the last start() and stop call is returned
  53. virtual float getTime() = 0;
  54. //! Mean time to date based on the number of times the stopwatch has been
  55. //! _stopped_ (ie finished sessions) and the current total time
  56. virtual float getAverageTime() = 0;
  57. };
  58. //////////////////////////////////////////////////////////////////
  59. // Begin Stopwatch timer class definitions for all OS platforms //
  60. //////////////////////////////////////////////////////////////////
  61. #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
  62. // includes, system
  63. #define WINDOWS_LEAN_AND_MEAN
  64. #include <windows.h>
  65. #undef min
  66. #undef max
  67. //! Windows specific implementation of StopWatch
  68. class StopWatchWin : public StopWatchInterface {
  69. public:
  70. //! Constructor, default
  71. StopWatchWin()
  72. : start_time(),
  73. end_time(),
  74. diff_time(0.0f),
  75. total_time(0.0f),
  76. running(false),
  77. clock_sessions(0),
  78. freq(0),
  79. freq_set(false) {
  80. if (!freq_set) {
  81. // helper variable
  82. LARGE_INTEGER temp;
  83. // get the tick frequency from the OS
  84. QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER *>(&temp));
  85. // convert to type in which it is needed
  86. freq = (static_cast<double>(temp.QuadPart)) / 1000.0;
  87. // rememeber query
  88. freq_set = true;
  89. }
  90. }
  91. // Destructor
  92. ~StopWatchWin() {}
  93. public:
  94. //! Start time measurement
  95. inline void start();
  96. //! Stop time measurement
  97. inline void stop();
  98. //! Reset time counters to zero
  99. inline void reset();
  100. //! Time in msec. after start. If the stop watch is still running (i.e. there
  101. //! was no call to stop()) then the elapsed time is returned, otherwise the
  102. //! time between the last start() and stop call is returned
  103. inline float getTime();
  104. //! Mean time to date based on the number of times the stopwatch has been
  105. //! _stopped_ (ie finished sessions) and the current total time
  106. inline float getAverageTime();
  107. private:
  108. // member variables
  109. //! Start of measurement
  110. LARGE_INTEGER start_time;
  111. //! End of measurement
  112. LARGE_INTEGER end_time;
  113. //! Time difference between the last start and stop
  114. float diff_time;
  115. //! TOTAL time difference between starts and stops
  116. float total_time;
  117. //! flag if the stop watch is running
  118. bool running;
  119. //! Number of times clock has been started
  120. //! and stopped to allow averaging
  121. int clock_sessions;
  122. //! tick frequency
  123. double freq;
  124. //! flag if the frequency has been set
  125. bool freq_set;
  126. };
  127. // functions, inlined
  128. ////////////////////////////////////////////////////////////////////////////////
  129. //! Start time measurement
  130. ////////////////////////////////////////////////////////////////////////////////
  131. inline void StopWatchWin::start() {
  132. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER *>(&start_time));
  133. running = true;
  134. }
  135. ////////////////////////////////////////////////////////////////////////////////
  136. //! Stop time measurement and increment add to the current diff_time summation
  137. //! variable. Also increment the number of times this clock has been run.
  138. ////////////////////////////////////////////////////////////////////////////////
  139. inline void StopWatchWin::stop() {
  140. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER *>(&end_time));
  141. diff_time = static_cast<float>(((static_cast<double>(end_time.QuadPart) -
  142. static_cast<double>(start_time.QuadPart)) /
  143. freq));
  144. total_time += diff_time;
  145. clock_sessions++;
  146. running = false;
  147. }
  148. ////////////////////////////////////////////////////////////////////////////////
  149. //! Reset the timer to 0. Does not change the timer running state but does
  150. //! recapture this point in time as the current start time if it is running.
  151. ////////////////////////////////////////////////////////////////////////////////
  152. inline void StopWatchWin::reset() {
  153. diff_time = 0;
  154. total_time = 0;
  155. clock_sessions = 0;
  156. if (running) {
  157. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER *>(&start_time));
  158. }
  159. }
  160. ////////////////////////////////////////////////////////////////////////////////
  161. //! Time in msec. after start. If the stop watch is still running (i.e. there
  162. //! was no call to stop()) then the elapsed time is returned added to the
  163. //! current diff_time sum, otherwise the current summed time difference alone
  164. //! is returned.
  165. ////////////////////////////////////////////////////////////////////////////////
  166. inline float StopWatchWin::getTime() {
  167. // Return the TOTAL time to date
  168. float retval = total_time;
  169. if (running) {
  170. LARGE_INTEGER temp;
  171. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER *>(&temp));
  172. retval += static_cast<float>(((static_cast<double>(temp.QuadPart) -
  173. static_cast<double>(start_time.QuadPart)) /
  174. freq));
  175. }
  176. return retval;
  177. }
  178. ////////////////////////////////////////////////////////////////////////////////
  179. //! Time in msec. for a single run based on the total number of COMPLETED runs
  180. //! and the total time.
  181. ////////////////////////////////////////////////////////////////////////////////
  182. inline float StopWatchWin::getAverageTime() {
  183. return (clock_sessions > 0) ? (total_time / clock_sessions) : 0.0f;
  184. }
  185. #else
  186. // Declarations for Stopwatch on Linux and Mac OSX
  187. // includes, system
  188. #include <sys/time.h>
  189. #include <ctime>
  190. //! Windows specific implementation of StopWatch
  191. class StopWatchLinux : public StopWatchInterface {
  192. public:
  193. //! Constructor, default
  194. StopWatchLinux()
  195. : start_time(),
  196. diff_time(0.0),
  197. total_time(0.0),
  198. running(false),
  199. clock_sessions(0) {}
  200. // Destructor
  201. virtual ~StopWatchLinux() {}
  202. public:
  203. //! Start time measurement
  204. inline void start();
  205. //! Stop time measurement
  206. inline void stop();
  207. //! Reset time counters to zero
  208. inline void reset();
  209. //! Time in msec. after start. If the stop watch is still running (i.e. there
  210. //! was no call to stop()) then the elapsed time is returned, otherwise the
  211. //! time between the last start() and stop call is returned
  212. inline float getTime();
  213. //! Mean time to date based on the number of times the stopwatch has been
  214. //! _stopped_ (ie finished sessions) and the current total time
  215. inline float getAverageTime();
  216. private:
  217. // helper functions
  218. //! Get difference between start time and current time
  219. inline float getDiffTime();
  220. private:
  221. // member variables
  222. //! Start of measurement
  223. struct timeval start_time;
  224. //! Time difference between the last start and stop
  225. float diff_time;
  226. //! TOTAL time difference between starts and stops
  227. float total_time;
  228. //! flag if the stop watch is running
  229. bool running;
  230. //! Number of times clock has been started
  231. //! and stopped to allow averaging
  232. int clock_sessions;
  233. };
  234. // functions, inlined
  235. ////////////////////////////////////////////////////////////////////////////////
  236. //! Start time measurement
  237. ////////////////////////////////////////////////////////////////////////////////
  238. inline void StopWatchLinux::start() {
  239. gettimeofday(&start_time, 0);
  240. running = true;
  241. }
  242. ////////////////////////////////////////////////////////////////////////////////
  243. //! Stop time measurement and increment add to the current diff_time summation
  244. //! variable. Also increment the number of times this clock has been run.
  245. ////////////////////////////////////////////////////////////////////////////////
  246. inline void StopWatchLinux::stop() {
  247. diff_time = getDiffTime();
  248. total_time += diff_time;
  249. running = false;
  250. clock_sessions++;
  251. }
  252. ////////////////////////////////////////////////////////////////////////////////
  253. //! Reset the timer to 0. Does not change the timer running state but does
  254. //! recapture this point in time as the current start time if it is running.
  255. ////////////////////////////////////////////////////////////////////////////////
  256. inline void StopWatchLinux::reset() {
  257. diff_time = 0;
  258. total_time = 0;
  259. clock_sessions = 0;
  260. if (running) {
  261. gettimeofday(&start_time, 0);
  262. }
  263. }
  264. ////////////////////////////////////////////////////////////////////////////////
  265. //! Time in msec. after start. If the stop watch is still running (i.e. there
  266. //! was no call to stop()) then the elapsed time is returned added to the
  267. //! current diff_time sum, otherwise the current summed time difference alone
  268. //! is returned.
  269. ////////////////////////////////////////////////////////////////////////////////
  270. inline float StopWatchLinux::getTime() {
  271. // Return the TOTAL time to date
  272. float retval = total_time;
  273. if (running) {
  274. retval += getDiffTime();
  275. }
  276. return retval;
  277. }
  278. ////////////////////////////////////////////////////////////////////////////////
  279. //! Time in msec. for a single run based on the total number of COMPLETED runs
  280. //! and the total time.
  281. ////////////////////////////////////////////////////////////////////////////////
  282. inline float StopWatchLinux::getAverageTime() {
  283. return (clock_sessions > 0) ? (total_time / clock_sessions) : 0.0f;
  284. }
  285. ////////////////////////////////////////////////////////////////////////////////
  286. ////////////////////////////////////////////////////////////////////////////////
  287. inline float StopWatchLinux::getDiffTime() {
  288. struct timeval t_time;
  289. gettimeofday(&t_time, 0);
  290. // time difference in milli-seconds
  291. return static_cast<float>(1000.0 * (t_time.tv_sec - start_time.tv_sec) +
  292. (0.001 * (t_time.tv_usec - start_time.tv_usec)));
  293. }
  294. #endif // WIN32
  295. ////////////////////////////////////////////////////////////////////////////////
  296. //! Timer functionality exported
  297. ////////////////////////////////////////////////////////////////////////////////
  298. //! Create a new timer
  299. //! @return true if a time has been created, otherwise false
  300. //! @param name of the new timer, 0 if the creation failed
  301. ////////////////////////////////////////////////////////////////////////////////
  302. inline bool sdkCreateTimer(StopWatchInterface **timer_interface) {
  303. // printf("sdkCreateTimer called object %08x\n", (void *)*timer_interface);
  304. #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
  305. *timer_interface = reinterpret_cast<StopWatchInterface *>(new StopWatchWin());
  306. #else
  307. *timer_interface =
  308. reinterpret_cast<StopWatchInterface *>(new StopWatchLinux());
  309. #endif
  310. return (*timer_interface != NULL) ? true : false;
  311. }
  312. ////////////////////////////////////////////////////////////////////////////////
  313. //! Delete a timer
  314. //! @return true if a time has been deleted, otherwise false
  315. //! @param name of the timer to delete
  316. ////////////////////////////////////////////////////////////////////////////////
  317. inline bool sdkDeleteTimer(StopWatchInterface **timer_interface) {
  318. // printf("sdkDeleteTimer called object %08x\n", (void *)*timer_interface);
  319. if (*timer_interface) {
  320. delete *timer_interface;
  321. *timer_interface = NULL;
  322. }
  323. return true;
  324. }
  325. ////////////////////////////////////////////////////////////////////////////////
  326. //! Start the time with name \a name
  327. //! @param name name of the timer to start
  328. ////////////////////////////////////////////////////////////////////////////////
  329. inline bool sdkStartTimer(StopWatchInterface **timer_interface) {
  330. // printf("sdkStartTimer called object %08x\n", (void *)*timer_interface);
  331. if (*timer_interface) {
  332. (*timer_interface)->start();
  333. }
  334. return true;
  335. }
  336. ////////////////////////////////////////////////////////////////////////////////
  337. //! Stop the time with name \a name. Does not reset.
  338. //! @param name name of the timer to stop
  339. ////////////////////////////////////////////////////////////////////////////////
  340. inline bool sdkStopTimer(StopWatchInterface **timer_interface) {
  341. // printf("sdkStopTimer called object %08x\n", (void *)*timer_interface);
  342. if (*timer_interface) {
  343. (*timer_interface)->stop();
  344. }
  345. return true;
  346. }
  347. ////////////////////////////////////////////////////////////////////////////////
  348. //! Resets the timer's counter.
  349. //! @param name name of the timer to reset.
  350. ////////////////////////////////////////////////////////////////////////////////
  351. inline bool sdkResetTimer(StopWatchInterface **timer_interface) {
  352. // printf("sdkResetTimer called object %08x\n", (void *)*timer_interface);
  353. if (*timer_interface) {
  354. (*timer_interface)->reset();
  355. }
  356. return true;
  357. }
  358. ////////////////////////////////////////////////////////////////////////////////
  359. //! Return the average time for timer execution as the total time
  360. //! for the timer dividied by the number of completed (stopped) runs the timer
  361. //! has made.
  362. //! Excludes the current running time if the timer is currently running.
  363. //! @param name name of the timer to return the time of
  364. ////////////////////////////////////////////////////////////////////////////////
  365. inline float sdkGetAverageTimerValue(StopWatchInterface **timer_interface) {
  366. // printf("sdkGetAverageTimerValue called object %08x\n", (void
  367. // *)*timer_interface);
  368. if (*timer_interface) {
  369. return (*timer_interface)->getAverageTime();
  370. } else {
  371. return 0.0f;
  372. }
  373. }
  374. ////////////////////////////////////////////////////////////////////////////////
  375. //! Total execution time for the timer over all runs since the last reset
  376. //! or timer creation.
  377. //! @param name name of the timer to obtain the value of.
  378. ////////////////////////////////////////////////////////////////////////////////
  379. inline float sdkGetTimerValue(StopWatchInterface **timer_interface) {
  380. // printf("sdkGetTimerValue called object %08x\n", (void *)*timer_interface);
  381. if (*timer_interface) {
  382. return (*timer_interface)->getTime();
  383. } else {
  384. return 0.0f;
  385. }
  386. }
  387. #endif // COMMON_HELPER_TIMER_H_