| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_TIME_MEASURE_H |
| 20 | 20 |
#define LEMON_TIME_MEASURE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup timecount |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Tools for measuring cpu usage |
| 25 | 25 |
|
| 26 | 26 |
#ifdef WIN32 |
| 27 | 27 |
#define WIN32_LEAN_AND_MEAN |
| 28 | 28 |
#define NOMINMAX |
| 29 | 29 |
#include <windows.h> |
| 30 | 30 |
#include <cmath> |
| 31 | 31 |
#else |
| 32 |
#include <unistd.h> |
|
| 32 | 33 |
#include <sys/times.h> |
| 33 | 34 |
#include <sys/time.h> |
| 34 | 35 |
#endif |
| 35 | 36 |
|
| 36 | 37 |
#include <string> |
| 37 | 38 |
#include <fstream> |
| 38 | 39 |
#include <iostream> |
| 39 | 40 |
|
| 40 | 41 |
namespace lemon {
|
| 41 | 42 |
|
| 42 | 43 |
/// \addtogroup timecount |
| 43 | 44 |
/// @{
|
| 44 | 45 |
|
| 45 | 46 |
/// A class to store (cpu)time instances. |
| 46 | 47 |
|
| 47 | 48 |
/// This class stores five time values. |
| 48 | 49 |
/// - a real time |
| 49 | 50 |
/// - a user cpu time |
| 50 | 51 |
/// - a system cpu time |
| 51 | 52 |
/// - a user cpu time of children |
| 52 | 53 |
/// - a system cpu time of children |
| 53 | 54 |
/// |
| 54 | 55 |
/// TimeStamp's can be added to or substracted from each other and |
| 55 | 56 |
/// they can be pushed to a stream. |
| 56 | 57 |
/// |
| 57 | 58 |
/// In most cases, perhaps the \ref Timer or the \ref TimeReport |
| 58 | 59 |
/// class is what you want to use instead. |
| 59 | 60 |
|
| 60 | 61 |
class TimeStamp |
| 61 | 62 |
{
|
| 62 | 63 |
double utime; |
| 63 | 64 |
double stime; |
| 64 | 65 |
double cutime; |
| 65 | 66 |
double cstime; |
| 66 | 67 |
double rtime; |
| 67 | 68 |
|
| 68 | 69 |
void _reset() {
|
| 69 | 70 |
utime = stime = cutime = cstime = rtime = 0; |
| 70 | 71 |
} |
| 71 | 72 |
|
| 72 | 73 |
public: |
| 73 | 74 |
|
| 74 | 75 |
///Read the current time values of the process |
| 75 | 76 |
void stamp() |
| 76 | 77 |
{
|
| 77 | 78 |
#ifndef WIN32 |
| 78 | 79 |
timeval tv; |
| 79 | 80 |
gettimeofday(&tv, 0); |
| 80 | 81 |
rtime=tv.tv_sec+double(tv.tv_usec)/1e6; |
| 81 | 82 |
|
| 82 | 83 |
tms ts; |
| 83 | 84 |
double tck=sysconf(_SC_CLK_TCK); |
| 84 | 85 |
times(&ts); |
| 85 | 86 |
utime=ts.tms_utime/tck; |
| 86 | 87 |
stime=ts.tms_stime/tck; |
| 87 | 88 |
cutime=ts.tms_cutime/tck; |
| 88 | 89 |
cstime=ts.tms_cstime/tck; |
| 89 | 90 |
#else |
| 90 | 91 |
static const double ch = 4294967296.0e-7; |
| 91 | 92 |
static const double cl = 1.0e-7; |
| 92 | 93 |
|
| 93 | 94 |
FILETIME system; |
| 94 | 95 |
GetSystemTimeAsFileTime(&system); |
| 95 | 96 |
rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
| 96 | 97 |
|
| 97 | 98 |
FILETIME create, exit, kernel, user; |
| 98 | 99 |
if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
|
| 99 | 100 |
utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
| 100 | 101 |
stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
| 101 | 102 |
cutime = 0; |
| 102 | 103 |
cstime = 0; |
| 103 | 104 |
} else {
|
| 104 | 105 |
rtime = 0; |
| 105 | 106 |
utime = 0; |
| 106 | 107 |
stime = 0; |
| 107 | 108 |
cutime = 0; |
| 108 | 109 |
cstime = 0; |
| 109 | 110 |
} |
| 110 | 111 |
#endif |
| 111 | 112 |
} |
| 112 | 113 |
|
| 113 | 114 |
/// Constructor initializing with zero |
| 114 | 115 |
TimeStamp() |
| 115 | 116 |
{ _reset(); }
|
| 116 | 117 |
///Constructor initializing with the current time values of the process |
| 117 | 118 |
TimeStamp(void *) { stamp();}
|
| 118 | 119 |
|
| 119 | 120 |
///Set every time value to zero |
| 120 | 121 |
TimeStamp &reset() {_reset();return *this;}
|
| 121 | 122 |
|
| 122 | 123 |
///\e |
| 123 | 124 |
TimeStamp &operator+=(const TimeStamp &b) |
| 124 | 125 |
{
|
| 125 | 126 |
utime+=b.utime; |
| 126 | 127 |
stime+=b.stime; |
| 127 | 128 |
cutime+=b.cutime; |
| 128 | 129 |
cstime+=b.cstime; |
| 129 | 130 |
rtime+=b.rtime; |
| 130 | 131 |
return *this; |
| 131 | 132 |
} |
| 132 | 133 |
///\e |
| 133 | 134 |
TimeStamp operator+(const TimeStamp &b) const |
| 134 | 135 |
{
|
| 135 | 136 |
TimeStamp t(*this); |
| 136 | 137 |
return t+=b; |
| 137 | 138 |
} |
| 138 | 139 |
///\e |
| 139 | 140 |
TimeStamp &operator-=(const TimeStamp &b) |
| 140 | 141 |
{
|
| 141 | 142 |
utime-=b.utime; |
| 142 | 143 |
stime-=b.stime; |
| 143 | 144 |
cutime-=b.cutime; |
| 144 | 145 |
cstime-=b.cstime; |
| 145 | 146 |
rtime-=b.rtime; |
| 146 | 147 |
return *this; |
| 147 | 148 |
} |
| 148 | 149 |
///\e |
| 149 | 150 |
TimeStamp operator-(const TimeStamp &b) const |
| 150 | 151 |
{
|
| 151 | 152 |
TimeStamp t(*this); |
| 152 | 153 |
return t-=b; |
| 153 | 154 |
} |
| 154 | 155 |
///\e |
| 155 | 156 |
TimeStamp &operator*=(double b) |
| 156 | 157 |
{
|
| 157 | 158 |
utime*=b; |
| 158 | 159 |
stime*=b; |
| 159 | 160 |
cutime*=b; |
| 160 | 161 |
cstime*=b; |
| 161 | 162 |
rtime*=b; |
| 162 | 163 |
return *this; |
| 163 | 164 |
} |
| 164 | 165 |
///\e |
| 165 | 166 |
TimeStamp operator*(double b) const |
| 166 | 167 |
{
|
| 167 | 168 |
TimeStamp t(*this); |
| 168 | 169 |
return t*=b; |
| 169 | 170 |
} |
| 170 | 171 |
friend TimeStamp operator*(double b,const TimeStamp &t); |
| 171 | 172 |
///\e |
| 172 | 173 |
TimeStamp &operator/=(double b) |
| 173 | 174 |
{
|
| 174 | 175 |
utime/=b; |
| 175 | 176 |
stime/=b; |
| 176 | 177 |
cutime/=b; |
| 177 | 178 |
cstime/=b; |
| 178 | 179 |
rtime/=b; |
| 179 | 180 |
return *this; |
| 180 | 181 |
} |
| 181 | 182 |
///\e |
| 182 | 183 |
TimeStamp operator/(double b) const |
| 183 | 184 |
{
|
| 184 | 185 |
TimeStamp t(*this); |
| 185 | 186 |
return t/=b; |
| 186 | 187 |
} |
| 187 | 188 |
///The time ellapsed since the last call of stamp() |
| 188 | 189 |
TimeStamp ellapsed() const |
| 189 | 190 |
{
|
| 190 | 191 |
TimeStamp t(NULL); |
| 191 | 192 |
return t-*this; |
| 192 | 193 |
} |
| 193 | 194 |
|
| 194 | 195 |
friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
| 195 | 196 |
|
| 196 | 197 |
///Gives back the user time of the process |
| 197 | 198 |
double userTime() const |
| 198 | 199 |
{
|
| 199 | 200 |
return utime; |
| 200 | 201 |
} |
| 201 | 202 |
///Gives back the system time of the process |
| 202 | 203 |
double systemTime() const |
| 203 | 204 |
{
|
| 204 | 205 |
return stime; |
| 205 | 206 |
} |
| 206 | 207 |
///Gives back the user time of the process' children |
| 207 | 208 |
|
| 208 | 209 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
| 209 | 210 |
/// |
| 210 | 211 |
double cUserTime() const |
| 211 | 212 |
{
|
| 212 | 213 |
return cutime; |
| 213 | 214 |
} |
| 214 | 215 |
///Gives back the user time of the process' children |
| 215 | 216 |
|
| 216 | 217 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
| 217 | 218 |
/// |
| 218 | 219 |
double cSystemTime() const |
| 219 | 220 |
{
|
| 220 | 221 |
return cstime; |
| 221 | 222 |
} |
| 222 | 223 |
///Gives back the real time |
| 223 | 224 |
double realTime() const {return rtime;}
|
0 comments (0 inline)