AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Timer.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_TIMER_H
18 #define AGX_TIMER_H
19 
21 #include <agx/agxCore_export.h>
22 #include <agx/Integer.h>
23 
24 #include <agx/agx.h>
25 
26 #if defined(_MSC_VER)
27 //#define AGX_USE_HPET // Internal. Comment to use HTC instead. Change only when compiling AgX.
28 #ifndef AGX_USE_HPET
29 #include <intrin.h>
30 #pragma intrinsic(__rdtsc)
31 #endif
32 #endif
33 
34 
35 #ifdef __APPLE__
36  #include <sys/types.h>
37  #include <sys/sysctl.h>
38 #endif
39 
40 // Still supporting compilers without full support for C++11, so cannot use chrono yet.
41 #if AGX_USE_CPP11_TIMERS()
42 #include <chrono>
43 #endif
44 
45 #ifdef AGX_APPLE_IOS
46 #include <mach/mach_time.h>
47 #endif
48 
49 #ifdef _WIN32
50 
51 #else
52 #include <unistd.h>
53 #endif
54 
55 
56 
57 
58 namespace agx {
59 
74  {
75  public:
76 
82  Timer(bool startImmediately = false);
83 
88  void start();
89 
95  void stop();
96 
100  void reset(bool startAfterReset = false);
101 
106  Real64 getTime() const;
107 
109  UInt64 getNumTicks() const;
110 
112  UInt64 getStartTick() const;
113 
115  bool isRunning() const;
116 
118  static UInt64 getCurrentTick();
119 
120 #ifndef SWIG
121  static Real64 getTime( UInt64 startTick );
123 #endif
124 
126  static Real64 convertToMs( UInt64 ticks );
127 
129  static void initClockSpeed();
130 
131  private:
132  static agx::Real clockSpeed();
133 #if defined(_MSC_VER)
134  static UInt64 getCurrentTickWindows();
136 #endif
137 
138  private:
139 
140  #if AGX_USE_CPP11_TIMERS()
141  std::high_resolution_clock::time_point m_start;
142  std::duration<agx::Real64> m_total;
143  #else
144  UInt64 m_start;
145  UInt64 m_total;
146  #endif
147 
148  bool m_running;
149 
150  static agx::Real s_clockSpeed;
151  };
152 
153 
154  /* Implementation */
155  inline Timer::Timer(bool startImmediately)
156  {
157  reset();
158  if (startImmediately)
159  this->start();
160  }
161 
162 
164  {
165  if (m_running)
166  return;
167 
168  m_running = true;
169 
170  #if AGX_USE_CPP11_TIMERS()
171  m_start = std::high_resolution_clock::now();
172  #else
173  m_start = getCurrentTick();
174  #endif
175  }
176 
177 
179  {
180  if (m_running) {
181  m_running = false;
182 
183 #if AGX_USE_CPP11_TIMERS()
184  std::high_resolution_clock::time_point now = std::high_resolution_clock::now();
185  std::duration<Real64> duration = duration_cast<std::duration<Real64>>(now - m_start);
186  m_total += duration;
187 #else
188  UInt64 now = getCurrentTick();
189  m_total += (now - m_start);
190 #endif
191  }
192  }
193 
194 
195  AGX_FORCE_INLINE void Timer::reset(bool startAfterReset)
196  {
197  m_start = 0L;
198  m_total = 0L;
199 
200  m_running = false;
201  if (startAfterReset)
202  this->start();
203  }
204 
205 
207  {
208  return m_total;
209  }
210 
211 
213  {
214  return m_start;
215  }
216 
217 
219  {
220  return m_running;
221  }
222 
223 
225  {
226  #if AGX_USE_CPP11_TIMERS()
227  if (m_running)
228  {
229  std::high_resolution_clock::time_point now = std::high_resolution_clock::now();
230  std::duration<Real64> duration = duration_cast<std::duration<Real64>>(now - m_start);
231  std::duration<Real64> total = m_total + duration;
232  return total.count() * Real64(1000);
233  }
234  else
235  {
236  return m_total.count() * Real64(1000);;
237  }
238 
239  #else
240  return m_running ? (clockSpeed() * (Real64)(getCurrentTick() - m_start + m_total)) : clockSpeed() * (Real64)m_total;
241  #endif
242  }
243 
244 
245  AGX_FORCE_INLINE agx::Real Timer::clockSpeed()
246  {
247  #ifdef AGX_DEBUG
248  agxAssert1(s_clockSpeed >= 0, "Timer class not properly initialized, forgot to call agx::init() ?");
249  #endif
250 
251  return s_clockSpeed;
252  }
253 
254 
256  {
257 #if defined(_MSC_VER)
258 #ifdef AGX_USE_HPET
259  const UInt64 retVal = getCurrentTickWindows();
260 #else
261  const UInt64 retVal = __rdtsc();
262 #endif
263  return retVal;
264 #elif defined(AGX_APPLE_IOS)
265  return mach_absolute_time();
266 #elif defined (__GNUC__) && ! defined(__INTEL_COMPILER) && defined(__ia64__)
267  unsigned long long int val;
268  __asm__ __volatile__("mov %0=ar.itc" : "=r"(val) :: "memory");
269  return(val);
270 #elif defined(__INTEL_COMPILER) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
271  return __getReg(_IA64_REG_AR_ITC);
272 #elif defined(__ECC) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
273  return __getReg(_IA64_REG_AR_ITC);
274 #elif (defined(__GNUC__) || defined(__ICC)) && (defined(__i386__) || defined(__x86_64__)) && !defined(HAVE_TICK_COUNTER)
275 
276  // "The CPUID instruction serializes the processor pipeline so that all of the preceding
277  // instructions must retire before it begins execution. ... This is thought to provide
278  //a more accurate cycle count on the code being measured." [intel person at forum on intel.com]
279 
280  // cpuid writes to eax,ebx,ecx, edx, tell g++ about registers being clobbered
281  // in 64bits ebx is 32bits of the larger rbx register so should not require
282  // any ifdef's in 64bit
283  // would be better to use pushad, cpuid, popad but for some unknown reason gcc
284  // doesn't recognize pushad/popad
285  // Update: gcc has different name for pushad/popad, l postfix instead of d,
286  // but those instructions doesn't work in 64bit
287 
288 #ifdef __LP64__
289  unsigned long long int lowbits, highbits;
290  __asm__ __volatile__("rdtsc": "=a" (lowbits), "=d"(highbits) : : );
291 #else
292  unsigned long int lowbits, highbits;
293  __asm__ __volatile__("pushal\n\t" "cpuid\n\t" "popal\n\t" "rdtsc": "=a" (lowbits), "=d"(highbits) );
294  //__asm__ __volatile__("push %%ebx\n" "push %%ecx\n" "cpuid\n" "rdtsc\n" "pop %%ebx\n" "pop %%ecx": "=a" (lowbits), "=d"(highbits));
295 #endif
296 
297  return (((unsigned long long int)highbits<<32) | (unsigned long long int)lowbits);
298 
299 #else
300  agxAbort1("Timer::getCurrentTick failed!");
301  return -1;
302 #endif
303 
304  }
305 
307  {
308  return Timer::convertToMs(Timer::getCurrentTick() - startTick);
309  }
310 
312  {
313  return clockSpeed() * (Real64)ticks;
314  }
315 
316 
317 
318 } // namespace agx
319 #endif
UInt64 getStartTick() const
Definition: Timer.h:212
double Real64
Definition: Real.h:45
Timer(bool startImmediately=false)
Creates a timer.
Definition: Timer.h:155
bool isRunning() const
Definition: Timer.h:218
void stop()
Stops the timer.
Definition: Timer.h:178
UInt64 getNumTicks() const
Report execution time in CPU cycles.
Definition: Timer.h:206
UInt64 AGXCORE_EXPORT getStartTick()
#define AGX_FORCE_INLINE
Definition: Object.h:62
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Definition: Timer.h:73
static UInt64 getCurrentTick()
Definition: Timer.h:255
Real64 getTime() const
Report total elapsed time since start in milliseconds, excluding intervals when the timer was suspend...
Definition: Timer.h:224
void start()
Starts the timer.
Definition: Timer.h:163
uint64_t UInt64
Definition: Integer.h:41
static Real64 convertToMs(UInt64 ticks)
Definition: Timer.h:311
#define agxAssert1(expr, msg)
Definition: debug.h:144
double Real
Definition: Real.h:42
#define agxAbort1(msg)
Definition: debug.h:165
#define AGXCORE_EXPORT
void reset(bool startAfterReset=false)
Clear all data.
Definition: Timer.h:195