1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-09-13 14:27:08 +03:00

0029349: Foundation Classes, OSD_Timer - add missing theThisThreadOnly constructor option available in OSD_Chronometer

OSD_Timer constructor now has theThisThreadOnly option passed to OSD_Chronometer,
which is FALSE by default.

OSD_Chronometer now provides methods ::UserTimeCPU(), ::SystemTimeCPU() allowing
to fetch CPU times without akward syntax through overloaded ::Show().
This commit is contained in:
kgv
2017-11-28 08:42:40 +03:00
committed by bugmaster
parent 18d8e3e794
commit 2da5126371
4 changed files with 155 additions and 201 deletions

View File

@@ -18,33 +18,26 @@
#include <OSD_Chronometer.hxx>
#include <Standard_Stream.hxx>
// ====================== PLATFORM-SPECIFIC PART ========================
#ifndef _WIN32
//---------- Systemes autres que WNT : ----------------------------------
#include <sys/times.h>
#include <unistd.h>
#ifdef SOLARIS
# include <sys/resource.h>
#include <sys/resource.h>
#endif
//=======================================================================
//Selon les plateformes on doit avoir le nombre de clicks par secondes
//qui est l unite de mesure du temps.
//=======================================================================
#ifndef sysconf
# define _sysconf sysconf
#define _sysconf sysconf
#endif
#if defined(DECOSF1)
# include <time.h>
#include <time.h>
#endif
# ifndef CLK_TCK
# define CLK_TCK CLOCKS_PER_SEC
# endif
#ifndef CLK_TCK
#define CLK_TCK CLOCKS_PER_SEC
#endif
#if (defined(__APPLE__))
#include <mach/task.h>
@@ -55,7 +48,8 @@
//function : GetProcessCPU
//purpose :
//=======================================================================
void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
void OSD_Chronometer::GetProcessCPU (Standard_Real& theUserSeconds,
Standard_Real& theSystemSeconds)
{
#if defined(__linux__) || defined(__FreeBSD__) || defined(__ANDROID__) || defined(__QNX__)
static const long aCLK_TCK = sysconf(_SC_CLK_TCK);
@@ -63,11 +57,11 @@ void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real&
static const long aCLK_TCK = CLK_TCK;
#endif
tms CurrentTMS;
times (&CurrentTMS);
tms aCurrentTMS;
times (&aCurrentTMS);
UserSeconds = (Standard_Real)CurrentTMS.tms_utime / aCLK_TCK;
SystemSeconds = (Standard_Real)CurrentTMS.tms_stime / aCLK_TCK;
theUserSeconds = (Standard_Real)aCurrentTMS.tms_utime / aCLK_TCK;
theSystemSeconds = (Standard_Real)aCurrentTMS.tms_stime / aCLK_TCK;
}
//=======================================================================
@@ -109,8 +103,6 @@ void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
#else
//---------------------------- Systeme WNT --------------------------------
#include <windows.h>
//=======================================================================
@@ -133,16 +125,17 @@ static inline __int64 EncodeFILETIME (PFILETIME pFt)
//function : GetProcessCPU
//purpose :
//=======================================================================
void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
void OSD_Chronometer::GetProcessCPU (Standard_Real& theUserSeconds,
Standard_Real& theSystemSeconds)
{
#ifndef OCCT_UWP
FILETIME ftStart, ftExit, ftKernel, ftUser;
::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser);
UserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
theUserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
theSystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
#else
UserSeconds = 0.0;
SystemSeconds = 0.0;
theUserSeconds = 0.0;
theSystemSeconds = 0.0;
#endif
}
@@ -150,33 +143,35 @@ void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real&
//function : GetThreadCPU
//purpose :
//=======================================================================
void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
void OSD_Chronometer::GetThreadCPU (Standard_Real& theUserSeconds,
Standard_Real& theSystemSeconds)
{
#ifndef OCCT_UWP
FILETIME ftStart, ftExit, ftKernel, ftUser;
::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser);
UserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
theUserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
theSystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
#else
UserSeconds = 0.0;
SystemSeconds = 0.0;
theUserSeconds = 0.0;
theSystemSeconds = 0.0;
#endif
}
#endif /* _WIN32 */
// ====================== PLATFORM-INDEPENDENT PART ========================
//=======================================================================
//function : OSD_Chronometer
//purpose :
//=======================================================================
OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly)
OSD_Chronometer::OSD_Chronometer (Standard_Boolean theThisThreadOnly)
: myStartCpuUser (0.0),
myStartCpuSys (0.0),
myCumulCpuUser (0.0),
myCumulCpuSys (0.0),
myIsStopped (Standard_True),
myIsThreadOnly (theThisThreadOnly)
{
ThreadOnly = ThisThreadOnly;
Start_user = Start_sys = 0.;
Cumul_user = Cumul_sys = 0.;
Stopped = Standard_True;
//
}
//=======================================================================
@@ -193,9 +188,9 @@ OSD_Chronometer::~OSD_Chronometer()
//=======================================================================
void OSD_Chronometer::Reset ()
{
Stopped = Standard_True;
Start_user = Start_sys = 0.;
Cumul_user = Cumul_sys = 0.;
myIsStopped = Standard_True;
myStartCpuUser = myStartCpuSys = 0.;
myCumulCpuUser = myCumulCpuSys = 0.;
}
@@ -213,21 +208,21 @@ void OSD_Chronometer::Restart ()
//function : Stop
//purpose :
//=======================================================================
void OSD_Chronometer::Stop ()
void OSD_Chronometer::Stop()
{
if ( !Stopped ) {
if (!myIsStopped)
{
Standard_Real Curr_user, Curr_sys;
if ( ThreadOnly )
if (myIsThreadOnly)
GetThreadCPU (Curr_user, Curr_sys);
else
GetProcessCPU (Curr_user, Curr_sys);
Cumul_user += Curr_user - Start_user;
Cumul_sys += Curr_sys - Start_sys;
myCumulCpuUser += Curr_user - myStartCpuUser;
myCumulCpuSys += Curr_sys - myStartCpuSys;
Stopped = Standard_True;
myIsStopped = Standard_True;
}
// else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush;
}
//=======================================================================
@@ -236,15 +231,15 @@ void OSD_Chronometer::Stop ()
//=======================================================================
void OSD_Chronometer::Start ()
{
if ( Stopped ) {
if ( ThreadOnly )
GetThreadCPU (Start_user, Start_sys);
if (myIsStopped)
{
if (myIsThreadOnly)
GetThreadCPU (myStartCpuUser, myStartCpuSys);
else
GetProcessCPU (Start_user, Start_sys);
GetProcessCPU (myStartCpuUser, myStartCpuSys);
Stopped = Standard_False;
myIsStopped = Standard_False;
}
// else cerr << "WARNING: OSD_Chronometer already running !\n" << flush;
}
//=======================================================================
@@ -253,76 +248,42 @@ void OSD_Chronometer::Start ()
//=======================================================================
void OSD_Chronometer::Show() const
{
Show (cout);
Show (std::cout);
}
//=======================================================================
//function : Show
//purpose :
//=======================================================================
void OSD_Chronometer::Show (Standard_OStream& os) const
void OSD_Chronometer::Show (Standard_OStream& theOStream) const
{
Standard_Real aCumulUserSec = Cumul_user;
Standard_Real aCumulSysSec = Cumul_sys;
if (!Stopped)
{
Standard_Real aCurrUser, aCurrSys;
if (ThreadOnly)
GetThreadCPU (aCurrUser, aCurrSys);
else
GetProcessCPU (aCurrUser, aCurrSys);
aCumulUserSec += aCurrUser - Start_user;
aCumulSysSec += aCurrSys - Start_sys;
}
std::streamsize prec = os.precision (12);
os << "CPU user time: " << aCumulUserSec << " seconds " << endl;
os << "CPU system time: " << aCumulSysSec << " seconds " << endl;
os.precision (prec);
Standard_Real aCumulUserSec = 0.0, aCumulSysSec = 0.0;
Show (aCumulUserSec, aCumulSysSec);
std::streamsize prec = theOStream.precision (12);
theOStream << "CPU user time: " << aCumulUserSec << " seconds\n";
theOStream << "CPU system time: " << aCumulSysSec << " seconds\n";
theOStream.precision (prec);
}
//=======================================================================
//function : Show
//purpose : Returns cpu user time
//purpose :
//=======================================================================
void OSD_Chronometer::Show (Standard_Real& theUserSec) const
void OSD_Chronometer::Show (Standard_Real& theUserSec, Standard_Real& theSystemSec) const
{
theUserSec = Cumul_user;
if (Stopped)
theUserSec = myCumulCpuUser;
theSystemSec = myCumulCpuSys;
if (myIsStopped)
{
return;
}
Standard_Real aCurrUser, aCurrSys;
if (ThreadOnly)
if (myIsThreadOnly)
GetThreadCPU (aCurrUser, aCurrSys);
else
GetProcessCPU (aCurrUser, aCurrSys);
theUserSec += aCurrUser - Start_user;
}
//=======================================================================
//function : Show
//purpose : Returns both user and system cpu times
//=======================================================================
void OSD_Chronometer::Show (Standard_Real& theUserSec,
Standard_Real& theSystemSec) const
{
theUserSec = Cumul_user;
theSystemSec = Cumul_sys;
if (Stopped)
{
return;
}
Standard_Real aCurrUser, aCurrSys;
if (ThreadOnly)
GetThreadCPU (aCurrUser, aCurrSys);
else
GetProcessCPU (aCurrUser, aCurrSys);
theUserSec += aCurrUser - Start_user;
theSystemSec += aCurrSys - Start_sys;
theUserSec += aCurrUser - myStartCpuUser;
theSystemSec += aCurrSys - myStartCpuSys;
}