Commit 9b8317b7 authored by Mark Olesen's avatar Mark Olesen
Browse files

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.
parent 14f841b2
......@@ -26,6 +26,9 @@ Description
\*---------------------------------------------------------------------------*/
#include "OSspecific.H"
#include "clock.H"
#include "clockTime.H"
#include "cpuTime.H"
using namespace Foam;
......@@ -34,8 +37,65 @@ using namespace Foam;
int main(int argc, char *argv[])
{
Info<<"rmDir" << nl;
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;
return 0;
......
......@@ -10,6 +10,7 @@ fileStat.C
POSIX.C
cpuTime/cpuTime.C
clockTime/clockTime.C
clockValue/clockValue.C
cpuInfo/cpuInfo.C
memInfo/memInfo.C
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -24,46 +24,44 @@ License
\*---------------------------------------------------------------------------*/
#include "clockTime.H"
#include <sys/time.h>
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
void Foam::clockTime::getTime(timeType& t)
{
gettimeofday(&t, 0);
}
Foam::clockTime::clockTime()
:
start_(true),
last_(start_)
{}
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);
}
Foam::clockTime::clockTime(const clockValue& clockval)
:
start_(clockval),
last_(start_)
{}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::clockTime::clockTime()
void Foam::clockTime::resetTime()
{
getTime(startTime_);
lastTime_ = startTime_;
newTime_ = startTime_;
last_.update();
start_ = last_;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
double Foam::clockTime::elapsedTime() const
{
getTime(newTime_);
return timeDifference(startTime_, newTime_);
last_.update();
return (last_ - start_);
}
double Foam::clockTime::timeIncrement() const
{
lastTime_ = newTime_;
getTime(newTime_);
return timeDifference(lastTime_, newTime_);
const value_type prev(last_);
last_.update();
return (last_ - prev);
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -36,7 +36,7 @@ SourceFiles
#ifndef clockTime_H
#define clockTime_H
#include <sys/types.h>
#include "clockValue.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -49,35 +49,33 @@ namespace Foam
class clockTime
{
// Private data
// Private Data
//- Time structure used
typedef struct timeval timeType;
//- The values being used.
typedef clockValue value_type;
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);
//- Start time, at the time of construction
value_type start_;
//- Last time when elapsedTime or timeIncrement was called
mutable value_type last_;
public:
// Constructors
//- Construct with the current clock time
//- Construct with the current clock time for the starting value
clockTime();
//- Construct with the given clock value for the starting value
clockTime(const clockValue& clockval);
// Member Functions
//- Reset to use the current time for the start time
void resetTime();
//- Return time (in seconds) from the start
double elapsedTime() const;
......
/*---------------------------------------------------------------------------*\
========= |
\\ / 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;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation |
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -28,54 +28,63 @@ License
// * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * //
const long Foam::cpuTime::Hz_(sysconf(_SC_CLK_TCK));
const long Foam::cpuTime::clockTicks_(sysconf(_SC_CLK_TCK));
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::cpuTime::getTime(timeType& t)
{
times(&t);
}
double Foam::cpuTime::timeDifference(const timeType& beg, const timeType& end)
double Foam::cpuTime::diff(const value_type& a, const value_type& b)
{
return
(
double
(
(end.tms_utime + end.tms_stime)
- (beg.tms_utime + beg.tms_stime)
)/Hz_
double((a.tms_utime + a.tms_stime) - (b.tms_utime + b.tms_stime))
/ clockTicks_
);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::cpuTime::cpuTime()
Foam::cpuTime::value_type::value_type()
{
getTime(startTime_);
lastTime_ = startTime_;
newTime_ = startTime_;
update();
}
Foam::cpuTime::cpuTime()
:
start_(),
last_(start_)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::cpuTime::value_type::update()
{
::times(this);
}
void Foam::cpuTime::resetCpuTime()
{
last_.update();
start_ = last_;
}
double Foam::cpuTime::elapsedCpuTime() const
{
getTime(newTime_);
return timeDifference(startTime_, newTime_);
last_.update();
return diff(last_, start_);
}
double Foam::cpuTime::cpuTimeIncrement() const
{
lastTime_ = newTime_;
getTime(newTime_);
return timeDifference(lastTime_, newTime_);
const value_type prev(last_);
last_.update();
return diff(last_, prev);
}
......
......@@ -2,8 +2,8 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -52,26 +52,33 @@ namespace Foam
class cpuTime
{