AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_TIMER_H
18#define AGX_TIMER_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#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 #include <mach/mach_time.h>
39#endif
40
41// Still supporting compilers without full support for C++11, so cannot use chrono yet.
42#if AGX_USE_CPP11_TIMERS()
43#include <chrono>
44#endif
45
46#ifdef AGX_APPLE_IOS
47#include <mach/mach_time.h>
48#endif
49
50#ifdef _WIN32
51
52#else
53#include <unistd.h>
54#endif
55
56
57
58
59namespace agx {
60
75 {
76 public:
77
83 Timer(bool startImmediately = false);
84
89 void start();
90
96 void stop();
97
101 void reset(bool startAfterReset = false);
102
107 Real64 getTime() const;
108
110 UInt64 getNumTicks() const;
111
113 UInt64 getStartTick() const;
114
116 bool isRunning() const;
117
119 static UInt64 getCurrentTick();
120
121#ifndef SWIG
123 static Real64 getTime( UInt64 startTick );
124#endif
125
127 static Real64 convertToMs( UInt64 ticks );
128
130 static void initClockSpeed();
131
132 private:
133 static agx::Real clockSpeed();
134#if defined(_MSC_VER)
136 static UInt64 getCurrentTickWindows();
137#endif
138
139 private:
140
141 #if AGX_USE_CPP11_TIMERS()
142 std::high_resolution_clock::time_point m_start;
143 std::duration<agx::Real64> m_total;
144 #else
145 UInt64 m_start;
146 UInt64 m_total;
147 #endif
148
149 bool m_running;
150
151 static agx::Real s_clockSpeed;
152 };
153
154
155 /* Implementation */
156 inline Timer::Timer(bool startImmediately)
157 {
158 reset();
159 if (startImmediately)
160 this->start();
161 }
162
163
165 {
166 if (m_running)
167 return;
168
169 m_running = true;
170
171 #if AGX_USE_CPP11_TIMERS()
172 m_start = std::high_resolution_clock::now();
173 #else
174 m_start = getCurrentTick();
175 #endif
176 }
177
178
180 {
181 if (m_running) {
182 m_running = false;
183
184#if AGX_USE_CPP11_TIMERS()
185 std::high_resolution_clock::time_point now = std::high_resolution_clock::now();
186 std::duration<Real64> duration = duration_cast<std::duration<Real64>>(now - m_start);
187 m_total += duration;
188#else
189 UInt64 now = getCurrentTick();
190 m_total += (now - m_start);
191#endif
192 }
193 }
194
195
196 AGX_FORCE_INLINE void Timer::reset(bool startAfterReset)
197 {
198 m_start = 0L;
199 m_total = 0L;
200
201 m_running = false;
202 if (startAfterReset)
203 this->start();
204 }
205
206
208 {
209 return m_total;
210 }
211
212
214 {
215 return m_start;
216 }
217
218
220 {
221 return m_running;
222 }
223
224
226 {
227 #if AGX_USE_CPP11_TIMERS()
228 if (m_running)
229 {
230 std::high_resolution_clock::time_point now = std::high_resolution_clock::now();
231 std::duration<Real64> duration = duration_cast<std::duration<Real64>>(now - m_start);
232 std::duration<Real64> total = m_total + duration;
233 return total.count() * Real64(1000);
234 }
235 else
236 {
237 return m_total.count() * Real64(1000);;
238 }
239
240 #else
241 return m_running ? (clockSpeed() * (Real64)(getCurrentTick() - m_start + m_total)) : clockSpeed() * (Real64)m_total;
242 #endif
243 }
244
245
246 AGX_FORCE_INLINE agx::Real Timer::clockSpeed()
247 {
248 #ifdef AGX_DEBUG
249 agxAssert1(s_clockSpeed >= 0, "Timer class not properly initialized, forgot to call agx::init() ?");
250 #endif
251
252 return s_clockSpeed;
253 }
254
255
257 {
258#if defined(_MSC_VER)
259#ifdef AGX_USE_HPET
260 const UInt64 retVal = getCurrentTickWindows();
261#else
262 const UInt64 retVal = __rdtsc();
263#endif
264 return retVal;
265#elif defined(AGX_APPLE_IOS) || defined(__APPLE__)
266 return mach_absolute_time();
267#elif defined (__GNUC__) && ! defined(__INTEL_COMPILER) && defined(__ia64__)
268 unsigned long long int val;
269 __asm__ __volatile__("mov %0=ar.itc" : "=r"(val) :: "memory");
270 return(val);
271#elif defined(__INTEL_COMPILER) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
272 return __getReg(_IA64_REG_AR_ITC);
273#elif defined(__ECC) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
274 return __getReg(_IA64_REG_AR_ITC);
275#elif (defined(__GNUC__) || defined(__ICC)) && (defined(__i386__) || defined(__x86_64__)) && !defined(HAVE_TICK_COUNTER)
276
277 // "The CPUID instruction serializes the processor pipeline so that all of the preceding
278 // instructions must retire before it begins execution. ... This is thought to provide
279 //a more accurate cycle count on the code being measured." [intel person at forum on intel.com]
280
281 // cpuid writes to eax,ebx,ecx, edx, tell g++ about registers being clobbered
282 // in 64bits ebx is 32bits of the larger rbx register so should not require
283 // any ifdef's in 64bit
284 // would be better to use pushad, cpuid, popad but for some unknown reason gcc
285 // doesn't recognize pushad/popad
286 // Update: gcc has different name for pushad/popad, l postfix instead of d,
287 // but those instructions doesn't work in 64bit
288
289#ifdef __LP64__
290 unsigned long long int lowbits, highbits;
291 __asm__ __volatile__("rdtsc": "=a" (lowbits), "=d"(highbits) : : );
292#else
293 unsigned long int lowbits, highbits;
294 __asm__ __volatile__("pushal\n\t" "cpuid\n\t" "popal\n\t" "rdtsc": "=a" (lowbits), "=d"(highbits) );
295 //__asm__ __volatile__("push %%ebx\n" "push %%ecx\n" "cpuid\n" "rdtsc\n" "pop %%ebx\n" "pop %%ecx": "=a" (lowbits), "=d"(highbits));
296#endif
297
298 return (((unsigned long long int)highbits<<32) | (unsigned long long int)lowbits);
299
300#else
301 agxAbort1("Timer::getCurrentTick failed!");
302 return -1;
303#endif
304
305 }
306
308 {
309 return Timer::convertToMs(Timer::getCurrentTick() - startTick);
310 }
311
313 {
314 return clockSpeed() * (Real64)ticks;
315 }
316
317
318
319} // namespace agx
320#endif
#define AGXCORE_EXPORT
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Definition Timer.h:75
static UInt64 getCurrentTick()
Definition Timer.h:256
static Real64 convertToMs(UInt64 ticks)
Definition Timer.h:312
void stop()
Stops the timer.
Definition Timer.h:179
void start()
Starts the timer.
Definition Timer.h:164
bool isRunning() const
Definition Timer.h:219
Real64 getTime() const
Report total elapsed time since start in milliseconds, excluding intervals when the timer was suspend...
Definition Timer.h:225
UInt64 getNumTicks() const
Report execution time in CPU cycles.
Definition Timer.h:207
static void initClockSpeed()
For internal use only.
UInt64 getStartTick() const
Definition Timer.h:213
void reset(bool startAfterReset=false)
Clear all data.
Definition Timer.h:196
Timer(bool startImmediately=false)
Creates a timer.
Definition Timer.h:156
#define agxAssert1(expr, msg)
Definition debug.h:144
#define agxAbort1(msg)
Definition debug.h:165
#define AGX_FORCE_INLINE
Definition macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
double Real64
Definition Real.h:45
uint64_t UInt64
Definition Integer.h:33
double Real
Definition Real.h:42
UInt64 AGXCORE_EXPORT getStartTick()