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 | { |
d8d01f6e |
60 | #if defined(__linux__) || 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 | |
202 | //======================================================================= |
203 | //function : Stop |
270a5d4e |
204 | //purpose : |
7fd59977 |
205 | //======================================================================= |
270a5d4e |
206 | void OSD_Chronometer::Stop () |
7fd59977 |
207 | { |
208 | if ( !Stopped ) { |
209 | Standard_Real Curr_user, Curr_sys; |
210 | if ( ThreadOnly ) |
211 | GetThreadCPU (Curr_user, Curr_sys); |
212 | else |
213 | GetProcessCPU (Curr_user, Curr_sys); |
214 | |
215 | Cumul_user += Curr_user - Start_user; |
216 | Cumul_sys += Curr_sys - Start_sys; |
217 | |
270a5d4e |
218 | Stopped = Standard_True; |
219 | } |
7fd59977 |
220 | // else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush; |
221 | } |
222 | |
223 | //======================================================================= |
224 | //function : Start |
270a5d4e |
225 | //purpose : |
7fd59977 |
226 | //======================================================================= |
270a5d4e |
227 | void OSD_Chronometer::Start () |
7fd59977 |
228 | { |
229 | if ( Stopped ) { |
230 | if ( ThreadOnly ) |
231 | GetThreadCPU (Start_user, Start_sys); |
232 | else |
233 | GetProcessCPU (Start_user, Start_sys); |
270a5d4e |
234 | |
7fd59977 |
235 | Stopped = Standard_False; |
270a5d4e |
236 | } |
7fd59977 |
237 | // else cerr << "WARNING: OSD_Chronometer already running !\n" << flush; |
238 | } |
239 | |
240 | //======================================================================= |
241 | //function : Show |
270a5d4e |
242 | //purpose : |
7fd59977 |
243 | //======================================================================= |
7e7bbb3a |
244 | void OSD_Chronometer::Show() const |
7fd59977 |
245 | { |
246 | Show (cout); |
247 | } |
248 | |
249 | //======================================================================= |
250 | //function : Show |
270a5d4e |
251 | //purpose : |
7fd59977 |
252 | //======================================================================= |
7e7bbb3a |
253 | void OSD_Chronometer::Show (Standard_OStream& os) const |
7fd59977 |
254 | { |
7e7bbb3a |
255 | Standard_Real aCumulUserSec = Cumul_user; |
256 | Standard_Real aCumulSysSec = Cumul_sys; |
257 | if (!Stopped) |
258 | { |
259 | Standard_Real aCurrUser, aCurrSys; |
260 | if (ThreadOnly) |
261 | GetThreadCPU (aCurrUser, aCurrSys); |
262 | else |
263 | GetProcessCPU (aCurrUser, aCurrSys); |
264 | |
265 | aCumulUserSec += aCurrUser - Start_user; |
266 | aCumulSysSec += aCurrSys - Start_sys; |
267 | } |
268 | |
270a5d4e |
269 | std::streamsize prec = os.precision (12); |
7e7bbb3a |
270 | os << "CPU user time: " << aCumulUserSec << " seconds " << endl; |
271 | os << "CPU system time: " << aCumulSysSec << " seconds " << endl; |
7fd59977 |
272 | os.precision (prec); |
7fd59977 |
273 | } |
274 | |
275 | //======================================================================= |
276 | //function : Show |
277 | //purpose : Returns cpu user time |
278 | //======================================================================= |
7e7bbb3a |
279 | void OSD_Chronometer::Show (Standard_Real& theUserSec) const |
7fd59977 |
280 | { |
7e7bbb3a |
281 | theUserSec = Cumul_user; |
282 | if (Stopped) |
283 | { |
284 | return; |
285 | } |
286 | |
287 | Standard_Real aCurrUser, aCurrSys; |
288 | if (ThreadOnly) |
289 | GetThreadCPU (aCurrUser, aCurrSys); |
290 | else |
291 | GetProcessCPU (aCurrUser, aCurrSys); |
292 | |
293 | theUserSec += aCurrUser - Start_user; |
7fd59977 |
294 | } |
7e7bbb3a |
295 | |
7fd59977 |
296 | //======================================================================= |
297 | //function : Show |
298 | //purpose : Returns both user and system cpu times |
299 | //======================================================================= |
7e7bbb3a |
300 | void OSD_Chronometer::Show (Standard_Real& theUserSec, |
301 | Standard_Real& theSystemSec) const |
7fd59977 |
302 | { |
7e7bbb3a |
303 | theUserSec = Cumul_user; |
304 | theSystemSec = Cumul_sys; |
305 | if (Stopped) |
306 | { |
307 | return; |
308 | } |
309 | |
310 | Standard_Real aCurrUser, aCurrSys; |
311 | if (ThreadOnly) |
312 | GetThreadCPU (aCurrUser, aCurrSys); |
313 | else |
314 | GetProcessCPU (aCurrUser, aCurrSys); |
7fd59977 |
315 | |
7e7bbb3a |
316 | theUserSec += aCurrUser - Start_user; |
317 | theSystemSec += aCurrSys - Start_sys; |
318 | } |