You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.2 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_tick_count_H
  24. #define __TBB_tick_count_H
  25. #include "tbb_stddef.h"
  26. #if _WIN32||_WIN64
  27. #include "machine/windows_api.h"
  28. #elif __linux__
  29. #include <ctime>
  30. #else /* generic Unix */
  31. #include <sys/time.h>
  32. #endif /* (choice of OS) */
  33. namespace tbb {
  34. //! Absolute timestamp
  35. /** @ingroup timing */
  36. class tick_count {
  37. public:
  38. //! Relative time interval.
  39. class interval_t {
  40. long long value;
  41. explicit interval_t( long long value_ ) : value(value_) {}
  42. public:
  43. //! Construct a time interval representing zero time duration
  44. interval_t() : value(0) {};
  45. //! Construct a time interval representing sec seconds time duration
  46. explicit interval_t( double sec );
  47. //! Return the length of a time interval in seconds
  48. double seconds() const;
  49. friend class tbb::tick_count;
  50. //! Extract the intervals from the tick_counts and subtract them.
  51. friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
  52. //! Add two intervals.
  53. friend interval_t operator+( const interval_t& i, const interval_t& j ) {
  54. return interval_t(i.value+j.value);
  55. }
  56. //! Subtract two intervals.
  57. friend interval_t operator-( const interval_t& i, const interval_t& j ) {
  58. return interval_t(i.value-j.value);
  59. }
  60. //! Accumulation operator
  61. interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;}
  62. //! Subtraction operator
  63. interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;}
  64. private:
  65. static long long ticks_per_second(){
  66. #if _WIN32||_WIN64
  67. LARGE_INTEGER qpfreq;
  68. int rval = QueryPerformanceFrequency(&qpfreq);
  69. __TBB_ASSERT_EX(rval, "QueryPerformanceFrequency returned zero");
  70. return static_cast<long long>(qpfreq.QuadPart);
  71. #elif __linux__
  72. return static_cast<long long>(1E9);
  73. #else /* generic Unix */
  74. return static_cast<long long>(1E6);
  75. #endif /* (choice of OS) */
  76. }
  77. };
  78. //! Construct an absolute timestamp initialized to zero.
  79. tick_count() : my_count(0) {};
  80. //! Return current time.
  81. static tick_count now();
  82. //! Subtract two timestamps to get the time interval between
  83. friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
  84. //! Return the resolution of the clock in seconds per tick.
  85. static double resolution() { return 1.0 / interval_t::ticks_per_second(); }
  86. private:
  87. long long my_count;
  88. };
  89. inline tick_count tick_count::now() {
  90. tick_count result;
  91. #if _WIN32||_WIN64
  92. LARGE_INTEGER qpcnt;
  93. int rval = QueryPerformanceCounter(&qpcnt);
  94. __TBB_ASSERT_EX(rval, "QueryPerformanceCounter failed");
  95. result.my_count = qpcnt.QuadPart;
  96. #elif __linux__
  97. struct timespec ts;
  98. int status = clock_gettime( CLOCK_REALTIME, &ts );
  99. __TBB_ASSERT_EX( status==0, "CLOCK_REALTIME not supported" );
  100. result.my_count = static_cast<long long>(1000000000UL)*static_cast<long long>(ts.tv_sec) + static_cast<long long>(ts.tv_nsec);
  101. #else /* generic Unix */
  102. struct timeval tv;
  103. int status = gettimeofday(&tv, NULL);
  104. __TBB_ASSERT_EX( status==0, "gettimeofday failed" );
  105. result.my_count = static_cast<long long>(1000000)*static_cast<long long>(tv.tv_sec) + static_cast<long long>(tv.tv_usec);
  106. #endif /*(choice of OS) */
  107. return result;
  108. }
  109. inline tick_count::interval_t::interval_t( double sec ) {
  110. value = static_cast<long long>(sec*interval_t::ticks_per_second());
  111. }
  112. inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) {
  113. return tick_count::interval_t( t1.my_count-t0.my_count );
  114. }
  115. inline double tick_count::interval_t::seconds() const {
  116. return value*tick_count::resolution();
  117. }
  118. } // namespace tbb
  119. #endif /* __TBB_tick_count_H */