20 #ifndef _osaStopwatch_h
21 #define _osaStopwatch_h
28 #if (CISST_OS == CISST_WINDOWS) || (CISST_OS == CISST_CYGWIN)
30 #elif (CISST_OS == CISST_QNX)
33 #define timersub(a, b, res) \
35 (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
36 (res)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
37 if ((res)->tv_nsec < 0) \
40 (res)->tv_nsec += 1000000000L; \
45 #elif (CISST_OS == CISST_LINUX_XENOMAI)
47 #include <native/timer.h>
49 #elif (CISST_OS == CISST_LINUX_RTAI) || (CISST_OS == CISST_LINUX) || (CISST_OS == CISST_DARWIN) || (CISST_OS == CISST_SOLARIS)
51 #if (CISST_OS == CISST_SOLARIS) || (CISST_COMPILER == CISST_GCC)
53 #define timersub(a, b, res) \
55 (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
56 (res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
57 if ((res)->tv_usec < 0) \
60 (res)->tv_usec += 1000000; \
65 #endif // CISST_OS == CISST_SOLARIS
89 #if (CISST_OS == CISST_WINDOWS) || (CISST_OS == CISST_CYGWIN)
91 HasHighPerformanceCounter(
false)
94 #if (CISST_OS == CISST_WINDOWS) || (CISST_OS == CISST_CYGWIN)
95 static LARGE_INTEGER frequency = { 0, 0 };
96 HasHighPerformanceCounter = (::QueryPerformanceFrequency( &frequency )) ?
true :
false;
97 if (HasHighPerformanceCounter) {
98 this->TimeGranularity = 1.0 /
static_cast<double>(frequency.QuadPart);
99 CMN_LOG_INIT_WARNING <<
"Class osaStopwatch: Can use HighPerformance Counter, time granularity is: "
100 << TimeGranularity *
cmn_ms <<
" ms" << std::endl;
102 this->TimeGranularity = 1.0e-3;
103 CMN_LOG_INIT_WARNING <<
"Class osaStopwatch: Can NOT use HighPerformance Counter, time granularity is about: "
104 << TimeGranularity *
cmn_ms <<
" ms" << std::endl;
107 #elif (CISST_OS == CISST_LINUX_XENOMAI)
109 this->TimeGranularity = 1.0e-9;
110 #elif (CISST_OS == CISST_LINUX_RTAI) || (CISST_OS == CISST_LINUX) || (CISST_OS == CISST_DARWIN) || (CISST_OS == CISST_SOLARIS) || (CISST_OS == CISST_QNX)
111 this->TimeGranularity = 1.0e-6;
125 #if (CISST_OS == CISST_WINDOWS) || (CISST_OS == CISST_CYGWIN)
127 #elif (CISST_OS == CISST_LINUX_XENOMAI)
131 #elif (CISST_OS == CISST_LINUX_RTAI) || (CISST_OS == CISST_LINUX) || (CISST_OS == CISST_DARWIN) || (CISST_OS == CISST_SOLARIS)
132 StartTimeOfDay.tv_sec = 0;
133 StartTimeOfDay.tv_usec = 0;
134 #elif (CISST_OS == CISST_QNX)
135 StartTimeOfDay.tv_sec = 0;
136 StartTimeOfDay.tv_nsec = 0;
148 #if (CISST_OS == CISST_WINDOWS) || (CISST_OS == CISST_CYGWIN)
149 if (HasHighPerformanceCounter) {
151 ::QueryPerformanceCounter(&ticks);
155 StartTicks = ::GetTickCount();
158 #elif (CISST_OS == CISST_LINUX_XENOMAI)
160 StartTicks = rt_timer_read();
162 #elif (CISST_OS == CISST_LINUX_RTAI) || (CISST_OS == CISST_LINUX) || (CISST_OS == CISST_DARWIN) || (CISST_OS == CISST_SOLARIS)
163 gettimeofday(&StartTimeOfDay, 0);
164 #elif (CISST_OS == CISST_QNX)
165 if (clock_gettime(CLOCK_REALTIME, &StartTimeOfDay) == -1) {
179 AccumulatedTime += GetLastTimeInterval();
201 return static_cast<MillisecondsCounter>((AccumulatedTime + GetLastTimeInterval()) * 1000.0);
210 return AccumulatedTime;
212 return AccumulatedTime + GetLastTimeInterval();
219 #if (CISST_OS == CISST_WINDOWS) || (CISST_OS == CISST_CYGWIN)
222 if (HasHighPerformanceCounter) {
224 QueryPerformanceCounter(&ticks);
226 totalSeconds = endTicks - StartTicks;
228 endTicks = ::GetTickCount();
229 totalSeconds =
static_cast<double>(endTicks - StartTicks) / 1000.0;
231 #elif (CISST_OS == CISST_LINUX_XENOMAI)
235 #elif (CISST_OS == CISST_LINUX_RTAI) || (CISST_OS == CISST_LINUX) || (CISST_OS == CISST_DARWIN) || (CISST_OS == CISST_SOLARIS)
236 timeval endTimeOfDay;
238 gettimeofday(&endTimeOfDay, 0);
239 timersub(&endTimeOfDay, &StartTimeOfDay, &diffTime);
240 SecondsCounter totalSeconds = diffTime.tv_sec + diffTime.tv_usec / 1000000.0;
241 #elif (CISST_OS == CISST_QNX)
242 struct timespec endTimeOfDay;
243 struct timespec diffTime;
245 if (clock_gettime(CLOCK_REALTIME, &endTimeOfDay) == -1) {
248 timersub(&endTimeOfDay, &StartTimeOfDay, &diffTime);
249 totalSeconds = diffTime.tv_sec + diffTime.tv_nsec *
cmn_ns;
258 #if (CISST_OS == CISST_WINDOWS) || (CISST_OS == CISST_CYGWIN)
259 bool HasHighPerformanceCounter;
261 double TimeGranularity;
263 #if (CISST_OS == CISST_WINDOWS) || (CISST_OS == CISST_CYGWIN)
265 #elif (CISST_OS == CISST_LINUX_XENOMAI)
269 #elif (CISST_OS == CISST_LINUX_RTAI) || (CISST_OS == CISST_LINUX) || (CISST_OS == CISST_DARWIN) || (CISST_OS == CISST_SOLARIS)
270 timeval StartTimeOfDay;
271 #elif (CISST_OS == CISST_QNX)
272 struct timespec StartTimeOfDay;
278 #endif // _osaStopwatch_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
#define CISST_DEPRECATED
Definition: cmnPortability.h:310
#define CMN_LOG_RUN_ERROR
Definition: cmnLogger.h:166
void Reset(void)
Definition: osaStopwatch.h:121
unsigned long MillisecondsCounter
Definition: osaStopwatch.h:85
Portability across compilers and operating systems tools.
MillisecondsCounter CISST_DEPRECATED GetCurrentRead(void) const
Definition: osaStopwatch.h:196
bool IsRunning(void) const
Definition: osaStopwatch.h:186
#define CMN_LOG_INIT_ERROR
Definition: cmnLogger.h:162
Declaration of units and unit conversion methodsThis file include the definition and implementation o...
Declaration of cmnLogger amd macros for human readable logging.
SecondsCounter GetElapsedTime(void) const
Definition: osaStopwatch.h:207
Definition: osaStopwatch.h:82
Macros to export the symbols of cisstOSAbstraction (in a Dll).
const double cmn_ms
Definition: cmnUnits.h:190
void Stop(void)
Definition: osaStopwatch.h:174
const double cmn_ns
Definition: cmnUnits.h:208
double CISST_EXPORT GetTimeGranularity(void) const
osaStopwatch(void)
Definition: osaStopwatch.h:88
#define CMN_LOG_INIT_WARNING
Definition: cmnLogger.h:163
double SecondsCounter
Definition: osaStopwatch.h:86
void Start(void)
Definition: osaStopwatch.h:142