diff --git a/src/OSD/OSD_Chronometer.cxx b/src/OSD/OSD_Chronometer.cxx index 699e9c2e11..b2de7b0252 100644 --- a/src/OSD/OSD_Chronometer.cxx +++ b/src/OSD/OSD_Chronometer.cxx @@ -18,33 +18,26 @@ #include #include -// ====================== PLATFORM-SPECIFIC PART ======================== #ifndef _WIN32 -//---------- Systemes autres que WNT : ---------------------------------- - #include #include #ifdef SOLARIS -# include + #include #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 + #include #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 @@ -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 //======================================================================= @@ -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; } diff --git a/src/OSD/OSD_Chronometer.hxx b/src/OSD/OSD_Chronometer.hxx index 697eb20793..9753497f0d 100644 --- a/src/OSD/OSD_Chronometer.hxx +++ b/src/OSD/OSD_Chronometer.hxx @@ -20,12 +20,9 @@ #include #include #include - -#include #include #include - //! 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. @@ -40,17 +37,18 @@ public: 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(); @@ -73,16 +71,36 @@ public: //! Shows the current CPU user and system time on the output //! stream . //! 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, @@ -95,33 +113,15 @@ public: //! 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 diff --git a/src/OSD/OSD_Timer.cxx b/src/OSD/OSD_Timer.cxx index 754bc0f9cd..0b36bd6bff 100644 --- a/src/OSD/OSD_Timer.cxx +++ b/src/OSD/OSD_Timer.cxx @@ -14,17 +14,10 @@ // 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 #ifndef _WIN32 -//---------- No Windows NT Systems ---------------------------------- - #include //======================================================================= @@ -42,7 +35,6 @@ static inline Standard_Real GetWallClockTime () } #else -//------------------- Windows NT ------------------ #include @@ -69,36 +61,36 @@ static inline Standard_Real GetWallClockTime () #endif /* _WIN32 */ -// ====================== PLATFORM-INDEPENDENT PART ======================== +namespace +{ + //! 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 : OSD_Timer //purpose : //======================================================================= -OSD_Timer::OSD_Timer() +OSD_Timer::OSD_Timer (Standard_Boolean theThisThreadOnly) +: OSD_Chronometer (theThisThreadOnly), + myTimeStart (0.0), + myTimeCumul (0.0) { - TimeStart = TimeCumul = 0.; -} - -//======================================================================= -//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. -//======================================================================= - -static void Compute (Standard_Real Time, - Standard_Integer& heure, - Standard_Integer& minut, - Standard_Real& second) -{ - Standard_Integer sec = (Standard_Integer)Time; - heure = sec / 3600; - minut = (sec - heure * 3600) / 60; - second = Time - heure * 3600 - minut * 60; + // } //======================================================================= @@ -108,8 +100,8 @@ static void Compute (Standard_Real Time, void OSD_Timer::Reset (const Standard_Real theTimeElapsedSec) { - TimeStart = 0.0; - TimeCumul = theTimeElapsedSec; + myTimeStart = 0.0; + myTimeCumul = theTimeElapsedSec; OSD_Chronometer::Reset(); } @@ -120,7 +112,7 @@ void OSD_Timer::Reset (const Standard_Real theTimeElapsedSec) void OSD_Timer::Reset () { - TimeStart = TimeCumul = 0.; + myTimeStart = myTimeCumul = 0.0; OSD_Chronometer::Reset(); } @@ -131,8 +123,8 @@ void OSD_Timer::Reset () void OSD_Timer::Restart () { - TimeStart = GetWallClockTime(); - TimeCumul = 0.; + myTimeStart = GetWallClockTime(); + myTimeCumul = 0.0; OSD_Chronometer::Restart(); } @@ -153,12 +145,12 @@ void OSD_Timer::Show() const Standard_Real OSD_Timer::ElapsedTime() const { - if (Stopped) + if (myIsStopped) { - return TimeCumul; + return myTimeCumul; } - return TimeCumul + GetWallClockTime() - TimeStart; + return myTimeCumul + GetWallClockTime() - myTimeStart; } //======================================================================= @@ -171,10 +163,10 @@ void OSD_Timer::Show (Standard_Real& theSeconds, 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); } @@ -183,22 +175,20 @@ void OSD_Timer::Show (Standard_Real& theSeconds, //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); + timeToHoursMinutesSeconds (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); + std::streamsize prec = theOStream.precision (12); + theOStream << "Elapsed time: " << anHours << " Hours " << + aMinutes << " Minutes " << + aSeconds << " Seconds\n"; + OSD_Chronometer::Show (theOStream); + theOStream.precision (prec); } //======================================================================= @@ -208,11 +198,11 @@ void OSD_Timer::Show (Standard_OStream& os) const 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"; } //======================================================================= @@ -222,9 +212,9 @@ void OSD_Timer::Stop () void OSD_Timer::Start() { - if (Stopped) { - TimeStart = GetWallClockTime(); + if (myIsStopped) + { + myTimeStart = GetWallClockTime(); OSD_Chronometer::Start(); } -// else cout << "WARNING: OSD_Timer already Running !\n"; } diff --git a/src/OSD/OSD_Timer.hxx b/src/OSD/OSD_Timer.hxx index c343e37746..2b483283bf 100644 --- a/src/OSD/OSD_Timer.hxx +++ b/src/OSD/OSD_Timer.hxx @@ -45,7 +45,10 @@ public: 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); @@ -81,8 +84,8 @@ public: private: - Standard_Real TimeStart; - Standard_Real TimeCumul; + Standard_Real myTimeStart; + Standard_Real myTimeCumul; };