Go to the documentation of this file.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::cerr ;
00040 using std::endl ;
00041
00042 #include "BESStopWatch.h"
00043 #include "BESDebug.h"
00044
00045 bool
00046 BESStopWatch::start()
00047 {
00048
00049 if( getrusage( RUSAGE_SELF, &_start_usage ) != 0 )
00050 {
00051 int myerrno = errno ;
00052 char *c_err = strerror( myerrno ) ;
00053 string err = "getrusage failed in start: " ;
00054 if( c_err )
00055 {
00056 err += c_err ;
00057 }
00058 else
00059 {
00060 err += "unknown error" ;
00061 }
00062 BESDEBUG( "timing", err ) ;
00063 _started = false ;
00064 }
00065 else
00066 {
00067 _started = true ;
00068 }
00069
00070
00071
00072 _stopped = false ;
00073
00074 return _started ;
00075 }
00076
00077 bool
00078 BESStopWatch::stop()
00079 {
00080
00081 if( _started )
00082 {
00083
00084 if( getrusage( RUSAGE_SELF, &_stop_usage ) != 0 )
00085 {
00086 int myerrno = errno ;
00087 char *c_err = strerror( myerrno ) ;
00088 string err = "getrusage failed in stop: " ;
00089 if( c_err )
00090 {
00091 err += c_err ;
00092 }
00093 else
00094 {
00095 err += "unknown error" ;
00096 }
00097 BESDEBUG( "timing", err ) ;
00098 _started = false ;
00099 _stopped = false ;
00100 }
00101 else
00102 {
00103
00104
00105 bool success = timeval_subtract() ;
00106 if( !success )
00107 {
00108 BESDEBUG( "timing", "failed to get timing" ) ;
00109 _started = false ;
00110 _stopped = false ;
00111 }
00112 else
00113 {
00114 _stopped = true ;
00115 }
00116 }
00117 }
00118
00119 return _stopped ;
00120 }
00121
00122 bool
00123 BESStopWatch::timeval_subtract()
00124 {
00125 struct timeval &start = _start_usage.ru_utime ;
00126 struct timeval &stop = _stop_usage.ru_utime ;
00127
00128
00129 if( stop.tv_usec < start.tv_usec )
00130 {
00131 int nsec = (start.tv_usec - stop.tv_usec) / 1000000 + 1 ;
00132 start.tv_usec -= 1000000 * nsec ;
00133 start.tv_sec += nsec ;
00134 }
00135 if( stop.tv_usec - start.tv_usec > 1000000 )
00136 {
00137 int nsec = (start.tv_usec - stop.tv_usec) / 1000000 ;
00138 start.tv_usec += 1000000 * nsec ;
00139 start.tv_sec -= nsec ;
00140 }
00141
00142
00143
00144 _result.tv_sec = stop.tv_sec - start.tv_sec ;
00145 _result.tv_usec = stop.tv_usec - start.tv_usec ;
00146
00147
00148 return !(stop.tv_sec < start.tv_sec) ;
00149 }
00150
00157 void
00158 BESStopWatch::dump( ostream &strm ) const
00159 {
00160 strm << BESIndent::LMarg << "BESStopWatch::dump - ("
00161 << (void *)this << ")" << endl ;
00162 }
00163