lemon/time_measure.h
changeset 135 6e7aee618f03
parent 120 137278093143
child 143 c3b45bb643b0
equal deleted inserted replaced
1:12dac57b5e7d 2:ba83b6b3cd62
    21 
    21 
    22 ///\ingroup timecount
    22 ///\ingroup timecount
    23 ///\file
    23 ///\file
    24 ///\brief Tools for measuring cpu usage
    24 ///\brief Tools for measuring cpu usage
    25 
    25 
       
    26 #ifdef WIN32
       
    27 #include <windows.h>
       
    28 #include <cmath>
       
    29 #else
    26 #include <sys/times.h>
    30 #include <sys/times.h>
    27 
       
    28 #include <sys/time.h>
    31 #include <sys/time.h>
       
    32 #endif
       
    33 
    29 #include <fstream>
    34 #include <fstream>
    30 #include <iostream>
    35 #include <iostream>
    31 #include <unistd.h>
       
    32 
    36 
    33 namespace lemon {
    37 namespace lemon {
    34 
    38 
    35   /// \addtogroup timecount
    39   /// \addtogroup timecount
    36   /// @{
    40   /// @{
    52   ///
    56   ///
    53   ///\author Alpar Juttner
    57   ///\author Alpar Juttner
    54 
    58 
    55   class TimeStamp
    59   class TimeStamp
    56   {
    60   {
    57     struct rtms 
    61     double utime;
    58     {
    62     double stime;
    59       double tms_utime;
    63     double cutime;
    60       double tms_stime;
    64     double cstime;
    61       double tms_cutime;
    65     double rtime;
    62       double tms_cstime;
    66   
    63       rtms() {}
       
    64       rtms(tms ts) : tms_utime(ts.tms_utime), tms_stime(ts.tms_stime),
       
    65 		     tms_cutime(ts.tms_cutime), tms_cstime(ts.tms_cstime) {}
       
    66     };
       
    67     rtms ts;
       
    68     double real_time;
       
    69   
       
    70     rtms &getTms() {return ts;}
       
    71     const rtms &getTms() const {return ts;}
       
    72 
       
    73     void _reset() { 
    67     void _reset() { 
    74       ts.tms_utime = ts.tms_stime = ts.tms_cutime = ts.tms_cstime = 0; 
    68       utime = stime = cutime = cstime = rtime = 0;
    75       real_time = 0;
       
    76     }
    69     }
    77 
    70 
    78   public:
    71   public:
    79 
    72 
    80     ///Read the current time values of the process
    73     ///Read the current time values of the process
    81     void stamp()
    74     void stamp()
    82     {
    75     {
       
    76 #ifndef WIN32
    83       timeval tv;
    77       timeval tv;
    84       tms _ts;
    78       gettimeofday(&tv, 0);
    85       times(&_ts);
    79       rtime=tv.tv_sec+double(tv.tv_usec)/1e6;
    86       gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
    80 
    87       ts=_ts;
    81       tms ts;
       
    82       double tck=sysconf(_SC_CLK_TCK);
       
    83       times(&ts);
       
    84       utime=ts.tms_utime/tck;
       
    85       stime=ts.tms_stime/tck;
       
    86       cutime=ts.tms_cutime/tck;
       
    87       cstime=ts.tms_cstime/tck;
       
    88 #else
       
    89       static const double ch = 4294967296.0e-7;
       
    90       static const double cl = 1.0e-7;
       
    91 
       
    92       FILETIME system;
       
    93       GetSystemTimeAsFileTime(&system);
       
    94       rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime;
       
    95 
       
    96       FILETIME create, exit, kernel, user;
       
    97       if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
       
    98 	utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime;
       
    99 	stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime;
       
   100 	cutime = 0;
       
   101 	cstime = 0;
       
   102       } else {
       
   103 	rtime = 0;
       
   104 	utime = 0;
       
   105 	stime = 0;
       
   106 	cutime = 0;
       
   107 	cstime = 0;
       
   108       }
       
   109 #endif      
    88     }
   110     }
    89   
   111   
    90     /// Constructor initializing with zero
   112     /// Constructor initializing with zero
    91     TimeStamp()
   113     TimeStamp()
    92     { _reset(); }
   114     { _reset(); }
    97     TimeStamp &reset() {_reset();return *this;}
   119     TimeStamp &reset() {_reset();return *this;}
    98 
   120 
    99     ///\e
   121     ///\e
   100     TimeStamp &operator+=(const TimeStamp &b)
   122     TimeStamp &operator+=(const TimeStamp &b)
   101     {
   123     {
   102       ts.tms_utime+=b.ts.tms_utime;
   124       utime+=b.utime;
   103       ts.tms_stime+=b.ts.tms_stime;
   125       stime+=b.stime;
   104       ts.tms_cutime+=b.ts.tms_cutime;
   126       cutime+=b.cutime;
   105       ts.tms_cstime+=b.ts.tms_cstime;
   127       cstime+=b.cstime;
   106       real_time+=b.real_time;
   128       rtime+=b.rtime;
   107       return *this;
   129       return *this;
   108     }
   130     }
   109     ///\e
   131     ///\e
   110     TimeStamp operator+(const TimeStamp &b) const
   132     TimeStamp operator+(const TimeStamp &b) const
   111     {
   133     {
   113       return t+=b;
   135       return t+=b;
   114     }
   136     }
   115     ///\e
   137     ///\e
   116     TimeStamp &operator-=(const TimeStamp &b)
   138     TimeStamp &operator-=(const TimeStamp &b)
   117     {
   139     {
   118       ts.tms_utime-=b.ts.tms_utime;
   140       utime-=b.utime;
   119       ts.tms_stime-=b.ts.tms_stime;
   141       stime-=b.stime;
   120       ts.tms_cutime-=b.ts.tms_cutime;
   142       cutime-=b.cutime;
   121       ts.tms_cstime-=b.ts.tms_cstime;
   143       cstime-=b.cstime;
   122       real_time-=b.real_time;
   144       rtime-=b.rtime;
   123       return *this;
   145       return *this;
   124     }
   146     }
   125     ///\e
   147     ///\e
   126     TimeStamp operator-(const TimeStamp &b) const
   148     TimeStamp operator-(const TimeStamp &b) const
   127     {
   149     {
   129       return t-=b;
   151       return t-=b;
   130     }
   152     }
   131     ///\e
   153     ///\e
   132     TimeStamp &operator*=(double b)
   154     TimeStamp &operator*=(double b)
   133     {
   155     {
   134       ts.tms_utime*=b;
   156       utime*=b;
   135       ts.tms_stime*=b;
   157       stime*=b;
   136       ts.tms_cutime*=b;
   158       cutime*=b;
   137       ts.tms_cstime*=b;
   159       cstime*=b;
   138       real_time*=b;
   160       rtime*=b;
   139       return *this;
   161       return *this;
   140     }
   162     }
   141     ///\e
   163     ///\e
   142     TimeStamp operator*(double b) const
   164     TimeStamp operator*(double b) const
   143     {
   165     {
   146     }
   168     }
   147     friend TimeStamp operator*(double b,const TimeStamp &t);
   169     friend TimeStamp operator*(double b,const TimeStamp &t);
   148     ///\e
   170     ///\e
   149     TimeStamp &operator/=(double b)
   171     TimeStamp &operator/=(double b)
   150     {
   172     {
   151       ts.tms_utime/=b;
   173       utime/=b;
   152       ts.tms_stime/=b;
   174       stime/=b;
   153       ts.tms_cutime/=b;
   175       cutime/=b;
   154       ts.tms_cstime/=b;
   176       cstime/=b;
   155       real_time/=b;
   177       rtime/=b;
   156       return *this;
   178       return *this;
   157     }
   179     }
   158     ///\e
   180     ///\e
   159     TimeStamp operator/(double b) const
   181     TimeStamp operator/(double b) const
   160     {
   182     {
   171     friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
   193     friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
   172   
   194   
   173     ///Gives back the user time of the process
   195     ///Gives back the user time of the process
   174     double userTime() const
   196     double userTime() const
   175     {
   197     {
   176       return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
   198       return utime;
   177     }
   199     }
   178     ///Gives back the system time of the process
   200     ///Gives back the system time of the process
   179     double systemTime() const
   201     double systemTime() const
   180     {
   202     {
   181       return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
   203       return stime;
   182     }
   204     }
   183     ///Gives back the user time of the process' children
   205     ///Gives back the user time of the process' children
       
   206 
       
   207     ///\note On <tt>WIN32</tt> platform this value is not calculated. 
       
   208     ///
   184     double cUserTime() const
   209     double cUserTime() const
   185     {
   210     {
   186       return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
   211       return cutime;
   187     }
   212     }
   188     ///Gives back the user time of the process' children
   213     ///Gives back the user time of the process' children
       
   214 
       
   215     ///\note On <tt>WIN32</tt> platform this value is not calculated. 
       
   216     ///
   189     double cSystemTime() const
   217     double cSystemTime() const
   190     {
   218     {
   191       return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
   219       return cstime;
   192     }
   220     }
   193     ///Gives back the real time
   221     ///Gives back the real time
   194     double realTime() const {return real_time;}
   222     double realTime() const {return rtime;}
   195   };
   223   };
   196 
   224 
   197   TimeStamp operator*(double b,const TimeStamp &t) 
   225   TimeStamp operator*(double b,const TimeStamp &t) 
   198   {
   226   {
   199     return t*b;
   227     return t*b;
   210   /// \li \c s: system cpu time,
   238   /// \li \c s: system cpu time,
   211   /// \li \c cu: user cpu time of children,
   239   /// \li \c cu: user cpu time of children,
   212   /// \li \c cs: system cpu time of children,
   240   /// \li \c cs: system cpu time of children,
   213   /// \li \c real: real time.
   241   /// \li \c real: real time.
   214   /// \relates TimeStamp
   242   /// \relates TimeStamp
       
   243   /// \note On <tt>WIN32</tt> platform the cummulative values are not
       
   244   /// calculated.
   215   inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
   245   inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
   216   {
   246   {
   217     long cls = sysconf(_SC_CLK_TCK);
   247     os << "u: " << t.userTime() <<
   218     os << "u: " << double(t.getTms().tms_utime)/cls <<
   248       "s, s: " << t.systemTime() <<
   219       "s, s: " << double(t.getTms().tms_stime)/cls <<
   249       "s, cu: " << t.cUserTime() <<
   220       "s, cu: " << double(t.getTms().tms_cutime)/cls <<
   250       "s, cs: " << t.cSystemTime() <<
   221       "s, cs: " << double(t.getTms().tms_cstime)/cls <<
       
   222       "s, real: " << t.realTime() << "s";
   251       "s, real: " << t.realTime() << "s";
   223     return os;
   252     return os;
   224   }
   253   }
   225 
   254 
   226   ///Class for measuring the cpu time and real time usage of the process
   255   ///Class for measuring the cpu time and real time usage of the process
   402     double systemTime() const
   431     double systemTime() const
   403     {
   432     {
   404       return operator TimeStamp().systemTime();
   433       return operator TimeStamp().systemTime();
   405     }
   434     }
   406     ///Gives back the ellapsed user time of the process' children
   435     ///Gives back the ellapsed user time of the process' children
       
   436 
       
   437     ///\note On <tt>WIN32</tt> platform this value is not calculated. 
       
   438     ///
   407     double cUserTime() const
   439     double cUserTime() const
   408     {
   440     {
   409       return operator TimeStamp().cUserTime();
   441       return operator TimeStamp().cUserTime();
   410     }
   442     }
   411     ///Gives back the ellapsed user time of the process' children
   443     ///Gives back the ellapsed user time of the process' children
       
   444 
       
   445     ///\note On <tt>WIN32</tt> platform this value is not calculated. 
       
   446     ///
   412     double cSystemTime() const
   447     double cSystemTime() const
   413     {
   448     {
   414       return operator TimeStamp().cSystemTime();
   449       return operator TimeStamp().cSystemTime();
   415     }
   450     }
   416     ///Gives back the ellapsed real time
   451     ///Gives back the ellapsed real time