900e91c847ac17b8cf43641ade45e693498acc47
[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 #include <OSD_Chronometer.ixx>
18 #include <Standard_Stream.hxx>
19
20 // ====================== PLATFORM-SPECIFIC PART ========================
21
22 #ifndef WNT
23
24 //---------- Systemes autres que WNT : ----------------------------------
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37
38 #ifdef HAVE_SYS_TIMES_H
39 # include <sys/times.h>
40 #endif
41
42 #ifdef SOLARIS
43 # include <sys/resource.h>
44 #endif
45
46 //=======================================================================
47 //Selon les plateformes on doit avoir le nombre de clicks par secondes
48 //qui est l unite de mesure du temps.
49 //=======================================================================
50 #ifndef sysconf
51 # define _sysconf sysconf
52 #endif
53
54 #if defined(HAVE_TIME_H) || defined(WNT) || defined(DECOSF1)
55 # include <time.h>
56 #endif
57
58 #  ifndef CLK_TCK
59 #   define CLK_TCK      CLOCKS_PER_SEC
60 #  endif
61
62 #ifdef HAVE_LIMITS
63 # include <limits>
64 #elif defined (HAVE_LIMITS_H)
65 # include <limits.h>
66 #endif
67
68 #if (defined(__APPLE__))
69   #include <mach/task.h>
70   #include <mach/mach.h>
71 #endif
72
73 //=======================================================================
74 //function : GetProcessCPU
75 //purpose  :
76 //=======================================================================
77 void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
78 {
79 #if defined(LIN) || defined(linux) || defined(__FreeBSD__)
80   static const long aCLK_TCK = sysconf(_SC_CLK_TCK);
81 #else
82   static const long aCLK_TCK = CLK_TCK;
83 #endif
84
85   tms CurrentTMS;
86   times (&CurrentTMS);
87
88   UserSeconds   = (Standard_Real)CurrentTMS.tms_utime / aCLK_TCK;
89   SystemSeconds = (Standard_Real)CurrentTMS.tms_stime / aCLK_TCK;
90 }
91
92 //=======================================================================
93 //function : GetThreadCPU
94 //purpose  :
95 //=======================================================================
96 void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
97                                     Standard_Real& theSystemSeconds)
98 {
99   theUserSeconds = theSystemSeconds = 0.0;
100 #if (defined(__APPLE__))
101   struct task_thread_times_info aTaskInfo;
102   mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
103   if (KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO,
104       (task_info_t )&aTaskInfo, &aTaskInfoCount))
105   {
106     theUserSeconds   = Standard_Real(aTaskInfo.user_time.seconds)   + 0.000001 * aTaskInfo.user_time.microseconds;
107     theSystemSeconds = Standard_Real(aTaskInfo.system_time.seconds) + 0.000001 * aTaskInfo.system_time.microseconds;
108   }
109 #elif defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)
110   // on Linux, only user times are available for threads via clock_gettime()
111   struct timespec t;
112   if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &t))
113   {
114     theUserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec;
115   }
116 #elif defined(SOLARIS)
117   // on Solaris, both user and system times are available as LWP times
118   struct rusage rut;
119   if (!getrusage (RUSAGE_LWP, &rut))
120   {
121     theUserSeconds   = rut.ru_utime.tv_sec + 0.000001 * rut.ru_utime.tv_usec;
122     theSystemSeconds = rut.ru_stime.tv_sec + 0.000001 * rut.ru_stime.tv_usec;
123   }
124 #else
125   #pragma error "OS is not supported yet; code to be ported"
126 #endif
127 }
128
129 #else
130
131 //---------------------------- Systeme WNT --------------------------------
132
133 #define STRICT
134 #include <windows.h>
135
136 //=======================================================================
137 //function : EncodeFILETIME
138 //purpose  : Encode time defined by FILETIME structure
139 //           (100s nanoseconds since January 1, 1601) to 64-bit integer
140 //=======================================================================
141 static inline __int64 EncodeFILETIME (PFILETIME pFt)
142 {
143   __int64 qw;
144
145   qw   = pFt -> dwHighDateTime;
146   qw <<= 32;
147   qw  |= pFt -> dwLowDateTime;
148
149   return qw;
150 }
151
152 //=======================================================================
153 //function : GetProcessCPU
154 //purpose  :
155 //=======================================================================
156 void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
157 {
158   FILETIME ftStart, ftExit, ftKernel, ftUser;
159   ::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser);
160   UserSeconds   = 0.0000001 * EncodeFILETIME (&ftUser);
161   SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
162 }
163
164 //=======================================================================
165 //function : GetThreadCPU
166 //purpose  :
167 //=======================================================================
168 void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
169 {
170   FILETIME ftStart, ftExit, ftKernel, ftUser;
171   ::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser);
172   UserSeconds   = 0.0000001 * EncodeFILETIME (&ftUser);
173   SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
174 }
175
176 #endif /* WNT */
177
178 // ====================== PLATFORM-INDEPENDENT PART ========================
179
180 //=======================================================================
181 //function : OSD_Chronometer
182 //purpose  :
183 //=======================================================================
184 OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly)
185 {
186   ThreadOnly = ThisThreadOnly;
187   Start_user = Start_sys = 0.;
188   Cumul_user = Cumul_sys = 0.;
189   Stopped    = Standard_True;
190 }
191
192 //=======================================================================
193 //function :  Destroy
194 //purpose  :
195 //=======================================================================
196 void OSD_Chronometer::Destroy ()
197 {
198 }
199
200 //=======================================================================
201 //function : Reset
202 //purpose  :
203 //=======================================================================
204 void OSD_Chronometer::Reset ()
205 {
206   Stopped    = Standard_True;
207   Start_user = Start_sys = 0.;
208   Cumul_user = Cumul_sys = 0.;
209 }
210
211 //=======================================================================
212 //function : Stop
213 //purpose  :
214 //=======================================================================
215 void OSD_Chronometer::Stop ()
216 {
217   if ( !Stopped ) {
218     Standard_Real Curr_user, Curr_sys;
219     if ( ThreadOnly )
220       GetThreadCPU (Curr_user, Curr_sys);
221     else
222       GetProcessCPU (Curr_user, Curr_sys);
223
224     Cumul_user += Curr_user - Start_user;
225     Cumul_sys  += Curr_sys  - Start_sys;
226
227     Stopped = Standard_True;
228   }
229 //  else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush;
230 }
231
232 //=======================================================================
233 //function : Start
234 //purpose  :
235 //=======================================================================
236 void OSD_Chronometer::Start ()
237 {
238   if ( Stopped ) {
239     if ( ThreadOnly )
240       GetThreadCPU (Start_user, Start_sys);
241     else
242       GetProcessCPU (Start_user, Start_sys);
243
244     Stopped = Standard_False;
245   }
246 //  else cerr << "WARNING: OSD_Chronometer already running !\n" << flush;
247 }
248
249 //=======================================================================
250 //function : Show
251 //purpose  :
252 //=======================================================================
253 void OSD_Chronometer::Show ()
254 {
255   Show (cout);
256 }
257
258 //=======================================================================
259 //function : Show
260 //purpose  :
261 //=======================================================================
262 void OSD_Chronometer::Show (Standard_OStream& os)
263 {
264   Standard_Boolean StopSav = Stopped;
265   if (!StopSav) Stop();
266   std::streamsize prec = os.precision (12);
267   os << "CPU user time: "   << Cumul_user  << " seconds " << endl;
268   os << "CPU system time: " << Cumul_sys   << " seconds " << endl;
269   os.precision (prec);
270   if (!StopSav) Start();
271 }
272
273 //=======================================================================
274 //function : Show
275 //purpose  : Returns cpu user time
276 //=======================================================================
277 void OSD_Chronometer::Show (Standard_Real& second)
278 {
279   Standard_Boolean StopSav = Stopped;
280   if (!StopSav) Stop();
281   second = Cumul_user;
282   if (!StopSav) Start();
283 }
284 //=======================================================================
285 //function : Show
286 //purpose  : Returns both user and system cpu times
287 //=======================================================================
288 void OSD_Chronometer::Show (Standard_Real& user,
289                             Standard_Real& system)
290 {
291   Standard_Boolean StopSav = Stopped;
292   if (!StopSav) Stop();
293   user = Cumul_user;
294   system = Cumul_sys;
295   if (!StopSav) Start();
296 }
297