diff --git a/src/OSspecific/POSIX/cpuTime/cpuTime.C b/src/OSspecific/POSIX/cpuTime/cpuTime.C index 0c93b77463..3a4ab8e689 100644 --- a/src/OSspecific/POSIX/cpuTime/cpuTime.C +++ b/src/OSspecific/POSIX/cpuTime/cpuTime.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -24,58 +24,44 @@ License \*---------------------------------------------------------------------------*/ #include "cpuTime.H" -#include - -// * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * // - -const long Foam::cpuTime::Hz_(sysconf(_SC_CLK_TCK)); - // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -void Foam::cpuTime::getTime(timeType& t) +inline double Foam::cpuTime::timeDifference +( + const clock_t prev, + const clock_t cur +) const { - times(&t); -} - - -double Foam::cpuTime::timeDifference(const timeType& beg, const timeType& end) -{ - return - ( - double - ( - (end.tms_utime + end.tms_stime) - - (beg.tms_utime + beg.tms_stime) - )/Hz_ - ); + return double(cur - prev)/CLOCKS_PER_SEC; } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::cpuTime::cpuTime() -{ - getTime(startTime_); - lastTime_ = startTime_; - newTime_ = startTime_; -} +: + startTime_(clock()), + prevTime_(startTime_), + curTime_(startTime_) + +{} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // double Foam::cpuTime::elapsedCpuTime() const { - getTime(newTime_); - return timeDifference(startTime_, newTime_); + curTime_ = clock(); + return timeDifference(startTime_, curTime_); } double Foam::cpuTime::cpuTimeIncrement() const { - lastTime_ = newTime_; - getTime(newTime_); - return timeDifference(lastTime_, newTime_); + prevTime_ = curTime_; + curTime_ = clock(); + return timeDifference(prevTime_, curTime_); } diff --git a/src/OSspecific/POSIX/cpuTime/cpuTime.H b/src/OSspecific/POSIX/cpuTime/cpuTime.H index 77d4aeee4e..5003c8ebc3 100644 --- a/src/OSspecific/POSIX/cpuTime/cpuTime.H +++ b/src/OSspecific/POSIX/cpuTime/cpuTime.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -27,6 +27,9 @@ Class Description Starts timing CPU usage and return elapsed time from start. + Uses the POSIX clock() function which return the processor time consumed + in clock ticks @ CLOCKS_PER_SEC clock ticks per second. + See also clockTime @@ -39,7 +42,6 @@ SourceFiles #define cpuTime_H #include -#include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -54,24 +56,25 @@ class cpuTime { // Private Data - //- Time structure used - typedef struct tms timeType; + //- Start CPU clock tick count + clock_t startTime_; - //- Clock-ticks per second - static const long Hz_; + //- Previous CPU clock tick count + mutable clock_t prevTime_; + + //- Current CPU clock tick count + mutable clock_t curTime_; - //- The start time - timeType startTime_; - mutable timeType lastTime_; - mutable timeType newTime_; // Private Member Functions - //- Retrieve the current time values from the system - static void getTime(timeType&); - - //- Difference between two times - static double timeDifference(const timeType& beg, const timeType& end); + //- Return the difference between two times clock tick counts + // as elapsed CPU time (in seconds) + inline double timeDifference + ( + const clock_t prev, + const clock_t cur + ) const; public: