b2de7b0252922bc5f5f8ba04c714c0e59ac5db68
[occt.git] / src / OSD / OSD_Chronometer.cxx
1 // Created on: 1992-11-16
2 // Created by: Mireille MERCIEN
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <OSD_Chronometer.hxx>
19 #include <Standard_Stream.hxx>
20
21 #ifndef _WIN32
22
23 #include <sys/times.h>
24 #include <unistd.h>
25
26 #ifdef SOLARIS
27   #include <sys/resource.h>
28 #endif
29
30 #ifndef sysconf
31   #define _sysconf sysconf
32 #endif
33
34 #if defined(DECOSF1)
35   #include <time.h>
36 #endif
37
38 #ifndef CLK_TCK
39   #define CLK_TCK CLOCKS_PER_SEC
40 #endif
41
42 #if (defined(__APPLE__))
43   #include <mach/task.h>
44   #include <mach/mach.h>
45 #endif
46
47 //=======================================================================
48 //function : GetProcessCPU
49 //purpose  :
50 //=======================================================================
51 void OSD_Chronometer::GetProcessCPU (Standard_Real& theUserSeconds,
52                                      Standard_Real& theSystemSeconds)
53 {
54 #if defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(__QNX__)
55   static const long aCLK_TCK = sysconf(_SC_CLK_TCK);
56 #else
57   static const long aCLK_TCK = CLK_TCK;
58 #endif
59
60   tms aCurrentTMS;
61   times (&aCurrentTMS);
62
63   theUserSeconds   = (Standard_Real)aCurrentTMS.tms_utime / aCLK_TCK;
64   theSystemSeconds = (Standard_Real)aCurrentTMS.tms_stime / aCLK_TCK;
65 }
66
67 //=======================================================================
68 //function : GetThreadCPU
69 //purpose  :
70 //=======================================================================
71 void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
72                                     Standard_Real& theSystemSeconds)
73 {
74   theUserSeconds = theSystemSeconds = 0.0;
75 #if (defined(__APPLE__))
76   struct task_thread_times_info aTaskInfo;
77   mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
78   if (KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO,
79       (task_info_t )&aTaskInfo, &aTaskInfoCount))
80   {
81     theUserSeconds   = Standard_Real(aTaskInfo.user_time.seconds)   + 0.000001 * aTaskInfo.user_time.microseconds;
82     theSystemSeconds = Standard_Real(aTaskInfo.system_time.seconds) + 0.000001 * aTaskInfo.system_time.microseconds;
83   }
84 #elif (defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)) || defined(__ANDROID__) || defined(__QNX__)
85   // on Linux, only user times are available for threads via clock_gettime()
86   struct timespec t;
87   if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &t))
88   {
89     theUserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec;
90   }
91 #elif defined(SOLARIS)
92   // on Solaris, both user and system times are available as LWP times
93   struct rusage rut;
94   if (!getrusage (RUSAGE_LWP, &rut))
95   {
96     theUserSeconds   = rut.ru_utime.tv_sec + 0.000001 * rut.ru_utime.tv_usec;
97     theSystemSeconds = rut.ru_stime.tv_sec + 0.000001 * rut.ru_stime.tv_usec;
98   }
99 #else
100   #pragma error "OS is not supported yet; code to be ported"
101 #endif
102 }
103
104 #else
105
106 #include <windows.h>
107
108 //=======================================================================
109 //function : EncodeFILETIME
110 //purpose  : Encode time defined by FILETIME structure
111 //           (100s nanoseconds since January 1, 1601) to 64-bit integer
112 //=======================================================================
113 static inline __int64 EncodeFILETIME (PFILETIME pFt)
114 {
115   __int64 qw;
116
117   qw   = pFt -> dwHighDateTime;
118   qw <<= 32;
119   qw  |= pFt -> dwLowDateTime;
120
121   return qw;
122 }
123
124 //=======================================================================
125 //function : GetProcessCPU
126 //purpose  :
127 //=======================================================================
128 void OSD_Chronometer::GetProcessCPU (Standard_Real& theUserSeconds,
129                                      Standard_Real& theSystemSeconds)
130 {
131 #ifndef OCCT_UWP
132   FILETIME ftStart, ftExit, ftKernel, ftUser;
133   ::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser);
134   theUserSeconds   = 0.0000001 * EncodeFILETIME (&ftUser);
135   theSystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
136 #else
137   theUserSeconds = 0.0;
138   theSystemSeconds = 0.0;
139 #endif
140 }
141
142 //=======================================================================
143 //function : GetThreadCPU
144 //purpose  :
145 //=======================================================================
146 void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
147                                     Standard_Real& theSystemSeconds)
148 {
149 #ifndef OCCT_UWP
150   FILETIME ftStart, ftExit, ftKernel, ftUser;
151   ::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser);
152   theUserSeconds   = 0.0000001 * EncodeFILETIME (&ftUser);
153   theSystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
154 #else
155   theUserSeconds = 0.0;
156   theSystemSeconds = 0.0;
157 #endif
158 }
159
160 #endif /* _WIN32 */
161
162 //=======================================================================
163 //function : OSD_Chronometer
164 //purpose  :
165 //=======================================================================
166 OSD_Chronometer::OSD_Chronometer (Standard_Boolean theThisThreadOnly)
167 : myStartCpuUser (0.0),
168   myStartCpuSys  (0.0),
169   myCumulCpuUser (0.0),
170   myCumulCpuSys  (0.0),
171   myIsStopped    (Standard_True),
172   myIsThreadOnly (theThisThreadOnly)
173 {
174   //
175 }
176
177 //=======================================================================
178 //function : ~OSD_Chronometer
179 //purpose  : Destructor
180 //=======================================================================
181 OSD_Chronometer::~OSD_Chronometer()
182 {
183 }
184
185 //=======================================================================
186 //function : Reset
187 //purpose  :
188 //=======================================================================
189 void OSD_Chronometer::Reset ()
190 {
191   myIsStopped    = Standard_True;
192   myStartCpuUser = myStartCpuSys = 0.;
193   myCumulCpuUser = myCumulCpuSys = 0.;
194 }
195
196
197 //=======================================================================
198 //function : Restart
199 //purpose  :
200 //=======================================================================
201 void OSD_Chronometer::Restart ()
202 {
203   Reset();
204   Start();
205 }
206
207 //=======================================================================
208 //function : Stop
209 //purpose  :
210 //=======================================================================
211 void OSD_Chronometer::Stop()
212 {
213   if (!myIsStopped)
214   {
215     Standard_Real Curr_user, Curr_sys;
216     if (myIsThreadOnly)
217       GetThreadCPU (Curr_user, Curr_sys);
218     else
219       GetProcessCPU (Curr_user, Curr_sys);
220
221     myCumulCpuUser += Curr_user - myStartCpuUser;
222     myCumulCpuSys  += Curr_sys  - myStartCpuSys;
223
224     myIsStopped = Standard_True;
225   }
226 }
227
228 //=======================================================================
229 //function : Start
230 //purpose  :
231 //=======================================================================
232 void OSD_Chronometer::Start ()
233 {
234   if (myIsStopped)
235   {
236     if (myIsThreadOnly)
237       GetThreadCPU (myStartCpuUser, myStartCpuSys);
238     else
239       GetProcessCPU (myStartCpuUser, myStartCpuSys);
240
241     myIsStopped = Standard_False;
242   }
243 }
244
245 //=======================================================================
246 //function : Show
247 //purpose  :
248 //=======================================================================
249 void OSD_Chronometer::Show() const
250 {
251   Show (std::cout);
252 }
253
254 //=======================================================================
255 //function : Show
256 //purpose  :
257 //=======================================================================
258 void OSD_Chronometer::Show (Standard_OStream& theOStream) const
259 {
260   Standard_Real aCumulUserSec = 0.0, aCumulSysSec = 0.0;
261   Show (aCumulUserSec, aCumulSysSec);
262   std::streamsize prec = theOStream.precision (12);
263   theOStream << "CPU user time: "   << aCumulUserSec << " seconds\n";
264   theOStream << "CPU system time: " << aCumulSysSec  << " seconds\n";
265   theOStream.precision (prec);
266 }
267
268 //=======================================================================
269 //function : Show
270 //purpose  :
271 //=======================================================================
272 void OSD_Chronometer::Show (Standard_Real& theUserSec, Standard_Real& theSystemSec) const
273 {
274   theUserSec   = myCumulCpuUser;
275   theSystemSec = myCumulCpuSys;
276   if (myIsStopped)
277   {
278     return;
279   }
280
281   Standard_Real aCurrUser, aCurrSys;
282   if (myIsThreadOnly)
283     GetThreadCPU  (aCurrUser, aCurrSys);
284   else
285     GetProcessCPU (aCurrUser, aCurrSys);
286
287   theUserSec   += aCurrUser - myStartCpuUser;
288   theSystemSec += aCurrSys  - myStartCpuSys;
289 }