tick_count.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB_tick_count_H
  14. #define __TBB_tick_count_H
  15. #include "tbb_stddef.h"
  16. #if _WIN32||_WIN64
  17. #include "machine/windows_api.h"
  18. #elif __linux__
  19. #include <ctime>
  20. #else /* generic Unix */
  21. #include <sys/time.h>
  22. #endif /* (choice of OS) */
  23. namespace tbb {
  24. //! Absolute timestamp
  25. /** @ingroup timing */
  26. class tick_count {
  27. public:
  28. //! Relative time interval.
  29. class interval_t {
  30. long long value;
  31. explicit interval_t( long long value_ ) : value(value_) {}
  32. public:
  33. //! Construct a time interval representing zero time duration
  34. interval_t() : value(0) {};
  35. //! Construct a time interval representing sec seconds time duration
  36. explicit interval_t( double sec );
  37. //! Return the length of a time interval in seconds
  38. double seconds() const;
  39. friend class tbb::tick_count;
  40. //! Extract the intervals from the tick_counts and subtract them.
  41. friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
  42. //! Add two intervals.
  43. friend interval_t operator+( const interval_t& i, const interval_t& j ) {
  44. return interval_t(i.value+j.value);
  45. }
  46. //! Subtract two intervals.
  47. friend interval_t operator-( const interval_t& i, const interval_t& j ) {
  48. return interval_t(i.value-j.value);
  49. }
  50. //! Accumulation operator
  51. interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;}
  52. //! Subtraction operator
  53. interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;}
  54. private:
  55. static long long ticks_per_second(){
  56. #if _WIN32||_WIN64
  57. LARGE_INTEGER qpfreq;
  58. int rval = QueryPerformanceFrequency(&qpfreq);
  59. __TBB_ASSERT_EX(rval, "QueryPerformanceFrequency returned zero");
  60. return static_cast<long long>(qpfreq.QuadPart);
  61. #elif __linux__
  62. return static_cast<long long>(1E9);
  63. #else /* generic Unix */
  64. return static_cast<long long>(1E6);
  65. #endif /* (choice of OS) */
  66. }
  67. };
  68. //! Construct an absolute timestamp initialized to zero.
  69. tick_count() : my_count(0) {};
  70. //! Return current time.
  71. static tick_count now();
  72. //! Subtract two timestamps to get the time interval between
  73. friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
  74. //! Return the resolution of the clock in seconds per tick.
  75. static double resolution() { return 1.0 / interval_t::ticks_per_second(); }
  76. private:
  77. long long my_count;
  78. };
  79. inline tick_count tick_count::now() {
  80. tick_count result;
  81. #if _WIN32||_WIN64
  82. LARGE_INTEGER qpcnt;
  83. int rval = QueryPerformanceCounter(&qpcnt);
  84. __TBB_ASSERT_EX(rval, "QueryPerformanceCounter failed");
  85. result.my_count = qpcnt.QuadPart;
  86. #elif __linux__
  87. struct timespec ts;
  88. int status = clock_gettime( CLOCK_REALTIME, &ts );
  89. __TBB_ASSERT_EX( status==0, "CLOCK_REALTIME not supported" );
  90. result.my_count = static_cast<long long>(1000000000UL)*static_cast<long long>(ts.tv_sec) + static_cast<long long>(ts.tv_nsec);
  91. #else /* generic Unix */
  92. struct timeval tv;
  93. int status = gettimeofday(&tv, NULL);
  94. __TBB_ASSERT_EX( status==0, "gettimeofday failed" );
  95. result.my_count = static_cast<long long>(1000000)*static_cast<long long>(tv.tv_sec) + static_cast<long long>(tv.tv_usec);
  96. #endif /*(choice of OS) */
  97. return result;
  98. }
  99. inline tick_count::interval_t::interval_t( double sec ) {
  100. value = static_cast<long long>(sec*interval_t::ticks_per_second());
  101. }
  102. inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) {
  103. return tick_count::interval_t( t1.my_count-t0.my_count );
  104. }
  105. inline double tick_count::interval_t::seconds() const {
  106. return value*tick_count::resolution();
  107. }
  108. } // namespace tbb
  109. #endif /* __TBB_tick_count_H */