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-2026 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 "paraSysTimer.h"
38
39#include <ctime>
40#if defined(__unix__) || defined(__APPLE__)
41#include <sys/resource.h>
42#elif defined(_MSC_VER)
43#include <windows.h> // GetProcessTimes() -- getrusage() is not available on the MS compiler
44#endif
45
46using namespace UG;
47
48///
49/// read the process' user / system CPU time in seconds
50///
51static void
53 double &userSeconds,
54 double &systemSeconds
55 )
56{
57#if defined(__unix__) || defined(__APPLE__)
58 struct rusage ru;
59 if( getrusage(RUSAGE_SELF, &ru) == 0 )
60 {
61 userSeconds = (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec * 1e-6;
62 systemSeconds = (double)ru.ru_stime.tv_sec + (double)ru.ru_stime.tv_usec * 1e-6;
63 return;
64 }
65 userSeconds = 0.0;
66 systemSeconds = 0.0;
67#elif defined(_MSC_VER)
68 /// Windows / MS compiler: getrusage() is unavailable, so recover the GetProcessTimes() path
69 /// that the std::chrono modernization (commit 06ea3d83) dropped. It reports the process'
70 /// cumulative user and kernel (system) CPU time as FILETIMEs in 100-nanosecond units; combine
71 /// each FILETIME's high/low halves (ULARGE_INTEGER avoids the signed-shift the old __int64 code
72 /// used) and scale by 1e7 to get seconds.
73 FILETIME ftCreationTime, ftExitTime, ftKernelTime, ftUserTime;
74 if( GetProcessTimes( GetCurrentProcess(),
75 &ftCreationTime, &ftExitTime, &ftKernelTime, &ftUserTime ) )
76 {
77 ULARGE_INTEGER u, k;
78 u.LowPart = ftUserTime.dwLowDateTime; u.HighPart = ftUserTime.dwHighDateTime;
79 k.LowPart = ftKernelTime.dwLowDateTime; k.HighPart = ftKernelTime.dwHighDateTime;
80 userSeconds = (double)u.QuadPart / 1.0e7; // 100-ns units -> seconds
81 systemSeconds = (double)k.QuadPart / 1.0e7;
82 return;
83 }
84 userSeconds = 0.0;
85 systemSeconds = 0.0;
86#else
87 /// portable fallback: std::clock() reports total processor time; there is no
88 /// standard user/system split, so charge it all to user time.
89 userSeconds = (double)std::clock() / (double)CLOCKS_PER_SEC;
90 systemSeconds = 0.0;
91#endif
92}
93
94void
95ParaSysTimer::start(
96 void
97 )
98{
99 startPoint = std::chrono::steady_clock::now();
101}
102
103void
105 void
106 )
107{
108 stopPoint = std::chrono::steady_clock::now();
110}
111
112double
114 void
115 )
116{
117 return std::chrono::duration<double>( startPoint.time_since_epoch() ).count();
118}
119
120double
122 void
123 )
124{
125 return std::chrono::duration<double>( std::chrono::steady_clock::now() - startPoint ).count();
126}
127
128double
130 void
131 )
132{
133 return std::chrono::duration<double>( stopPoint - startPoint ).count();
134}
135
136double
138 void
139 )
140{
141 return uTimeStop - uTimeStart;
142}
143
144double
146 void
147 )
148{
149 return sTimeStop - sTimeStart;
150}
double sTimeStart
process system CPU seconds at start()
Definition: paraSysTimer.h:68
double getUTime(void)
get user time between start timne and stop time
double uTimeStop
process user CPU seconds at stop()
Definition: paraSysTimer.h:67
double getSTime(void)
get system time between start timne and stop time
void stop(void)
stop timer
double getStartTime(void)
get start time
double uTimeStart
process user CPU seconds at start()
Definition: paraSysTimer.h:66
double getRTimeInterval(void)
get elapsed time from start time
double sTimeStop
process system CPU seconds at stop()
Definition: paraSysTimer.h:69
std::chrono::steady_clock::time_point startPoint
real time at start()
Definition: paraSysTimer.h:64
std::chrono::steady_clock::time_point stopPoint
real time at stop()
Definition: paraSysTimer.h:65
double getRTime(void)
get real time between start timne and stop time
static void readProcessCpuTime(double &userSeconds, double &systemSeconds)
read the process' user / system CPU time in seconds
System timer.