b311480e |
1 | // Created on: 1992-11-16 |
2 | // Created by: Mireille MERCIEN |
3 | // Copyright (c) 1992-1999 Matra Datavision |
973c2be1 |
4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
5 | // |
973c2be1 |
6 | // This file is part of Open CASCADE Technology software library. |
b311480e |
7 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
13 | // |
973c2be1 |
14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. |
7fd59977 |
16 | |
42cf5bc1 |
17 | #include <OSD_Chronometer.hxx> |
2491eec3 |
18 | |
19 | #include <Standard_ProgramError.hxx> |
7fd59977 |
20 | #include <Standard_Stream.hxx> |
21 | |
57c28b61 |
22 | #ifndef _WIN32 |
7fd59977 |
23 | |
03155c18 |
24 | #include <sys/times.h> |
25 | #include <unistd.h> |
7fd59977 |
26 | |
27 | #ifdef SOLARIS |
2da51263 |
28 | #include <sys/resource.h> |
7fd59977 |
29 | #endif |
30 | |
270a5d4e |
31 | #ifndef sysconf |
2da51263 |
32 | #define _sysconf sysconf |
7fd59977 |
33 | #endif |
34 | |
03155c18 |
35 | #if defined(DECOSF1) |
2da51263 |
36 | #include <time.h> |
7fd59977 |
37 | #endif |
38 | |
2da51263 |
39 | #ifndef CLK_TCK |
40 | #define CLK_TCK CLOCKS_PER_SEC |
41 | #endif |
7fd59977 |
42 | |
270a5d4e |
43 | #if (defined(__APPLE__)) |
44 | #include <mach/task.h> |
45 | #include <mach/mach.h> |
46 | #endif |
47 | |
7fd59977 |
48 | //======================================================================= |
49 | //function : GetProcessCPU |
270a5d4e |
50 | //purpose : |
7fd59977 |
51 | //======================================================================= |
2da51263 |
52 | void OSD_Chronometer::GetProcessCPU (Standard_Real& theUserSeconds, |
53 | Standard_Real& theSystemSeconds) |
7fd59977 |
54 | { |
565baee6 |
55 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__) |
7fd59977 |
56 | static const long aCLK_TCK = sysconf(_SC_CLK_TCK); |
57 | #else |
58 | static const long aCLK_TCK = CLK_TCK; |
59 | #endif |
270a5d4e |
60 | |
2da51263 |
61 | tms aCurrentTMS; |
62 | times (&aCurrentTMS); |
7fd59977 |
63 | |
2da51263 |
64 | theUserSeconds = (Standard_Real)aCurrentTMS.tms_utime / aCLK_TCK; |
65 | theSystemSeconds = (Standard_Real)aCurrentTMS.tms_stime / aCLK_TCK; |
7fd59977 |
66 | } |
67 | |
68 | //======================================================================= |
69 | //function : GetThreadCPU |
270a5d4e |
70 | //purpose : |
7fd59977 |
71 | //======================================================================= |
270a5d4e |
72 | void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds, |
73 | Standard_Real& theSystemSeconds) |
7fd59977 |
74 | { |
270a5d4e |
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)) |
81 | { |
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; |
84 | } |
d8d01f6e |
85 | #elif (defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)) || defined(__ANDROID__) || defined(__QNX__) |
7fd59977 |
86 | // on Linux, only user times are available for threads via clock_gettime() |
87 | struct timespec t; |
270a5d4e |
88 | if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &t)) |
7fd59977 |
89 | { |
270a5d4e |
90 | theUserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec; |
7fd59977 |
91 | } |
92 | #elif defined(SOLARIS) |
93 | // on Solaris, both user and system times are available as LWP times |
94 | struct rusage rut; |
270a5d4e |
95 | if (!getrusage (RUSAGE_LWP, &rut)) |
7fd59977 |
96 | { |
270a5d4e |
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; |
7fd59977 |
99 | } |
100 | #else |
270a5d4e |
101 | #pragma error "OS is not supported yet; code to be ported" |
102 | #endif |
7fd59977 |
103 | } |
104 | |
105 | #else |
106 | |
7fd59977 |
107 | #include <windows.h> |
108 | |
109 | //======================================================================= |
110 | //function : EncodeFILETIME |
111 | //purpose : Encode time defined by FILETIME structure |
112 | // (100s nanoseconds since January 1, 1601) to 64-bit integer |
113 | //======================================================================= |
270a5d4e |
114 | static inline __int64 EncodeFILETIME (PFILETIME pFt) |
7fd59977 |
115 | { |
116 | __int64 qw; |
117 | |
118 | qw = pFt -> dwHighDateTime; |
119 | qw <<= 32; |
120 | qw |= pFt -> dwLowDateTime; |
121 | |
122 | return qw; |
270a5d4e |
123 | } |
7fd59977 |
124 | |
125 | //======================================================================= |
126 | //function : GetProcessCPU |
270a5d4e |
127 | //purpose : |
7fd59977 |
128 | //======================================================================= |
2da51263 |
129 | void OSD_Chronometer::GetProcessCPU (Standard_Real& theUserSeconds, |
130 | Standard_Real& theSystemSeconds) |
7fd59977 |
131 | { |
742cc8b0 |
132 | #ifndef OCCT_UWP |
7fd59977 |
133 | FILETIME ftStart, ftExit, ftKernel, ftUser; |
134 | ::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser); |
2da51263 |
135 | theUserSeconds = 0.0000001 * EncodeFILETIME (&ftUser); |
136 | theSystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel); |
742cc8b0 |
137 | #else |
2da51263 |
138 | theUserSeconds = 0.0; |
139 | theSystemSeconds = 0.0; |
742cc8b0 |
140 | #endif |
7fd59977 |
141 | } |
142 | |
143 | //======================================================================= |
144 | //function : GetThreadCPU |
270a5d4e |
145 | //purpose : |
7fd59977 |
146 | //======================================================================= |
2da51263 |
147 | void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds, |
148 | Standard_Real& theSystemSeconds) |
7fd59977 |
149 | { |
742cc8b0 |
150 | #ifndef OCCT_UWP |
7fd59977 |
151 | FILETIME ftStart, ftExit, ftKernel, ftUser; |
152 | ::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser); |
2da51263 |
153 | theUserSeconds = 0.0000001 * EncodeFILETIME (&ftUser); |
154 | theSystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel); |
742cc8b0 |
155 | #else |
2da51263 |
156 | theUserSeconds = 0.0; |
157 | theSystemSeconds = 0.0; |
742cc8b0 |
158 | #endif |
7fd59977 |
159 | } |
160 | |
57c28b61 |
161 | #endif /* _WIN32 */ |
7fd59977 |
162 | |
7fd59977 |
163 | //======================================================================= |
164 | //function : OSD_Chronometer |
270a5d4e |
165 | //purpose : |
7fd59977 |
166 | //======================================================================= |
2da51263 |
167 | OSD_Chronometer::OSD_Chronometer (Standard_Boolean theThisThreadOnly) |
168 | : myStartCpuUser (0.0), |
169 | myStartCpuSys (0.0), |
170 | myCumulCpuUser (0.0), |
171 | myCumulCpuSys (0.0), |
172 | myIsStopped (Standard_True), |
173 | myIsThreadOnly (theThisThreadOnly) |
7fd59977 |
174 | { |
2da51263 |
175 | // |
7fd59977 |
176 | } |
177 | |
178 | //======================================================================= |
6da30ff1 |
179 | //function : ~OSD_Chronometer |
180 | //purpose : Destructor |
7fd59977 |
181 | //======================================================================= |
6da30ff1 |
182 | OSD_Chronometer::~OSD_Chronometer() |
7fd59977 |
183 | { |
184 | } |
185 | |
2491eec3 |
186 | //======================================================================= |
187 | //function : SetThisThreadOnly |
188 | //purpose : |
189 | //======================================================================= |
190 | void OSD_Chronometer::SetThisThreadOnly (Standard_Boolean theIsThreadOnly) |
191 | { |
192 | if (!myIsStopped) |
193 | { |
194 | throw Standard_ProgramError ("OSD_Chronometer::SetThreadOnly() called for started Timer"); |
195 | } |
196 | myIsThreadOnly = theIsThreadOnly; |
197 | } |
198 | |
7fd59977 |
199 | //======================================================================= |
200 | //function : Reset |
270a5d4e |
201 | //purpose : |
7fd59977 |
202 | //======================================================================= |
203 | void OSD_Chronometer::Reset () |
204 | { |
2da51263 |
205 | myIsStopped = Standard_True; |
206 | myStartCpuUser = myStartCpuSys = 0.; |
207 | myCumulCpuUser = myCumulCpuSys = 0.; |
7fd59977 |
208 | } |
209 | |
44fae8b1 |
210 | |
211 | //======================================================================= |
212 | //function : Restart |
213 | //purpose : |
214 | //======================================================================= |
215 | void OSD_Chronometer::Restart () |
216 | { |
6b55f8e3 |
217 | Reset(); |
44fae8b1 |
218 | Start(); |
219 | } |
220 | |
7fd59977 |
221 | //======================================================================= |
222 | //function : Stop |
270a5d4e |
223 | //purpose : |
7fd59977 |
224 | //======================================================================= |
2da51263 |
225 | void OSD_Chronometer::Stop() |
7fd59977 |
226 | { |
2da51263 |
227 | if (!myIsStopped) |
228 | { |
7fd59977 |
229 | Standard_Real Curr_user, Curr_sys; |
2da51263 |
230 | if (myIsThreadOnly) |
7fd59977 |
231 | GetThreadCPU (Curr_user, Curr_sys); |
232 | else |
233 | GetProcessCPU (Curr_user, Curr_sys); |
234 | |
2da51263 |
235 | myCumulCpuUser += Curr_user - myStartCpuUser; |
236 | myCumulCpuSys += Curr_sys - myStartCpuSys; |
7fd59977 |
237 | |
2da51263 |
238 | myIsStopped = Standard_True; |
270a5d4e |
239 | } |
7fd59977 |
240 | } |
241 | |
242 | //======================================================================= |
243 | //function : Start |
270a5d4e |
244 | //purpose : |
7fd59977 |
245 | //======================================================================= |
270a5d4e |
246 | void OSD_Chronometer::Start () |
7fd59977 |
247 | { |
2da51263 |
248 | if (myIsStopped) |
249 | { |
250 | if (myIsThreadOnly) |
251 | GetThreadCPU (myStartCpuUser, myStartCpuSys); |
7fd59977 |
252 | else |
2da51263 |
253 | GetProcessCPU (myStartCpuUser, myStartCpuSys); |
270a5d4e |
254 | |
2da51263 |
255 | myIsStopped = Standard_False; |
270a5d4e |
256 | } |
7fd59977 |
257 | } |
258 | |
259 | //======================================================================= |
260 | //function : Show |
270a5d4e |
261 | //purpose : |
7fd59977 |
262 | //======================================================================= |
7e7bbb3a |
263 | void OSD_Chronometer::Show() const |
7fd59977 |
264 | { |
2da51263 |
265 | Show (std::cout); |
7fd59977 |
266 | } |
267 | |
268 | //======================================================================= |
269 | //function : Show |
270a5d4e |
270 | //purpose : |
7fd59977 |
271 | //======================================================================= |
2da51263 |
272 | void OSD_Chronometer::Show (Standard_OStream& theOStream) const |
7fd59977 |
273 | { |
2da51263 |
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); |
7fd59977 |
280 | } |
281 | |
282 | //======================================================================= |
283 | //function : Show |
2da51263 |
284 | //purpose : |
7fd59977 |
285 | //======================================================================= |
2da51263 |
286 | void OSD_Chronometer::Show (Standard_Real& theUserSec, Standard_Real& theSystemSec) const |
7fd59977 |
287 | { |
2da51263 |
288 | theUserSec = myCumulCpuUser; |
289 | theSystemSec = myCumulCpuSys; |
290 | if (myIsStopped) |
7e7bbb3a |
291 | { |
292 | return; |
293 | } |
294 | |
295 | Standard_Real aCurrUser, aCurrSys; |
2da51263 |
296 | if (myIsThreadOnly) |
7e7bbb3a |
297 | GetThreadCPU (aCurrUser, aCurrSys); |
298 | else |
299 | GetProcessCPU (aCurrUser, aCurrSys); |
7fd59977 |
300 | |
2da51263 |
301 | theUserSec += aCurrUser - myStartCpuUser; |
302 | theSystemSec += aCurrSys - myStartCpuSys; |
7e7bbb3a |
303 | } |