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:
Mark Olesen
2018-03-15 10:01:51 +01:00
parent fb4c99b698
commit 77338c8bd0
10 changed files with 495 additions and 141 deletions

View File

@ -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;

View File

@ -10,6 +10,7 @@ fileStat.C
POSIX.C
cpuTime/cpuTime.C
clockTime/clockTime.C
clockValue/clockValue.C
cpuInfo/cpuInfo.C
memInfo/memInfo.C

View File

@ -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 * * * * * * * * * * * //
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 * * * * * * * * * * * * * * //
Foam::clockTime::clockTime()
{
getTime(startTime_);
lastTime_ = startTime_;
newTime_ = startTime_;
}
:
start_(true),
last_(start_)
{}
Foam::clockTime::clockTime(const clockValue& clockval)
:
start_(clockval),
last_(start_)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::clockTime::resetTime()
{
last_.update();
start_ = last_;
}
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);
}

View File

@ -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;

View 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;
}
// ************************************************************************* //

View 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
// ************************************************************************* //

View File

@ -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);
}

View File

@ -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
{
// Private data
//- Time structure used, with additional methods
struct value_type : tms
{
//- Construct with the current clock time
value_type();
//- Time structure used
typedef struct tms timeType;
//- Update with the current clock time
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
static const long Hz_;
static const long clockTicks_;
//- 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);
//- Difference between two times (a - b)
static double diff(const value_type& a, const value_type& b);
public:
@ -84,6 +91,9 @@ public:
// Member Functions
//- Reset to use the current time for the start time
void resetCpuTime();
//- Return CPU time (in seconds) from the start
double elapsedCpuTime() const;

View File

@ -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.
@ -31,7 +31,7 @@ License
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const char *Foam::clock::monthNames[] =
static const char *monthNames[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
@ -49,61 +49,58 @@ time_t Foam::clock::getTime()
const struct tm Foam::clock::rawDate()
{
time_t t = getTime();
struct tm *timeStruct = localtime(&t);
return *timeStruct;
struct tm *curr = localtime(&t);
return *curr;
}
Foam::string Foam::clock::dateTime()
std::string Foam::clock::dateTime()
{
std::ostringstream osBuffer;
time_t t = getTime();
struct tm *timeStruct = localtime(&t);
struct tm *curr = localtime(&t);
osBuffer
std::ostringstream os;
os
<< std::setfill('0')
<< std::setw(4) << timeStruct->tm_year + 1900
<< '-' << std::setw(2) << timeStruct->tm_mon + 1
<< '-' << std::setw(2) << timeStruct->tm_mday
<< std::setw(4) << curr->tm_year + 1900
<< '-' << std::setw(2) << curr->tm_mon + 1
<< '-' << std::setw(2) << curr->tm_mday
<< 'T'
<< std::setw(2) << timeStruct->tm_hour
<< ':' << std::setw(2) << timeStruct->tm_min
<< ':' << std::setw(2) << timeStruct->tm_sec;
<< std::setw(2) << curr->tm_hour
<< ':' << std::setw(2) << curr->tm_min
<< ':' << 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();
struct tm *timeStruct = localtime(&t);
struct tm *curr = localtime(&t);
osBuffer
<< monthNames[timeStruct->tm_mon]
<< ' ' << std::setw(2) << std::setfill('0') << timeStruct->tm_mday
<< ' ' << std::setw(4) << timeStruct->tm_year + 1900;
std::ostringstream os;
os
<< monthNames[curr->tm_mon]
<< ' ' << 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();
struct tm *timeStruct = localtime(&t);
struct tm *curr = localtime(&t);
osBuffer
std::ostringstream os;
os
<< std::setfill('0')
<< std::setw(2) << timeStruct->tm_hour
<< ':' << std::setw(2) << timeStruct->tm_min
<< ':' << std::setw(2) << timeStruct->tm_sec;
<< std::setw(2) << curr->tm_hour
<< ':' << std::setw(2) << curr->tm_min
<< ':' << std::setw(2) << curr->tm_sec;
return osBuffer.str();
return os.str();
}
@ -111,9 +108,8 @@ Foam::string Foam::clock::clockTime()
Foam::clock::clock()
:
startTime_(getTime()),
lastTime_(startTime_),
newTime_(startTime_)
start_(getTime()),
last_(start_)
{}
@ -121,16 +117,17 @@ Foam::clock::clock()
time_t Foam::clock::elapsedClockTime() const
{
newTime_ = getTime();
return newTime_ - startTime_;
last_ = getTime();
return last_ - start_;
}
time_t Foam::clock::clockTimeIncrement() const
{
lastTime_ = newTime_;
newTime_ = getTime();
return newTime_ - lastTime_;
const value_type prev(last_);
last_ = getTime();
return last_ - prev;
}

View File

@ -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,6 +36,7 @@ SourceFiles
#define clock_H
#include <ctime>
#include <string>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -52,28 +53,24 @@ class clock
{
// Private data
//- Start time in seconds
time_t startTime_;
//- Time structure used
typedef time_t value_type;
//- Time when clockTimeIncrement() was last called
mutable time_t lastTime_;
//- Latest time from either elapsedClockTime() or clockTimeIncrement()
mutable time_t newTime_;
//- Names of the months
static const char *monthNames[];
//- Start time in seconds, at the time of construction
value_type start_;
//- Last time when elapsedClockTime or clockTimeIncrement was called
mutable value_type last_;
public:
// Constructors
//- Null constructor which stores the start time
//- Construct null, storing the start time
clock();
// Member Functions
// Static Member Functions
//- Get the current clock time in seconds
static time_t getTime();
@ -83,13 +80,16 @@ public:
//- Return the current wall-clock date/time as a string
// 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
static string date();
static std::string date();
//- 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
time_t elapsedClockTime() const;