StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
clock.h
1 #ifndef _CLOCK_H_
2 #define _CLOCK_H_
3 
4 #include <unistd.h>
5 
6 #if defined(__linux__) || defined(__APPLE__)
7 #ifndef linux
8 #define linux
9 #endif
10 #endif
11 
12 #ifdef linux
13 #include <sys/time.h>
14 #else
15 #include <time.h>
16 #endif
17 
18 #if defined(i686) || defined(i386)
19 typedef unsigned long long RTS_CPUCYCLE; // always 64 bits...
20 #include <I386/i386Lib.h>
21 #elif defined(vxworks)
22 typedef unsigned int RTS_CPUCYCLE; // always 64 bits...
23 #include <MVME/mvmeFastTickerLib.h>
24 #else
25 typedef unsigned long long RTS_CPUCYCLE;
26 #endif
27 
28 #ifdef vxworks
29 #include <taskLib.h>
30 #endif
31 
32 class RtsTimer
33 {
34  public:
35 #ifdef linux
36  timeval ts_old;
37  timeval ts_last;
38  timeval ts_new;
39 #else
40  timespec ts_old;
41  timespec ts_last;
42  timespec ts_new;
43 #endif
44  double t;
45  int ref_cnt;
46 
47  RtsTimer() {
48  reset();
49  }
50 
51  void reset() {
52 #ifdef linux
53  gettimeofday(&ts_old,NULL);
54 #else
55  clock_gettime(CLOCK_REALTIME, &ts_old);
56 #endif
57  ts_last = ts_old;
58  ref_cnt = 0;
59  }
60 
61  double currtime() {
62 #ifdef linux
63  gettimeofday(&ts_new, NULL);
64 #else
65  clock_gettime(CLOCK_REALTIME, &ts_new);
66 #endif
67  t = ts_new.tv_sec - ts_old.tv_sec;
68 
69 #ifdef linux
70  t += ((double)(ts_new.tv_usec - ts_old.tv_usec))/1000000.0;
71 #else
72  t += ((double)(ts_new.tv_nsec - ts_old.tv_nsec))/1000000000.0;
73 #endif
74  return t;
75  }
76 
77 
78  double actualtime() {
79 #ifdef linux
80  gettimeofday(&ts_new, NULL);
81 #else
82  clock_gettime(CLOCK_REALTIME, &ts_new);
83 #endif
84  t = ts_new.tv_sec;
85 
86 #ifdef linux
87  t += ((double)(ts_new.tv_usec))/1000000.0;
88 #else
89  t += ((double)(ts_new.tv_nsec))/1000000000.0;
90 #endif
91  return t;
92  }
93 
94  double record_time() {
95 #ifdef linux
96  gettimeofday(&ts_new, NULL);
97 #else
98  clock_gettime(CLOCK_REALTIME, &ts_new);
99 #endif
100  t = ts_new.tv_sec - ts_last.tv_sec;
101 
102 #ifdef linux
103  t += ((double)(ts_new.tv_usec - ts_last.tv_usec))/1000000.0;
104 #else
105  t += ((double)(ts_new.tv_nsec - ts_last.tv_nsec))/1000000000.0;
106 #endif
107  ts_last = ts_new;
108  return t;
109  }
110 
111  double stopwatch;
112  int stopwatchcounting;
113 
114  void StopWatchReset() {
115  stopwatchcounting = 0;
116  stopwatch = 0;
117  }
118 
119  double StopWatchStop() {
120  if(stopwatchcounting) stopwatch += record_time();
121  stopwatchcounting = 0;
122  return stopwatch;
123  }
124 
125  double StopWatchStart() {
126  if(stopwatchcounting) stopwatch += record_time();
127  else record_time();
128  stopwatchcounting = 1;
129  return stopwatch;
130  }
131 
132  double rt_start() {
133  ref_cnt++;
134  if(ref_cnt == 1) return record_time();
135  return 0.0;
136  }
137 
138  double rt_stop() {
139  ref_cnt--;
140  if(ref_cnt < 0) ref_cnt = 0;
141  if(ref_cnt == 0) return record_time();
142  return 0.0;
143  }
144 
145 };
146 
147 // Returns medium resolution time (~1ms)
148 // Since last call in floating point number
149 // Uses: ~.6 usec in overhead per call...
150 //
151 inline double record_time()
152 {
153  static bool nfirst = false;
154 #ifdef linux
155  static timeval ts_old;
156  static timeval ts_new;
157 #else
158  static timespec ts_old;
159  static timespec ts_new;
160 #endif
161  static double t;
162 
163  if(nfirst)
164  {
165 #ifdef linux
166  gettimeofday(&ts_new, NULL);
167 #else
168  clock_gettime(CLOCK_REALTIME, &ts_new);
169 #endif
170  t = ts_new.tv_sec - ts_old.tv_sec;
171 
172 #ifdef linux
173  t += ((double)(ts_new.tv_usec - ts_old.tv_usec))/1000000.0;
174 #else
175  t += ((double)(ts_new.tv_nsec - ts_old.tv_nsec))/1000000000.0;
176 #endif
177  ts_old = ts_new;
178  return t;
179  }
180 
181  nfirst = true;
182 #ifdef linux
183  gettimeofday(&ts_old,NULL);
184 #else
185  clock_gettime(CLOCK_REALTIME, &ts_old);
186 #endif
187  return 0;
188 }
189 
190 inline double record_abs_time(int set)
191 {
192  static bool nfirst = false;
193 #ifdef linux
194  static timeval ts_old;
195  static timeval ts_new;
196 #else
197  static timespec ts_old;
198  static timespec ts_new;
199 #endif
200  static double t;
201 
202  if(nfirst)
203  {
204 #ifdef linux
205  gettimeofday(&ts_new, NULL);
206 #else
207  clock_gettime(CLOCK_REALTIME, &ts_new);
208 #endif
209  t = ts_new.tv_sec - ts_old.tv_sec;
210 
211 #ifdef linux
212  t += ((double)(ts_new.tv_usec - ts_old.tv_usec))/1000000.0;
213 #else
214  t += ((double)(ts_new.tv_nsec - ts_old.tv_nsec))/1000000000.0;
215 #endif
216  // ts_old = ts_new;
217  return t;
218  }
219 
220  if(set || nfirst) {
221  nfirst = true;
222 #ifdef linux
223  gettimeofday(&ts_old,NULL);
224 #else
225  clock_gettime(CLOCK_REALTIME, &ts_old);
226 #endif
227  }
228  return 0;
229 }
230 
231 // Inline wrappers for tonkos fast clocks:
232 // RTS_CPUCYCLE type is a 64 or 32 bit int depending on platforms clock
233 //
234 // call getCpuCycle_init() once
235 // call timeCpuCycle to time the clock
236 // call setCpuCycleTime to set the cycle period yourself (in usecs)
237 // call cpu2usec() to get the real time for some difference
238 static float cycle_mult = 0.0;
239 
240 inline void getCpuCycle_init()
241 {
242 #ifdef vxworks
243  mvmeFastTickerInit();
244 #endif
245 }
246 
247 inline void setCpuCycleTime(float m)
248 {
249  cycle_mult = m;
250 }
251 
252 inline float getCpuCycleTime()
253 {
254  return cycle_mult;
255 }
256 
257 // Returns high resolution clock tick by platform
258 inline RTS_CPUCYCLE getCpuCycle64()
259 {
260 #if defined(i686) || defined(i386)
261  return (RTS_CPUCYCLE)getfast_l();
262 #elif defined(vxworks)
263  return (RTS_CPUCYCLE)mvmeFastTickerGet();
264 #else
265 
266  // If we don't have fast timer, fake it.
267 #ifdef linux
268  timeval ts;
269 #else
270  timespec ts;
271 #endif
272  RTS_CPUCYCLE x;
273 #ifdef linux
274  gettimeofday(&ts,NULL);
275 #else
276  clock_gettime(CLOCK_REALTIME, &ts);
277 #endif
278 
279  x = ts.tv_sec;
280  x *= 1000000000;
281 #ifdef linux
282  x += ts.tv_usec*1000;
283 #else
284  x += ts.tv_nsec;
285 #endif
286  return x;
287 #endif
288 }
289 
290 inline unsigned int getCpuCycle()
291 {
292 #if defined(i686) || defined(i386)
293  return getfast();
294 #elif defined(vxworks)
295  return mvmeFastTickerGet();
296 #else
297  RTS_CPUCYCLE x = getCpuCycle64();
298  return (unsigned int)(x & 0xffffffff);
299 #endif
300 }
301 
302 inline void timeCpuCycle()
303 {
304  RTS_CPUCYCLE t1, t2;
305 
306  int i=0;
307  do {
308  t1 = getCpuCycle();
309 #ifdef vxworks // 1 second either way....
310  taskDelay(100);
311 #else
312  //timespec ts;
313  //ts.tv_sec = 0;
314  //ts.tv_nsec = 100000000;
315  usleep((unsigned long)1000000);
316  //nanosleep(&ts,NULL);
317 #endif
318  t2 = getCpuCycle();
319 
320  cycle_mult = 1e6 / (float)(t2 - t1); // cycle_mult usec/cycle
321  i++;
322  } while((t1 > t2) || (i>3)); // keep trying untill cycle mult > 0, but not too long
323 }
324 
325 inline float cpu2usec(RTS_CPUCYCLE x)
326 {
327  // printf("cycle_mult=%f %d %d \n",cycle_mult, (int)(x>>32),(int)x);
328  return ((float)x) * cycle_mult;
329 }
330 
331 
332 // record time with high precision clock
333 #if defined linux && defined i686
334 
335 inline double hrecord_time()
336 {
337  static int first=1;
338 
339  static unsigned long long lt=0;
340  unsigned long long t;
341  unsigned long long et;
342  static double clockpersec;
343 
344  if(first) {
345  first = 0;
346  lt = getfast_l();
347  sleep(1);
348  t = getfast_l();
349 
350  if(t > lt)
351  clockpersec = t-lt;
352  else
353  clockpersec = t + (0xffffffffffffffffLL - lt);
354 
355 
356  lt = getfast_l();
357  return 0.0;
358  }
359 
360  t = getfast_l();
361 
362  if(t > lt)
363  et = t-lt;
364  else
365  et = t + (0xffffffffffffffffLL - lt);
366 
367  lt = t;
368 
369  return (((double)et) / clockpersec);
370 }
371 #endif
372 
373 #endif
Definition: clock.h:32