AGXDynamics  2.31.1.0
HighAccuracyTimer.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_HIGHACCURACYTIMER_H
18  #define AGX_HIGHACCURACYTIMER_H
19 
20 #include <agx/config/AGX_USE_CPP11_TIMERS.h>
21 #include <agx/agxCore_export.h>
22 #include <agx/Integer.h>
23 
24 #include <agx/agx.h>
25 
26 #ifdef __APPLE__
27 #include <sys/types.h>
28 #include <sys/sysctl.h>
29 #endif
30 
31 // Still supporting compilers without full support for C++11, so cannot use chrono yet.
32 #if AGX_USE_CPP11_TIMERS()
33 #include <chrono>
34 #endif
35 
36 #ifdef AGX_APPLE_IOS
37 #include <mach/mach_time.h>
38 #endif
39 
40 #ifdef _WIN32
41 
42 #else
43 #include <unistd.h>
44 #endif
45 
46 
47 
48 
49 namespace agx {
50 
63  {
64  public:
65 
71  HighAccuracyTimer(bool startImmediately = false);
72 
77  void start();
78 
84  void stop();
85 
89  void reset(bool startAfterReset = false);
90 
95  Real64 getTime() const;
96 
98  UInt64 getNumTicks() const;
99 
101  UInt64 getStartTick() const;
102 
104  bool isRunning() const;
105 
107  static UInt64 getCurrentTick();
108 
110  static Real64 convertToMs(UInt64 ticks);
111 
113  static void initClockSpeed();
114 
115  private:
116  static agx::Real clockSpeed();
117 
119  static UInt64 getCurrentTickWindows();
120 
121 
122  private:
123 
124 #if AGX_USE_CPP11_TIMERS()
125  std::high_resolution_clock::time_point m_start;
126  std::duration<agx::Real64> m_total;
127 #else
128  UInt64 m_start;
129  UInt64 m_total;
130 #endif
131 
132  bool m_running;
133 
134  static agx::Real s_highAccuracyClockSpeed;
135  };
136 
137 
138  /* Implementation */
139  inline HighAccuracyTimer::HighAccuracyTimer(bool startImmediately)
140  {
141  reset();
142  if (startImmediately)
143  this->start();
144  }
145 
146 
148  {
149  if (m_running)
150  return;
151 
152  m_running = true;
153 
154 #if AGX_USE_CPP11_TIMERS()
155  m_start = std::high_resolution_clock::now();
156 #else
157  m_start = getCurrentTick();
158 #endif
159  }
160 
161 
163  {
164  if (m_running) {
165  m_running = false;
166 
167 #if AGX_USE_CPP11_TIMERS()
168  std::high_resolution_clock::time_point now = std::high_resolution_clock::now();
169  std::duration<Real64> duration = duration_cast<std::duration<Real64>>(now - m_start);
170  m_total += duration;
171 #else
172  UInt64 now = getCurrentTick();
173  m_total += (now - m_start);
174 #endif
175  }
176  }
177 
178 
179  AGX_FORCE_INLINE void HighAccuracyTimer::reset(bool startAfterReset)
180  {
181  m_start = 0L;
182  m_total = 0L;
183 
184  m_running = false;
185  if (startAfterReset)
186  this->start();
187  }
188 
189 
191  {
192  return m_total;
193  }
194 
195 
197  {
198  return m_start;
199  }
200 
201 
203  {
204  return m_running;
205  }
206 
207 
209  {
210 #if AGX_USE_CPP11_TIMERS()
211  if (m_running)
212  {
213  std::high_resolution_clock::time_point now = std::high_resolution_clock::now();
214  std::duration<Real64> duration = duration_cast<std::duration<Real64>>(now - m_start);
215  std::duration<Real64> total = m_total + duration;
216  return total.count() * Real64(1000);
217  }
218  else
219  {
220  return m_total.count() * Real64(1000);;
221  }
222 
223 #else
224  return m_running ? (clockSpeed() * (Real64)(getCurrentTick() - m_start + m_total)) : clockSpeed() * (Real64)m_total;
225 #endif
226  }
227 
228 
229  AGX_FORCE_INLINE agx::Real HighAccuracyTimer::clockSpeed()
230  {
231 #ifdef AGX_DEBUG
232  agxAssert1(s_highAccuracyClockSpeed >= 0, "HighAccuracyTimer class not properly initialized, forgot to call agx::init() ?");
233 #endif
234 
235  return s_highAccuracyClockSpeed;
236  }
237 
238 
240  {
241 #if defined(_MSC_VER)
242  const UInt64 retVal = getCurrentTickWindows();
243  return retVal;
244 #elif defined(AGX_APPLE_IOS)
245  return mach_absolute_time();
246 #elif defined (__GNUC__) && ! defined(__INTEL_COMPILER) && defined(__ia64__)
247  unsigned long long int val;
248  __asm__ __volatile__("mov %0=ar.itc" : "=r"(val) :: "memory");
249  return(val);
250 #elif defined(__INTEL_COMPILER) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
251  return __getReg(_IA64_REG_AR_ITC);
252 #elif defined(__ECC) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
253  return __getReg(_IA64_REG_AR_ITC);
254 #elif (defined(__GNUC__) || defined(__ICC)) && (defined(__i386__) || defined(__x86_64__)) && !defined(HAVE_TICK_COUNTER)
255 
256  // "The CPUID instruction serializes the processor pipeline so that all of the preceding
257  // instructions must retire before it begins execution. ... This is thought to provide
258  //a more accurate cycle count on the code being measured." [intel person at forum on intel.com]
259 
260  // cpuid writes to eax,ebx,ecx, edx, tell g++ about registers being clobbered
261  // in 64bits ebx is 32bits of the larger rbx register so should not require
262  // any ifdef's in 64bit
263  // would be better to use pushad, cpuid, popad but for some unknownn reason gcc
264  // doesn't recognize pushad/popad
265  // Update: gcc has different name for pushad/popad, l postfix instead of d,
266  // but those instructions doesn't work in 64bit
267 
268 #ifdef __LP64__
269  unsigned long long int lowbits, highbits;
270  __asm__ __volatile__("rdtsc": "=a" (lowbits), "=d"(highbits) : : "ebx", "ecx");
271 #else
272  unsigned long int lowbits, highbits;
273  __asm__ __volatile__("pushal\n\t" "cpuid\n\t" "popal\n\t" "rdtsc": "=a" (lowbits), "=d"(highbits));
274  //__asm__ __volatile__("push %%ebx\n" "push %%ecx\n" "cpuid\n" "rdtsc\n" "pop %%ebx\n" "pop %%ecx": "=a" (lowbits), "=d"(highbits));
275 #endif
276 
277  return (((unsigned long long int)highbits << 32) | (unsigned long long int)lowbits);
278 
279 #else
280  agxAbort1("Timer::getCurrentTick failed!");
281  return -1;
282 #endif
283 
284  }
285 
286 
288  {
289  return clockSpeed() * (Real64)ticks;
290  }
291 
292 
293 
294 } // namespace agx
295 #endif
double Real64
Definition: Real.h:45
static Real64 convertToMs(UInt64 ticks)
Return the given number of ticks in ms.
static UInt64 getCurrentTick()
Return raw value of the TSC register (in Windows: QueryPerformanceTimer).
The HighAccurayTimer class can replace the regular Timer class in high level applications where the n...
UInt64 AGXCORE_EXPORT getStartTick()
UInt64 getStartTick() const
Return the start tick.
#define AGX_FORCE_INLINE
Definition: Object.h:63
Real64 getTime() const
Report total elapsed time since start in milliseconds, excluding intervals when the timer was suspend...
HighAccuracyTimer(bool startImmediately=false)
Creates a HighAccuracyTimer.
bool isRunning() const
Return true if running.
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
void start()
Starts the HighAccuracyTimer.
uint64_t UInt64
Definition: Integer.h:41
#define agxAssert1(expr, msg)
Definition: debug.h:144
void stop()
Stops the HighAccuracyTimer.
double Real
Definition: Real.h:42
UInt64 getNumTicks() const
Report execution time in CPU cycles.
void reset(bool startAfterReset=false)
Clear all data.
#define agxAbort1(msg)
Definition: debug.h:165
#define AGXCORE_EXPORT