CpuTimer.h

00001 # ifndef Conjecture_CpuTimer_h
00002 # define Conjecture_CpuTimer_h Conjecture_CpuTimer_h
00003 
00004 // Insert SVN header substitution variable here
00005 // Class Champion: Unassigned
00006 
00007 # include "Root.h"  // parent
00008 
00009 # include <sys/time.h>
00010 # include <sys/resource.h>
00011 # include <iomanip>  // setw
00012 
00013 namespace Conjecture {
00014 
00015     // FUTURE FIX: Are these macros necessary?
00016 # define TIME_USAGE(usage) \
00017         struct rusage usage; \
00018         if ( getrusage(RUSAGE_SELF,&usage) != 0 ) { cerr << "ERROR: getrusage failed!" << endl; exit(1); }
00019     
00020 // end_usage   must be a 'struct rusage' variable
00021 // start_usage must be a 'struct rusage' variable
00022 // time must be a 'unsigned' variable
00023 # define TIME_USAGE_DIFF(end_usage, start_usage, time) \
00024                 time = \
00025                     ((now.ru_utime.tv_sec - start.ru_utime.tv_sec) * 1000000) + \
00026                     (now.ru_utime.tv_usec - start.ru_utime.tv_usec) + \
00027                     ((now.ru_stime.tv_sec - start.ru_stime.tv_sec) * 1000000) + \
00028                     (now.ru_stime.tv_usec - start.ru_stime.tv_usec); 
00029     
00030     // iters must be an unsigned number 
00031 # define TIME_START_ITERATION(iters) { \
00032                  TIME_USAGE(start); \
00033                  for ( unsigned __i = 0; __i < iters; ++__i )
00034  
00035 # define TIME_END_ITERATION(tdiff) \
00036                TIME_USAGE(now); \
00037                TIME_USAGE_DIFF(now, start, tdiff); \
00038             }
00039 
00040     
00068     class CpuTimer : public Root {
00069       public:
00070         // Types
00071 
00072         // Constructors/Destructors
00073         CpuTimer();
00074 
00075         // Accessors
00076         inline const bool &          running() const { return this->_running; }
00077         inline const struct rusage & reading() const { return this->_reading; }
00078         inline const struct rusage & start()   const { return this->_start; }
00079         
00080         // Input/Output
00081         
00082         // Interface
00083 
00088         void start();
00089 
00094         void stop();
00095 
00100         inline unsigned time()            const {return  read() / 1000000; }
00101 
00106         inline unsigned timeUser()        const { return readUser() / 1000000; }
00107 
00112         inline unsigned timeSystem()      const { return readSystem() / 1000000; }
00113 
00118         inline unsigned millitime()       const { return read() / 1000; }
00119 
00124         inline unsigned millitimeUser()   const { return readUser() / 1000; }
00125 
00130         inline unsigned millitimeSystem() const { return readSystem() / 1000; }
00131 
00136         inline unsigned microtime()       const { return read() ; }
00137 
00142         inline unsigned microtimeUser()   const { return readUser() ; }
00143 
00148         inline unsigned microtimeSystem() const { return readSystem() ; }
00149 
00154         void print(ostream & os = cout) const;
00155 
00160         void printSummary(ostream & os = cout) const;
00161         void printMilliSummary(ostream & os = cout) const;
00162         void printMicroSummary(ostream & os = cout) const;
00163 
00164         static void test(int argc = 0, const char* argv[] = NULL);
00165     
00166       protected:
00167         // Accessors
00168         inline void            runningIs(const bool & running) { this->_running = running; }
00169         inline void            readingIs(const struct rusage & reading) { this->_reading = reading; }
00170         inline void            startIs(const struct rusage & start) { this->_start = start; }
00171         
00172         // Methods 
00173         
00174       private: 
00175         // Accessors
00176         inline bool &          runningRef() { return this->_running; }
00177         inline struct rusage & readingRef() { return this->_reading; }
00178         inline struct rusage & startRef() { return this->_start; }
00179         
00180         // Methods 
00181 
00182         inline void getInfo( struct rusage & info ) const {
00183             if ( getrusage(RUSAGE_SELF,&info) != 0 ) {
00184                 cerr << "ERROR: getrusage failed!" << endl;
00185                 // Throw an exception instead of crashing!
00186                 exit(1);
00187             }
00188         }
00189 
00190         unsigned long read() const;
00191         unsigned long readUser() const;
00192         unsigned long readSystem() const;
00193         
00194         // State
00195         bool            _running;
00196         struct rusage   _reading;
00197         struct rusage   _start;
00198     };
00199 }
00200 
00201 # endif // Conjecture_CpuTimer_h
00202 

Generated on Thu Jun 15 19:56:09 2006 for Conjecture by  doxygen 1.4.6