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.
18 #include <OSD_Chronometer.hxx>
19 #include <Standard_Stream.hxx>
23 #include <sys/times.h>
27 #include <sys/resource.h>
31 #define _sysconf sysconf
39 #define CLK_TCK CLOCKS_PER_SEC
42 #if (defined(__APPLE__))
43 #include <mach/task.h>
44 #include <mach/mach.h>
47 //=======================================================================
48 //function : GetProcessCPU
50 //=======================================================================
51 void OSD_Chronometer::GetProcessCPU (Standard_Real& theUserSeconds,
52 Standard_Real& theSystemSeconds)
54 #if defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__)
55 static const long aCLK_TCK = sysconf(_SC_CLK_TCK);
57 static const long aCLK_TCK = CLK_TCK;
63 theUserSeconds = (Standard_Real)aCurrentTMS.tms_utime / aCLK_TCK;
64 theSystemSeconds = (Standard_Real)aCurrentTMS.tms_stime / aCLK_TCK;
67 //=======================================================================
68 //function : GetThreadCPU
70 //=======================================================================
71 void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
72 Standard_Real& theSystemSeconds)
74 theUserSeconds = theSystemSeconds = 0.0;
75 #if (defined(__APPLE__))
76 struct task_thread_times_info aTaskInfo;
77 mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
78 if (KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO,
79 (task_info_t )&aTaskInfo, &aTaskInfoCount))
81 theUserSeconds = Standard_Real(aTaskInfo.user_time.seconds) + 0.000001 * aTaskInfo.user_time.microseconds;
82 theSystemSeconds = Standard_Real(aTaskInfo.system_time.seconds) + 0.000001 * aTaskInfo.system_time.microseconds;
84 #elif (defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)) || defined(__ANDROID__) || defined(__QNX__)
85 // on Linux, only user times are available for threads via clock_gettime()
87 if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &t))
89 theUserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec;
91 #elif defined(SOLARIS)
92 // on Solaris, both user and system times are available as LWP times
94 if (!getrusage (RUSAGE_LWP, &rut))
96 theUserSeconds = rut.ru_utime.tv_sec + 0.000001 * rut.ru_utime.tv_usec;
97 theSystemSeconds = rut.ru_stime.tv_sec + 0.000001 * rut.ru_stime.tv_usec;
100 #pragma error "OS is not supported yet; code to be ported"
108 //=======================================================================
109 //function : EncodeFILETIME
110 //purpose : Encode time defined by FILETIME structure
111 // (100s nanoseconds since January 1, 1601) to 64-bit integer
112 //=======================================================================
113 static inline __int64 EncodeFILETIME (PFILETIME pFt)
117 qw = pFt -> dwHighDateTime;
119 qw |= pFt -> dwLowDateTime;
124 //=======================================================================
125 //function : GetProcessCPU
127 //=======================================================================
128 void OSD_Chronometer::GetProcessCPU (Standard_Real& theUserSeconds,
129 Standard_Real& theSystemSeconds)
132 FILETIME ftStart, ftExit, ftKernel, ftUser;
133 ::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser);
134 theUserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
135 theSystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
137 theUserSeconds = 0.0;
138 theSystemSeconds = 0.0;
142 //=======================================================================
143 //function : GetThreadCPU
145 //=======================================================================
146 void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
147 Standard_Real& theSystemSeconds)
150 FILETIME ftStart, ftExit, ftKernel, ftUser;
151 ::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser);
152 theUserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
153 theSystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
155 theUserSeconds = 0.0;
156 theSystemSeconds = 0.0;
162 //=======================================================================
163 //function : OSD_Chronometer
165 //=======================================================================
166 OSD_Chronometer::OSD_Chronometer (Standard_Boolean theThisThreadOnly)
167 : myStartCpuUser (0.0),
169 myCumulCpuUser (0.0),
171 myIsStopped (Standard_True),
172 myIsThreadOnly (theThisThreadOnly)
177 //=======================================================================
178 //function : ~OSD_Chronometer
179 //purpose : Destructor
180 //=======================================================================
181 OSD_Chronometer::~OSD_Chronometer()
185 //=======================================================================
188 //=======================================================================
189 void OSD_Chronometer::Reset ()
191 myIsStopped = Standard_True;
192 myStartCpuUser = myStartCpuSys = 0.;
193 myCumulCpuUser = myCumulCpuSys = 0.;
197 //=======================================================================
200 //=======================================================================
201 void OSD_Chronometer::Restart ()
207 //=======================================================================
210 //=======================================================================
211 void OSD_Chronometer::Stop()
215 Standard_Real Curr_user, Curr_sys;
217 GetThreadCPU (Curr_user, Curr_sys);
219 GetProcessCPU (Curr_user, Curr_sys);
221 myCumulCpuUser += Curr_user - myStartCpuUser;
222 myCumulCpuSys += Curr_sys - myStartCpuSys;
224 myIsStopped = Standard_True;
228 //=======================================================================
231 //=======================================================================
232 void OSD_Chronometer::Start ()
237 GetThreadCPU (myStartCpuUser, myStartCpuSys);
239 GetProcessCPU (myStartCpuUser, myStartCpuSys);
241 myIsStopped = Standard_False;
245 //=======================================================================
248 //=======================================================================
249 void OSD_Chronometer::Show() const
254 //=======================================================================
257 //=======================================================================
258 void OSD_Chronometer::Show (Standard_OStream& theOStream) const
260 Standard_Real aCumulUserSec = 0.0, aCumulSysSec = 0.0;
261 Show (aCumulUserSec, aCumulSysSec);
262 std::streamsize prec = theOStream.precision (12);
263 theOStream << "CPU user time: " << aCumulUserSec << " seconds\n";
264 theOStream << "CPU system time: " << aCumulSysSec << " seconds\n";
265 theOStream.precision (prec);
268 //=======================================================================
271 //=======================================================================
272 void OSD_Chronometer::Show (Standard_Real& theUserSec, Standard_Real& theSystemSec) const
274 theUserSec = myCumulCpuUser;
275 theSystemSec = myCumulCpuSys;
281 Standard_Real aCurrUser, aCurrSys;
283 GetThreadCPU (aCurrUser, aCurrSys);
285 GetProcessCPU (aCurrUser, aCurrSys);
287 theUserSec += aCurrUser - myStartCpuUser;
288 theSystemSec += aCurrSys - myStartCpuSys;