00001 /* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ 00002 00029 #ifndef __LINBOX__TIMER__C__ 00030 #define __LINBOX__TIMER__C__ 00031 // Description: 00032 // - various timer objects 00033 // - to be rewritten to be more efficient 00034 00035 #include <cmath> 00036 00037 extern "C" { 00038 # include <sys/time.h> 00039 # include <sys/resource.h> 00040 // int getrusage (int, struct rusage*) ; 00041 } 00042 00043 #include <iostream> 00044 00045 #include <assert.h> 00046 00047 #include "timer.h" 00048 00050 long BaseTimer::seed() 00051 { 00052 struct timeval tp; 00053 gettimeofday(&tp, 0) ; 00054 return(tp.tv_usec); 00055 } 00056 00058 std::ostream& BaseTimer::print( std::ostream& o ) const 00059 { return o << _t ; } 00060 00062 BaseTimer& BaseTimer::operator = (const BaseTimer & T) 00063 { 00064 _t = T._t ; 00065 return *this ; 00066 } 00067 00069 const BaseTimer BaseTimer::operator - (const BaseTimer & T) const 00070 { 00071 BaseTimer Tmp ; 00072 Tmp._t = _t - T._t ; 00073 return Tmp ; 00074 } 00075 00076 const BaseTimer BaseTimer::operator - () 00077 { 00078 BaseTimer Tmp ; 00079 Tmp._t = -_t ; 00080 return Tmp ; 00081 } 00082 00083 const BaseTimer BaseTimer::operator + (const BaseTimer & T) const 00084 { 00085 BaseTimer Tmp ; 00086 Tmp._t = _t + T._t ; 00087 return Tmp ; 00088 } 00089 00091 void RealTimer::start() 00092 { 00093 struct timeval tmp2 ; 00094 gettimeofday (&tmp2, 0) ; 00095 00096 // real time 00097 _start_t = (double) tmp2.tv_sec + 00098 ((double) tmp2.tv_usec)/ (double)BaseTimer::MSPSEC ; 00099 } 00100 00101 00104 void RealTimer::stop() 00105 { 00106 struct timeval tmp2 ; 00107 gettimeofday (&tmp2, 0) ; 00108 00109 // real time 00110 _t += (double) tmp2.tv_sec + 00111 ((double) tmp2.tv_usec)/ (double)BaseTimer::MSPSEC - _start_t ; 00112 } 00113 00115 void UserTimer::start() 00116 { 00117 struct rusage tmp1 ; // to getrusage (sys+user times) 00118 getrusage (RUSAGE_SELF, &tmp1) ; 00119 // user time 00120 _start_t = (double) tmp1.ru_utime.tv_sec + 00121 ((double) tmp1.ru_utime.tv_usec)/ (double)MSPSEC ; 00122 } 00123 00124 00127 void UserTimer::stop() 00128 { 00129 struct rusage tmp1 ; // to getrusage (sys+user times) 00130 getrusage (RUSAGE_SELF, &tmp1) ; 00131 // user time 00132 _t += (double) tmp1.ru_utime.tv_sec + 00133 ((double) tmp1.ru_utime.tv_usec)/ (double)MSPSEC - _start_t ; 00134 } 00135 00136 00138 void SysTimer::start() 00139 { 00140 struct rusage tmp1 ; // to getrusage (sys+user times) 00141 getrusage (RUSAGE_SELF, &tmp1) ; 00142 // user time 00143 _start_t = (double) tmp1.ru_stime.tv_sec + 00144 ((double) tmp1.ru_stime.tv_usec)/ (double)MSPSEC ; 00145 } 00146 00147 00150 void SysTimer::stop() 00151 { 00152 struct rusage tmp1 ; // to getrusage (sys+user times) 00153 getrusage (RUSAGE_SELF, &tmp1) ; 00154 // user time 00155 _t += (double) tmp1.ru_stime.tv_sec + 00156 ((double) tmp1.ru_stime.tv_usec)/ (double)MSPSEC - _start_t ; 00157 } 00158 00159 00160 00162 void Timer::clear() 00163 { rt.clear() ; ut.clear(); st.clear(); _count = 0; b_IsRunning=false; b_Paused=false; } 00164 00166 void Timer::start() 00167 { 00168 assert(!b_IsRunning || (b_IsRunning && b_Paused )); 00169 if (!b_IsRunning || (b_IsRunning && b_Paused )) 00170 { 00171 b_IsRunning=true; rt.start() ; ut.start(); st.start(); _count = 0; 00172 } 00173 } 00174 00175 void Timer::continueTimer() 00176 { 00177 #ifdef CF_TEST 00178 assert ( b_Paused ) ; 00179 #endif 00180 if (b_Paused) 00181 { 00182 if (b_IsRunning) 00183 { 00184 rt.start() ; ut.start(); st.start(); _count = 0; 00185 } 00186 b_Paused=false; 00187 } 00188 } 00189 00191 void Timer::stop() 00192 { 00193 #ifdef CF_TEST 00194 assert ( !b_Paused && b_IsRunning) ; 00195 #endif 00196 00197 assert ( !b_Paused ) ; 00198 00199 if (b_IsRunning) 00200 { 00201 rt.stop() ; ut.stop(); st.stop(); _count = 1; b_IsRunning=false; 00202 } 00203 } 00204 00205 void Timer::pauseTimer() 00206 { 00207 b_Paused=true; 00208 if (b_IsRunning) { rt.stop() ; ut.stop(); st.stop(); _count = 1; } 00209 } 00210 00211 00212 std::ostream& Timer::print( std::ostream& o ) const 00213 { 00214 o << "user time: " << usertime() << '\n' ; 00215 o << "sys. time: " << systime() << '\n' ; 00216 return o << "real time: " << realtime() << std::endl ; 00217 } 00218 00220 Timer& Timer::operator = (const Timer & T) 00221 { 00222 ut = T.ut ; 00223 st = T.st ; 00224 rt = T.rt ; 00225 _count = T._count; 00226 return *this ; 00227 } 00228 00230 const Timer Timer::operator - (const Timer & T) const 00231 { 00232 Timer Tmp ; 00233 Tmp.ut = ut - T.ut ; 00234 Tmp.st = st - T.st ; 00235 Tmp.rt = rt - T.rt ; 00236 Tmp._count = _count - T._count; 00237 return Tmp ; 00238 } 00239 00240 const Timer Timer::operator - () 00241 { 00242 Timer Tmp ; 00243 Tmp.ut = -ut ; 00244 Tmp.st = -st ; 00245 Tmp.rt = -rt ; 00246 Tmp._count = - _count; 00247 return Tmp ; 00248 } 00249 00250 const Timer Timer::operator + (const Timer & T) const 00251 { 00252 Timer Tmp ; 00253 Tmp.ut = ut + T.ut ; 00254 Tmp.st = st + T.st ; 00255 Tmp.rt = rt + T.rt ; 00256 Tmp._count = _count + T._count; 00257 return Tmp ; 00258 } 00259 00260 00261 #endif