Home ⌂Doc Index ◂Up ▴
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-2020 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 #ifndef __TBB_tick_count_H
18 #define __TBB_tick_count_H
19 
20 #include "tbb_stddef.h"
21 
22 #if _WIN32||_WIN64
23 #include "machine/windows_api.h"
24 #elif __linux__
25 #include <ctime>
26 #else /* generic Unix */
27 #include <sys/time.h>
28 #endif /* (choice of OS) */
29 
30 namespace tbb {
31 
33 
34 class tick_count {
35 public:
37  class interval_t {
38  long long value;
39  explicit interval_t( long long value_ ) : value(value_) {}
40  public:
42  interval_t() : value(0) {};
43 
45  explicit interval_t( double sec );
46 
48  double seconds() const;
49 
50  friend class tbb::tick_count;
51 
53  friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
54 
56  friend interval_t operator+( const interval_t& i, const interval_t& j ) {
57  return interval_t(i.value+j.value);
58  }
59 
61  friend interval_t operator-( const interval_t& i, const interval_t& j ) {
62  return interval_t(i.value-j.value);
63  }
64 
66  interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;}
67 
69  interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;}
70  private:
71  static long long ticks_per_second(){
72 #if _WIN32||_WIN64
73  LARGE_INTEGER qpfreq;
74  int rval = QueryPerformanceFrequency(&qpfreq);
75  __TBB_ASSERT_EX(rval, "QueryPerformanceFrequency returned zero");
76  return static_cast<long long>(qpfreq.QuadPart);
77 #elif __linux__
78  return static_cast<long long>(1E9);
79 #else /* generic Unix */
80  return static_cast<long long>(1E6);
81 #endif /* (choice of OS) */
82  }
83  };
84 
86  tick_count() : my_count(0) {};
87 
89  static tick_count now();
90 
92  friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
93 
95  static double resolution() { return 1.0 / interval_t::ticks_per_second(); }
96 
97 private:
98  long long my_count;
99 };
100 
102  tick_count result;
103 #if _WIN32||_WIN64
104  LARGE_INTEGER qpcnt;
105  int rval = QueryPerformanceCounter(&qpcnt);
106  __TBB_ASSERT_EX(rval, "QueryPerformanceCounter failed");
107  result.my_count = qpcnt.QuadPart;
108 #elif __linux__
109  struct timespec ts;
110  int status = clock_gettime( CLOCK_REALTIME, &ts );
111  __TBB_ASSERT_EX( status==0, "CLOCK_REALTIME not supported" );
112  result.my_count = static_cast<long long>(1000000000UL)*static_cast<long long>(ts.tv_sec) + static_cast<long long>(ts.tv_nsec);
113 #else /* generic Unix */
114  struct timeval tv;
115  int status = gettimeofday(&tv, NULL);
116  __TBB_ASSERT_EX( status==0, "gettimeofday failed" );
117  result.my_count = static_cast<long long>(1000000)*static_cast<long long>(tv.tv_sec) + static_cast<long long>(tv.tv_usec);
118 #endif /*(choice of OS) */
119  return result;
120 }
121 
123  value = static_cast<long long>(sec*interval_t::ticks_per_second());
124 }
125 
126 inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) {
127  return tick_count::interval_t( t1.my_count-t0.my_count );
128 }
129 
130 inline double tick_count::interval_t::seconds() const {
131  return value*tick_count::resolution();
132 }
133 
134 } // namespace tbb
135 
136 #endif /* __TBB_tick_count_H */
static tick_count now()
Return current time.
Definition: tick_count.h:101
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:126
Absolute timestamp.
Definition: tick_count.h:34
interval_t()
Construct a time interval representing zero time duration.
Definition: tick_count.h:42
interval_t & operator-=(const interval_t &i)
Subtraction operator.
Definition: tick_count.h:69
static double resolution()
Return the resolution of the clock in seconds per tick.
Definition: tick_count.h:95
interval_t(long long value_)
Definition: tick_count.h:39
Relative time interval.
Definition: tick_count.h:37
long long my_count
Definition: tick_count.h:98
static long long ticks_per_second()
Definition: tick_count.h:71
friend interval_t operator-(const interval_t &i, const interval_t &j)
Subtract two intervals.
Definition: tick_count.h:61
#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:167
friend interval_t operator+(const interval_t &i, const interval_t &j)
Add two intervals.
Definition: tick_count.h:56
interval_t & operator+=(const interval_t &i)
Accumulation operator.
Definition: tick_count.h:66
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
tick_count()
Construct an absolute timestamp initialized to zero.
Definition: tick_count.h:86
The graph class.
double seconds() const
Return the length of a time interval in seconds.
Definition: tick_count.h:130
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:126

Copyright © 2005-2020 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.