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.hxx>
19 #include <Standard_ProgramError.hxx>
20 #include <Standard_Stream.hxx>
24 #include <sys/times.h>
28 #include <sys/resource.h>
32 #define _sysconf sysconf
40 #define CLK_TCK CLOCKS_PER_SEC
43 #if (defined(__APPLE__))
44 #include <mach/task.h>
45 #include <mach/mach.h>
48 //=======================================================================
49 //function : GetProcessCPU
51 //=======================================================================
52 void OSD_Chronometer::GetProcessCPU (Standard_Real& theUserSeconds,
53 Standard_Real& theSystemSeconds)
55 #if defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
56 static const long aCLK_TCK = sysconf(_SC_CLK_TCK);
58 static const long aCLK_TCK = CLK_TCK;
64 theUserSeconds = (Standard_Real)aCurrentTMS.tms_utime / aCLK_TCK;
65 theSystemSeconds = (Standard_Real)aCurrentTMS.tms_stime / aCLK_TCK;
68 //=======================================================================
69 //function : GetThreadCPU
71 //=======================================================================
72 void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
73 Standard_Real& theSystemSeconds)
75 theUserSeconds = theSystemSeconds = 0.0;
76 #if (defined(__APPLE__))
77 struct task_thread_times_info aTaskInfo;
78 mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
79 if (KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO,
80 (task_info_t )&aTaskInfo, &aTaskInfoCount))
82 theUserSeconds = Standard_Real(aTaskInfo.user_time.seconds) + 0.000001 * aTaskInfo.user_time.microseconds;
83 theSystemSeconds = Standard_Real(aTaskInfo.system_time.seconds) + 0.000001 * aTaskInfo.system_time.microseconds;
85 #elif (defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)) || defined(__ANDROID__) || defined(__QNX__)
86 // on Linux, only user times are available for threads via clock_gettime()
88 if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &t))
90 theUserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec;
92 #elif defined(SOLARIS)
93 // on Solaris, both user and system times are available as LWP times
95 if (!getrusage (RUSAGE_LWP, &rut))
97 theUserSeconds = rut.ru_utime.tv_sec + 0.000001 * rut.ru_utime.tv_usec;
98 theSystemSeconds = rut.ru_stime.tv_sec + 0.000001 * rut.ru_stime.tv_usec;
101 #pragma error "OS is not supported yet; code to be ported"
109 //=======================================================================
110 //function : EncodeFILETIME
111 //purpose : Encode time defined by FILETIME structure
112 // (100s nanoseconds since January 1, 1601) to 64-bit integer
113 //=======================================================================
114 static inline __int64 EncodeFILETIME (PFILETIME pFt)
118 qw = pFt -> dwHighDateTime;
120 qw |= pFt -> dwLowDateTime;
125 //=======================================================================
126 //function : GetProcessCPU
128 //=======================================================================
129 void OSD_Chronometer::GetProcessCPU (Standard_Real& theUserSeconds,
130 Standard_Real& theSystemSeconds)
133 FILETIME ftStart, ftExit, ftKernel, ftUser;
134 ::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser);
135 theUserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
136 theSystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
138 theUserSeconds = 0.0;
139 theSystemSeconds = 0.0;
143 //=======================================================================
144 //function : GetThreadCPU
146 //=======================================================================
147 void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
148 Standard_Real& theSystemSeconds)
151 FILETIME ftStart, ftExit, ftKernel, ftUser;
152 ::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser);
153 theUserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
154 theSystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
156 theUserSeconds = 0.0;
157 theSystemSeconds = 0.0;
163 //=======================================================================
164 //function : OSD_Chronometer
166 //=======================================================================
167 OSD_Chronometer::OSD_Chronometer (Standard_Boolean theThisThreadOnly)
168 : myStartCpuUser (0.0),
170 myCumulCpuUser (0.0),
172 myIsStopped (Standard_True),
173 myIsThreadOnly (theThisThreadOnly)
178 //=======================================================================
179 //function : ~OSD_Chronometer
180 //purpose : Destructor
181 //=======================================================================
182 OSD_Chronometer::~OSD_Chronometer()
186 //=======================================================================
187 //function : SetThisThreadOnly
189 //=======================================================================
190 void OSD_Chronometer::SetThisThreadOnly (Standard_Boolean theIsThreadOnly)
194 throw Standard_ProgramError ("OSD_Chronometer::SetThreadOnly() called for started Timer");
196 myIsThreadOnly = theIsThreadOnly;
199 //=======================================================================
202 //=======================================================================
203 void OSD_Chronometer::Reset ()
205 myIsStopped = Standard_True;
206 myStartCpuUser = myStartCpuSys = 0.;
207 myCumulCpuUser = myCumulCpuSys = 0.;
211 //=======================================================================
214 //=======================================================================
215 void OSD_Chronometer::Restart ()
221 //=======================================================================
224 //=======================================================================
225 void OSD_Chronometer::Stop()
229 Standard_Real Curr_user, Curr_sys;
231 GetThreadCPU (Curr_user, Curr_sys);
233 GetProcessCPU (Curr_user, Curr_sys);
235 myCumulCpuUser += Curr_user - myStartCpuUser;
236 myCumulCpuSys += Curr_sys - myStartCpuSys;
238 myIsStopped = Standard_True;
242 //=======================================================================
245 //=======================================================================
246 void OSD_Chronometer::Start ()
251 GetThreadCPU (myStartCpuUser, myStartCpuSys);
253 GetProcessCPU (myStartCpuUser, myStartCpuSys);
255 myIsStopped = Standard_False;
259 //=======================================================================
262 //=======================================================================
263 void OSD_Chronometer::Show() const
268 //=======================================================================
271 //=======================================================================
272 void OSD_Chronometer::Show (Standard_OStream& theOStream) const
274 Standard_Real aCumulUserSec = 0.0, aCumulSysSec = 0.0;
275 Show (aCumulUserSec, aCumulSysSec);
276 std::streamsize prec = theOStream.precision (12);
277 theOStream << "CPU user time: " << aCumulUserSec << " seconds\n";
278 theOStream << "CPU system time: " << aCumulSysSec << " seconds\n";
279 theOStream.precision (prec);
282 //=======================================================================
285 //=======================================================================
286 void OSD_Chronometer::Show (Standard_Real& theUserSec, Standard_Real& theSystemSec) const
288 theUserSec = myCumulCpuUser;
289 theSystemSec = myCumulCpuSys;
295 Standard_Real aCurrUser, aCurrSys;
297 GetThreadCPU (aCurrUser, aCurrSys);
299 GetProcessCPU (aCurrUser, aCurrSys);
301 theUserSec += aCurrUser - myStartCpuUser;
302 theSystemSec += aCurrSys - myStartCpuSys;