StopWatch.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
3  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "StopWatch.h"
19 
20 // ========================================================================== ;
21 // ;
22 // private auxiliary functions
23 // ;
24 // ========================================================================== ;
25 
26 // ========================================================================== ;
27 // ;
28 // protected auxiliary functions
29 // ;
30 // ========================================================================== ;
31 
32 // ========================================================================= ;
33 // ;
34 // constructors / destructor ;
35 // ;
36 // ========================================================================= ;
37 
39  : CMessage("CStopWatch", g_iDebugLevel, g_bDebug, g_bDebugFile),
40  m_bStartFlag(false),
41  m_bStopFlag(false),
42  m_iTimeType(util_REAL_TIME)
43 {
44  m_iFirst = 0;
45  m_iLast = 0;
46  m_fOverflowTime = 0.0;
47 
48 #if defined(_WIN32)
49  QueryPerformanceFrequency(&m_TempTime); // pointer to current frequency
50  frequencyE = m_TempTime.LowPart;
51  m_FirstTime.HighPart = 0;
52  m_FirstTime.LowPart = 0;
53  m_LastTime.HighPart = 0;
54  m_LastTime.LowPart = 0;
55  m_TempTime.HighPart = 0;
56  m_TempTime.LowPart = 0;
57  m_ActualTime.HighPart = 0;
58  m_ActualTime.LowPart = 0;
59 #elif defined(__QNX__)
60  m_FirstTime.tv_sec = 0;
61  m_FirstTime.tv_nsec = 0;
62  m_LastTime.tv_sec = 0;
63  m_LastTime.tv_nsec = 0;
64  m_TempTime.tv_sec = 0;
65  m_TempTime.tv_nsec = 500000;
66  clock_setres(CLOCK_REALTIME, &m_TempTime);
67  m_ActualTime.tv_sec = 0;
68  m_ActualTime.tv_nsec = 0;
69 #else
70  m_FirstTime.tv_sec = 0;
71  m_FirstTime.tv_usec = 0;
72  m_LastTime.tv_sec = 0;
73  m_LastTime.tv_usec = 0;
74  m_TempTime.tv_sec = 0;
75  m_TempTime.tv_usec = 0;
76  m_ActualTime.tv_sec = 0;
77  m_ActualTime.tv_usec = 0;
78 #endif
79 }
80 
82  : CMessage("CStopWatch", g_iDebugLevel, g_bDebug, g_bDebugFile),
83  m_bStartFlag(false),
84  m_bStopFlag(false),
85  m_iTimeType(iTimeType)
86 {
87  m_iFirst = 0;
88  m_iLast = 0;
89  m_fOverflowTime = 0.0;
90 
91 #if defined(_WIN32)
92  QueryPerformanceFrequency(&m_TempTime); // pointer to current frequency
93  frequencyE = m_TempTime.LowPart;
94  m_FirstTime.HighPart = 0;
95  m_FirstTime.LowPart = 0;
96  m_LastTime.HighPart = 0;
97  m_LastTime.LowPart = 0;
98  m_TempTime.HighPart = 0;
99  m_TempTime.LowPart = 0;
100  m_ActualTime.HighPart = 0;
101  m_ActualTime.LowPart = 0;
102 #elif defined(__QNX__)
103  m_FirstTime.tv_sec = 0;
104  m_FirstTime.tv_nsec = 0;
105  m_LastTime.tv_sec = 0;
106  m_LastTime.tv_nsec = 0;
107  m_TempTime.tv_sec = 0;
108  m_TempTime.tv_nsec = 500000;
109  clock_setres(CLOCK_REALTIME, &m_TempTime);
110  m_ActualTime.tv_sec = 0;
111  m_ActualTime.tv_nsec = 0;
112 #else
113  m_FirstTime.tv_sec = 0;
114  m_FirstTime.tv_usec = 0;
115  m_LastTime.tv_sec = 0;
116  m_LastTime.tv_usec = 0;
117  m_TempTime.tv_sec = 0;
118  m_TempTime.tv_usec = 0;
119  m_ActualTime.tv_sec = 0;
120  m_ActualTime.tv_usec = 0;
121 #endif
122 }
123 
125 {
126  error(-1, "copy constructor : method should not be called!");
127 }
128 
129 // ========================================================================= ;
130 // ;
131 // operators
132 // ;
133 // ========================================================================= ;
134 
136 {
137  error(-1, "assignment operator : method should not be called!");
138  return *this;
139 }
140 
141 // ========================================================================= ;
142 // ;
143 // query functions
144 // ;
145 // ========================================================================= ;
146 
147 // ========================================================================= ;
148 // ;
149 // modifiy functions
150 // ;
151 // ========================================================================= ;
152 
153 // ========================================================================= ;
154 // ;
155 // I/O
156 // ;
157 // ========================================================================= ;
158 
159 /*
160  This member function computes the difference between the taken start and stop times, if both are valid.
161  Attention: cpu-time is calculated in a wrong way, if process lasts longer than 35 min or 2 processes
162  are stoped with the second beginning more than half an hour after the first one was started.
163  Look for function testOverflow in any of these cases.
164  difference or $0.0$ if call was invalid
165 */
167 {
168  // --- check
169  if ( !(m_bStartFlag && m_bStopFlag) )
170  {
171  warning("executionTime() : return 0.0, for you must call 'start()' and 'stop()' first");
172  return 0.0;
173  };
174 
176  {
177  if ((m_iLast < m_iFirst) && (m_fOverflowTime == 0))
178  {
179  warning("executionTime() : return 0.0, for start time is bigger than stop time and no overflow was detected");
180  return 0.0;
181  }
182  else
183  {
184  double fTempTime;
185  testOverflow();
186  fTempTime = m_fOverflowTime;
187  m_fOverflowTime = 0.0;
188  return fTempTime + (double(m_iLast - m_iFirst)) / CLOCKS_PER_SEC;
189  };
190  }
191  else
192  {
193  #if defined(__QNX__)
194  return (m_LastTime.tv_sec-m_FirstTime.tv_sec
195  +(double(m_LastTime.tv_nsec-m_FirstTime.tv_nsec)/1e+9));
196  #elif defined(_WIN32)
197  return double(m_LastTime.LowPart-m_FirstTime.LowPart)/frequencyE;
198  #else
199  return (m_LastTime.tv_sec-m_FirstTime.tv_sec
200  +(double(m_LastTime.tv_usec-m_FirstTime.tv_usec)/1e+6));
201  #endif
202  };
203 };
204 
205 // ========================================================================= ;
206 // ;
207 // exec functions
208 // ;
209 // ========================================================================= ;
210 
212 {
214  {
215  m_iFirst=clock();
216  m_bStartFlag = true;
217  m_bStopFlag = false;
218  }
219  else
220  {
221 #if defined(__QNX__)
222  clock_gettime(CLOCK_REALTIME,&m_FirstTime);
223 #elif defined(_WIN32)
224  QueryPerformanceCounter(&m_FirstTime);
225 #else
226  gettimeofday(&m_FirstTime,0);
227 #endif
228  m_bStartFlag = true;
229  m_bStopFlag = false;
230  };
231 };
232 
233 /*
234  This member function sets the stop time of the watch to the current clock value, if a valid start time exists.
235 */
237 {
238  if (m_bStartFlag)
239  {
241  {
242  m_iLast = clock();
243  }
244  else
245  {
246  #if defined(__QNX__)
247  clock_gettime(CLOCK_REALTIME,&m_LastTime);
248  #elif defined(_WIN32)
249  QueryPerformanceCounter(&m_LastTime);
250  #else
251  gettimeofday(&m_LastTime,0);
252  #endif
253  };
254  m_bStopFlag = true;
255  }
256  else
257  {
258  m_bStopFlag = false;
259  warning("stop() : you must call 'start()' first");
260  };
261 };
262 
263 // -------------------------------------------------------------------------- ;
264 
265 /*
266  This member function continues the run of the clock after is has been stopped.
267 */
269 {
270  if (m_bStartFlag && m_bStopFlag)
271  {
273  {
274  m_iFirst = m_iFirst + (clock() - m_iLast);
275  m_bStopFlag = false;
276  }
277  else
278  {
279  #if defined(__QNX__)
280  clock_gettime(CLOCK_REALTIME,&m_TempTime);
281  m_FirstTime.tv_sec += m_TempTime.tv_sec-m_LastTime.tv_sec;
282  m_FirstTime.tv_nsec += m_TempTime.tv_nsec-m_LastTime.tv_nsec;
283  #elif defined(_WIN32)
284  QueryPerformanceCounter(&m_TempTime);
285  m_FirstTime.HighPart += m_TempTime.HighPart-m_LastTime.HighPart;
286  m_FirstTime.LowPart += m_TempTime.LowPart-m_LastTime.LowPart;
287  #else
288  gettimeofday(&m_TempTime,0);
289  m_FirstTime.tv_sec += m_TempTime.tv_sec-m_LastTime.tv_sec;
290  m_FirstTime.tv_usec += m_TempTime.tv_usec-m_LastTime.tv_usec;
291  #endif
292  };
293  }
294  else
295  warning("cont() : you must call 'start()' and 'stop()' first");
296 };
297 
298 // This member function returns the current real time in [s].
300 {
301  #if defined(__QNX__)
302  clock_gettime(CLOCK_REALTIME,&m_ActualTime);
303  return (m_ActualTime.tv_sec+(double(m_ActualTime.tv_nsec)/1e+9));
304  #elif defined(_WIN32)
305  QueryPerformanceCounter(&m_ActualTime);
306  return (double(m_ActualTime.LowPart)/frequencyE);
307  #else
308  gettimeofday(&m_ActualTime,0);
309  return (m_ActualTime.tv_sec
310  +(double(m_ActualTime.tv_usec)/1e+6));
311  #endif
312 };
313 
314 // This member function returns the real time resolution in [s].
316 {
317  #if defined(__QNX__)
318  clock_getres(CLOCK_REALTIME,&m_TempTime);
319  return (m_TempTime.tv_sec+(double(m_TempTime.tv_nsec)/1e+9));
320  #elif defined(_WIN32)
321  warning("unkown real time resolution\n");
322  return 0.001;
323  #else
324  warning("unkown real time resolution\n");
325  return 0.001;
326  #endif
327 };
328 
329 /*
330  This member function waits the given realtime in [ms].
331  uiTime: realtime in [ms]
332 */
333 void CStopWatch::wait(unsigned int uiTime)
334 {
335  bool bTimeOutFlag = false;
336 #if defined(__QNX__)
337  unsigned int uiSec, uiNSec;
338  uiSec = uiTime / 1000;
339  uiNSec = (uiTime % 1000) * 1000000;
340  clock_gettime(CLOCK_REALTIME, &m_TempTime);
341  m_TempTime.tv_sec = m_TempTime.tv_sec + uiSec + (m_TempTime.tv_nsec + uiNSec) / 1000000000;
342  m_TempTime.tv_nsec = (m_FirstTime.tv_nsec + uiNSec) % 1000000000;
343  do
344  {
345  clock_gettime(CLOCK_REALTIME, &m_ActualTime);
346  if(m_ActualTime.tv_sec > m_TempTime.tv_sec)
347  bTimeOutFlag = true;
348  else if((m_ActualTime.tv_sec == m_TempTime.tv_sec) && (m_ActualTime.tv_nsec > m_TempTime.tv_nsec))
349  bTimeOutFlag = true;
350  }
351  while(!bTimeOutFlag);
352 #elif defined(_WIN32)
353  Sleep(uiTime);
354 #else
355  unsigned int uiSec, uiUSec;
356  uiSec = uiTime / 1000;
357  uiUSec = (uiTime % 1000) * 1000;
358  gettimeofday(&m_TempTime, 0);
359  m_TempTime.tv_sec = m_TempTime.tv_sec + uiSec + (m_TempTime.tv_usec + uiUSec) / 1000000;
360  m_TempTime.tv_usec = (m_TempTime.tv_usec + uiUSec) % 1000000;
361  do
362  {
363  gettimeofday(&m_ActualTime, 0);
364  if(m_ActualTime.tv_sec > m_TempTime.tv_sec)
365  bTimeOutFlag = true;
366  else if((m_ActualTime.tv_sec == m_TempTime.tv_sec) && (m_ActualTime.tv_usec > m_TempTime.tv_usec))
367  bTimeOutFlag = true;
368  }
369  while(!bTimeOutFlag);
370 #endif
371 };
372 
373 // -------------------------------------------------------------------------- ;
374 
375 /*
376  This member function returns the current date and time in local representation
377  char* to hold the local date and time
378 */
379 void CStopWatch::date(char* acDate) const
380 {
381  time_t currentTime;
382  struct tm *localTime;
383 
384  // get the current time
385  currentTime = time (NULL);
386 
387  // convert it to local time representation
388  localTime = localtime (&currentTime);
389 
390  // print it out in a buffer
391  strftime (acDate, 256, "%a %b %d %I:%M:%S %p %Z %Y", localTime);
392 };
393 
394 // -------------------------------------------------------------------------- ;
395 
396 /*
397  This member function returns the current weekday in local representation
398  string to hold the local weekday
399 */
400 void CStopWatch::weekday(char* acWeekDay) const
401 {
402  time_t currentTime;
403  struct tm *localTime;
404 
405  // get the current time
406  currentTime = time (NULL);
407 
408  // convert it to local time representation
409  localTime = localtime (&currentTime);
410 
411  // print it out in a buffer
412  strftime (acWeekDay, 256, "%A", localTime);
413 };
414 
415 // ========================================================================= ;
416 
417 /*
418  This function tests, if there is an overflow at the clock counter
419  Function should be called at least every 30 min., if the cpu_time is stoped
420  Otherwise there might be more than one overflow and results are wrong!
421 */
423 {
425  {
426  stop();
427  if (m_iLast < m_iFirst)
428  {
429  m_fOverflowTime += (double(m_iLast - m_iFirst + ULONG_MAX)) / CLOCKS_PER_SEC;
430  start();
431  }
432  else
433  cont();
434  }
435  else
436  warning("testOverflow() : overflow has to be tested only when measuring cpu-time");
437 };
438 
439 // ========================================================================= ;
CStopWatch & operator=(const CStopWatch &)
Definition: StopWatch.cpp:135
void wait(unsigned int uiTime)
Definition: StopWatch.cpp:333
long m_iLast
Definition: StopWatch.h:48
void weekday(char *acWeekday) const
Definition: StopWatch.cpp:400
long m_iFirst
Definition: StopWatch.h:47
void stop()
Definition: StopWatch.cpp:236
timeval m_FirstTime
Definition: StopWatch.h:63
void error(const int iErrorCode, const char *pcErrorMessage,...) const
Definition: Message.cpp:204
util_TimeMeasurementType
Definition: StopWatch.h:29
timeval m_LastTime
Definition: StopWatch.h:64
util_TimeMeasurementType m_iTimeType
Definition: StopWatch.h:72
void cont()
Definition: StopWatch.cpp:268
double executionTime()
Definition: StopWatch.cpp:166
int g_iDebugLevel
Definition: Message.cpp:20
double realTime()
Definition: StopWatch.cpp:299
void start()
Definition: StopWatch.cpp:211
void warning(const char *pcWarningMessage,...) const
Definition: Message.cpp:257
timeval m_TempTime
Definition: StopWatch.h:65
bool g_bDebug
Definition: Message.cpp:22
bool g_bDebugFile
Definition: Message.cpp:21
bool m_bStartFlag
Definition: StopWatch.h:69
void date(char *acDate) const
Definition: StopWatch.cpp:379
double realTimeResolution()
Definition: StopWatch.cpp:315
timeval m_ActualTime
Definition: StopWatch.h:66
double m_fOverflowTime
Definition: StopWatch.h:49
void testOverflow()
Definition: StopWatch.cpp:422
bool m_bStopFlag
Definition: StopWatch.h:70


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Nov 25 2019 03:48:19