BESStopWatch.cc

Go to the documentation of this file.
00001 // BESStopWatch.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
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     // get timer for current usage
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     // either we started the stop watch, or failed to start it. Either way,
00071     // no timings are available, so set stopped to false.
00072     _stopped = false ;
00073 
00074     return _started ;
00075 }
00076 
00077 bool
00078 BESStopWatch::stop()
00079 {
00080     // if we have started, the we can stop. Otherwise just fall through.
00081     if( _started )
00082     {
00083         // get timer for current usage
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             // get the difference between the _start_usage and the
00104             // _stop_usage and save the difference.
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     /* Perform the carry for the later subtraction by updating y. */
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     /* Compute the time remaining to wait.
00143     tv_usec  is certainly positive. */
00144     _result.tv_sec = stop.tv_sec - start.tv_sec ;
00145     _result.tv_usec = stop.tv_usec - start.tv_usec ;
00146 
00147     /* Return 1 if result is negative. */
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 

Generated on Wed May 12 09:53:07 2010 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.4.7