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 | |
18 | #include <OSD_Chronometer.hxx> |
7fd59977 |
19 | #include <Standard_Stream.hxx> |
20 | |
21 | // ====================== PLATFORM-SPECIFIC PART ======================== |
57c28b61 |
22 | #ifndef _WIN32 |
7fd59977 |
23 | |
24 | //---------- Systemes autres que WNT : ---------------------------------- |
25 | |
03155c18 |
26 | #include <sys/times.h> |
27 | #include <unistd.h> |
7fd59977 |
28 | |
29 | #ifdef SOLARIS |
30 | # include <sys/resource.h> |
31 | #endif |
32 | |
33 | //======================================================================= |
34 | //Selon les plateformes on doit avoir le nombre de clicks par secondes |
270a5d4e |
35 | //qui est l unite de mesure du temps. |
7fd59977 |
36 | //======================================================================= |
270a5d4e |
37 | #ifndef sysconf |
7fd59977 |
38 | # define _sysconf sysconf |
39 | #endif |
40 | |
03155c18 |
41 | #if defined(DECOSF1) |
7fd59977 |
42 | # include <time.h> |
43 | #endif |
44 | |
45 | # ifndef CLK_TCK |
46 | # define CLK_TCK CLOCKS_PER_SEC |
47 | # endif |
48 | |
270a5d4e |
49 | #if (defined(__APPLE__)) |
50 | #include <mach/task.h> |
51 | #include <mach/mach.h> |
52 | #endif |
53 | |
7fd59977 |
54 | //======================================================================= |
55 | //function : GetProcessCPU |
270a5d4e |
56 | //purpose : |
7fd59977 |
57 | //======================================================================= |
58 | void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds) |
59 | { |
c381fda2 |
60 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(__QNX__) |
7fd59977 |
61 | static const long aCLK_TCK = sysconf(_SC_CLK_TCK); |
62 | #else |
63 | static const long aCLK_TCK = CLK_TCK; |
64 | #endif |
270a5d4e |
65 | |
7fd59977 |
66 | tms CurrentTMS; |
67 | times (&CurrentTMS); |
68 | |
69 | UserSeconds = (Standard_Real)CurrentTMS.tms_utime / aCLK_TCK; |
70 | SystemSeconds = (Standard_Real)CurrentTMS.tms_stime / aCLK_TCK; |
71 | } |
72 | |
73 | //======================================================================= |
74 | //function : GetThreadCPU |
270a5d4e |
75 | //purpose : |
7fd59977 |
76 | //======================================================================= |
270a5d4e |
77 | void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds, |
78 | Standard_Real& theSystemSeconds) |
7fd59977 |
79 | { |
270a5d4e |
80 | theUserSeconds = theSystemSeconds = 0.0; |
81 | #if (defined(__APPLE__)) |
82 | struct task_thread_times_info aTaskInfo; |
83 | mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT; |
84 | if (KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, |
85 | (task_info_t )&aTaskInfo, &aTaskInfoCount)) |
86 | { |
87 | theUserSeconds = Standard_Real(aTaskInfo.user_time.seconds) + 0.000001 * aTaskInfo.user_time.microseconds; |
88 | theSystemSeconds = Standard_Real(aTaskInfo.system_time.seconds) + 0.000001 * aTaskInfo.system_time.microseconds; |
89 | } |
d8d01f6e |
90 | #elif (defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)) || defined(__ANDROID__) || defined(__QNX__) |
7fd59977 |
91 | // on Linux, only user times are available for threads via clock_gettime() |
92 | struct timespec t; |
270a5d4e |
93 | if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &t)) |
7fd59977 |
94 | { |
270a5d4e |
95 | theUserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec; |
7fd59977 |
96 | } |
97 | #elif defined(SOLARIS) |
98 | // on Solaris, both user and system times are available as LWP times |
99 | struct rusage rut; |
270a5d4e |
100 | if (!getrusage (RUSAGE_LWP, &rut)) |
7fd59977 |
101 | { |
270a5d4e |
102 | theUserSeconds = rut.ru_utime.tv_sec + 0.000001 * rut.ru_utime.tv_usec; |
103 | theSystemSeconds = rut.ru_stime.tv_sec + 0.000001 * rut.ru_stime.tv_usec; |
7fd59977 |
104 | } |
105 | #else |
270a5d4e |
106 | #pragma error "OS is not supported yet; code to be ported" |
107 | #endif |
7fd59977 |
108 | } |
109 | |
110 | #else |
111 | |
112 | //---------------------------- Systeme WNT -------------------------------- |
113 | |
114 | #define STRICT |
115 | #include <windows.h> |
116 | |
117 | //======================================================================= |
118 | //function : EncodeFILETIME |
119 | //purpose : Encode time defined by FILETIME structure |
120 | // (100s nanoseconds since January 1, 1601) to 64-bit integer |
121 | //======================================================================= |
270a5d4e |
122 | static inline __int64 EncodeFILETIME (PFILETIME pFt) |
7fd59977 |
123 | { |
124 | __int64 qw; |
125 | |
126 | qw = pFt -> dwHighDateTime; |
127 | qw <<= 32; |
128 | qw |= pFt -> dwLowDateTime; |
129 | |
130 | return qw; |
270a5d4e |
131 | } |
7fd59977 |
132 | |
133 | //======================================================================= |
134 | //function : GetProcessCPU |
270a5d4e |
135 | //purpose : |
7fd59977 |
136 | //======================================================================= |
137 | void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds) |
138 | { |
742cc8b0 |
139 | #ifndef OCCT_UWP |
7fd59977 |
140 | FILETIME ftStart, ftExit, ftKernel, ftUser; |
141 | ::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser); |
142 | UserSeconds = 0.0000001 * EncodeFILETIME (&ftUser); |
143 | SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel); |
742cc8b0 |
144 | #else |
145 | UserSeconds = 0.0; |
146 | SystemSeconds = 0.0; |
147 | #endif |
7fd59977 |
148 | } |
149 | |
150 | //======================================================================= |
151 | //function : GetThreadCPU |
270a5d4e |
152 | //purpose : |
7fd59977 |
153 | //======================================================================= |
154 | void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds) |
155 | { |
742cc8b0 |
156 | #ifndef OCCT_UWP |
7fd59977 |
157 | FILETIME ftStart, ftExit, ftKernel, ftUser; |
158 | ::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser); |
159 | UserSeconds = 0.0000001 * EncodeFILETIME (&ftUser); |
160 | SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel); |
742cc8b0 |
161 | #else |
162 | UserSeconds = 0.0; |
163 | SystemSeconds = 0.0; |
164 | #endif |
7fd59977 |
165 | } |
166 | |
57c28b61 |
167 | #endif /* _WIN32 */ |
7fd59977 |
168 | |
169 | // ====================== PLATFORM-INDEPENDENT PART ======================== |
170 | |
171 | //======================================================================= |
172 | //function : OSD_Chronometer |
270a5d4e |
173 | //purpose : |
7fd59977 |
174 | //======================================================================= |
175 | OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly) |
176 | { |
177 | ThreadOnly = ThisThreadOnly; |
178 | Start_user = Start_sys = 0.; |
179 | Cumul_user = Cumul_sys = 0.; |
270a5d4e |
180 | Stopped = Standard_True; |
7fd59977 |
181 | } |
182 | |
183 | //======================================================================= |
6da30ff1 |
184 | //function : ~OSD_Chronometer |
185 | //purpose : Destructor |
7fd59977 |
186 | //======================================================================= |
6da30ff1 |
187 | OSD_Chronometer::~OSD_Chronometer() |
7fd59977 |
188 | { |
189 | } |
190 | |
191 | //======================================================================= |
192 | //function : Reset |
270a5d4e |
193 | //purpose : |
7fd59977 |
194 | //======================================================================= |
195 | void OSD_Chronometer::Reset () |
196 | { |
197 | Stopped = Standard_True; |
198 | Start_user = Start_sys = 0.; |
199 | Cumul_user = Cumul_sys = 0.; |
200 | } |
201 | |
44fae8b1 |
202 | |
203 | //======================================================================= |
204 | //function : Restart |
205 | //purpose : |
206 | //======================================================================= |
207 | void OSD_Chronometer::Restart () |
208 | { |
209 | Stopped = Standard_True; |
210 | Start(); |
211 | } |
212 | |
7fd59977 |
213 | //======================================================================= |
214 | //function : Stop |
270a5d4e |
215 | //purpose : |
7fd59977 |
216 | //======================================================================= |
270a5d4e |
217 | void OSD_Chronometer::Stop () |
7fd59977 |
218 | { |
219 | if ( !Stopped ) { |
220 | Standard_Real Curr_user, Curr_sys; |
221 | if ( ThreadOnly ) |
222 | GetThreadCPU (Curr_user, Curr_sys); |
223 | else |
224 | GetProcessCPU (Curr_user, Curr_sys); |
225 | |
226 | Cumul_user += Curr_user - Start_user; |
227 | Cumul_sys += Curr_sys - Start_sys; |
228 | |
270a5d4e |
229 | Stopped = Standard_True; |
230 | } |
7fd59977 |
231 | // else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush; |
232 | } |
233 | |
234 | //======================================================================= |
235 | //function : Start |
270a5d4e |
236 | //purpose : |
7fd59977 |
237 | //======================================================================= |
270a5d4e |
238 | void OSD_Chronometer::Start () |
7fd59977 |
239 | { |
240 | if ( Stopped ) { |
241 | if ( ThreadOnly ) |
242 | GetThreadCPU (Start_user, Start_sys); |
243 | else |
244 | GetProcessCPU (Start_user, Start_sys); |
270a5d4e |
245 | |
7fd59977 |
246 | Stopped = Standard_False; |
270a5d4e |
247 | } |
7fd59977 |
248 | // else cerr << "WARNING: OSD_Chronometer already running !\n" << flush; |
249 | } |
250 | |
251 | //======================================================================= |
252 | //function : Show |
270a5d4e |
253 | //purpose : |
7fd59977 |
254 | //======================================================================= |
7e7bbb3a |
255 | void OSD_Chronometer::Show() const |
7fd59977 |
256 | { |
257 | Show (cout); |
258 | } |
259 | |
260 | //======================================================================= |
261 | //function : Show |
270a5d4e |
262 | //purpose : |
7fd59977 |
263 | //======================================================================= |
7e7bbb3a |
264 | void OSD_Chronometer::Show (Standard_OStream& os) const |
7fd59977 |
265 | { |
7e7bbb3a |
266 | Standard_Real aCumulUserSec = Cumul_user; |
267 | Standard_Real aCumulSysSec = Cumul_sys; |
268 | if (!Stopped) |
269 | { |
270 | Standard_Real aCurrUser, aCurrSys; |
271 | if (ThreadOnly) |
272 | GetThreadCPU (aCurrUser, aCurrSys); |
273 | else |
274 | GetProcessCPU (aCurrUser, aCurrSys); |
275 | |
276 | aCumulUserSec += aCurrUser - Start_user; |
277 | aCumulSysSec += aCurrSys - Start_sys; |
278 | } |
279 | |
270a5d4e |
280 | std::streamsize prec = os.precision (12); |
7e7bbb3a |
281 | os << "CPU user time: " << aCumulUserSec << " seconds " << endl; |
282 | os << "CPU system time: " << aCumulSysSec << " seconds " << endl; |
7fd59977 |
283 | os.precision (prec); |
7fd59977 |
284 | } |
285 | |
286 | //======================================================================= |
287 | //function : Show |
288 | //purpose : Returns cpu user time |
289 | //======================================================================= |
7e7bbb3a |
290 | void OSD_Chronometer::Show (Standard_Real& theUserSec) const |
7fd59977 |
291 | { |
7e7bbb3a |
292 | theUserSec = Cumul_user; |
293 | if (Stopped) |
294 | { |
295 | return; |
296 | } |
297 | |
298 | Standard_Real aCurrUser, aCurrSys; |
299 | if (ThreadOnly) |
300 | GetThreadCPU (aCurrUser, aCurrSys); |
301 | else |
302 | GetProcessCPU (aCurrUser, aCurrSys); |
303 | |
304 | theUserSec += aCurrUser - Start_user; |
7fd59977 |
305 | } |
7e7bbb3a |
306 | |
7fd59977 |
307 | //======================================================================= |
308 | //function : Show |
309 | //purpose : Returns both user and system cpu times |
310 | //======================================================================= |
7e7bbb3a |
311 | void OSD_Chronometer::Show (Standard_Real& theUserSec, |
312 | Standard_Real& theSystemSec) const |
7fd59977 |
313 | { |
7e7bbb3a |
314 | theUserSec = Cumul_user; |
315 | theSystemSec = Cumul_sys; |
316 | if (Stopped) |
317 | { |
318 | return; |
319 | } |
320 | |
321 | Standard_Real aCurrUser, aCurrSys; |
322 | if (ThreadOnly) |
323 | GetThreadCPU (aCurrUser, aCurrSys); |
324 | else |
325 | GetProcessCPU (aCurrUser, aCurrSys); |
7fd59977 |
326 | |
7e7bbb3a |
327 | theUserSec += aCurrUser - Start_user; |
328 | theSystemSec += aCurrSys - Start_sys; |
329 | } |