1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/include/time_measure.h Mon Apr 26 17:39:38 2004 +0000
1.3 @@ -0,0 +1,182 @@
1.4 +// -*- c++ -*-
1.5 +#ifndef HUGO_TIME_MEASURE_H
1.6 +#define HUGO_TIME_MEASURE_H
1.7 +
1.8 +///ingroup graphs
1.9 +///\file
1.10 +///\brief Tools for measuring cpu usage
1.11 +
1.12 +#include <sys/time.h>
1.13 +#include <sys/times.h>
1.14 +#include <fstream>
1.15 +#include <iostream>
1.16 +#include <unistd.h>
1.17 +
1.18 +namespace hugo {
1.19 +
1.20 + /// \addtogroup misc
1.21 + /// @{
1.22 +
1.23 + /// A class to store (cpu)time instances.
1.24 +
1.25 + /// This class stores five time values.
1.26 + /// - a real time
1.27 + /// - a user cpu time
1.28 + /// - a system cpu time
1.29 + /// - a user cpu time of children
1.30 + /// - a system cpu time of children
1.31 + ///
1.32 + /// TimeStamp's can be added to or substracted from each other and
1.33 + /// they can be pushed to a stream.
1.34 +
1.35 + class TimeStamp
1.36 + {
1.37 + tms ts;
1.38 + double real_time;
1.39 +
1.40 + public:
1.41 +
1.42 + tms &getTms() {return ts;}
1.43 + const tms &getTms() const {return ts;}
1.44 + ///Read the current time values of the process
1.45 + void stamp()
1.46 + {
1.47 + timeval tv;
1.48 + times(&ts);
1.49 + gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6;
1.50 + }
1.51 +
1.52 + /// Constructor initializing with zero
1.53 + TimeStamp()
1.54 + { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;}
1.55 + ///Constructor initializing with the current time values of the process
1.56 + TimeStamp(void *) { stamp();}
1.57 +
1.58 + ///
1.59 + TimeStamp &operator+=(const TimeStamp &b)
1.60 + {
1.61 + ts.tms_utime+=b.ts.tms_utime;
1.62 + ts.tms_stime+=b.ts.tms_stime;
1.63 + ts.tms_cutime+=b.ts.tms_cutime;
1.64 + ts.tms_cstime+=b.ts.tms_cstime;
1.65 + real_time+=b.real_time;
1.66 + return *this;
1.67 + }
1.68 + ///
1.69 + TimeStamp operator+(const TimeStamp &b) const
1.70 + {
1.71 + TimeStamp t(*this);
1.72 + return t+=b;
1.73 + }
1.74 + ///
1.75 + TimeStamp &operator-=(const TimeStamp &b)
1.76 + {
1.77 + ts.tms_utime-=b.ts.tms_utime;
1.78 + ts.tms_stime-=b.ts.tms_stime;
1.79 + ts.tms_cutime-=b.ts.tms_cutime;
1.80 + ts.tms_cstime-=b.ts.tms_cstime;
1.81 + real_time-=b.real_time;
1.82 + return *this;
1.83 + }
1.84 + ///
1.85 + TimeStamp operator-(const TimeStamp &b) const
1.86 + {
1.87 + TimeStamp t(*this);
1.88 + return t-=b;
1.89 + }
1.90 +
1.91 + ///The time ellapsed since the last call of stamp()
1.92 + TimeStamp ellapsed() const
1.93 + {
1.94 + TimeStamp t(NULL);
1.95 + return t-*this;
1.96 + }
1.97 +
1.98 + friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t);
1.99 +
1.100 + ///Gives back the user time of the process
1.101 + double getUserTime() const
1.102 + {
1.103 + return double(ts.tms_utime)/sysconf(_SC_CLK_TCK);
1.104 + }
1.105 + ///Gives back the system time of the process
1.106 + double getSystemTime() const
1.107 + {
1.108 + return double(ts.tms_stime)/sysconf(_SC_CLK_TCK);
1.109 + }
1.110 + ///Gives back the user time of the process' children
1.111 + double getCUserTime() const
1.112 + {
1.113 + return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK);
1.114 + }
1.115 + ///Gives back the user time of the process' children
1.116 + double getCSystemTime() const
1.117 + {
1.118 + return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK);
1.119 + }
1.120 + ///Gives back the real time of the process
1.121 + double getRealTime() const {return real_time;}
1.122 + };
1.123 +
1.124 + ///Class measuring the cpu time and real time usage of the process
1.125 + class Timer
1.126 + {
1.127 + TimeStamp start_time;
1.128 +
1.129 + void _reset() {start_time.stamp();}
1.130 +
1.131 + public:
1.132 + ///Constructor. It starts with zero time counters
1.133 + Timer() {_reset();}
1.134 +
1.135 + ///Computes the ellapsed time
1.136 +
1.137 + ///This conversion computes the ellapsed time
1.138 + ///since the construction of \c t or since
1.139 + ///the last \c t.reset().
1.140 + operator TimeStamp ()
1.141 + {
1.142 + TimeStamp t;
1.143 + t.stamp();
1.144 + return t-start_time;
1.145 + }
1.146 +
1.147 + ///Resets the time counters
1.148 + TimeStamp reset()
1.149 + {
1.150 + TimeStamp t(start_time);
1.151 + _reset();
1.152 + return start_time-t;
1.153 + }
1.154 + };
1.155 +
1.156 + ///Prints the time counters
1.157 +
1.158 + ///Prints the time counters in the folloing form:
1.159 + ///
1.160 + /// u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs
1.161 + ///
1.162 + /// where the values are the
1.163 + /// - a user cpu time,
1.164 + /// - a system cpu time,
1.165 + /// - a user cpu time of children,
1.166 + /// - a system cpu time of children and
1.167 + /// - a real time,
1.168 + ///
1.169 + ///respectively
1.170 + inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t)
1.171 + {
1.172 + long cls = sysconf(_SC_CLK_TCK);
1.173 + os << "u: " << double(t.getTms().tms_utime)/cls <<
1.174 + "s, s: " << double(t.getTms().tms_stime)/cls <<
1.175 + "s, cu: " << double(t.getTms().tms_cutime)/cls <<
1.176 + "s, cs: " << double(t.getTms().tms_cstime)/cls <<
1.177 + "s, real: " << t.getRealTime() << "s";
1.178 + return os;
1.179 + }
1.180 +
1.181 + /// @}
1.182 +
1.183 +} //namespace hugo
1.184 +
1.185 +#endif //HUGO_TIME_MEASURE_H