src/hugo/time_measure.h
author jacint
Fri, 30 Jul 2004 10:24:05 +0000
changeset 749 8e933219691e
parent 539 fb261e3a9a0f
child 814 d2d747fe1db3
permissions -rw-r--r--
bug fixing
     1 // -*- c++ -*-
     2 #ifndef HUGO_TIME_MEASURE_H
     3 #define HUGO_TIME_MEASURE_H
     4 
     5 ///\ingroup misc
     6 ///\file
     7 ///\brief Tools for measuring cpu usage
     8 
     9 #include <sys/time.h>
    10 #include <sys/times.h>
    11 #include <fstream>
    12 #include <iostream>
    13 #include <unistd.h>
    14 
    15 namespace hugo {
    16 
    17   /// \addtogroup misc
    18   /// @{
    19 
    20   /// A class to store (cpu)time instances.
    21 
    22   /// This class stores five time values.
    23   /// - a real time
    24   /// - a user cpu time
    25   /// - a system cpu time
    26   /// - a user cpu time of children
    27   /// - a system cpu time of children
    28   ///
    29   /// TimeStamp's can be added to or substracted from each other and
    30   /// they can be pushed to a stream.
    31   ///
    32   /// In most cases, perhaps \ref Timer class is what you want to use instead.
    33   ///
    34   ///\author Alpar Juttner
    35 
    36   class TimeStamp
    37   {
    38     tms ts;
    39     double real_time;
    40   
    41   public:
    42 
    43     tms &getTms() {return ts;}
    44     const tms &getTms() const {return ts;}
    45     ///Read the current time values of the process
    46     void stamp()
    47     {
    48       timeval tv;
    49       times(&ts);
    50       gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
    51     }
    52   
    53     /// Constructor initializing with zero
    54     TimeStamp()
    55     { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
    56     ///Constructor initializing with the current time values of the process
    57     TimeStamp(void *) { stamp();}
    58   
    59     ///
    60     TimeStamp &operator+=(const TimeStamp &b)
    61     {
    62       ts.tms_utime+=b.ts.tms_utime;
    63       ts.tms_stime+=b.ts.tms_stime;
    64       ts.tms_cutime+=b.ts.tms_cutime;
    65       ts.tms_cstime+=b.ts.tms_cstime;
    66       real_time+=b.real_time;
    67       return *this;
    68     }
    69     ///
    70     TimeStamp operator+(const TimeStamp &b) const
    71     {
    72       TimeStamp t(*this);
    73       return t+=b;
    74     }
    75     ///
    76     TimeStamp &operator-=(const TimeStamp &b)
    77     {
    78       ts.tms_utime-=b.ts.tms_utime;
    79       ts.tms_stime-=b.ts.tms_stime;
    80       ts.tms_cutime-=b.ts.tms_cutime;
    81       ts.tms_cstime-=b.ts.tms_cstime;
    82       real_time-=b.real_time;
    83       return *this;
    84     }
    85     ///
    86     TimeStamp operator-(const TimeStamp &b) const
    87     {
    88       TimeStamp t(*this);
    89       return t-=b;
    90     }
    91 
    92     ///The time ellapsed since the last call of stamp()
    93     TimeStamp ellapsed() const
    94     {
    95       TimeStamp t(NULL);
    96       return t-*this;
    97     }
    98   
    99     friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
   100   
   101     ///Gives back the user time of the process
   102     double getUserTime() const
   103     {
   104       return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
   105     }
   106     ///Gives back the system time of the process
   107     double getSystemTime() const
   108     {
   109       return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
   110     }
   111     ///Gives back the user time of the process' children
   112     double getCUserTime() const
   113     {
   114       return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
   115     }
   116     ///Gives back the user time of the process' children
   117     double getCSystemTime() const
   118     {
   119       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   120     }
   121     ///Gives back the real time of the process
   122     double getRealTime() const {return real_time;}
   123   };
   124 
   125   ///Class measuring the cpu time and real time usage of the process
   126 
   127   ///Class measuring the cpu time and real time usage of the process.
   128   ///It is quite easy-to-use, here is a short example.
   129   ///\code
   130   ///#include<hugo/time_measure.h>
   131   ///#include<iostream>
   132 
   133   ///int main()
   134   ///{
   135   ///
   136   ///  ...
   137   ///
   138   ///  Timer T;
   139   ///  doSomething();
   140   ///  std::cout << T << '\n';
   141   ///  T.reset();
   142   ///  doSomethingElse();
   143   ///  std::cout << T << '\n';
   144   ///
   145   ///  ...
   146   ///
   147   ///}
   148   ///\endcode
   149   ///
   150   ///\todo This shouldn't be Unix (Linux) specific.
   151   ///
   152   ///\author Alpar Juttner
   153   class Timer
   154   {
   155     TimeStamp start_time;
   156 
   157     void _reset() {start_time.stamp();}
   158   
   159   public: 
   160     ///Constructor. It starts with zero time counters
   161     Timer() {_reset();}
   162 
   163     ///Computes the ellapsed time
   164 
   165     ///This conversion computes the ellapsed time
   166     ///since the construction of \c t or since
   167     ///the last \c t.reset().
   168     operator TimeStamp ()
   169     {
   170       TimeStamp t;
   171       t.stamp();
   172       return t-start_time;
   173     }
   174 
   175     ///Resets the time counters
   176     TimeStamp reset()
   177     {
   178       TimeStamp t(start_time);
   179       _reset();
   180       return start_time-t;
   181     }
   182   };
   183 
   184   ///Prints the time counters
   185 
   186   ///Prints the time counters in the following form:
   187   ///
   188   /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt>
   189   ///
   190   /// where the values are the
   191   /// \li \c u: user cpu time,
   192   /// \li \c s: system cpu time,
   193   /// \li \c cu: user cpu time of children,
   194   /// \li \c cs: system cpu time of children,
   195   /// \li \c real: real time.
   196   inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
   197   {
   198     long cls = sysconf(_SC_CLK_TCK);
   199     os << "u: " << double(t.getTms().tms_utime)/cls <<
   200       "s, s: " << double(t.getTms().tms_stime)/cls <<
   201       "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   202       "s, cs: " << double(t.getTms().tms_cstime)/cls <<
   203       "s, real: " << t.getRealTime() << "s";
   204     return os;
   205   }
   206 
   207   /// @}  
   208 
   209 } //namespace hugo
   210 
   211 #endif //HUGO_TIME_MEASURE_H