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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <OSD_Chronometer.ixx>
18 #include <Standard_Stream.hxx>
20 // ====================== PLATFORM-SPECIFIC PART ========================
24 //---------- Systemes autres que WNT : ----------------------------------
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
38 #ifdef HAVE_SYS_TIMES_H
39 # include <sys/times.h>
43 # include <sys/resource.h>
46 //=======================================================================
47 //Selon les plateformes on doit avoir le nombre de clicks par secondes
48 //qui est l unite de mesure du temps.
49 //=======================================================================
51 # define _sysconf sysconf
54 #if defined(HAVE_TIME_H) || defined(WNT) || defined(DECOSF1)
59 # define CLK_TCK CLOCKS_PER_SEC
64 #elif defined (HAVE_LIMITS_H)
68 #if (defined(__APPLE__))
69 #include <mach/task.h>
70 #include <mach/mach.h>
73 //=======================================================================
74 //function : GetProcessCPU
76 //=======================================================================
77 void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
79 #if defined(LIN) || defined(linux) || defined(__FreeBSD__)
80 static const long aCLK_TCK = sysconf(_SC_CLK_TCK);
82 static const long aCLK_TCK = CLK_TCK;
88 UserSeconds = (Standard_Real)CurrentTMS.tms_utime / aCLK_TCK;
89 SystemSeconds = (Standard_Real)CurrentTMS.tms_stime / aCLK_TCK;
92 //=======================================================================
93 //function : GetThreadCPU
95 //=======================================================================
96 void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
97 Standard_Real& theSystemSeconds)
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))
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;
109 #elif defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)
110 // on Linux, only user times are available for threads via clock_gettime()
112 if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &t))
114 theUserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec;
116 #elif defined(SOLARIS)
117 // on Solaris, both user and system times are available as LWP times
119 if (!getrusage (RUSAGE_LWP, &rut))
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;
125 #pragma error "OS is not supported yet; code to be ported"
131 //---------------------------- Systeme WNT --------------------------------
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)
145 qw = pFt -> dwHighDateTime;
147 qw |= pFt -> dwLowDateTime;
152 //=======================================================================
153 //function : GetProcessCPU
155 //=======================================================================
156 void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
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);
164 //=======================================================================
165 //function : GetThreadCPU
167 //=======================================================================
168 void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
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);
178 // ====================== PLATFORM-INDEPENDENT PART ========================
180 //=======================================================================
181 //function : OSD_Chronometer
183 //=======================================================================
184 OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly)
186 ThreadOnly = ThisThreadOnly;
187 Start_user = Start_sys = 0.;
188 Cumul_user = Cumul_sys = 0.;
189 Stopped = Standard_True;
192 //=======================================================================
195 //=======================================================================
196 void OSD_Chronometer::Destroy ()
200 //=======================================================================
203 //=======================================================================
204 void OSD_Chronometer::Reset ()
206 Stopped = Standard_True;
207 Start_user = Start_sys = 0.;
208 Cumul_user = Cumul_sys = 0.;
211 //=======================================================================
214 //=======================================================================
215 void OSD_Chronometer::Stop ()
218 Standard_Real Curr_user, Curr_sys;
220 GetThreadCPU (Curr_user, Curr_sys);
222 GetProcessCPU (Curr_user, Curr_sys);
224 Cumul_user += Curr_user - Start_user;
225 Cumul_sys += Curr_sys - Start_sys;
227 Stopped = Standard_True;
229 // else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush;
232 //=======================================================================
235 //=======================================================================
236 void OSD_Chronometer::Start ()
240 GetThreadCPU (Start_user, Start_sys);
242 GetProcessCPU (Start_user, Start_sys);
244 Stopped = Standard_False;
246 // else cerr << "WARNING: OSD_Chronometer already running !\n" << flush;
249 //=======================================================================
252 //=======================================================================
253 void OSD_Chronometer::Show ()
258 //=======================================================================
261 //=======================================================================
262 void OSD_Chronometer::Show (Standard_OStream& os)
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;
270 if (!StopSav) Start();
273 //=======================================================================
275 //purpose : Returns cpu user time
276 //=======================================================================
277 void OSD_Chronometer::Show (Standard_Real& second)
279 Standard_Boolean StopSav = Stopped;
280 if (!StopSav) Stop();
282 if (!StopSav) Start();
284 //=======================================================================
286 //purpose : Returns both user and system cpu times
287 //=======================================================================
288 void OSD_Chronometer::Show (Standard_Real& user,
289 Standard_Real& system)
291 Standard_Boolean StopSav = Stopped;
292 if (!StopSav) Stop();
295 if (!StopSav) Start();