00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <cerrno>
00034 #include <string>
00035 #include <iostream>
00036 #include <cstring>
00037
00038 using std::string ;
00039 using std::endl ;
00040
00041 #include "BESStopWatch.h"
00042 #include "BESDebug.h"
00043
00044 bool
00045 BESStopWatch::start()
00046 {
00047
00048 if( getrusage( RUSAGE_SELF, &_start_usage ) != 0 )
00049 {
00050 int myerrno = errno ;
00051 char *c_err = strerror( myerrno ) ;
00052 string err = "getrusage failed in start: " ;
00053 if( c_err )
00054 {
00055 err += c_err ;
00056 }
00057 else
00058 {
00059 err += "unknown error" ;
00060 }
00061 BESDEBUG( "timing", err ) ;
00062 _started = false ;
00063 }
00064 else
00065 {
00066 _started = true ;
00067 }
00068
00069
00070
00071 _stopped = false ;
00072
00073 return _started ;
00074 }
00075
00076 bool
00077 BESStopWatch::stop()
00078 {
00079
00080 if( _started )
00081 {
00082
00083 if( getrusage( RUSAGE_SELF, &_stop_usage ) != 0 )
00084 {
00085 int myerrno = errno ;
00086 char *c_err = strerror( myerrno ) ;
00087 string err = "getrusage failed in stop: " ;
00088 if( c_err )
00089 {
00090 err += c_err ;
00091 }
00092 else
00093 {
00094 err += "unknown error" ;
00095 }
00096 BESDEBUG( "timing", err ) ;
00097 _started = false ;
00098 _stopped = false ;
00099 }
00100 else
00101 {
00102
00103
00104 bool success = timeval_subtract() ;
00105 if( !success )
00106 {
00107 BESDEBUG( "timing", "failed to get timing" ) ;
00108 _started = false ;
00109 _stopped = false ;
00110 }
00111 else
00112 {
00113 _stopped = true ;
00114 }
00115 }
00116 }
00117
00118 return _stopped ;
00119 }
00120
00121 bool
00122 BESStopWatch::timeval_subtract()
00123 {
00124 struct timeval &start = _start_usage.ru_utime ;
00125 struct timeval &stop = _stop_usage.ru_utime ;
00126
00127
00128 if( stop.tv_usec < start.tv_usec )
00129 {
00130 int nsec = (start.tv_usec - stop.tv_usec) / 1000000 + 1 ;
00131 start.tv_usec -= 1000000 * nsec ;
00132 start.tv_sec += nsec ;
00133 }
00134 if( stop.tv_usec - start.tv_usec > 1000000 )
00135 {
00136 int nsec = (start.tv_usec - stop.tv_usec) / 1000000 ;
00137 start.tv_usec += 1000000 * nsec ;
00138 start.tv_sec -= nsec ;
00139 }
00140
00141
00142
00143 _result.tv_sec = stop.tv_sec - start.tv_sec ;
00144 _result.tv_usec = stop.tv_usec - start.tv_usec ;
00145
00146
00147 return !(stop.tv_sec < start.tv_sec) ;
00148 }
00149
00156 void
00157 BESStopWatch::dump( ostream &strm ) const
00158 {
00159 strm << BESIndent::LMarg << "BESStopWatch::dump - ("
00160 << (void *)this << ")" << endl ;
00161 }
00162