src/include/time_measure.h
author alpar
Mon, 26 Apr 2004 18:16:42 +0000
changeset 431 79a5641f2dbc
parent 428 3544872b38c2
child 440 f92099d27236
permissions -rw-r--r--
docs
alpar@428
     1
// -*- c++ -*-
alpar@428
     2
#ifndef HUGO_TIME_MEASURE_H
alpar@428
     3
#define HUGO_TIME_MEASURE_H
alpar@428
     4
alpar@431
     5
///ingroup misc
alpar@428
     6
///\file
alpar@428
     7
///\brief Tools for measuring cpu usage
alpar@428
     8
alpar@428
     9
#include <sys/time.h>
alpar@428
    10
#include <sys/times.h>
alpar@428
    11
#include <fstream>
alpar@428
    12
#include <iostream>
alpar@428
    13
#include <unistd.h>
alpar@428
    14
alpar@428
    15
namespace hugo {
alpar@428
    16
alpar@428
    17
  /// \addtogroup misc
alpar@428
    18
  /// @{
alpar@428
    19
alpar@428
    20
  /// A class to store (cpu)time instances.
alpar@428
    21
alpar@428
    22
  /// This class stores five time values.
alpar@428
    23
  /// - a real time
alpar@428
    24
  /// - a user cpu time
alpar@428
    25
  /// - a system cpu time
alpar@428
    26
  /// - a user cpu time of children
alpar@428
    27
  /// - a system cpu time of children
alpar@428
    28
  ///
alpar@428
    29
  /// TimeStamp's can be added to or substracted from each other and
alpar@428
    30
  /// they can be pushed to a stream.
alpar@428
    31
alpar@428
    32
  class TimeStamp
alpar@428
    33
  {
alpar@428
    34
    tms ts;
alpar@428
    35
    double real_time;
alpar@428
    36
  
alpar@428
    37
  public:
alpar@428
    38
alpar@428
    39
    tms &getTms() {return ts;}
alpar@428
    40
    const tms &getTms() const {return ts;}
alpar@428
    41
    ///Read the current time values of the process
alpar@428
    42
    void stamp()
alpar@428
    43
    {
alpar@428
    44
      timeval tv;
alpar@428
    45
      times(&ts);
alpar@428
    46
      gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
alpar@428
    47
    }
alpar@428
    48
  
alpar@428
    49
    /// Constructor initializing with zero
alpar@428
    50
    TimeStamp()
alpar@428
    51
    { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
alpar@428
    52
    ///Constructor initializing with the current time values of the process
alpar@428
    53
    TimeStamp(void *) { stamp();}
alpar@428
    54
  
alpar@428
    55
    ///
alpar@428
    56
    TimeStamp &operator+=(const TimeStamp &b)
alpar@428
    57
    {
alpar@428
    58
      ts.tms_utime+=b.ts.tms_utime;
alpar@428
    59
      ts.tms_stime+=b.ts.tms_stime;
alpar@428
    60
      ts.tms_cutime+=b.ts.tms_cutime;
alpar@428
    61
      ts.tms_cstime+=b.ts.tms_cstime;
alpar@428
    62
      real_time+=b.real_time;
alpar@428
    63
      return *this;
alpar@428
    64
    }
alpar@428
    65
    ///
alpar@428
    66
    TimeStamp operator+(const TimeStamp &b) const
alpar@428
    67
    {
alpar@428
    68
      TimeStamp t(*this);
alpar@428
    69
      return t+=b;
alpar@428
    70
    }
alpar@428
    71
    ///
alpar@428
    72
    TimeStamp &operator-=(const TimeStamp &b)
alpar@428
    73
    {
alpar@428
    74
      ts.tms_utime-=b.ts.tms_utime;
alpar@428
    75
      ts.tms_stime-=b.ts.tms_stime;
alpar@428
    76
      ts.tms_cutime-=b.ts.tms_cutime;
alpar@428
    77
      ts.tms_cstime-=b.ts.tms_cstime;
alpar@428
    78
      real_time-=b.real_time;
alpar@428
    79
      return *this;
alpar@428
    80
    }
alpar@428
    81
    ///
alpar@428
    82
    TimeStamp operator-(const TimeStamp &b) const
alpar@428
    83
    {
alpar@428
    84
      TimeStamp t(*this);
alpar@428
    85
      return t-=b;
alpar@428
    86
    }
alpar@428
    87
alpar@428
    88
    ///The time ellapsed since the last call of stamp()
alpar@428
    89
    TimeStamp ellapsed() const
alpar@428
    90
    {
alpar@428
    91
      TimeStamp t(NULL);
alpar@428
    92
      return t-*this;
alpar@428
    93
    }
alpar@428
    94
  
alpar@428
    95
    friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
alpar@428
    96
  
alpar@428
    97
    ///Gives back the user time of the process
alpar@428
    98
    double getUserTime() const
alpar@428
    99
    {
alpar@428
   100
      return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
alpar@428
   101
    }
alpar@428
   102
    ///Gives back the system time of the process
alpar@428
   103
    double getSystemTime() const
alpar@428
   104
    {
alpar@428
   105
      return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
alpar@428
   106
    }
alpar@428
   107
    ///Gives back the user time of the process' children
alpar@428
   108
    double getCUserTime() const
alpar@428
   109
    {
alpar@428
   110
      return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
alpar@428
   111
    }
alpar@428
   112
    ///Gives back the user time of the process' children
alpar@428
   113
    double getCSystemTime() const
alpar@428
   114
    {
alpar@428
   115
      return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
alpar@428
   116
    }
alpar@428
   117
    ///Gives back the real time of the process
alpar@428
   118
    double getRealTime() const {return real_time;}
alpar@428
   119
  };
alpar@428
   120
alpar@428
   121
  ///Class measuring the cpu time and real time usage of the process
alpar@428
   122
  class Timer
alpar@428
   123
  {
alpar@428
   124
    TimeStamp start_time;
alpar@428
   125
alpar@428
   126
    void _reset() {start_time.stamp();}
alpar@428
   127
  
alpar@428
   128
  public: 
alpar@428
   129
    ///Constructor. It starts with zero time counters
alpar@428
   130
    Timer() {_reset();}
alpar@428
   131
alpar@428
   132
    ///Computes the ellapsed time
alpar@428
   133
alpar@428
   134
    ///This conversion computes the ellapsed time
alpar@428
   135
    ///since the construction of \c t or since
alpar@428
   136
    ///the last \c t.reset().
alpar@428
   137
    operator TimeStamp ()
alpar@428
   138
    {
alpar@428
   139
      TimeStamp t;
alpar@428
   140
      t.stamp();
alpar@428
   141
      return t-start_time;
alpar@428
   142
    }
alpar@428
   143
alpar@428
   144
    ///Resets the time counters
alpar@428
   145
    TimeStamp reset()
alpar@428
   146
    {
alpar@428
   147
      TimeStamp t(start_time);
alpar@428
   148
      _reset();
alpar@428
   149
      return start_time-t;
alpar@428
   150
    }
alpar@428
   151
  };
alpar@428
   152
alpar@428
   153
  ///Prints the time counters
alpar@428
   154
alpar@428
   155
  ///Prints the time counters in the folloing form:
alpar@428
   156
  ///
alpar@428
   157
  /// u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs 
alpar@428
   158
  ///
alpar@428
   159
  /// where the values are the
alpar@428
   160
  /// - a user cpu time,
alpar@428
   161
  /// - a system cpu time,
alpar@428
   162
  /// - a user cpu time of children,
alpar@428
   163
  /// - a system cpu time of children and
alpar@428
   164
  /// - a real time,
alpar@428
   165
  ///
alpar@428
   166
  ///respectively
alpar@428
   167
  inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
alpar@428
   168
  {
alpar@428
   169
    long cls = sysconf(_SC_CLK_TCK);
alpar@428
   170
    os << "u: " << double(t.getTms().tms_utime)/cls <<
alpar@428
   171
      "s, s: " << double(t.getTms().tms_stime)/cls <<
alpar@428
   172
      "s, cu: " << double(t.getTms().tms_cutime)/cls <<
alpar@428
   173
      "s, cs: " << double(t.getTms().tms_cstime)/cls <<
alpar@428
   174
      "s, real: " << t.getRealTime() << "s";
alpar@428
   175
    return os;
alpar@428
   176
  }
alpar@428
   177
alpar@428
   178
  /// @}  
alpar@428
   179
alpar@428
   180
} //namespace hugo
alpar@428
   181
alpar@428
   182
#endif //HUGO_TIME_MEASURE_H