0025215: Porting to Android - fix minor issues
[occt.git] / src / OSD / OSD_Chronometer.cxx
CommitLineData
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
17#include <OSD_Chronometer.ixx>
18#include <Standard_Stream.hxx>
19
20// ====================== PLATFORM-SPECIFIC PART ========================
21
22#ifndef WNT
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//=======================================================================
58void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
59{
9bf6baed 60#if defined(LIN) || defined(linux) || defined(__FreeBSD__) || defined(__ANDROID__)
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 77void 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 }
9bf6baed 90#elif (defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)) || defined(__ANDROID__)
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 122static 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//=======================================================================
137void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
138{
139 FILETIME ftStart, ftExit, ftKernel, ftUser;
140 ::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser);
141 UserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
142 SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
143}
144
145//=======================================================================
146//function : GetThreadCPU
270a5d4e 147//purpose :
7fd59977 148//=======================================================================
149void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
150{
151 FILETIME ftStart, ftExit, ftKernel, ftUser;
152 ::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser);
153 UserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
154 SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
155}
156
157#endif /* WNT */
158
159// ====================== PLATFORM-INDEPENDENT PART ========================
160
161//=======================================================================
162//function : OSD_Chronometer
270a5d4e 163//purpose :
7fd59977 164//=======================================================================
165OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly)
166{
167 ThreadOnly = ThisThreadOnly;
168 Start_user = Start_sys = 0.;
169 Cumul_user = Cumul_sys = 0.;
270a5d4e 170 Stopped = Standard_True;
7fd59977 171}
172
173//=======================================================================
174//function : Destroy
270a5d4e 175//purpose :
7fd59977 176//=======================================================================
270a5d4e 177void OSD_Chronometer::Destroy ()
7fd59977 178{
179}
180
181//=======================================================================
182//function : Reset
270a5d4e 183//purpose :
7fd59977 184//=======================================================================
185void OSD_Chronometer::Reset ()
186{
187 Stopped = Standard_True;
188 Start_user = Start_sys = 0.;
189 Cumul_user = Cumul_sys = 0.;
190}
191
192//=======================================================================
193//function : Stop
270a5d4e 194//purpose :
7fd59977 195//=======================================================================
270a5d4e 196void OSD_Chronometer::Stop ()
7fd59977 197{
198 if ( !Stopped ) {
199 Standard_Real Curr_user, Curr_sys;
200 if ( ThreadOnly )
201 GetThreadCPU (Curr_user, Curr_sys);
202 else
203 GetProcessCPU (Curr_user, Curr_sys);
204
205 Cumul_user += Curr_user - Start_user;
206 Cumul_sys += Curr_sys - Start_sys;
207
270a5d4e 208 Stopped = Standard_True;
209 }
7fd59977 210// else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush;
211}
212
213//=======================================================================
214//function : Start
270a5d4e 215//purpose :
7fd59977 216//=======================================================================
270a5d4e 217void OSD_Chronometer::Start ()
7fd59977 218{
219 if ( Stopped ) {
220 if ( ThreadOnly )
221 GetThreadCPU (Start_user, Start_sys);
222 else
223 GetProcessCPU (Start_user, Start_sys);
270a5d4e 224
7fd59977 225 Stopped = Standard_False;
270a5d4e 226 }
7fd59977 227// else cerr << "WARNING: OSD_Chronometer already running !\n" << flush;
228}
229
230//=======================================================================
231//function : Show
270a5d4e 232//purpose :
7fd59977 233//=======================================================================
234void OSD_Chronometer::Show ()
235{
236 Show (cout);
237}
238
239//=======================================================================
240//function : Show
270a5d4e 241//purpose :
7fd59977 242//=======================================================================
243void OSD_Chronometer::Show (Standard_OStream& os)
244{
245 Standard_Boolean StopSav = Stopped;
246 if (!StopSav) Stop();
270a5d4e 247 std::streamsize prec = os.precision (12);
7fd59977 248 os << "CPU user time: " << Cumul_user << " seconds " << endl;
249 os << "CPU system time: " << Cumul_sys << " seconds " << endl;
250 os.precision (prec);
251 if (!StopSav) Start();
252}
253
254//=======================================================================
255//function : Show
256//purpose : Returns cpu user time
257//=======================================================================
258void OSD_Chronometer::Show (Standard_Real& second)
259{
260 Standard_Boolean StopSav = Stopped;
261 if (!StopSav) Stop();
270a5d4e 262 second = Cumul_user;
7fd59977 263 if (!StopSav) Start();
264}
265//=======================================================================
266//function : Show
267//purpose : Returns both user and system cpu times
268//=======================================================================
269void OSD_Chronometer::Show (Standard_Real& user,
270 Standard_Real& system)
271{
272 Standard_Boolean StopSav = Stopped;
273 if (!StopSav) Stop();
270a5d4e 274 user = Cumul_user;
275 system = Cumul_sys;
7fd59977 276 if (!StopSav) Start();
277}
278