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().
#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>
//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);
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;
}
//=======================================================================
#else
-//---------------------------- Systeme WNT --------------------------------
-
#include <windows.h>
//=======================================================================
//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
}
//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;
+ //
}
//=======================================================================
//=======================================================================
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.;
}
//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;
}
//=======================================================================
//=======================================================================
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;
}
//=======================================================================
//=======================================================================
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
-//=======================================================================
-void OSD_Chronometer::Show (Standard_Real& theUserSec) const
-{
- theUserSec = Cumul_user;
- if (Stopped)
- {
- return;
- }
-
- Standard_Real aCurrUser, aCurrSys;
- if (ThreadOnly)
- GetThreadCPU (aCurrUser, aCurrSys);
- else
- GetProcessCPU (aCurrUser, aCurrSys);
-
- theUserSec += aCurrUser - Start_user;
-}
-
-//=======================================================================
-//function : Show
-//purpose : Returns both user and system cpu times
+//purpose :
//=======================================================================
-void OSD_Chronometer::Show (Standard_Real& theUserSec,
- Standard_Real& theSystemSec) const
+void OSD_Chronometer::Show (Standard_Real& theUserSec, Standard_Real& theSystemSec) const
{
- theUserSec = Cumul_user;
- theSystemSec = Cumul_sys;
- 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;
- theSystemSec += aCurrSys - Start_sys;
+ theUserSec += aCurrUser - myStartCpuUser;
+ theSystemSec += aCurrSys - myStartCpuSys;
}
#include <Standard.hxx>
#include <Standard_DefineAlloc.hxx>
#include <Standard_Handle.hxx>
-
-#include <Standard_Boolean.hxx>
#include <Standard_Real.hxx>
#include <Standard_OStream.hxx>
-
//! This class measures CPU time (both user and system) consumed
//! by current process or thread. The chronometer can be started
//! and stopped multiple times, and measures cumulative time.
DEFINE_STANDARD_ALLOC
-
//! Initializes a stopped Chronometer.
//!
//! If ThisThreadOnly is True, measured CPU time will account
//! time of the current thread only; otherwise CPU of the
//! process (all threads, and completed children) is measured.
- Standard_EXPORT OSD_Chronometer(const Standard_Boolean ThisThreadOnly = Standard_False);
+ Standard_EXPORT OSD_Chronometer (Standard_Boolean theThisThreadOnly = Standard_False);
+
+ //! Destructor.
Standard_EXPORT virtual ~OSD_Chronometer();
//! Return true if timer has been started.
- Standard_Boolean IsStarted() const { return !Stopped; }
+ Standard_Boolean IsStarted() const { return !myIsStopped; }
//! Stops and Reinitializes the Chronometer.
Standard_EXPORT virtual void Reset();
//! Shows the current CPU user and system time on the output
//! stream <os>.
//! The chronometer can be running (laps Time) or stopped.
- Standard_EXPORT virtual void Show (Standard_OStream& os) const;
-
+ Standard_EXPORT virtual void Show (Standard_OStream& theOStream) const;
+
+ //! Returns the current CPU user time in seconds.
+ //! The chronometer can be running (laps Time) or stopped.
+ Standard_Real UserTimeCPU() const
+ {
+ Standard_Real aUserTime = 0.0, aSysTime = 0.0;
+ Show (aUserTime, aSysTime);
+ return aUserTime;
+ }
+
+ //! Returns the current CPU system time in seconds.
+ //! The chronometer can be running (laps Time) or stopped.
+ Standard_Real SystemTimeCPU() const
+ {
+ Standard_Real aUserTime = 0.0, aSysTime = 0.0;
+ Show (aUserTime, aSysTime);
+ return aSysTime;
+ }
+
//! Returns the current CPU user time in a variable.
//! The chronometer can be running (laps Time) or stopped.
- Standard_EXPORT void Show (Standard_Real& theUserSeconds) const;
+ void Show (Standard_Real& theUserSeconds) const { theUserSeconds = UserTimeCPU(); }
//! Returns the current CPU user and system time in variables.
//! The chronometer can be running (laps Time) or stopped.
- Standard_EXPORT void Show (Standard_Real& theUserSeconds, Standard_Real& theSystemSeconds) const;
-
+ Standard_EXPORT void Show (Standard_Real& theUserSec, Standard_Real& theSystemSec) const;
+
+public:
+
//! Returns CPU time (user and system) consumed by the current
//! process since its start, in seconds. The actual precision of
//! the measurement depends on granularity provided by the system,
//! differently on different platforms and CPUs.
Standard_EXPORT static void GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds);
-
-
-
protected:
-
-
- Standard_Boolean Stopped;
-
-
-private:
-
-
-
- Standard_Boolean ThreadOnly;
- Standard_Real Start_user;
- Standard_Real Start_sys;
- Standard_Real Cumul_user;
- Standard_Real Cumul_sys;
-
+ Standard_Real myStartCpuUser;
+ Standard_Real myStartCpuSys;
+ Standard_Real myCumulCpuUser;
+ Standard_Real myCumulCpuSys;
+ Standard_Boolean myIsStopped;
+ Standard_Boolean myIsThreadOnly;
};
-
-
-
-
-
-
#endif // _OSD_Chronometer_HeaderFile
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
-// Update: J.P. TIRAULT Sep,1993
-// On heterogeneous platforms we need to use the
-// system call gettimeofday. This function is portable and give us
-// elapsed time in seconds and microseconds.
-
#include <OSD_Timer.hxx>
#ifndef _WIN32
-//---------- No Windows NT Systems ----------------------------------
-
#include <sys/time.h>
//=======================================================================
}
#else
-//------------------- Windows NT ------------------
#include <windows.h>
#endif /* _WIN32 */
-// ====================== PLATFORM-INDEPENDENT PART ========================
-
-//=======================================================================
-//function : OSD_Timer
-//purpose :
-//=======================================================================
-
-OSD_Timer::OSD_Timer()
+namespace
{
- TimeStart = TimeCumul = 0.;
+ //! Auxiliary function splits elapsed time in seconds into Hours, Minutes and Seconds.
+ //! @param theTimeSec [in] elapsed time in seconds
+ //! @param theHours [out] clamped elapsed hours
+ //! @param theMinutes [out] clamped elapsed minutes within range [0, 59]
+ //! @param theSeconds [out] clamped elapsed seconds within range [0, 60)
+ static void timeToHoursMinutesSeconds (Standard_Real theTimeSec,
+ Standard_Integer& theHours,
+ Standard_Integer& theMinutes,
+ Standard_Real& theSeconds)
+ {
+ Standard_Integer aSec = (Standard_Integer)theTimeSec;
+ theHours = aSec / 3600;
+ theMinutes = (aSec - theHours * 3600) / 60;
+ theSeconds = theTimeSec - theHours * 3600 - theMinutes * 60;
+ }
}
//=======================================================================
-//function : Compute
-//purpose : Calcul les Heures,Minutes,Secondes,Millisecondes a partir
-// de deux variables input qui sont:
-// TimeCumulInt : Contient un periode de temps exprimee en secondes,
-// MicroCumulInt: Contient cette meme periode exprimee en
-// microsecondes.
+//function : OSD_Timer
+//purpose :
//=======================================================================
-static void Compute (Standard_Real Time,
- Standard_Integer& heure,
- Standard_Integer& minut,
- Standard_Real& second)
+OSD_Timer::OSD_Timer (Standard_Boolean theThisThreadOnly)
+: OSD_Chronometer (theThisThreadOnly),
+ myTimeStart (0.0),
+ myTimeCumul (0.0)
{
- Standard_Integer sec = (Standard_Integer)Time;
- heure = sec / 3600;
- minut = (sec - heure * 3600) / 60;
- second = Time - heure * 3600 - minut * 60;
+ //
}
//=======================================================================
void OSD_Timer::Reset (const Standard_Real theTimeElapsedSec)
{
- TimeStart = 0.0;
- TimeCumul = theTimeElapsedSec;
+ myTimeStart = 0.0;
+ myTimeCumul = theTimeElapsedSec;
OSD_Chronometer::Reset();
}
void OSD_Timer::Reset ()
{
- TimeStart = TimeCumul = 0.;
+ myTimeStart = myTimeCumul = 0.0;
OSD_Chronometer::Reset();
}
void OSD_Timer::Restart ()
{
- TimeStart = GetWallClockTime();
- TimeCumul = 0.;
+ myTimeStart = GetWallClockTime();
+ myTimeCumul = 0.0;
OSD_Chronometer::Restart();
}
Standard_Real OSD_Timer::ElapsedTime() const
{
- if (Stopped)
+ if (myIsStopped)
{
- return TimeCumul;
+ return myTimeCumul;
}
- return TimeCumul + GetWallClockTime() - TimeStart;
+ return myTimeCumul + GetWallClockTime() - myTimeStart;
}
//=======================================================================
Standard_Integer& theHours,
Standard_Real& theCPUtime) const
{
- const Standard_Real aTimeCumul = Stopped
- ? TimeCumul
- : TimeCumul + GetWallClockTime() - TimeStart;
- Compute (aTimeCumul, theHours, theMinutes, theSeconds);
+ const Standard_Real aTimeCumul = myIsStopped
+ ? myTimeCumul
+ : myTimeCumul + GetWallClockTime() - myTimeStart;
+ timeToHoursMinutesSeconds (aTimeCumul, theHours, theMinutes, theSeconds);
OSD_Chronometer::Show (theCPUtime);
}
//purpose :
//=======================================================================
-void OSD_Timer::Show (Standard_OStream& os) const
+void OSD_Timer::Show (Standard_OStream& theOStream) const
{
- const Standard_Real aTimeCumul = Stopped
- ? TimeCumul
- : TimeCumul + GetWallClockTime() - TimeStart;
+ const Standard_Real aTimeCumul = ElapsedTime();
Standard_Integer anHours, aMinutes;
Standard_Real aSeconds;
- Compute (aTimeCumul, anHours, aMinutes, aSeconds);
-
- std::streamsize prec = os.precision (12);
- os << "Elapsed time: " << anHours << " Hours " <<
- aMinutes << " Minutes " <<
- aSeconds << " Seconds " << endl;
- OSD_Chronometer::Show(os);
- os.precision (prec);
+ timeToHoursMinutesSeconds (aTimeCumul, anHours, aMinutes, aSeconds);
+
+ std::streamsize prec = theOStream.precision (12);
+ theOStream << "Elapsed time: " << anHours << " Hours " <<
+ aMinutes << " Minutes " <<
+ aSeconds << " Seconds\n";
+ OSD_Chronometer::Show (theOStream);
+ theOStream.precision (prec);
}
//=======================================================================
void OSD_Timer::Stop ()
{
- if (!Stopped) {
- TimeCumul += GetWallClockTime () - TimeStart;
+ if (!myIsStopped)
+ {
+ myTimeCumul += GetWallClockTime() - myTimeStart;
OSD_Chronometer::Stop();
}
-// else cout << "WARNING: OSD_Timer already Stopped !\n";
}
//=======================================================================
void OSD_Timer::Start()
{
- if (Stopped) {
- TimeStart = GetWallClockTime();
+ if (myIsStopped)
+ {
+ myTimeStart = GetWallClockTime();
OSD_Chronometer::Start();
}
-// else cout << "WARNING: OSD_Timer already Running !\n";
}
DEFINE_STANDARD_ALLOC
//! Builds a Chronometer initialized and stopped.
- Standard_EXPORT OSD_Timer();
+ //! @param theThisThreadOnly when TRUE, measured CPU time will account time of the current thread only;
+ //! otherwise CPU of the process (all threads, and completed children) is measured;
+ //! this flag does NOT affect ElapsedTime() value, only values returned by OSD_Chronometer
+ Standard_EXPORT OSD_Timer (Standard_Boolean theThisThreadOnly = Standard_False);
//! Stops and reinitializes the timer with specified elapsed time.
Standard_EXPORT void Reset (const Standard_Real theTimeElapsedSec);
private:
- Standard_Real TimeStart;
- Standard_Real TimeCumul;
+ Standard_Real myTimeStart;
+ Standard_Real myTimeCumul;
};