AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
HighAccuracyTimer.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_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#include <mach/mach_time.h>
30#endif
31
32// Still supporting compilers without full support for C++11, so cannot use chrono yet.
33#if AGX_USE_CPP11_TIMERS()
34#include <chrono>
35#endif
36
37#ifdef AGX_APPLE_IOS
38#include <mach/mach_time.h>
39#endif
40
41#ifdef _WIN32
42
43#else
44#include <unistd.h>
45#endif
46
47
48
49
50namespace agx {
51
64 {
65 public:
66
72 HighAccuracyTimer(bool startImmediately = false);
73
78 void start();
79
85 void stop();
86
90 void reset(bool startAfterReset = false);
91
96 Real64 getTime() const;
97
99 UInt64 getNumTicks() const;
100
102 UInt64 getStartTick() const;
103
105 bool isRunning() const;
106
108 static UInt64 getCurrentTick();
109
111 static Real64 convertToMs(UInt64 ticks);
112
114 static void initClockSpeed();
115
116 private:
117 static agx::Real clockSpeed();
118
120 static UInt64 getCurrentTickWindows();
121
122
123 private:
124
125#if AGX_USE_CPP11_TIMERS()
126 std::high_resolution_clock::time_point m_start;
127 std::duration<agx::Real64> m_total;
128#else
129 UInt64 m_start;
130 UInt64 m_total;
131#endif
132
133 bool m_running;
134
135 static agx::Real s_highAccuracyClockSpeed;
136 };
137
138
139 /* Implementation */
140 inline HighAccuracyTimer::HighAccuracyTimer(bool startImmediately)
141 {
142 reset();
143 if (startImmediately)
144 this->start();
145 }
146
147
149 {
150 if (m_running)
151 return;
152
153 m_running = true;
154
155#if AGX_USE_CPP11_TIMERS()
156 m_start = std::high_resolution_clock::now();
157#else
158 m_start = getCurrentTick();
159#endif
160 }
161
162
164 {
165 if (m_running) {
166 m_running = false;
167
168#if AGX_USE_CPP11_TIMERS()
169 std::high_resolution_clock::time_point now = std::high_resolution_clock::now();
170 std::duration<Real64> duration = duration_cast<std::duration<Real64>>(now - m_start);
171 m_total += duration;
172#else
173 UInt64 now = getCurrentTick();
174 m_total += (now - m_start);
175#endif
176 }
177 }
178
179
181 {
182 m_start = 0L;
183 m_total = 0L;
184
185 m_running = false;
186 if (startAfterReset)
187 this->start();
188 }
189
190
192 {
193 return m_total;
194 }
195
196
198 {
199 return m_start;
200 }
201
202
204 {
205 return m_running;
206 }
207
208
210 {
211#if AGX_USE_CPP11_TIMERS()
212 if (m_running)
213 {
214 std::high_resolution_clock::time_point now = std::high_resolution_clock::now();
215 std::duration<Real64> duration = duration_cast<std::duration<Real64>>(now - m_start);
216 std::duration<Real64> total = m_total + duration;
217 return total.count() * Real64(1000);
218 }
219 else
220 {
221 return m_total.count() * Real64(1000);;
222 }
223
224#else
225 return m_running ? (clockSpeed() * (Real64)(getCurrentTick() - m_start + m_total)) : clockSpeed() * (Real64)m_total;
226#endif
227 }
228
229
230 AGX_FORCE_INLINE agx::Real HighAccuracyTimer::clockSpeed()
231 {
232#ifdef AGX_DEBUG
233 agxAssert1(s_highAccuracyClockSpeed >= 0, "HighAccuracyTimer class not properly initialized, forgot to call agx::init() ?");
234#endif
235
236 return s_highAccuracyClockSpeed;
237 }
238
239
241 {
242#if defined(_MSC_VER)
243 const UInt64 retVal = getCurrentTickWindows();
244 return retVal;
245#elif defined(AGX_APPLE_IOS) || defined(__APPLE__)
246 return mach_absolute_time();
247#elif defined (__GNUC__) && ! defined(__INTEL_COMPILER) && defined(__ia64__)
248 unsigned long long int val;
249 __asm__ __volatile__("mov %0=ar.itc" : "=r"(val) :: "memory");
250 return(val);
251#elif defined(__INTEL_COMPILER) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
252 return __getReg(_IA64_REG_AR_ITC);
253#elif defined(__ECC) && defined(__ia64__) && !defined(HAVE_TICK_COUNTER)
254 return __getReg(_IA64_REG_AR_ITC);
255#elif (defined(__GNUC__) || defined(__ICC)) && (defined(__i386__) || defined(__x86_64__)) && !defined(HAVE_TICK_COUNTER)
256
257 // "The CPUID instruction serializes the processor pipeline so that all of the preceding
258 // instructions must retire before it begins execution. ... This is thought to provide
259 //a more accurate cycle count on the code being measured." [intel person at forum on intel.com]
260
261 // cpuid writes to eax,ebx,ecx, edx, tell g++ about registers being clobbered
262 // in 64bits ebx is 32bits of the larger rbx register so should not require
263 // any ifdef's in 64bit
264 // would be better to use pushad, cpuid, popad but for some unknownn reason gcc
265 // doesn't recognize pushad/popad
266 // Update: gcc has different name for pushad/popad, l postfix instead of d,
267 // but those instructions doesn't work in 64bit
268
269#ifdef __LP64__
270 unsigned long long int lowbits, highbits;
271 __asm__ __volatile__("rdtsc": "=a" (lowbits), "=d"(highbits) : : "ebx", "ecx");
272#else
273 unsigned long int lowbits, highbits;
274 __asm__ __volatile__("pushal\n\t" "cpuid\n\t" "popal\n\t" "rdtsc": "=a" (lowbits), "=d"(highbits));
275 //__asm__ __volatile__("push %%ebx\n" "push %%ecx\n" "cpuid\n" "rdtsc\n" "pop %%ebx\n" "pop %%ecx": "=a" (lowbits), "=d"(highbits));
276#endif
277
278 return (((unsigned long long int)highbits << 32) | (unsigned long long int)lowbits);
279
280#else
281 agxAbort1("Timer::getCurrentTick failed!");
282 return -1;
283#endif
284
285 }
286
287
289 {
290 return clockSpeed() * (Real64)ticks;
291 }
292
293
294
295} // namespace agx
296#endif
#define AGXCORE_EXPORT
The HighAccurayTimer class can replace the regular Timer class in high level applications where the n...
UInt64 getStartTick() const
Return the start tick.
static Real64 convertToMs(UInt64 ticks)
Return the given number of ticks in ms.
UInt64 getNumTicks() const
Report execution time in CPU cycles.
Real64 getTime() const
Report total elapsed time since start in milliseconds, excluding intervals when the timer was suspend...
static UInt64 getCurrentTick()
Return raw value of the TSC register (in Windows: QueryPerformanceTimer).
void start()
Starts the HighAccuracyTimer.
bool isRunning() const
Return true if running.
void reset(bool startAfterReset=false)
Clear all data.
HighAccuracyTimer(bool startImmediately=false)
Creates a HighAccuracyTimer.
static void initClockSpeed()
For internal use only.
void stop()
Stops the HighAccuracyTimer.
#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()