00001 #ifndef _CLOCK_H_
00002 #define _CLOCK_H_
00003
00004 #include <unistd.h>
00005
00006 #ifdef __linux__
00007 #ifndef linux
00008 #define linux
00009 #endif
00010 #endif
00011
00012 #ifdef linux
00013 #include <sys/time.h>
00014 #else
00015 #include <time.h>
00016 #endif
00017
00018 #if defined(i686) || defined(i386)
00019 typedef unsigned long long RTS_CPUCYCLE;
00020 #include <I386/i386Lib.h>
00021 #elif defined(vxworks)
00022 typedef unsigned int RTS_CPUCYCLE;
00023 #include <MVME/mvmeFastTickerLib.h>
00024 #else
00025 typedef unsigned long long RTS_CPUCYCLE;
00026 #endif
00027
00028 #ifdef vxworks
00029 #include <taskLib.h>
00030 #endif
00031
00032 class RtsTimer
00033 {
00034 public:
00035 #ifdef linux
00036 timeval ts_old;
00037 timeval ts_last;
00038 timeval ts_new;
00039 #else
00040 timespec ts_old;
00041 timespec ts_last;
00042 timespec ts_new;
00043 #endif
00044 double t;
00045
00046 RtsTimer() {
00047 reset();
00048 }
00049
00050 void reset() {
00051 #ifdef linux
00052 gettimeofday(&ts_old,NULL);
00053 #else
00054 clock_gettime(CLOCK_REALTIME, &ts_old);
00055 #endif
00056 ts_last = ts_old;
00057 }
00058
00059 double currtime() {
00060 #ifdef linux
00061 gettimeofday(&ts_new, NULL);
00062 #else
00063 clock_gettime(CLOCK_REALTIME, &ts_new);
00064 #endif
00065 t = ts_new.tv_sec - ts_old.tv_sec;
00066
00067 #ifdef linux
00068 t += ((double)(ts_new.tv_usec - ts_old.tv_usec))/1000000.0;
00069 #else
00070 t += ((double)(ts_new.tv_nsec - ts_old.tv_nsec))/1000000000.0;
00071 #endif
00072 return t;
00073 }
00074
00075 double record_time() {
00076 #ifdef linux
00077 gettimeofday(&ts_new, NULL);
00078 #else
00079 clock_gettime(CLOCK_REALTIME, &ts_new);
00080 #endif
00081 t = ts_new.tv_sec - ts_last.tv_sec;
00082
00083 #ifdef linux
00084 t += ((double)(ts_new.tv_usec - ts_last.tv_usec))/1000000.0;
00085 #else
00086 t += ((double)(ts_new.tv_nsec - ts_last.tv_nsec))/1000000000.0;
00087 #endif
00088 ts_last = ts_new;
00089 return t;
00090 }
00091 };
00092
00093
00094
00095
00096
00097 inline double record_time()
00098 {
00099 static bool nfirst = false;
00100 #ifdef linux
00101 static timeval ts_old;
00102 static timeval ts_new;
00103 #else
00104 static timespec ts_old;
00105 static timespec ts_new;
00106 #endif
00107 static double t;
00108
00109 if(nfirst)
00110 {
00111 #ifdef linux
00112 gettimeofday(&ts_new, NULL);
00113 #else
00114 clock_gettime(CLOCK_REALTIME, &ts_new);
00115 #endif
00116 t = ts_new.tv_sec - ts_old.tv_sec;
00117
00118 #ifdef linux
00119 t += ((double)(ts_new.tv_usec - ts_old.tv_usec))/1000000.0;
00120 #else
00121 t += ((double)(ts_new.tv_nsec - ts_old.tv_nsec))/1000000000.0;
00122 #endif
00123 ts_old = ts_new;
00124 return t;
00125 }
00126
00127 nfirst = true;
00128 #ifdef linux
00129 gettimeofday(&ts_old,NULL);
00130 #else
00131 clock_gettime(CLOCK_REALTIME, &ts_old);
00132 #endif
00133 return 0;
00134 }
00135
00136 inline double record_abs_time(int set)
00137 {
00138 static bool nfirst = false;
00139 #ifdef linux
00140 static timeval ts_old;
00141 static timeval ts_new;
00142 #else
00143 static timespec ts_old;
00144 static timespec ts_new;
00145 #endif
00146 static double t;
00147
00148 if(nfirst)
00149 {
00150 #ifdef linux
00151 gettimeofday(&ts_new, NULL);
00152 #else
00153 clock_gettime(CLOCK_REALTIME, &ts_new);
00154 #endif
00155 t = ts_new.tv_sec - ts_old.tv_sec;
00156
00157 #ifdef linux
00158 t += ((double)(ts_new.tv_usec - ts_old.tv_usec))/1000000.0;
00159 #else
00160 t += ((double)(ts_new.tv_nsec - ts_old.tv_nsec))/1000000000.0;
00161 #endif
00162
00163 return t;
00164 }
00165
00166 if(set || nfirst) {
00167 nfirst = true;
00168 #ifdef linux
00169 gettimeofday(&ts_old,NULL);
00170 #else
00171 clock_gettime(CLOCK_REALTIME, &ts_old);
00172 #endif
00173 }
00174 return 0;
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184 static float cycle_mult = 0.0;
00185
00186 inline void getCpuCycle_init()
00187 {
00188 #ifdef vxworks
00189 mvmeFastTickerInit();
00190 #endif
00191 }
00192
00193 inline void setCpuCycleTime(float m)
00194 {
00195 cycle_mult = m;
00196 }
00197
00198 inline float getCpuCycleTime()
00199 {
00200 return cycle_mult;
00201 }
00202
00203
00204 inline RTS_CPUCYCLE getCpuCycle64()
00205 {
00206 #if defined(i686) || defined(i386)
00207 return (RTS_CPUCYCLE)getfast_l();
00208 #elif defined(vxworks)
00209 return (RTS_CPUCYCLE)mvmeFastTickerGet();
00210 #else
00211
00212
00213 #ifdef linux
00214 timeval ts;
00215 #else
00216 timespec ts;
00217 #endif
00218 RTS_CPUCYCLE x;
00219 #ifdef linux
00220 gettimeofday(&ts,NULL);
00221 #else
00222 clock_gettime(CLOCK_REALTIME, &ts);
00223 #endif
00224
00225 x = ts.tv_sec;
00226 x *= 1000000000;
00227 #ifdef linux
00228 x += ts.tv_usec*1000;
00229 #else
00230 x += ts.tv_nsec;
00231 #endif
00232 return x;
00233 #endif
00234 }
00235
00236 inline unsigned int getCpuCycle()
00237 {
00238 #if defined(i686) || defined(i386)
00239 return getfast();
00240 #elif defined(vxworks)
00241 return mvmeFastTickerGet();
00242 #else
00243 RTS_CPUCYCLE x = getCpuCycle64();
00244 return (unsigned int)(x & 0xffffffff);
00245 #endif
00246 }
00247
00248 inline void timeCpuCycle()
00249 {
00250 RTS_CPUCYCLE t1, t2;
00251
00252 int i=0;
00253 do {
00254 t1 = getCpuCycle();
00255 #ifdef vxworks // 1 second either way....
00256 taskDelay(100);
00257 #else
00258
00259
00260
00261 usleep((unsigned long)1000000);
00262
00263 #endif
00264 t2 = getCpuCycle();
00265
00266 cycle_mult = 1e6 / (float)(t2 - t1);
00267 i++;
00268 } while((t1 > t2) || (i>3));
00269 }
00270
00271 inline float cpu2usec(RTS_CPUCYCLE x)
00272 {
00273
00274 return ((float)x) * cycle_mult;
00275 }
00276
00277
00278
00279 #if defined linux && defined i686
00280
00281 inline double hrecord_time()
00282 {
00283 static int first=1;
00284
00285 static unsigned long long lt=0;
00286 unsigned long long t;
00287 unsigned long long et;
00288 static double clockpersec;
00289
00290 if(first) {
00291 first = 0;
00292 lt = getfast_l();
00293 sleep(1);
00294 t = getfast_l();
00295
00296 if(t > lt)
00297 clockpersec = t-lt;
00298 else
00299 clockpersec = t + (0xffffffffffffffffLL - lt);
00300
00301
00302 lt = getfast_l();
00303 return 0.0;
00304 }
00305
00306 t = getfast_l();
00307
00308 if(t > lt)
00309 et = t-lt;
00310 else
00311 et = t + (0xffffffffffffffffLL - lt);
00312
00313 lt = t;
00314
00315 return (((double)et) / clockpersec);
00316 }
00317 #endif
00318
00319 #endif