| 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; |
0 comments (0 inline)