155 lines
5.0 KiB

  1. /*
  2. Copyright 2005-2013 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. };
  65. //! Construct an absolute timestamp initialized to zero.
  66. tick_count() : my_count(0) {};
  67. //! Return current time.
  68. static tick_count now();
  69. //! Subtract two timestamps to get the time interval between
  70. friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
  71. private:
  72. long long my_count;
  73. };
  74. inline tick_count tick_count::now() {
  75. tick_count result;
  76. #if _WIN32||_WIN64
  77. LARGE_INTEGER qpcnt;
  78. QueryPerformanceCounter(&qpcnt);
  79. result.my_count = qpcnt.QuadPart;
  80. #elif __linux__
  81. struct timespec ts;
  82. #if TBB_USE_ASSERT
  83. int status =
  84. #endif /* TBB_USE_ASSERT */
  85. clock_gettime( CLOCK_REALTIME, &ts );
  86. __TBB_ASSERT( status==0, "CLOCK_REALTIME not supported" );
  87. result.my_count = static_cast<long long>(1000000000UL)*static_cast<long long>(ts.tv_sec) + static_cast<long long>(ts.tv_nsec);
  88. #else /* generic Unix */
  89. struct timeval tv;
  90. #if TBB_USE_ASSERT
  91. int status =
  92. #endif /* TBB_USE_ASSERT */
  93. gettimeofday(&tv, NULL);
  94. __TBB_ASSERT( 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. {
  101. #if _WIN32||_WIN64
  102. LARGE_INTEGER qpfreq;
  103. QueryPerformanceFrequency(&qpfreq);
  104. value = static_cast<long long>(sec*qpfreq.QuadPart);
  105. #elif __linux__
  106. value = static_cast<long long>(sec*1E9);
  107. #else /* generic Unix */
  108. value = static_cast<long long>(sec*1E6);
  109. #endif /* (choice of OS) */
  110. }
  111. inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) {
  112. return tick_count::interval_t( t1.my_count-t0.my_count );
  113. }
  114. inline double tick_count::interval_t::seconds() const {
  115. #if _WIN32||_WIN64
  116. LARGE_INTEGER qpfreq;
  117. QueryPerformanceFrequency(&qpfreq);
  118. return value/(double)qpfreq.QuadPart;
  119. #elif __linux__
  120. return value*1E-9;
  121. #else /* generic Unix */
  122. return value*1E-6;
  123. #endif /* (choice of OS) */
  124. }
  125. } // namespace tbb
  126. #endif /* __TBB_tick_count_H */