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