| 1 | /* -*- C++ -*- |
|---|
| 2 | * src/lemon/time_measure.h - Part of LEMON, a generic C++ optimization library |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|---|
| 5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
|---|
| 6 | * |
|---|
| 7 | * Permission to use, modify and distribute this software is granted |
|---|
| 8 | * provided that this copyright notice appears in all copies. For |
|---|
| 9 | * precise terms see the accompanying LICENSE file. |
|---|
| 10 | * |
|---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
|---|
| 12 | * express or implied, and with no claim as to its suitability for any |
|---|
| 13 | * purpose. |
|---|
| 14 | * |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #ifndef LEMON_TIME_MEASURE_H |
|---|
| 18 | #define LEMON_TIME_MEASURE_H |
|---|
| 19 | |
|---|
| 20 | ///\ingroup misc |
|---|
| 21 | ///\file |
|---|
| 22 | ///\brief Tools for measuring cpu usage |
|---|
| 23 | |
|---|
| 24 | #include <sys/time.h> |
|---|
| 25 | #include <sys/times.h> |
|---|
| 26 | #include <fstream> |
|---|
| 27 | #include <iostream> |
|---|
| 28 | #include <unistd.h> |
|---|
| 29 | |
|---|
| 30 | namespace lemon { |
|---|
| 31 | |
|---|
| 32 | /// \addtogroup misc |
|---|
| 33 | /// @{ |
|---|
| 34 | |
|---|
| 35 | /// A class to store (cpu)time instances. |
|---|
| 36 | |
|---|
| 37 | /// This class stores five time values. |
|---|
| 38 | /// - a real time |
|---|
| 39 | /// - a user cpu time |
|---|
| 40 | /// - a system cpu time |
|---|
| 41 | /// - a user cpu time of children |
|---|
| 42 | /// - a system cpu time of children |
|---|
| 43 | /// |
|---|
| 44 | /// TimeStamp's can be added to or substracted from each other and |
|---|
| 45 | /// they can be pushed to a stream. |
|---|
| 46 | /// |
|---|
| 47 | /// In most cases, perhaps \ref Timer class is what you want to use instead. |
|---|
| 48 | /// |
|---|
| 49 | ///\author Alpar Juttner |
|---|
| 50 | |
|---|
| 51 | class TimeStamp |
|---|
| 52 | { |
|---|
| 53 | tms ts; |
|---|
| 54 | double real_time; |
|---|
| 55 | |
|---|
| 56 | public: |
|---|
| 57 | |
|---|
| 58 | tms &getTms() {return ts;} |
|---|
| 59 | const tms &getTms() const {return ts;} |
|---|
| 60 | ///Read the current time values of the process |
|---|
| 61 | void stamp() |
|---|
| 62 | { |
|---|
| 63 | timeval tv; |
|---|
| 64 | times(&ts); |
|---|
| 65 | gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /// Constructor initializing with zero |
|---|
| 69 | TimeStamp() |
|---|
| 70 | { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;} |
|---|
| 71 | ///Constructor initializing with the current time values of the process |
|---|
| 72 | TimeStamp(void *) { stamp();} |
|---|
| 73 | |
|---|
| 74 | ///\e |
|---|
| 75 | TimeStamp &operator+=(const TimeStamp &b) |
|---|
| 76 | { |
|---|
| 77 | ts.tms_utime+=b.ts.tms_utime; |
|---|
| 78 | ts.tms_stime+=b.ts.tms_stime; |
|---|
| 79 | ts.tms_cutime+=b.ts.tms_cutime; |
|---|
| 80 | ts.tms_cstime+=b.ts.tms_cstime; |
|---|
| 81 | real_time+=b.real_time; |
|---|
| 82 | return *this; |
|---|
| 83 | } |
|---|
| 84 | ///\e |
|---|
| 85 | TimeStamp operator+(const TimeStamp &b) const |
|---|
| 86 | { |
|---|
| 87 | TimeStamp t(*this); |
|---|
| 88 | return t+=b; |
|---|
| 89 | } |
|---|
| 90 | ///\e |
|---|
| 91 | TimeStamp &operator-=(const TimeStamp &b) |
|---|
| 92 | { |
|---|
| 93 | ts.tms_utime-=b.ts.tms_utime; |
|---|
| 94 | ts.tms_stime-=b.ts.tms_stime; |
|---|
| 95 | ts.tms_cutime-=b.ts.tms_cutime; |
|---|
| 96 | ts.tms_cstime-=b.ts.tms_cstime; |
|---|
| 97 | real_time-=b.real_time; |
|---|
| 98 | return *this; |
|---|
| 99 | } |
|---|
| 100 | ///\e |
|---|
| 101 | TimeStamp operator-(const TimeStamp &b) const |
|---|
| 102 | { |
|---|
| 103 | TimeStamp t(*this); |
|---|
| 104 | return t-=b; |
|---|
| 105 | } |
|---|
| 106 | ///The time ellapsed since the last call of stamp() |
|---|
| 107 | TimeStamp ellapsed() const |
|---|
| 108 | { |
|---|
| 109 | TimeStamp t(NULL); |
|---|
| 110 | return t-*this; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
|---|
| 114 | |
|---|
| 115 | ///Gives back the user time of the process |
|---|
| 116 | double getUserTime() const |
|---|
| 117 | { |
|---|
| 118 | return double(ts.tms_utime)/sysconf(_SC_CLK_TCK); |
|---|
| 119 | } |
|---|
| 120 | ///Gives back the system time of the process |
|---|
| 121 | double getSystemTime() const |
|---|
| 122 | { |
|---|
| 123 | return double(ts.tms_stime)/sysconf(_SC_CLK_TCK); |
|---|
| 124 | } |
|---|
| 125 | ///Gives back the user time of the process' children |
|---|
| 126 | double getCUserTime() const |
|---|
| 127 | { |
|---|
| 128 | return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK); |
|---|
| 129 | } |
|---|
| 130 | ///Gives back the user time of the process' children |
|---|
| 131 | double getCSystemTime() const |
|---|
| 132 | { |
|---|
| 133 | return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK); |
|---|
| 134 | } |
|---|
| 135 | ///Gives back the real time of the process |
|---|
| 136 | double getRealTime() const {return real_time;} |
|---|
| 137 | }; |
|---|
| 138 | |
|---|
| 139 | ///Class measuring the cpu time and real time usage of the process |
|---|
| 140 | |
|---|
| 141 | ///Class measuring the cpu time and real time usage of the process. |
|---|
| 142 | ///It is quite easy-to-use, here is a short example. |
|---|
| 143 | ///\code |
|---|
| 144 | ///#include<lemon/time_measure.h> |
|---|
| 145 | ///#include<iostream> |
|---|
| 146 | /// |
|---|
| 147 | ///int main() |
|---|
| 148 | ///{ |
|---|
| 149 | /// |
|---|
| 150 | /// ... |
|---|
| 151 | /// |
|---|
| 152 | /// Timer T; |
|---|
| 153 | /// doSomething(); |
|---|
| 154 | /// std::cout << T << '\n'; |
|---|
| 155 | /// T.reset(); |
|---|
| 156 | /// doSomethingElse(); |
|---|
| 157 | /// std::cout << T << '\n'; |
|---|
| 158 | /// |
|---|
| 159 | /// ... |
|---|
| 160 | /// |
|---|
| 161 | ///} |
|---|
| 162 | ///\endcode |
|---|
| 163 | /// |
|---|
| 164 | ///\todo This shouldn't be Unix (Linux) specific. |
|---|
| 165 | /// |
|---|
| 166 | ///\author Alpar Juttner |
|---|
| 167 | class Timer |
|---|
| 168 | { |
|---|
| 169 | TimeStamp start_time; |
|---|
| 170 | |
|---|
| 171 | void _reset() {start_time.stamp();} |
|---|
| 172 | |
|---|
| 173 | public: |
|---|
| 174 | ///Constructor. It starts with zero time counters |
|---|
| 175 | Timer() {_reset();} |
|---|
| 176 | |
|---|
| 177 | ///Computes the ellapsed time |
|---|
| 178 | |
|---|
| 179 | ///This conversion computes the ellapsed time |
|---|
| 180 | ///since the construction of \c t or since |
|---|
| 181 | ///the last \c t.reset(). |
|---|
| 182 | operator TimeStamp () const |
|---|
| 183 | { |
|---|
| 184 | TimeStamp t; |
|---|
| 185 | t.stamp(); |
|---|
| 186 | return t-start_time; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | ///Resets the time counters |
|---|
| 190 | TimeStamp reset() |
|---|
| 191 | { |
|---|
| 192 | TimeStamp t(start_time); |
|---|
| 193 | _reset(); |
|---|
| 194 | return start_time-t; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | ///Gives back the ellapsed user time of the process |
|---|
| 199 | double getUserTime() const |
|---|
| 200 | { |
|---|
| 201 | return operator TimeStamp().getUserTime(); |
|---|
| 202 | } |
|---|
| 203 | ///Gives back the ellapsed system time of the process |
|---|
| 204 | double getSystemTime() const |
|---|
| 205 | { |
|---|
| 206 | return operator TimeStamp().getSystemTime(); |
|---|
| 207 | } |
|---|
| 208 | ///Gives back the ellapsed user time of the process' children |
|---|
| 209 | double getCUserTime() const |
|---|
| 210 | { |
|---|
| 211 | return operator TimeStamp().getCUserTime(); |
|---|
| 212 | } |
|---|
| 213 | ///Gives back the ellapsed user time of the process' children |
|---|
| 214 | double getCSystemTime() const |
|---|
| 215 | { |
|---|
| 216 | return operator TimeStamp().getCSystemTime(); |
|---|
| 217 | } |
|---|
| 218 | ///Gives back the ellapsed real time of the process |
|---|
| 219 | double getRealTime() const |
|---|
| 220 | { |
|---|
| 221 | return operator TimeStamp().getRealTime(); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | }; |
|---|
| 225 | |
|---|
| 226 | ///Prints the time counters |
|---|
| 227 | |
|---|
| 228 | ///Prints the time counters in the following form: |
|---|
| 229 | /// |
|---|
| 230 | /// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt> |
|---|
| 231 | /// |
|---|
| 232 | /// where the values are the |
|---|
| 233 | /// \li \c u: user cpu time, |
|---|
| 234 | /// \li \c s: system cpu time, |
|---|
| 235 | /// \li \c cu: user cpu time of children, |
|---|
| 236 | /// \li \c cs: system cpu time of children, |
|---|
| 237 | /// \li \c real: real time. |
|---|
| 238 | /// \relates TimeStamp |
|---|
| 239 | inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
|---|
| 240 | { |
|---|
| 241 | long cls = sysconf(_SC_CLK_TCK); |
|---|
| 242 | os << "u: " << double(t.getTms().tms_utime)/cls << |
|---|
| 243 | "s, s: " << double(t.getTms().tms_stime)/cls << |
|---|
| 244 | "s, cu: " << double(t.getTms().tms_cutime)/cls << |
|---|
| 245 | "s, cs: " << double(t.getTms().tms_cstime)/cls << |
|---|
| 246 | "s, real: " << t.getRealTime() << "s"; |
|---|
| 247 | return os; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | /// @} |
|---|
| 251 | |
|---|
| 252 | } //namespace lemon |
|---|
| 253 | |
|---|
| 254 | #endif //LEMON_TIME_MEASURE_H |
|---|