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
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.
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.
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.
22 #include <OSD_Chronometer.ixx>
23 #include <Standard_Stream.hxx>
25 // ====================== PLATFORM-SPECIFIC PART ========================
29 //---------- Systemes autres que WNT : ----------------------------------
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
43 #ifdef HAVE_SYS_TIMES_H
44 # include <sys/times.h>
48 # include <sys/resource.h>
51 //=======================================================================
52 //Selon les plateformes on doit avoir le nombre de clicks par secondes
53 //qui est l unite de mesure du temps.
54 //=======================================================================
56 # define _sysconf sysconf
59 #if defined(HAVE_TIME_H) || defined(WNT) || defined(DECOSF1)
64 # define CLK_TCK CLOCKS_PER_SEC
69 #elif defined (HAVE_LIMITS_H)
73 #if (defined(__APPLE__))
74 #include <mach/task.h>
75 #include <mach/mach.h>
78 //=======================================================================
79 //function : GetProcessCPU
81 //=======================================================================
82 void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
84 #if defined(LIN) || defined(linux) || defined(__FreeBSD__)
85 static const long aCLK_TCK = sysconf(_SC_CLK_TCK);
87 static const long aCLK_TCK = CLK_TCK;
93 UserSeconds = (Standard_Real)CurrentTMS.tms_utime / aCLK_TCK;
94 SystemSeconds = (Standard_Real)CurrentTMS.tms_stime / aCLK_TCK;
97 //=======================================================================
98 //function : GetThreadCPU
100 //=======================================================================
101 void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
102 Standard_Real& theSystemSeconds)
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))
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;
114 #elif defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)
115 // on Linux, only user times are available for threads via clock_gettime()
117 if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &t))
119 theUserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec;
121 #elif defined(SOLARIS)
122 // on Solaris, both user and system times are available as LWP times
124 if (!getrusage (RUSAGE_LWP, &rut))
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;
130 #pragma error "OS is not supported yet; code to be ported"
136 //---------------------------- Systeme WNT --------------------------------
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)
150 qw = pFt -> dwHighDateTime;
152 qw |= pFt -> dwLowDateTime;
157 //=======================================================================
158 //function : GetProcessCPU
160 //=======================================================================
161 void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
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);
169 //=======================================================================
170 //function : GetThreadCPU
172 //=======================================================================
173 void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
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);
183 // ====================== PLATFORM-INDEPENDENT PART ========================
185 //=======================================================================
186 //function : OSD_Chronometer
188 //=======================================================================
189 OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly)
191 ThreadOnly = ThisThreadOnly;
192 Start_user = Start_sys = 0.;
193 Cumul_user = Cumul_sys = 0.;
194 Stopped = Standard_True;
197 //=======================================================================
200 //=======================================================================
201 void OSD_Chronometer::Destroy ()
205 //=======================================================================
208 //=======================================================================
209 void OSD_Chronometer::Reset ()
211 Stopped = Standard_True;
212 Start_user = Start_sys = 0.;
213 Cumul_user = Cumul_sys = 0.;
216 //=======================================================================
219 //=======================================================================
220 void OSD_Chronometer::Stop ()
223 Standard_Real Curr_user, Curr_sys;
225 GetThreadCPU (Curr_user, Curr_sys);
227 GetProcessCPU (Curr_user, Curr_sys);
229 Cumul_user += Curr_user - Start_user;
230 Cumul_sys += Curr_sys - Start_sys;
232 Stopped = Standard_True;
234 // else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush;
237 //=======================================================================
240 //=======================================================================
241 void OSD_Chronometer::Start ()
245 GetThreadCPU (Start_user, Start_sys);
247 GetProcessCPU (Start_user, Start_sys);
249 Stopped = Standard_False;
251 // else cerr << "WARNING: OSD_Chronometer already running !\n" << flush;
254 //=======================================================================
257 //=======================================================================
258 void OSD_Chronometer::Show ()
263 //=======================================================================
266 //=======================================================================
267 void OSD_Chronometer::Show (Standard_OStream& os)
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;
275 if (!StopSav) Start();
278 //=======================================================================
280 //purpose : Returns cpu user time
281 //=======================================================================
282 void OSD_Chronometer::Show (Standard_Real& second)
284 Standard_Boolean StopSav = Stopped;
285 if (!StopSav) Stop();
287 if (!StopSav) Start();
289 //=======================================================================
291 //purpose : Returns both user and system cpu times
292 //=======================================================================
293 void OSD_Chronometer::Show (Standard_Real& user,
294 Standard_Real& system)
296 Standard_Boolean StopSav = Stopped;
297 if (!StopSav) Stop();
300 if (!StopSav) Start();