-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcputimer.cpp
217 lines (170 loc) · 5.3 KB
/
cputimer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "cputimer.h"
#include <time.h>
#include <stdio.h>
#ifndef WIN32
#include <sys/resource.h>
#endif
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
CPUTimer::CPUTimer()
{
CPUCurrSecs = 0;
CPUTotalSecs = 0;
CronoCurrSecs = 0;
CronoTotalSecs = 0;
started = false;
#if defined WIN32
//---------------------------------------------------------------------
hFile = GetCurrentProcess();
GetProcessTimes( hFile,
&FT_CreationTime,
&FT_ExitTime,
&FT_KernelTime,
&FT_UserTime );
FileTimeToLocalFileTime( &FT_CreationTime,
&FT_Aux1Time );
#endif
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
double CPUTimer::getCPUCurrSecs()
{
return CPUCurrSecs;
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
double CPUTimer::getTime()
{
return CPUTotalSecs;
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
double CPUTimer::getCronoCurrSecs()
{
return CronoCurrSecs;
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
double CPUTimer::getCronoTotalSecs()
{
return CronoTotalSecs;
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
void CPUTimer::start()
{
bool status = true;
CPUCurrSecs = 0;
CronoCurrSecs = 0;
#if defined WIN32
//---------------------------------------------------------------------
GetProcessTimes( hFile,
&FT_CreationTime,
&FT_ExitTime,
&FT_KernelTime,
&FT_UserTime );
FileTimeToLocalFileTime( &FT_KernelTime, &FT_Aux1Time );
cpu_ti = *(ULONG64 *) &FT_Aux1Time;
FileTimeToLocalFileTime( &FT_UserTime, &FT_Aux1Time );
cpu_ti += *(ULONG64 *) &FT_Aux1Time;
GetSystemTimeAsFileTime( &FT_Aux1Time );
FileTimeToLocalFileTime( &FT_Aux1Time, &FT_CronoStartTime );
FileTimeToSystemTime( &FT_CronoStartTime, &ST_CronoStartTime );
crono_ti = *(ULONG64 *) &FT_CronoStartTime;
//---------------------------------------------------------------------
#else
//---------------------------------------------------------------------
CPUTStart = zeit();
CronoTStart = real_zeit();
//---------------------------------------------------------------------
#endif
gottime = false;
started = status;
//return( status );
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
void CPUTimer::stop()
{
bool status = true;
if (started)
{
#if defined WIN32
// Tempo de CPU
// ----------------------------------------------------------------------------
GetProcessTimes( hFile,
&FT_CreationTime,
&FT_ExitTime,
&FT_KernelTime,
&FT_UserTime );
FileTimeToLocalFileTime( &FT_KernelTime, &FT_Aux1Time );
cpu_tf = *(ULONG64 *) &FT_Aux1Time;
FileTimeToLocalFileTime( &FT_UserTime, &FT_Aux1Time );
cpu_tf += *(ULONG64 *) &FT_Aux1Time;
aux = (LONG64) (cpu_tf - cpu_ti);
FT_Aux1Time = *(FILETIME *) &aux;
FileTimeToSystemTime( &FT_Aux1Time, &ST_CPUCurrTime );
CPUCurrSecs = (double) (aux / 10000000U) + ((aux % 10000000U) * 0.0000001);
CPUTotalSecs += CPUCurrSecs;
secs = (LONG64) (CPUTotalSecs * 10000000U);
FT_Aux1Time = *(FILETIME *) &secs;
FileTimeToSystemTime( &FT_Aux1Time, &ST_CPUTotalTime );
// ----------------------------------------------------------------------------
// Tempo cronol�gico
// ----------------------------------------------------------------------------
GetSystemTimeAsFileTime( &FT_Aux1Time );
FileTimeToLocalFileTime( &FT_Aux1Time, &FT_CronoStopTime );
FileTimeToSystemTime( &FT_CronoStopTime, &ST_CronoStopTime );
crono_tf = *(ULONG64 *) &FT_CronoStopTime;
aux = (LONG64) (crono_tf - crono_ti);
CronoCurrSecs = (double) (aux / 10000000U) + ((aux % 10000000U) * 0.0000001);
CronoTotalSecs += CronoCurrSecs;
secs = (LONG64) (CronoTotalSecs * 10000000U);
FT_Aux1Time = *(FILETIME *) &secs;
FileTimeToSystemTime( &FT_Aux1Time, &ST_CronoTotalTime );
gottime = true;
// ----------------------------------------------------------------------------
#else
CPUTStop = zeit();
CronoTStop = real_zeit();
CPUTotalSecs += CPUTStop - CPUTStart;
CronoTotalSecs += CronoTStop - CronoTStart;
#endif
}
else
{
fprintf(stderr,"CPUTimer::stop(): called without calling CPUTimer::start() first!\n");
status = false;
}
started = false;
//return status;
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
void CPUTimer::reset()
{
started = false;
CPUCurrSecs = 0;
CPUTotalSecs = 0;
CronoCurrSecs = 0;
CronoTotalSecs = 0;
#ifdef WIN32
memset( &ST_CronoStartTime, 0, sizeof(SYSTEMTIME) );
memset( &ST_CronoStopTime, 0, sizeof(SYSTEMTIME) );
memset( &ST_CronoTotalTime, 0, sizeof(SYSTEMTIME) );
#endif
}
void CPUTimer::operator += ( CPUTimer t )
{
CPUCurrSecs += t.getCPUCurrSecs();
CPUTotalSecs += t.getTime();
CronoCurrSecs += t.getCronoCurrSecs();
CronoTotalSecs += t.getCronoTotalSecs();
}
#ifndef WIN32
double CPUTimer::zeit (void)
{
struct rusage ru;
getrusage (RUSAGE_SELF, &ru);
return ((double) ru.ru_utime.tv_sec) +
((double) ru.ru_utime.tv_usec) / 1000000.0;
}
#endif
#ifndef WIN32
double CPUTimer::real_zeit (void)
{
return (double) time (0);
}
#endif