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