Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tick_count.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16 
17 
18 
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 */
friend interval_t operator-(const tick_count &t1, const tick_count &t0)
Subtract two timestamps to get the time interval between.
Definition: tick_count.h:130
interval_t & operator-=(const interval_t &i)
Subtraction operator.
Definition: tick_count.h:73
Absolute timestamp.
Definition: tick_count.h:38
interval_t & operator+=(const interval_t &i)
Accumulation operator.
Definition: tick_count.h:70
interval_t()
Construct a time interval representing zero time duration.
Definition: tick_count.h:46
friend interval_t operator+(const interval_t &i, const interval_t &j)
Add two intervals.
Definition: tick_count.h:60
#define __TBB_ASSERT_EX(predicate, comment)
"Extended" version is useful to suppress warnings if a variable is only used with an assert
Definition: tbb_stddef.h:171
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
The graph class.
long long my_count
Definition: tick_count.h:102
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
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
double seconds() const
Return the length of a time interval in seconds.
Definition: tick_count.h:134
static long long ticks_per_second()
Definition: tick_count.h:75
tick_count()
Construct an absolute timestamp initialized to zero.
Definition: tick_count.h:90
Relative time interval.
Definition: tick_count.h:41
static tick_count now()
Return current time.
Definition: tick_count.h:105
interval_t(long long value_)
Definition: tick_count.h:43

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.