メインページ   モジュール   名前空間一覧   クラス階層   アルファベット順一覧   構成   ファイル一覧   構成メンバ   ファイルメンバ   関連ページ    

TSystemTimer.cc

解説を見る。
00001 // =====================================================================
00002 //  $Id: TSystemTimer.cc,v 1.6 2004/03/07 10:30:34 goiwai Exp $
00003 //  $Name: CLDAQ-1-14-03 $
00004 //  $Log: TSystemTimer.cc,v $
00005 //  Revision 1.6  2004/03/07 10:30:34  goiwai
00006 //  ROOTに組みこむためのおまじないマクロを埋めこみました。
00007 //  全てにおいて完全に動作するわけではありません。
00008 //
00009 //  Revision 1.5  2004/03/07 07:29:38  goiwai
00010 //  TSystemTimer::WhatTimeIsItNow()が実装されてなかったので実装しました。
00011 //
00012 //  Revision 1.4  2003/10/06 16:38:26  goiwai
00013 //  *** empty log message ***
00014 //
00015 //  Revision 1.3  2003/07/30 16:21:21  goiwai
00016 //  ファイルにコミットログをつけることにしました.
00017 //
00018 // =====================================================================
00019 #include "TSystemTimer.hh"
00020 #include "TSystemClock.hh"
00021 
00022 static const Tdouble _clktck = (Tdouble)( sysconf( _SC_CLK_TCK ) );
00023 
00024 TSystemTimer::TSystemTimer( const Tstring& unit )
00025   : theStatus( tUndefined ),
00026     theUnit( unit ),
00027     theBeginOfRealTime(),
00028     theEndOfRealTime(),
00029     theBeginOfSystemTime(),
00030     theEndOfSystemTime(),
00031     theBeginOfUserTime(),
00032     theEndOfUserTime(),
00033     thePausedTime(),
00034     theRunningTime(),
00035     theIdlingTime(),
00036     theRealLapTime(),
00037     theSystemLapTime(),
00038     theUserLapTime()
00039 {
00040   initialize();
00041 }
00042 
00043 TSystemTimer::TSystemTimer( const TSystemTimer& right )
00044   : theStatus( right.theStatus ),
00045     theUnit( right.theUnit ),
00046     theBeginOfRealTime( right.theBeginOfRealTime ),
00047     theEndOfRealTime( right.theEndOfRealTime ),
00048     theBeginOfSystemTime( right.theBeginOfSystemTime ),
00049     theEndOfSystemTime( right.theEndOfSystemTime ),
00050     theBeginOfUserTime( right.theBeginOfUserTime ),
00051     theEndOfUserTime( right.theEndOfUserTime ),
00052     thePausedTime( right.thePausedTime ),
00053     theRunningTime( right.theRunningTime ),
00054     theIdlingTime( right.theIdlingTime ),
00055     theRealLapTime( right.theRealLapTime ),
00056     theSystemLapTime( right.theSystemLapTime ),
00057     theUserLapTime( right.theUserLapTime )
00058 {;}
00059 
00060 TSystemTimer::~TSystemTimer()
00061 {;}
00062 
00063 const TSystemTimer& TSystemTimer::operator=( const TSystemTimer& right )
00064 {
00065   theStatus = right.theStatus;
00066   theUnit = right.theUnit;
00067   theBeginOfRealTime = right.theBeginOfRealTime;
00068   theEndOfRealTime = right.theEndOfRealTime;
00069   theBeginOfSystemTime = right.theBeginOfSystemTime;
00070   theEndOfSystemTime = right.theEndOfSystemTime;
00071   theBeginOfUserTime = right.theBeginOfUserTime;
00072   theEndOfUserTime = right.theEndOfUserTime;
00073   thePausedTime = right.thePausedTime;
00074   theRunningTime = right.theRunningTime;
00075   theIdlingTime = right.theIdlingTime;
00076   theRealLapTime = right.theRealLapTime;
00077   theSystemLapTime = right.theSystemLapTime;
00078   theUserLapTime = right.theUserLapTime;
00079   return *this;
00080 }
00081 
00082 Tvoid TSystemTimer::Start()
00083 {
00084   if ( theStatus == tReady ) {
00085     start();
00086     return;
00087   } else if ( theStatus == tIdle ) {
00088     restart();
00089     return;
00090   } else {
00091     return;
00092   }
00093 }
00094 
00095 Tvoid TSystemTimer::Pause()
00096 {
00097   if ( theStatus == tRunning ) {
00098     pause();
00099     return;
00100   } else {
00101     return;
00102   }
00103 }
00104 
00105 Tvoid TSystemTimer::Lap()
00106 {
00107   if ( theStatus == tRunning ) {
00108     lap();
00109     return;
00110   } else if ( theStatus == tIdle ) {
00111     restart();
00112     lap();
00113     pause();
00114     return;
00115   } else {
00116     return;
00117   }
00118 }
00119 
00120 Tvoid TSystemTimer::Restart()
00121 {
00122   if ( theStatus == tIdle ) {
00123     restart();
00124     return;
00125   } else {
00126     return;
00127   }
00128 }
00129 
00130 Tvoid TSystemTimer::Stop()
00131 {
00132   if ( theStatus == tIdle ) {
00133     restart();
00134     stop();
00135     return;
00136   } else if ( theStatus == tRunning ) {
00137     stop();
00138     return;
00139   } else {
00140     return;
00141   }
00142 }
00143 
00144 Tvoid TSystemTimer::initialize()
00145 {
00146   clear();
00147   theStatus = tReady;
00148   return;
00149 }
00150 
00151 Tvoid TSystemTimer::clear()
00152 {
00153   theBeginOfRealTime = DBL_MIN;
00154   theEndOfRealTime = DBL_MIN;
00155   theBeginOfSystemTime = DBL_MIN;
00156   theEndOfSystemTime = DBL_MIN;
00157   theBeginOfUserTime = DBL_MIN;
00158   theEndOfUserTime = DBL_MIN;
00159   thePausedTime = 0.0;
00160   theRunningTime = 0.0;
00161   theIdlingTime = 0.0;
00162   theRealLapTime.clear();
00163   theSystemLapTime.clear();
00164   theUserLapTime.clear();
00165   return;
00166 }
00167 
00168 Tvoid TSystemTimer::start()
00169 {
00170   clear();
00171   struct tms buf;
00172   Tclock_t clk = times( &buf ); 
00173   if ( clk == -1 ) {
00174     perror( "TSystemTimer::start" );
00175     return;
00176   }
00177 
00178   theBeginOfRealTime = ( (Tdouble)( clk ) / _clktck );
00179   theBeginOfSystemTime = ( (Tdouble)( buf.tms_stime ) / _clktck );
00180   theBeginOfUserTime = ( (Tdouble)( buf.tms_utime ) / _clktck );
00181 
00182   theStatus = tRunning;
00183 
00184   return;
00185 }
00186 
00187 Tvoid TSystemTimer::stop()
00188 {
00189   struct tms buf;
00190   Tclock_t clk = times( &buf ); 
00191   if ( clk == -1 ) {
00192     perror( "TSystemTimer::start" );
00193     return;
00194   }
00195 
00196   theEndOfRealTime = ( (Tdouble)( clk ) / _clktck );
00197   theEndOfSystemTime = ( (Tdouble)( buf.tms_stime ) / _clktck );
00198   theEndOfUserTime = ( (Tdouble)( buf.tms_utime ) / _clktck );
00199   theRunningTime = theEndOfRealTime - theBeginOfRealTime - theIdlingTime;
00200 
00201   theStatus = tReady;
00202 
00203   return;
00204 }
00205 
00206 Tvoid TSystemTimer::restart()
00207 {
00208   struct tms buf;
00209   Tclock_t clk = times( &buf ); 
00210   if ( clk == -1 ) {
00211     perror( "TSystemTimer::restart" );
00212     return;
00213   }
00214 
00215   Tdouble restarted = ( (Tdouble)( clk ) / _clktck );
00216   theIdlingTime += ( restarted - thePausedTime );
00217 
00218   theStatus = tRunning;
00219   return;
00220 }
00221 
00222 Tvoid TSystemTimer::pause()
00223 {
00224   struct tms buf;
00225   Tclock_t clk = times( &buf ); 
00226   if ( clk == -1 ) {
00227     perror( "TSystemTimer::pause" );
00228     return;
00229   }
00230 
00231   thePausedTime = ( (Tdouble)( clk ) / _clktck );
00232 
00233   theStatus = tIdle;
00234   return;
00235 }
00236 
00237 Tvoid TSystemTimer::lap()
00238 {
00239   struct tms buf;
00240   Tclock_t clk = times( &buf ); 
00241   if ( clk == -1 ) {
00242     perror( "TSystemTimer::lap" );
00243     return;
00244   }
00245 
00246   Tdouble realbuf = ( (Tdouble)( clk ) / _clktck );
00247   Tdouble sysbuf = ( (Tdouble)( buf.tms_stime ) / _clktck );
00248   Tdouble userbuf = ( (Tdouble)( buf.tms_utime ) / _clktck );
00249 
00250   Tdouble factor = 1.0;
00251   if ( theUnit == Tmsec ) {
00252     factor = 1.0e+03;
00253   } else if ( theUnit == Tusec ) {
00254     factor = 1.0e+06;
00255   } else if ( theUnit == Tnsec ) {
00256     factor = 1.0e+09;
00257   } else {
00258     SetUnit( Tsec );
00259   }
00260 
00261   theRealLapTime.push_back( ( realbuf - theBeginOfRealTime ) * factor );
00262   theSystemLapTime.push_back( ( sysbuf - theBeginOfSystemTime ) * factor );
00263   theUserLapTime.push_back( ( userbuf - theBeginOfUserTime ) * factor );
00264 
00265   return;
00266 }
00267 
00268 Tdouble TSystemTimer::GetRealElapsedTime()
00269 {
00270   if ( theStatus == tRunning || theStatus == tIdle || theStatus == tReady ) {
00271     struct tms buf;
00272     Tclock_t clk = times( &buf ); 
00273     if ( clk == -1 ) {
00274       perror( "TSystemTimer::GetRealElapsedTime" );
00275       return DBL_MIN;
00276     }
00277     Tdouble realbuf = ( (Tdouble)( clk ) / _clktck );
00278     Tdouble factor = 1.0;
00279     if ( theUnit == Tmsec ) {
00280       factor = 1.0e+03;
00281     } else if ( theUnit == Tusec ) {
00282       factor = 1.0e+06;
00283     } else if ( theUnit == Tnsec ) {
00284       factor = 1.0e+09;
00285     } else {
00286       SetUnit( Tsec );
00287     }
00288     return ( realbuf - theBeginOfRealTime ) * factor;
00289   } else {
00290     return 0.0;
00291   }
00292 }
00293 
00294 Tdouble TSystemTimer::GetSystemElapsedTime()
00295 {
00296   if ( theStatus == tRunning || theStatus == tIdle || theStatus == tReady ) {
00297     struct tms buf;
00298     Tclock_t clk = times( &buf ); 
00299     if ( clk == -1 ) {
00300       perror( "TSystemTimer::GetSystemElapsedTime" );
00301       return DBL_MIN;
00302     }
00303     Tdouble sysbuf = ( (Tdouble)( buf.tms_stime ) / _clktck );
00304     Tdouble factor = 1.0;
00305     if ( theUnit == Tmsec ) {
00306       factor = 1.0e+03;
00307     } else if ( theUnit == Tusec ) {
00308       factor = 1.0e+06;
00309     } else if ( theUnit == Tnsec ) {
00310       factor = 1.0e+09;
00311     } else {
00312       SetUnit( Tsec );
00313     }
00314     return ( sysbuf - theBeginOfSystemTime ) * factor;
00315   } else {
00316     return 0.0;
00317   }
00318 }
00319 
00320 Tdouble TSystemTimer::GetUserElapsedTime()
00321 {
00322   if ( theStatus == tRunning || theStatus == tIdle || theStatus == tReady ) {
00323     struct tms buf;
00324     Tclock_t clk = times( &buf );
00325     if ( clk == -1 ) {
00326       perror( "TSystemTimer::GetUserElapsedTime" );
00327       return DBL_MIN;
00328     }
00329     Tdouble userbuf = ( (Tdouble)( buf.tms_utime ) / _clktck );
00330     Tdouble factor = 1.0;
00331     if ( theUnit == Tmsec ) {
00332       factor = 1.0e+03;
00333     } else if ( theUnit == Tusec ) {
00334       factor = 1.0e+06;
00335     } else if ( theUnit == Tnsec ) {
00336       factor = 1.0e+09;
00337     } else {
00338       SetUnit( Tsec );
00339     }
00340     return ( userbuf - theBeginOfUserTime ) * factor;
00341   } else {
00342     return 0.0;
00343   }
00344 }
00345 
00346 Tdouble TSystemTimer::GetTotalRunningTime()
00347 {
00348   if ( theStatus == tRunning || theStatus == tReady ) {
00349     struct tms buf;
00350     Tclock_t clk = times( &buf );
00351     if ( clk == -1 ) {
00352       perror( "TSystemTimer::GetTotalRunningTime" );
00353       return DBL_MIN;
00354     }
00355     Tdouble realbuf = ( (Tdouble)( clk ) / _clktck );
00356     Tdouble factor = 1.0;
00357     if ( theUnit == Tmsec ) {
00358       factor = 1.0e+03;
00359     } else if ( theUnit == Tusec ) {
00360       factor = 1.0e+06;
00361     } else if ( theUnit == Tnsec ) {
00362       factor = 1.0e+09;
00363     } else {
00364       SetUnit( Tsec );
00365     }
00366     return ( realbuf - theBeginOfRealTime - theIdlingTime ) * factor;
00367   } else if ( theStatus == tIdle ) {
00368     Tdouble factor = 1.0;
00369     if ( theUnit == Tmsec ) {
00370       factor = 1.0e+03;
00371     } else if ( theUnit == Tusec ) {
00372       factor = 1.0e+06;
00373     } else if ( theUnit == Tnsec ) {
00374       factor = 1.0e+09;
00375     } else {
00376       SetUnit( Tsec );
00377     }
00378     return ( thePausedTime - theBeginOfRealTime - theIdlingTime ) * factor;
00379   } else {
00380     return 0.0;
00381   }
00382 }
00383 
00384 Tdouble TSystemTimer::GetTotalIdlingTime()
00385 {
00386   if ( theStatus == tRunning || theStatus == tReady ) {
00387     Tdouble factor = 1.0;
00388     if ( theUnit == Tmsec ) {
00389       factor = 1.0e+03;
00390     } else if ( theUnit == Tusec ) {
00391       factor = 1.0e+06;
00392     } else if ( theUnit == Tnsec ) {
00393       factor = 1.0e+09;
00394     } else {
00395       SetUnit( Tsec );
00396     }
00397     return theIdlingTime * factor;
00398   } else if ( theStatus == tIdle ) {
00399     struct tms buf;
00400     Tclock_t clk = times( &buf );
00401     if ( clk == -1 ) {
00402       perror( "TSystemTimer::GetTotalIdlingTime" );
00403       return DBL_MIN;
00404     }
00405     Tdouble realbuf = ( (Tdouble)( clk ) / _clktck );
00406     Tdouble factor = 1.0;
00407     if ( theUnit == Tmsec ) {
00408       factor = 1.0e+03;
00409     } else if ( theUnit == Tusec ) {
00410       factor = 1.0e+06;
00411     } else if ( theUnit == Tnsec ) {
00412       factor = 1.0e+09;
00413     } else {
00414       SetUnit( Tsec );
00415     }
00416     return ( realbuf - thePausedTime + theIdlingTime ) * factor;
00417   } else {
00418     return 0.0;
00419   }
00420 }
00421 
00422 Tvoid TSystemTimer::SetUnit( const Tstring& unit )
00423 {
00424   Tdouble f = 1.0;
00425   if ( theUnit == unit ) {
00426     return;
00427 
00428 
00429   } else if ( theUnit == Tsec && unit == Tsec ) {
00430     f = 1.0;
00431   } else if ( theUnit == Tsec && unit == Tmsec ) {
00432     f = 1.0e+03;
00433   } else if ( theUnit == Tsec && unit == Tusec ) {
00434     f = 1.0e+06;
00435   } else if ( theUnit == Tsec && unit == Tnsec ) {
00436     f = 1.0e+09;
00437 
00438 
00439   } else if ( theUnit == Tmsec && unit == Tsec ) {
00440     f = 1.0e-03;
00441   } else if ( theUnit == Tmsec && unit == Tmsec ) {
00442     f = 1.0;
00443   } else if ( theUnit == Tmsec && unit == Tusec ) {
00444     f = 1.0e+03;
00445   } else if ( theUnit == Tmsec && unit == Tnsec ) {
00446     f = 1.0e+06;
00447 
00448 
00449   } else if ( theUnit == Tusec && unit == Tsec ) {
00450     f = 1.0e-06;
00451   } else if ( theUnit == Tusec && unit == Tmsec ) {
00452     f = 1.0e-03;
00453   } else if ( theUnit == Tusec && unit == Tusec ) {
00454     f = 1.0;
00455   } else if ( theUnit == Tusec && unit == Tnsec ) {
00456     f = 1.0e+03;
00457 
00458 
00459   } else if ( theUnit == Tnsec && unit == Tsec ) {
00460     f = 1.0e-09;
00461   } else if ( theUnit == Tnsec && unit == Tmsec ) {
00462     f = 1.0e-06;
00463   } else if ( theUnit == Tnsec && unit == Tusec ) {
00464     f = 1.0e-03;
00465   } else if ( theUnit == Tnsec && unit == Tnsec ) {
00466     f = 1.0;
00467 
00468 
00469   } else {
00470     return;
00471   }
00472   convertTimeScale( f );
00473   theUnit = unit;
00474   return;
00475 }
00476 
00477 Tvoid TSystemTimer::convertTimeScale( Tdouble factor )
00478 {
00479   for ( Tsize_t i = 0; i < theRealLapTime.size(); i ++ ) {
00480     theRealLapTime[ i ] *= factor;
00481     theSystemLapTime[ i ] *= factor;
00482     theUserLapTime[ i ] *= factor;
00483   }
00484   return;
00485 }
00486 
00487 Tostream& operator<<( Tostream& tos, const TSystemTimer& right )
00488 {
00489   TSystemTimer copy( right );
00490   tos << "Real=" << copy.GetRealElapsedTime() << copy.theUnit;
00491   tos << ",User=" << copy.GetUserElapsedTime() << copy.theUnit;
00492   tos << ",System=" << copy.GetSystemElapsedTime() << copy.theUnit;
00493   tos << ",Running=" << copy.GetTotalRunningTime() << copy.theUnit;
00494   tos << ",Idling=" << copy.GetTotalIdlingTime() << copy.theUnit;
00495 
00496   for ( Tint i = 0; i < copy.GetNumberOfLaps(); i ++ ) {
00497     tos << Tendl;
00498     tos << Ttab << "Real[";
00499     tos << setfill( '0' ) << setiosflags( Tios::right ) << setw( 3 ) << i;
00500     tos << "]=";
00501     tos << copy.theRealLapTime[ i ] << copy.theUnit;
00502 
00503     tos << ",User[";
00504     tos << setfill( '0' ) << setiosflags( Tios::right ) << setw( 3 ) << i;
00505     tos << "]=";
00506     tos << copy.theUserLapTime[ i ] << copy.theUnit;
00507 
00508     tos << ",System[";
00509     tos << setfill( '0' ) << setiosflags( Tios::right ) << setw( 3 ) << i;
00510     tos << "]=";
00511     tos << copy.theSystemLapTime[ i ] << copy.theUnit;
00512   }
00513 
00514   tos << Tflush;
00515   return tos;
00516 }
00517 
00518 Tstring TSystemTimer::WhatTimeIsItNow() const
00519 {
00520   TSystemClock clock;
00521   return clock.WhatTimeIsItNow();
00522 }
00523 
00524 #ifdef __CLDAQ_ROOT_DLL
00525     ClassImp(TSystemTimer)
00526 #endif


CLDAQ - a Class Library for DataAcQuisition (Version 1.14.3)
Go IWAI -- goiwai at users.sourceforge.jp