Main Page | Namespace List | Class Hierarchy | Compound List | File List | Namespace Members | Compound Members | File Members

abktimer.h

Go to the documentation of this file.
00001 /**************************************************************************
00002 ***    
00003 *** Copyright (c) 1995-2000 Regents of the University of California,
00004 ***               Andrew E. Caldwell, Andrew B. Kahng and Igor L. Markov
00005 *** Copyright (c) 2000-2004 Regents of the University of Michigan,
00006 ***               Saurabh N. Adya, Jarrod A. Roy and Igor L. Markov
00007 ***
00008 ***  Contact author(s): abk@cs.ucsd.edu, imarkov@umich.edu
00009 ***  Original Affiliation:   UCLA, Computer Science Department,
00010 ***                          Los Angeles, CA 90095-1596 USA
00011 ***
00012 ***  Permission is hereby granted, free of charge, to any person obtaining 
00013 ***  a copy of this software and associated documentation files (the
00014 ***  "Software"), to deal in the Software without restriction, including
00015 ***  without limitation 
00016 ***  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00017 ***  and/or sell copies of the Software, and to permit persons to whom the 
00018 ***  Software is furnished to do so, subject to the following conditions:
00019 ***
00020 ***  The above copyright notice and this permission notice shall be included
00021 ***  in all copies or substantial portions of the Software.
00022 ***
00023 *** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
00024 *** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00025 *** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
00026 *** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00027 *** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
00028 *** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
00029 *** THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00030 ***
00031 ***
00032 ***************************************************************************/
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00041 /*   Portable Timer Class    (SunOS, Solaris, Linux, NT  , '95  , DOS  )  *
00042                                                   
00043    ( with the help of the Regents of the University of California ;-)   *
00044 
00045  970723 ilm    Changed #include <bool.h> to #include <stl-config.h>
00046                for STL 2.01 compliance  
00047  CHANGES
00048 
00049  970901 ilm    completely reworked the Timer class to circumvent the
00050                  wrap-around problem on all platforms and provide separate 
00051                  user/system time readings on "supported platforms" 
00052                  (see below)
00053  970901 ilm    even though class Timer is (expectected to be) completely
00054                  portable, it has "better supported" platforms (currently 
00055                  _SUNPRO_CC). This means that platform-specific calls will
00056                  be used instead of common functions to provide better 
00057                  granularity of time readings and circumvent the wrap-around 
00058                  problem
00059                     However, the effect of the wrap-around problem should
00060                  be minimal on other platforms too. The only precaution
00061                  for users of "other" platforms (i.e. not _SUNPRO_CC)
00062                  is not to use Timer::expired() after the time wrap-around 
00063                  period of (INT_MAX+0.0)/CLOCKS_PER_SEC sec (can be 36sec)
00064                  use realTimeExpired() instead
00065 
00066                     Note: on Windows 95 (perhaps, on NT as well),
00067                  CLOCKS_PER_SEC is not big, therefore the granularity 
00068                  problem is not as bad as it was on Solaris with older
00069                  versions of class Timer
00070 */
00071 #ifndef _ABKTIMER_H_
00072 #define _ABKTIMER_H_
00073 
00074 #include <time.h>
00075 #include <iostream>
00076 #include "abkassert.h"
00077 
00078 //#ifndef _MSC_VER
00079 //#include <stl_config.h>
00080 //#endif
00081 
00082 #ifdef WIN32
00083 class WinTimes;
00084 #endif 
00085 
00086 #if defined(sun)
00087  #include <sys/resource.h>
00088  extern "C" int getrusage(int who, struct rusage *rusage);
00089 #elif defined(linux)
00090  #include <sys/resource.h>
00091 #endif      
00092 
00093 //: Used to record the CPU time of process 
00094 class Timer 
00095 {
00096 #ifndef WIN32
00097     time_t  realTime1, realTime2;
00098 #endif
00099     double  UserTime1, UserTime2;
00100 #if defined(linux) || defined(sun)
00101     double  SysTime1,  SysTime2;
00102 #endif
00103     double  timeLimit;
00104 
00105 #ifdef WIN32
00106     double getRealTimeOnTheFly();
00107     double getUserTimeOnTheFly();
00108     double getCombTimeOnTheFly();
00109 #else
00110     inline double getRealTimeOnTheFly();
00111     inline double getUserTimeOnTheFly();
00112 #if defined(sun) || defined(linux)
00113     inline double  getSysTimeOnTheFly();
00114 #endif
00115     inline double getCombTimeOnTheFly();
00116 
00117 #endif  //WIN32
00118 
00119 #if defined(sun) || defined (linux)
00120    struct rusage _ru;
00121 #endif
00122 
00123 #ifdef WIN32
00124    WinTimes *_winTimes;
00125 #endif
00126 
00127  protected:
00128     enum {TIMER_ON, TIMER_OFF, TIMER_SPLIT } status;
00129  public:
00130 
00131     Timer(double limitInSec=0.0);
00132 #ifndef WIN32
00133    ~Timer() { };
00134 #else
00135    ~Timer();
00136 #endif
00137     void start(double limitInSec=0.0);
00138     void stop();
00139     void split(); //call to allow taking a reading without interrupting timing
00140     void resume(); //call after taking a reading after calling split()
00141     bool isStopped()       const { return status == TIMER_OFF; }
00142     
00143     double  getUserTime()  const; // processor time in seconds 
00144     
00145     double   getSysTime()  const; // processor time in seconds 
00146     double  getCombTime()  const;
00147     // processor time in seconds  (sum of the prev. two)
00148     
00149     double  getRealTime() const; 
00150     double  getUnixTime() const;  
00151     // Unix time (large number for in randseed)
00152  
00153     inline bool    expired();
00154     // class expired and realtimeexpired can be used to check if 
00155     //  the time is over. The choice of CPU time over
00156     //  real time for expiration is explained by a much finer granularity of
00157     //  measurment. The author observed sensitivity of up to 0.001
00158     //  CPU sec. on Sparc architecture (while real seconds were integers).
00159     //  DO NOT CALL THIS METHOD ON "OTHER" PLATFORMS  (not __SUNPRO_CC)
00160     //  AFTER WRAP_AROUND HAPPENED: (INT_MAX+0.0)/CLOCKS_PER_SEC) sec
00161     //  (can be 36 mins), call realTimeExpired() instead
00162     inline bool    realTimeExpired();
00163 
00164     friend std::ostream& operator<<(std::ostream& os, const Timer& tm );
00165 };
00166 
00167 /* ----------------------      IMPLEMENTATIONS  --------------------*/
00168 #ifndef WIN32
00169 inline double Timer::getRealTimeOnTheFly() 
00170 { 
00171   time(&realTime2);
00172   return difftime(realTime2,realTime1);
00173 }
00174 
00175 inline double Timer::getUserTimeOnTheFly() 
00176 { 
00177 #if defined(sun) || defined(gnu)
00178    abkfatal(getrusage(RUSAGE_SELF,&_ru)!=-1,"Can't get time");
00179    return _ru.ru_utime.tv_sec+1e-6*_ru.ru_utime.tv_usec - UserTime1;
00180 #else
00181   return (clock()-UserTime1+0.0)/CLOCKS_PER_SEC;
00182 #endif
00183 }
00184 
00185 #if defined(sun) || defined(linux)
00186 inline double Timer::getSysTimeOnTheFly() 
00187 { 
00188   abkfatal(getrusage(RUSAGE_SELF,&_ru)!=-1,"Can't get time");
00189   return  _ru.ru_stime.tv_sec+1e-6*_ru.ru_stime.tv_usec - SysTime1;
00190 }
00191 #endif
00192 
00193 inline double Timer::getCombTimeOnTheFly() 
00194 { 
00195 #if defined(sun) || defined(linux)
00196    abkfatal(getrusage(RUSAGE_SELF,&_ru)!=-1,"Can't get time");
00197    return   _ru.ru_utime.tv_sec+1e-6*_ru.ru_utime.tv_usec - UserTime1
00198            +_ru.ru_stime.tv_sec+1e-6*_ru.ru_stime.tv_usec - SysTime1;
00199 #else
00200   return getUserTimeOnTheFly();
00201 #endif
00202 }
00203 #endif //WIN32
00204 
00205 inline bool Timer::expired() 
00206 { 
00207    return (timeLimit<getCombTimeOnTheFly()) && (timeLimit!=0.0); 
00208 }
00209 
00210 inline bool Timer::realTimeExpired() 
00211 { 
00212    return (timeLimit<getRealTimeOnTheFly()) && (timeLimit!=0.0); 
00213 }
00214 
00215 #endif

Generated on Mon Apr 25 01:09:23 2005 for Parquete by doxygen 1.3.2