Integration of OCCT 6.5.0 from SVN
[occt.git] / src / OSD / OSD_Chronometer.cxx
1 // File:        OSD_Chronometer.cxx
2 // Created:     Mon Nov 16 15:20:41 1992
3 // Author:      Mireille MERCIEN
4 //              <mip@sdsun3>
5
6 #include <OSD_Chronometer.ixx>
7 #include <Standard_Stream.hxx>
8
9 // ====================== PLATFORM-SPECIFIC PART ========================
10
11 #ifndef WNT
12
13 //---------- Systemes autres que WNT : ----------------------------------
14
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
18
19 #ifdef HAVE_SYS_TYPES_H
20 # include <sys/types.h>
21 #endif
22
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26
27 #ifdef HAVE_SYS_TIMES_H
28 # include <sys/times.h>
29 #endif
30
31 #ifdef SOLARIS
32 # include <sys/resource.h>
33 #endif
34
35 //=======================================================================
36 //Selon les plateformes on doit avoir le nombre de clicks par secondes
37 //qui est l unite de mesure du temps. 
38 //=======================================================================
39 #ifndef sysconf 
40 # define _sysconf sysconf
41 #endif
42
43 #if defined(HAVE_TIME_H) || defined(WNT) || defined(DECOSF1)
44 # include <time.h>
45 #endif
46
47 #  ifndef CLK_TCK
48 #   define CLK_TCK      CLOCKS_PER_SEC
49 #  endif
50
51 #ifdef HAVE_LIMITS
52 # include <limits>
53 #elif defined (HAVE_LIMITS_H)
54 # include <limits.h>
55 #endif
56
57 //=======================================================================
58 //function : GetProcessCPU
59 //purpose  : 
60 //=======================================================================
61 void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
62 {
63 #if defined(LIN) || defined(linux) || defined(__FreeBSD__)
64   static const long aCLK_TCK = sysconf(_SC_CLK_TCK);
65 #else
66   static const long aCLK_TCK = CLK_TCK;
67 #endif
68       
69   tms CurrentTMS;
70   times (&CurrentTMS);
71
72   UserSeconds   = (Standard_Real)CurrentTMS.tms_utime / aCLK_TCK;
73   SystemSeconds = (Standard_Real)CurrentTMS.tms_stime / aCLK_TCK;
74 }
75
76 //=======================================================================
77 //function : GetThreadCPU
78 //purpose  : 
79 //=======================================================================
80 void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
81 {
82   UserSeconds = SystemSeconds = 0.;
83
84 #if defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)
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     UserSeconds = 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     UserSeconds   = rut.ru_utime.tv_sec + 0.000001 * rut.ru_utime.tv_usec;
97     SystemSeconds = 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 //---------------------------- Systeme WNT --------------------------------
107
108 #define STRICT
109 #include <windows.h>
110
111 //=======================================================================
112 //function : EncodeFILETIME
113 //purpose  : Encode time defined by FILETIME structure
114 //           (100s nanoseconds since January 1, 1601) to 64-bit integer
115 //=======================================================================
116 static inline __int64 EncodeFILETIME (PFILETIME pFt) 
117 {
118   __int64 qw;
119
120   qw   = pFt -> dwHighDateTime;
121   qw <<= 32;
122   qw  |= pFt -> dwLowDateTime;
123
124   return qw;
125 }  
126
127 //=======================================================================
128 //function : GetProcessCPU
129 //purpose  : 
130 //=======================================================================
131 void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
132 {
133   FILETIME ftStart, ftExit, ftKernel, ftUser;
134   ::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser);
135   UserSeconds   = 0.0000001 * EncodeFILETIME (&ftUser);
136   SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
137 }
138
139 //=======================================================================
140 //function : GetThreadCPU
141 //purpose  : 
142 //=======================================================================
143 void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
144 {
145   FILETIME ftStart, ftExit, ftKernel, ftUser;
146   ::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser);
147   UserSeconds   = 0.0000001 * EncodeFILETIME (&ftUser);
148   SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
149 }
150
151 #endif /* WNT */
152
153 // ====================== PLATFORM-INDEPENDENT PART ========================
154
155 //=======================================================================
156 //function : OSD_Chronometer
157 //purpose  : 
158 //=======================================================================
159 OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly)
160 {
161   ThreadOnly = ThisThreadOnly;
162   Start_user = Start_sys = 0.;
163   Cumul_user = Cumul_sys = 0.;
164   Stopped    = Standard_True; 
165 }
166
167 //=======================================================================
168 //function :  Destroy
169 //purpose  : 
170 //=======================================================================
171 void OSD_Chronometer::Destroy () 
172 {
173 }
174
175 //=======================================================================
176 //function : Reset
177 //purpose  : 
178 //=======================================================================
179 void OSD_Chronometer::Reset ()
180 {
181   Stopped    = Standard_True;
182   Start_user = Start_sys = 0.;
183   Cumul_user = Cumul_sys = 0.;
184 }
185
186 //=======================================================================
187 //function : Stop
188 //purpose  : 
189 //=======================================================================
190 void OSD_Chronometer::Stop () 
191 {
192   if ( !Stopped ) {
193     Standard_Real Curr_user, Curr_sys;
194     if ( ThreadOnly )
195       GetThreadCPU (Curr_user, Curr_sys);
196     else
197       GetProcessCPU (Curr_user, Curr_sys);
198
199     Cumul_user += Curr_user - Start_user;
200     Cumul_sys  += Curr_sys  - Start_sys;
201
202     Stopped = Standard_True; 
203   } 
204 //  else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush;
205 }
206
207 //=======================================================================
208 //function : Start
209 //purpose  : 
210 //=======================================================================
211 void OSD_Chronometer::Start () 
212 {
213   if ( Stopped ) {
214     if ( ThreadOnly )
215       GetThreadCPU (Start_user, Start_sys);
216     else
217       GetProcessCPU (Start_user, Start_sys);
218  
219     Stopped = Standard_False;
220   } 
221 //  else cerr << "WARNING: OSD_Chronometer already running !\n" << flush;
222 }
223
224 //=======================================================================
225 //function : Show
226 //purpose  : 
227 //=======================================================================
228 void OSD_Chronometer::Show ()
229 {
230   Show (cout);
231 }
232
233 //=======================================================================
234 //function : Show
235 //purpose  : 
236 //=======================================================================
237 void OSD_Chronometer::Show (Standard_OStream& os)
238 {
239   Standard_Boolean StopSav = Stopped;
240   if (!StopSav) Stop();
241   std::streamsize prec = os.precision (12); 
242   os << "CPU user time: "   << Cumul_user  << " seconds " << endl;
243   os << "CPU system time: " << Cumul_sys   << " seconds " << endl;
244   os.precision (prec);
245   if (!StopSav) Start();
246 }
247
248 //=======================================================================
249 //function : Show
250 //purpose  : Returns cpu user time
251 //=======================================================================
252 void OSD_Chronometer::Show (Standard_Real& second)
253 {
254   Standard_Boolean StopSav = Stopped;
255   if (!StopSav) Stop();
256   second = Cumul_user; 
257   if (!StopSav) Start();
258 }
259 //=======================================================================
260 //function : Show
261 //purpose  : Returns both user and system cpu times
262 //=======================================================================
263 void OSD_Chronometer::Show (Standard_Real& user,
264                             Standard_Real& system)
265 {
266   Standard_Boolean StopSav = Stopped;
267   if (!StopSav) Stop();
268   user = Cumul_user; 
269   system = Cumul_sys; 
270   if (!StopSav) Start();
271 }
272