mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: reduce overhead for clockTime, cpuTime
- clockValue class for managing the clock values only, with a null constructor that does not query the system clock (can defer to later). Can also be used directly for +/- operations. - refactor clockTime, cpuTime, clock to reduce storage.
This commit is contained in:
@ -26,6 +26,9 @@ Description
|
|||||||
\*---------------------------------------------------------------------------*/
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include "OSspecific.H"
|
#include "OSspecific.H"
|
||||||
|
#include "clock.H"
|
||||||
|
#include "clockTime.H"
|
||||||
|
#include "cpuTime.H"
|
||||||
|
|
||||||
using namespace Foam;
|
using namespace Foam;
|
||||||
|
|
||||||
@ -34,8 +37,65 @@ using namespace Foam;
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
Info<<"rmDir" << nl;
|
||||||
rmDir("hmm");
|
rmDir("hmm");
|
||||||
|
|
||||||
|
{
|
||||||
|
Foam::clock sysClock();
|
||||||
|
|
||||||
|
Info<< "clock: date " << clock::date() << nl
|
||||||
|
<< "clock: time " << clock::clockTime() << nl
|
||||||
|
<< "clock: iso " << clock::dateTime() << nl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
clockValue a;
|
||||||
|
|
||||||
|
Info<< "clockValue() " << a << nl;
|
||||||
|
a.update();
|
||||||
|
Info<< "updated " << a << nl;
|
||||||
|
|
||||||
|
Info<< "sleep 4..." << endl;
|
||||||
|
sleep(4);
|
||||||
|
|
||||||
|
a.update();
|
||||||
|
Info<< " = " << a.seconds() << nl;
|
||||||
|
|
||||||
|
Info<< "sleep 2..." << endl;
|
||||||
|
sleep(2);
|
||||||
|
|
||||||
|
Info<< "elapsed = " << a.elapsed() << nl;
|
||||||
|
Info<< "elapsed = " << a.elapsed().seconds() << nl;
|
||||||
|
|
||||||
|
clockValue b = clockValue::now();
|
||||||
|
|
||||||
|
Info<< "(" << b << " - " << a << ") = " << (b - a) << nl;
|
||||||
|
Info<< "(" << b << " + " << a << ") = " << (b + a) << nl;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
clockTime clk;
|
||||||
|
|
||||||
|
Info<< "starting clockTime" << nl;
|
||||||
|
|
||||||
|
Info<< "sleep 4..." << endl;
|
||||||
|
sleep(4);
|
||||||
|
|
||||||
|
Info<< "increment = " << clk.timeIncrement() << nl;
|
||||||
|
Info<< "elapsed = " << clk.elapsedTime() << nl;
|
||||||
|
|
||||||
|
Info<< "sleep 4..." << endl;
|
||||||
|
sleep(4);
|
||||||
|
Info<< "increment = " << clk.timeIncrement() << nl;
|
||||||
|
Info<< "elapsed = " << clk.elapsedTime() << nl;
|
||||||
|
|
||||||
|
Info<< "sleep 2..." << endl;
|
||||||
|
sleep(2);
|
||||||
|
Info<< "elapsed = " << clk.elapsedTime() << nl;
|
||||||
|
Info<< "increment = " << clk.timeIncrement() << nl;
|
||||||
|
}
|
||||||
|
|
||||||
Info<< "End\n" << endl;
|
Info<< "End\n" << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -10,6 +10,7 @@ fileStat.C
|
|||||||
POSIX.C
|
POSIX.C
|
||||||
cpuTime/cpuTime.C
|
cpuTime/cpuTime.C
|
||||||
clockTime/clockTime.C
|
clockTime/clockTime.C
|
||||||
|
clockValue/clockValue.C
|
||||||
cpuInfo/cpuInfo.C
|
cpuInfo/cpuInfo.C
|
||||||
memInfo/memInfo.C
|
memInfo/memInfo.C
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
========= |
|
========= |
|
||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
|
||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
@ -24,46 +24,44 @@ License
|
|||||||
\*---------------------------------------------------------------------------*/
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include "clockTime.H"
|
#include "clockTime.H"
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
|
|
||||||
|
|
||||||
void Foam::clockTime::getTime(timeType& t)
|
|
||||||
{
|
|
||||||
gettimeofday(&t, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
double Foam::clockTime::timeDifference(const timeType& beg, const timeType& end)
|
|
||||||
{
|
|
||||||
return end.tv_sec - beg.tv_sec + 1e-6*(end.tv_usec - beg.tv_usec);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
Foam::clockTime::clockTime()
|
Foam::clockTime::clockTime()
|
||||||
{
|
:
|
||||||
getTime(startTime_);
|
start_(true),
|
||||||
lastTime_ = startTime_;
|
last_(start_)
|
||||||
newTime_ = startTime_;
|
{}
|
||||||
}
|
|
||||||
|
|
||||||
|
Foam::clockTime::clockTime(const clockValue& clockval)
|
||||||
|
:
|
||||||
|
start_(clockval),
|
||||||
|
last_(start_)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
void Foam::clockTime::resetTime()
|
||||||
|
{
|
||||||
|
last_.update();
|
||||||
|
start_ = last_;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
double Foam::clockTime::elapsedTime() const
|
double Foam::clockTime::elapsedTime() const
|
||||||
{
|
{
|
||||||
getTime(newTime_);
|
last_.update();
|
||||||
return timeDifference(startTime_, newTime_);
|
return (last_ - start_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
double Foam::clockTime::timeIncrement() const
|
double Foam::clockTime::timeIncrement() const
|
||||||
{
|
{
|
||||||
lastTime_ = newTime_;
|
const value_type prev(last_);
|
||||||
getTime(newTime_);
|
last_.update();
|
||||||
return timeDifference(lastTime_, newTime_);
|
return (last_ - prev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -36,7 +36,7 @@ SourceFiles
|
|||||||
#ifndef clockTime_H
|
#ifndef clockTime_H
|
||||||
#define clockTime_H
|
#define clockTime_H
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include "clockValue.H"
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
@ -49,35 +49,33 @@ namespace Foam
|
|||||||
|
|
||||||
class clockTime
|
class clockTime
|
||||||
{
|
{
|
||||||
// Private data
|
// Private Data
|
||||||
|
|
||||||
//- Time structure used
|
//- The values being used.
|
||||||
typedef struct timeval timeType;
|
typedef clockValue value_type;
|
||||||
|
|
||||||
timeType startTime_;
|
//- Start time, at the time of construction
|
||||||
|
value_type start_;
|
||||||
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);
|
|
||||||
|
|
||||||
|
//- Last time when elapsedTime or timeIncrement was called
|
||||||
|
mutable value_type last_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
//- Construct with the current clock time
|
//- Construct with the current clock time for the starting value
|
||||||
clockTime();
|
clockTime();
|
||||||
|
|
||||||
|
//- Construct with the given clock value for the starting value
|
||||||
|
clockTime(const clockValue& clockval);
|
||||||
|
|
||||||
|
|
||||||
// Member Functions
|
// Member Functions
|
||||||
|
|
||||||
|
//- Reset to use the current time for the start time
|
||||||
|
void resetTime();
|
||||||
|
|
||||||
//- Return time (in seconds) from the start
|
//- Return time (in seconds) from the start
|
||||||
double elapsedTime() const;
|
double elapsedTime() const;
|
||||||
|
|
||||||
|
|||||||
157
src/OSspecific/POSIX/clockValue/clockValue.C
Normal file
157
src/OSspecific/POSIX/clockValue/clockValue.C
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
========= |
|
||||||
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
|
\\ / O peration |
|
||||||
|
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
|
||||||
|
\\/ M anipulation |
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
License
|
||||||
|
This file is part of OpenFOAM.
|
||||||
|
|
||||||
|
OpenFOAM is free software: you can redistribute it and/or modify it
|
||||||
|
under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#include "clockValue.H"
|
||||||
|
#include "IOstreams.H"
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * Local Data * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
constexpr int factorMicro = (1000000);
|
||||||
|
constexpr int factorMicro2 = (500000);
|
||||||
|
|
||||||
|
} // end of anonymous
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
Foam::clockValue::clockValue()
|
||||||
|
:
|
||||||
|
clockValue(false)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
Foam::clockValue::clockValue(bool useNow)
|
||||||
|
{
|
||||||
|
if (useNow)
|
||||||
|
{
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
void Foam::clockValue::clear()
|
||||||
|
{
|
||||||
|
value_.tv_sec = 0;
|
||||||
|
value_.tv_usec = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Foam::clockValue::update()
|
||||||
|
{
|
||||||
|
gettimeofday(&value_, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Foam::clockValue Foam::clockValue::elapsed() const
|
||||||
|
{
|
||||||
|
return (now() -= *this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
long Foam::clockValue::seconds() const
|
||||||
|
{
|
||||||
|
long sec = value_.tv_sec;
|
||||||
|
if (sec > 0 && value_.tv_usec > factorMicro2)
|
||||||
|
{
|
||||||
|
++sec;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sec;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
Foam::clockValue::operator double () const
|
||||||
|
{
|
||||||
|
return (value_.tv_sec + 1e-6*value_.tv_usec);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
Foam::clockValue& Foam::clockValue::operator-=(const clockValue& rhs)
|
||||||
|
{
|
||||||
|
const value_type& b = rhs.value_;
|
||||||
|
|
||||||
|
value_.tv_sec -= b.tv_sec;
|
||||||
|
|
||||||
|
if (value_.tv_usec < b.tv_usec)
|
||||||
|
{
|
||||||
|
--(value_.tv_sec);
|
||||||
|
value_.tv_usec += factorMicro;
|
||||||
|
}
|
||||||
|
|
||||||
|
value_.tv_usec -= b.tv_usec;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Foam::clockValue& Foam::clockValue::operator+=(const clockValue& rhs)
|
||||||
|
{
|
||||||
|
const value_type& b = rhs.value_;
|
||||||
|
|
||||||
|
// Microseconds first
|
||||||
|
value_.tv_usec += b.tv_usec;
|
||||||
|
|
||||||
|
value_.tv_sec += b.tv_sec + (value_.tv_usec / factorMicro);
|
||||||
|
value_.tv_usec %= factorMicro;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
Foam::clockValue Foam::operator-(const clockValue& a, const clockValue& b)
|
||||||
|
{
|
||||||
|
clockValue result(a);
|
||||||
|
result -= b;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Foam::clockValue Foam::operator+(const clockValue& a, const clockValue& b)
|
||||||
|
{
|
||||||
|
clockValue result(a);
|
||||||
|
result += b;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ************************************************************************* //
|
||||||
124
src/OSspecific/POSIX/clockValue/clockValue.H
Normal file
124
src/OSspecific/POSIX/clockValue/clockValue.H
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
========= |
|
||||||
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
|
\\ / O peration |
|
||||||
|
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
|
||||||
|
\\/ M anipulation |
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
License
|
||||||
|
This file is part of OpenFOAM.
|
||||||
|
|
||||||
|
OpenFOAM is free software: you can redistribute it and/or modify it
|
||||||
|
under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Class
|
||||||
|
Foam::clockValue
|
||||||
|
|
||||||
|
Description
|
||||||
|
Access to clock value (approx. 2 microsecond resolution) with
|
||||||
|
some and basic operations.
|
||||||
|
|
||||||
|
SourceFiles
|
||||||
|
clockValue.C
|
||||||
|
|
||||||
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#ifndef clockValue_H
|
||||||
|
#define clockValue_H
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
namespace Foam
|
||||||
|
{
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
Class clockValue Declaration
|
||||||
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
class clockValue
|
||||||
|
{
|
||||||
|
// Private Data
|
||||||
|
|
||||||
|
//- Time structure used
|
||||||
|
typedef struct timeval value_type;
|
||||||
|
|
||||||
|
value_type value_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
// Constructors
|
||||||
|
|
||||||
|
//- Construct null, zero initialized
|
||||||
|
clockValue();
|
||||||
|
|
||||||
|
//- Construct zero initialized or with current time
|
||||||
|
clockValue(bool useNow);
|
||||||
|
|
||||||
|
|
||||||
|
// Factory Methods
|
||||||
|
|
||||||
|
//- Return the current time value from the system
|
||||||
|
inline static clockValue now()
|
||||||
|
{
|
||||||
|
return clockValue(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Member Functions
|
||||||
|
|
||||||
|
//- Reset to zero
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
//- Update to the current now() time from the system
|
||||||
|
void update();
|
||||||
|
|
||||||
|
//- The time elapsed from now() since the start time.
|
||||||
|
clockValue elapsed() const;
|
||||||
|
|
||||||
|
//- The value in seconds (rounded)
|
||||||
|
long seconds() const;
|
||||||
|
|
||||||
|
|
||||||
|
// Operators
|
||||||
|
|
||||||
|
//- Conversion operator to seconds
|
||||||
|
operator double() const;
|
||||||
|
|
||||||
|
//- Subtract time value
|
||||||
|
clockValue& operator-=(const clockValue& rhs);
|
||||||
|
|
||||||
|
//- Add time value
|
||||||
|
clockValue& operator+=(const clockValue& rhs);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Global Operators
|
||||||
|
|
||||||
|
//- Subtraction of clock values
|
||||||
|
clockValue operator-(const clockValue& a, const clockValue& b);
|
||||||
|
|
||||||
|
//- Addition of clock values
|
||||||
|
clockValue operator+(const clockValue& a, const clockValue& b);
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
} // End namespace Foam
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ************************************************************************* //
|
||||||
@ -2,8 +2,8 @@
|
|||||||
========= |
|
========= |
|
||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -28,54 +28,63 @@ License
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
const long Foam::cpuTime::Hz_(sysconf(_SC_CLK_TCK));
|
const long Foam::cpuTime::clockTicks_(sysconf(_SC_CLK_TCK));
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
|
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
|
||||||
|
|
||||||
void Foam::cpuTime::getTime(timeType& t)
|
double Foam::cpuTime::diff(const value_type& a, const value_type& b)
|
||||||
{
|
|
||||||
times(&t);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
double Foam::cpuTime::timeDifference(const timeType& beg, const timeType& end)
|
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
(
|
(
|
||||||
double
|
double((a.tms_utime + a.tms_stime) - (b.tms_utime + b.tms_stime))
|
||||||
(
|
/ clockTicks_
|
||||||
(end.tms_utime + end.tms_stime)
|
|
||||||
- (beg.tms_utime + beg.tms_stime)
|
|
||||||
)/Hz_
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
Foam::cpuTime::cpuTime()
|
Foam::cpuTime::value_type::value_type()
|
||||||
{
|
{
|
||||||
getTime(startTime_);
|
update();
|
||||||
lastTime_ = startTime_;
|
|
||||||
newTime_ = startTime_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Foam::cpuTime::cpuTime()
|
||||||
|
:
|
||||||
|
start_(),
|
||||||
|
last_(start_)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
void Foam::cpuTime::value_type::update()
|
||||||
|
{
|
||||||
|
::times(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Foam::cpuTime::resetCpuTime()
|
||||||
|
{
|
||||||
|
last_.update();
|
||||||
|
start_ = last_;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
double Foam::cpuTime::elapsedCpuTime() const
|
double Foam::cpuTime::elapsedCpuTime() const
|
||||||
{
|
{
|
||||||
getTime(newTime_);
|
last_.update();
|
||||||
return timeDifference(startTime_, newTime_);
|
return diff(last_, start_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
double Foam::cpuTime::cpuTimeIncrement() const
|
double Foam::cpuTime::cpuTimeIncrement() const
|
||||||
{
|
{
|
||||||
lastTime_ = newTime_;
|
const value_type prev(last_);
|
||||||
getTime(newTime_);
|
last_.update();
|
||||||
return timeDifference(lastTime_, newTime_);
|
return diff(last_, prev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
========= |
|
========= |
|
||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -52,26 +52,33 @@ namespace Foam
|
|||||||
|
|
||||||
class cpuTime
|
class cpuTime
|
||||||
{
|
{
|
||||||
// Private data
|
//- Time structure used, with additional methods
|
||||||
|
struct value_type : tms
|
||||||
|
{
|
||||||
|
//- Construct with the current clock time
|
||||||
|
value_type();
|
||||||
|
|
||||||
//- Time structure used
|
//- Update with the current clock time
|
||||||
typedef struct tms timeType;
|
void update();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Private Data
|
||||||
|
|
||||||
|
//- Start time, at the time of construction
|
||||||
|
value_type start_;
|
||||||
|
|
||||||
|
//- Last time when elapsedTime or timeIncrement was called
|
||||||
|
mutable value_type last_;
|
||||||
|
|
||||||
//- Clock-ticks per second
|
//- Clock-ticks per second
|
||||||
static const long Hz_;
|
static const long clockTicks_;
|
||||||
|
|
||||||
//- The start time
|
|
||||||
timeType startTime_;
|
|
||||||
mutable timeType lastTime_;
|
|
||||||
mutable timeType newTime_;
|
|
||||||
|
|
||||||
// Private Member Functions
|
// Private Member Functions
|
||||||
|
|
||||||
//- Retrieve the current time values from the system
|
//- Difference between two times (a - b)
|
||||||
static void getTime(timeType&);
|
static double diff(const value_type& a, const value_type& b);
|
||||||
|
|
||||||
//- Difference between two times
|
|
||||||
static double timeDifference(const timeType& beg, const timeType& end);
|
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -84,6 +91,9 @@ public:
|
|||||||
|
|
||||||
// Member Functions
|
// Member Functions
|
||||||
|
|
||||||
|
//- Reset to use the current time for the start time
|
||||||
|
void resetCpuTime();
|
||||||
|
|
||||||
//- Return CPU time (in seconds) from the start
|
//- Return CPU time (in seconds) from the start
|
||||||
double elapsedCpuTime() const;
|
double elapsedCpuTime() const;
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -31,7 +31,7 @@ License
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
||||||
|
|
||||||
const char *Foam::clock::monthNames[] =
|
static const char *monthNames[] =
|
||||||
{
|
{
|
||||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
||||||
@ -49,61 +49,58 @@ time_t Foam::clock::getTime()
|
|||||||
const struct tm Foam::clock::rawDate()
|
const struct tm Foam::clock::rawDate()
|
||||||
{
|
{
|
||||||
time_t t = getTime();
|
time_t t = getTime();
|
||||||
struct tm *timeStruct = localtime(&t);
|
struct tm *curr = localtime(&t);
|
||||||
return *timeStruct;
|
return *curr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Foam::string Foam::clock::dateTime()
|
std::string Foam::clock::dateTime()
|
||||||
{
|
{
|
||||||
std::ostringstream osBuffer;
|
|
||||||
|
|
||||||
time_t t = getTime();
|
time_t t = getTime();
|
||||||
struct tm *timeStruct = localtime(&t);
|
struct tm *curr = localtime(&t);
|
||||||
|
|
||||||
osBuffer
|
std::ostringstream os;
|
||||||
|
os
|
||||||
<< std::setfill('0')
|
<< std::setfill('0')
|
||||||
<< std::setw(4) << timeStruct->tm_year + 1900
|
<< std::setw(4) << curr->tm_year + 1900
|
||||||
<< '-' << std::setw(2) << timeStruct->tm_mon + 1
|
<< '-' << std::setw(2) << curr->tm_mon + 1
|
||||||
<< '-' << std::setw(2) << timeStruct->tm_mday
|
<< '-' << std::setw(2) << curr->tm_mday
|
||||||
<< 'T'
|
<< 'T'
|
||||||
<< std::setw(2) << timeStruct->tm_hour
|
<< std::setw(2) << curr->tm_hour
|
||||||
<< ':' << std::setw(2) << timeStruct->tm_min
|
<< ':' << std::setw(2) << curr->tm_min
|
||||||
<< ':' << std::setw(2) << timeStruct->tm_sec;
|
<< ':' << std::setw(2) << curr->tm_sec;
|
||||||
|
|
||||||
return osBuffer.str();
|
return os.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
Foam::string Foam::clock::date()
|
std::string Foam::clock::date()
|
||||||
{
|
{
|
||||||
std::ostringstream osBuffer;
|
|
||||||
|
|
||||||
time_t t = getTime();
|
time_t t = getTime();
|
||||||
struct tm *timeStruct = localtime(&t);
|
struct tm *curr = localtime(&t);
|
||||||
|
|
||||||
osBuffer
|
std::ostringstream os;
|
||||||
<< monthNames[timeStruct->tm_mon]
|
os
|
||||||
<< ' ' << std::setw(2) << std::setfill('0') << timeStruct->tm_mday
|
<< monthNames[curr->tm_mon]
|
||||||
<< ' ' << std::setw(4) << timeStruct->tm_year + 1900;
|
<< ' ' << std::setw(2) << std::setfill('0') << curr->tm_mday
|
||||||
|
<< ' ' << std::setw(4) << curr->tm_year + 1900;
|
||||||
|
|
||||||
return osBuffer.str();
|
return os.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Foam::string Foam::clock::clockTime()
|
std::string Foam::clock::clockTime()
|
||||||
{
|
{
|
||||||
std::ostringstream osBuffer;
|
|
||||||
|
|
||||||
time_t t = getTime();
|
time_t t = getTime();
|
||||||
struct tm *timeStruct = localtime(&t);
|
struct tm *curr = localtime(&t);
|
||||||
|
|
||||||
osBuffer
|
std::ostringstream os;
|
||||||
|
os
|
||||||
<< std::setfill('0')
|
<< std::setfill('0')
|
||||||
<< std::setw(2) << timeStruct->tm_hour
|
<< std::setw(2) << curr->tm_hour
|
||||||
<< ':' << std::setw(2) << timeStruct->tm_min
|
<< ':' << std::setw(2) << curr->tm_min
|
||||||
<< ':' << std::setw(2) << timeStruct->tm_sec;
|
<< ':' << std::setw(2) << curr->tm_sec;
|
||||||
|
|
||||||
return osBuffer.str();
|
return os.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -111,9 +108,8 @@ Foam::string Foam::clock::clockTime()
|
|||||||
|
|
||||||
Foam::clock::clock()
|
Foam::clock::clock()
|
||||||
:
|
:
|
||||||
startTime_(getTime()),
|
start_(getTime()),
|
||||||
lastTime_(startTime_),
|
last_(start_)
|
||||||
newTime_(startTime_)
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
||||||
@ -121,16 +117,17 @@ Foam::clock::clock()
|
|||||||
|
|
||||||
time_t Foam::clock::elapsedClockTime() const
|
time_t Foam::clock::elapsedClockTime() const
|
||||||
{
|
{
|
||||||
newTime_ = getTime();
|
last_ = getTime();
|
||||||
return newTime_ - startTime_;
|
return last_ - start_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
time_t Foam::clock::clockTimeIncrement() const
|
time_t Foam::clock::clockTimeIncrement() const
|
||||||
{
|
{
|
||||||
lastTime_ = newTime_;
|
const value_type prev(last_);
|
||||||
newTime_ = getTime();
|
|
||||||
return newTime_ - lastTime_;
|
last_ = getTime();
|
||||||
|
return last_ - prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
\\ / O peration |
|
\\ / O peration |
|
||||||
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
|
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
|
||||||
\\/ M anipulation |
|
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -36,6 +36,7 @@ SourceFiles
|
|||||||
#define clock_H
|
#define clock_H
|
||||||
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
@ -52,28 +53,24 @@ class clock
|
|||||||
{
|
{
|
||||||
// Private data
|
// Private data
|
||||||
|
|
||||||
//- Start time in seconds
|
//- Time structure used
|
||||||
time_t startTime_;
|
typedef time_t value_type;
|
||||||
|
|
||||||
//- Time when clockTimeIncrement() was last called
|
//- Start time in seconds, at the time of construction
|
||||||
mutable time_t lastTime_;
|
value_type start_;
|
||||||
|
|
||||||
//- Latest time from either elapsedClockTime() or clockTimeIncrement()
|
|
||||||
mutable time_t newTime_;
|
|
||||||
|
|
||||||
//- Names of the months
|
|
||||||
static const char *monthNames[];
|
|
||||||
|
|
||||||
|
//- Last time when elapsedClockTime or clockTimeIncrement was called
|
||||||
|
mutable value_type last_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
//- Null constructor which stores the start time
|
//- Construct null, storing the start time
|
||||||
clock();
|
clock();
|
||||||
|
|
||||||
|
|
||||||
// Member Functions
|
// Static Member Functions
|
||||||
|
|
||||||
//- Get the current clock time in seconds
|
//- Get the current clock time in seconds
|
||||||
static time_t getTime();
|
static time_t getTime();
|
||||||
@ -83,13 +80,16 @@ public:
|
|||||||
|
|
||||||
//- Return the current wall-clock date/time as a string
|
//- Return the current wall-clock date/time as a string
|
||||||
// format according to ISO-8601 (yyyy-mm-ddThh:mm:ss)
|
// format according to ISO-8601 (yyyy-mm-ddThh:mm:ss)
|
||||||
static string dateTime();
|
static std::string dateTime();
|
||||||
|
|
||||||
//- Return the current wall-clock date as a string
|
//- Return the current wall-clock date as a string
|
||||||
static string date();
|
static std::string date();
|
||||||
|
|
||||||
//- Return the current wall-clock time as a string
|
//- Return the current wall-clock time as a string
|
||||||
static string clockTime();
|
static std::string clockTime();
|
||||||
|
|
||||||
|
|
||||||
|
// Member Functions
|
||||||
|
|
||||||
//- Returns wall-clock time from clock instantiation
|
//- Returns wall-clock time from clock instantiation
|
||||||
time_t elapsedClockTime() const;
|
time_t elapsedClockTime() const;
|
||||||
|
|||||||
Reference in New Issue
Block a user