Scippy

UG

Ubiquity Generator framework

paraSysTimer.cpp
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and software framework */
4/* UG --- Ubquity Generator Framework */
5/* */
6/* Copyright Written by Yuji Shinano <shinano@zib.de>, */
7/* Copyright (C) 2021-2024 by Zuse Institute Berlin, */
8/* licensed under LGPL version 3 or later. */
9/* Commercial licenses are available through <licenses@zib.de> */
10/* */
11/* This code is free software; you can redistribute it and/or */
12/* modify it under the terms of the GNU Lesser General Public License */
13/* as published by the Free Software Foundation; either version 3 */
14/* of the License, or (at your option) any later version. */
15/* */
16/* This program is distributed in the hope that it will be useful, */
17/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
19/* GNU Lesser General Public License for more details. */
20/* */
21/* You should have received a copy of the GNU Lesser General Public License */
22/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
23/* */
24/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
25
26/**@file paraSysTimer.cpp
27 * @brief System timer.
28 * @author Yuji Shinano
29 *
30 *
31 *
32 */
33
34/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35
36
37#include <iostream>
38#include "paraSysTimer.h"
39using namespace UG;
40
41void
42ParaSysTimer::start(
43){
44#ifdef BSD
45 if (gettimeofday(&stTvTimeStart, (struct timezone *)0) < 0) {
46 std::cerr << "gettimeofday() error in ParaSysTimer::start" << std::endl;
47 exit(1);
48 }
49 if (getrusage(RUSAGE_SELF, &stRuStart) < 0) {
50 std::cerr << "getrusage() error in ParaSysTimer::start" << std::endl;
51 exit(1);
52 }
53#endif /* BSD */
54
55#ifdef _MSC_VER
56 _ftime(&timebStart);
57 if( ! GetProcessTimes(
58 GetCurrentProcess(), // specifies the process of interest
59 &ftCreationTime, // when the process was created
60 &ftExitTime, // when the process exited
61 &ftKernelTimeStart, // time the process has spent in kernel mode
62 &ftUserTimeStart // time the process has spent in user mode
63 ) ){
64 LPVOID lpMsgBuf;
65
66 FormatMessage(
67 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
68 NULL,
69 GetLastError(),
70 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
71 (LPTSTR) &lpMsgBuf,
72 0,
73 NULL
74 );
75
76 std::cerr << "GetProcessTimes() error in ParaSysTimer::start errcode = "
77 << lpMsgBuf << std::endl;
78 exit(1);
79 }
80
81#endif /* _MSC_VER */
82
83#ifdef SYSV
84 if ((lTimeStart = times(&stTmsStart)) == -1) {
85 std::cerr << "times() error inin ParaSysTimer::start" << std::endl;
86 exit(1);
87 }
88#endif /* SYSV */
89
90
91 return;
92}
93
94void
96){
97#ifdef BSD
98 if (getrusage(RUSAGE_SELF, &stRuStop) < 0) {
99 std::cerr << "getrusage() error in ParaSysTimer::stop" << std::endl;
100 exit(1);
101 }
102 if (gettimeofday(&stTvTimeStop, (struct timezone *) 0) < 0) {
103 std::cerr << "gettimeofday() error in ParaSysTimer::stop" << std::endl;
104 exit(1);
105 }
106#endif /* BSD */
107
108#ifdef _MSC_VER
109 if( ! GetProcessTimes(
110 GetCurrentProcess(), // specifies the process of interest
111 &ftCreationTime, // when the process was created
112 &ftExitTime, // when the process exited
113 &ftKernelTimeStop, // time the process has spent in kernel mode
114 &ftUserTimeStop // time the process has spent in user mode
115 ) ){
116 std::cerr << "GetProcessTimes() error in ParaSysTimer::start" << std::endl;
117 exit(1);
118 }
119 _ftime(&timebStop);
120#endif /* _MSC_VER */
121
122#ifdef SYSV
123 if ((lTimeStop = times(&stTmsStop)) == -1) {
124 std::cerr << "times() error in ParaSysTimer::stop" << std::endl;
125 exit(1);
126 }
127#endif /* SYSV */
128
129 return;
130}
131
132double
134 )
135{
136#ifdef BSD
137
138 double dStart = ((double) stTvTimeStart.tv_sec) * 1000000.0
139 + stTvTimeStart.tv_usec;
140 double dSeconds = dStart/1000000.0;
141#endif /* BSD */
142
143#ifdef _MSC_VER
144 double dSeconds;
145 dSeconds = timebStart.time
146 + (double)(timebStart.millitm)/1000;
147#endif /* _MSC_VER */
148
149#ifdef SYSV
150
151 double dSeconds = (double)lTimeStart/(double)TICKS;
152#endif /* SYSV */
153
154 return dSeconds;
155}
156
157double
159){
160#ifdef BSD
161 struct timeval stTempTvTimeStop;
162 struct rusage stTempRuStop;
163
164 if (getrusage(RUSAGE_SELF, &stTempRuStop) < 0) {
165 std::cerr << "getrusage() error in ParaSysTimer::getRTimeInterval" << std::endl;
166 exit(1);
167 }
168 if (gettimeofday(&stTempTvTimeStop, (struct timezone *) 0) < 0) {
169 std::cerr << "gettimeofday() error in ParaSysTimer::getRTimeInterval" << std::endl;
170 exit(1);
171 }
172
173 double dStart = ((double) stTvTimeStart.tv_sec) * 1000000.0
174 + stTvTimeStart.tv_usec;
175 double dStop = ((double) stTempTvTimeStop.tv_sec) * 1000000.0
176 + stTempTvTimeStop.tv_usec;
177 double dSeconds = (dStop - dStart)/1000000.0;
178#endif /* BSD */
179
180#ifdef _MSC_VER
181 struct _timeb timebTempStop;
182
183 FILETIME ftTempCreationTime, ftTempExitTime,
184 ftTempKernelTimeStop, ftTempUserTimeStop;
185
186 if( ! GetProcessTimes(
187 GetCurrentProcess(), // specifies the process of interest
188 &ftTempCreationTime, // when the process was created
189 &ftTempExitTime, // when the process exited
190 &ftTempKernelTimeStop, // time the process has spent in kernel mode
191 &ftTempUserTimeStop // time the process has spent in user mode
192 ) ){
193 std::cerr << "GetProcessTimes() error in ParaSysTimer::start" << std::endl;
194 exit(1);
195 }
196 _ftime(&timebTempStop);
197
198 double dSeconds;
199 if ( timebTempStop.millitm - timebStart.millitm >= 0 ){
200 dSeconds = (timebTempStop.time - timebStart.time)
201 + (double)(timebTempStop.millitm - timebStart.millitm)/1000;
202 } else {
203 dSeconds = (timebTempStop.time - timebStart.time - 1)
204 + (double)(timebTempStop.millitm + 1000
205 - timebStart.millitm)/1000;
206 }
207#endif /* _MSC_VER */
208
209#ifdef SYSV
210 long lTempTimeStop;
211 struct tms stTempTmsStop;
212
213 if ((lTempTimeStop = times(&stTempTmsStop)) == -1) {
214 std::cerr << "times() error in ParaSysTimer::stop" << std::endl;
215 exit(1);
216 }
217
218 double dSeconds = (double)(lTempTimeStop - lTimeStart)/(double)TICKS;
219#endif /* SYSV */
220
221 return dSeconds;
222}
223
224double
226){
227#ifdef BSD
228 double dStart = ((double) stTvTimeStart.tv_sec) * 1000000.0
229 + stTvTimeStart.tv_usec;
230 double dStop = ((double) stTvTimeStop.tv_sec) * 1000000.0
231 + stTvTimeStop.tv_usec;
232 double dSeconds = (dStop - dStart)/1000000.0;
233#endif /* BSD */
234
235#ifdef _MSC_VER
236 double dSeconds;
237 if ( timebStop.millitm - timebStart.millitm >= 0 ){
238 dSeconds = (timebStop.time - timebStart.time)
239 + (double)(timebStop.millitm - timebStart.millitm)/1000;
240 } else {
241 dSeconds = (timebStop.time - timebStart.time - 1)
242 + (double)(timebStop.millitm + 1000
243 - timebStart.millitm)/1000;
244 }
245#endif /* _MSC_VER */
246
247#ifdef SYSV
248 double dSeconds = (double)(lTimeStop - lTimeStart)/(double)TICKS;
249#endif /* SYSV */
250
251 return dSeconds;
252}
253double
255){
256#ifdef BSD
257 double dStart = ((double) stRuStart.ru_utime.tv_sec) * 1000000.0
258 + stRuStart.ru_utime.tv_usec;
259 double dStop = ((double) stRuStop.ru_utime.tv_sec) * 1000000.0
260 + stRuStop.ru_utime.tv_usec;
261 double dSeconds = (dStop - dStart)/1000000.0;
262#endif /* BSD */
263
264#ifdef _MSC_VER
265 double dSeconds;
266 __int64 i64Start, i64Stop;
267 i64Start = ftUserTimeStart.dwHighDateTime;
268 i64Start <<= 32;
269 i64Start |= ftUserTimeStart.dwLowDateTime;
270 i64Stop = ftUserTimeStop.dwHighDateTime;
271 i64Stop <<= 32;
272 i64Stop |= ftUserTimeStop.dwLowDateTime;
273
274 dSeconds = (double)( (i64Stop - i64Start) ) / 10000000.0;
275#endif /* _MSC_VER */
276
277#ifdef SYSV
278 double dSeconds = (double)(stTmsStop.tms_utime - stTmsStart.tms_utime)/
279 (double)TICKS;
280#endif /* SYSV */
281
282 return dSeconds;
283}
284
285double
287){
288#ifdef BSD
289 double dStart = ((double) stRuStart.ru_stime.tv_sec) * 1000000.0
290 + stRuStart.ru_stime.tv_usec;
291 double dStop = ((double)stRuStop.ru_stime.tv_sec) * 1000000.0
292 + stRuStop.ru_stime.tv_usec;
293 double dSeconds = (dStop - dStart)/1000000.0;
294#endif /* BSD */
295
296#ifdef _MSC_VER
297 double dSeconds;
298 __int64 i64Start, i64Stop;
299 i64Start = ftKernelTimeStart.dwHighDateTime;
300 i64Start <<= 32;
301 i64Start |= ftKernelTimeStart.dwLowDateTime;
302 i64Stop = ftKernelTimeStop.dwHighDateTime;
303 i64Stop <<= 32;
304 i64Stop |= ftKernelTimeStop.dwLowDateTime;
305
306 dSeconds = (double)( (i64Stop - i64Start) ) / 10000000.0;
307#endif /* _MSC_VER */
308
309#ifdef SYSV
310 double dSeconds = (double)(stTmsStop.tms_stime - stTmsStart.tms_stime)/
311 (double)TICKS;
312#endif /* SYSV */
313 return dSeconds;
314}
double getUTime(void)
get user time between start timne and stop time
double getSTime(void)
get system time between start timne and stop time
void stop(void)
stop timer
double getStartTime(void)
get start time
double getRTimeInterval(void)
get elapsed time from start time
double getRTime(void)
get real time between start timne and stop time
System timer.