ENH: replace OSspecific clockValue with std::chrono version (#1278)

- aids with portability and maintenance (#1238)
This commit is contained in:
Mark Olesen
2019-04-11 10:07:54 +02:00
committed by Andrew Heather
parent 51aae5f34d
commit b56fbc4377
14 changed files with 173 additions and 116 deletions

View File

@ -0,0 +1,3 @@
Test-OSspecific.C
EXE = $(FOAM_USER_APPBIN)/Test-OSspecific

View File

@ -0,0 +1,59 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 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/>.
Description
Report some basic os-specific values
\*---------------------------------------------------------------------------*/
#include "OSspecific.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<< "Report some basic OS-specific values" << nl;
Info<< nl
<< "host : " << hostName() << nl
<< "user : " << userName() << nl
<< "home : " << home() << nl;
Info<< nl
<< "cwd : " << cwd() << nl
<< "cwd -P : " << cwd(false) << nl
<< "cwd -L : " << cwd(true) << nl;
Info<< nl
<< "libs : " << dlLoaded() << nl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //

View File

@ -1,2 +0,0 @@
Test-POSIX.C
EXE = $(FOAM_USER_APPBIN)/Test-POSIX

View File

@ -0,0 +1,3 @@
Test-clock.C
EXE = $(FOAM_USER_APPBIN)/Test-clock

View File

@ -0,0 +1,2 @@
/* EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude */
/* EXE_LIBS = -lfiniteVolume */

View File

@ -2,10 +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) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011 OpenFOAM Foundation
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -24,6 +22,7 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description Description
Test some clock-related routines
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -31,21 +30,59 @@ Description
#include "clock.H" #include "clock.H"
#include "clockTime.H" #include "clockTime.H"
#include "cpuTime.H" #include "cpuTime.H"
#include "clockValue.H"
using namespace Foam; using namespace Foam;
template<class ClockValue>
void testEpoch()
{
Info<< nl << "Test epoch" << nl;
ClockValue now(true);
Info<< "epoch = " << now.str() << " # day-hh:mm::ss" << nl
<< "epoch = " << now << nl;
}
template<class ClockValue>
void testElapsed()
{
Info<< nl << "Test elapsed" << 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
<< "elapsed = " << a.elapsed().seconds() << nl
<< "elapsed = " << a.elapsed().str() << nl;
ClockValue b(true);
Info<< "(" << b << " - " << a << ") = " << (b - a) << nl;
Info<< "(" << b << " + " << a << ") = " << (b + a) << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program: // Main program:
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Info<<"cwd() " << cwd() << nl;
Info<<"cwd(-P) " << cwd(false) << nl;
Info<<"cwd(-L) " << cwd(true) << nl;
Info<<"rmDir" << nl;
rmDir("hmm");
{ {
Foam::clock sysClock(); Foam::clock sysClock();
@ -54,33 +91,10 @@ int main(int argc, char *argv[])
<< "clock: iso " << clock::dateTime() << nl; << "clock: iso " << clock::dateTime() << nl;
} }
Info<< "since epoch = " << clockValue::now().str() << nl; testEpoch<clockValue>();
{ testElapsed<clockValue>();
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;
Info<< "elapsed = " << a.elapsed().str() << nl;
clockValue b = clockValue::now();
Info<< "(" << b << " - " << a << ") = " << (b - a) << nl;
Info<< "(" << b << " + " << a << ") = " << (b + a) << nl;
}
{ {
clockTime clk; clockTime clk;

View File

@ -1,5 +1,3 @@
clockTime/clockTime.C
clockValue/clockValue.C
cpuInfo/cpuInfo.C cpuInfo/cpuInfo.C
cpuTime/cpuTime.C cpuTime/cpuTime.C
memInfo/memInfo.C memInfo/memInfo.C

View File

@ -4,6 +4,8 @@ global/global.Cver
global/argList/argList.C global/argList/argList.C
global/argList/argListHelp.C global/argList/argListHelp.C
global/clock/clock.C global/clock/clock.C
global/clockTime/clockTime.C
global/clockValue/clockValue.C
global/profiling/profiling.C global/profiling/profiling.C
global/profiling/profilingInformation.C global/profiling/profilingInformation.C
global/profiling/profilingSysInfo.C global/profiling/profilingSysInfo.C

View File

@ -75,6 +75,7 @@ std::string Foam::clock::dateTime()
return os.str(); return os.str();
} }
std::string Foam::clock::date() std::string Foam::clock::date()
{ {
time_t t = getTime(); time_t t = getTime();

View File

@ -62,6 +62,7 @@ class clockTime
//- Last time when elapsedTime or timeIncrement was called //- Last time when elapsedTime or timeIncrement was called
mutable value_type last_; mutable value_type last_;
public: public:
// Constructors // Constructors

View File

@ -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) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -25,40 +25,32 @@ License
#include "clockValue.H" #include "clockValue.H"
#include "IOstreams.H" #include "IOstreams.H"
#include <sys/time.h>
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
// * * * * * * * * * * * * * * * * Local Data * * * * * * * * * * * * * * * //
namespace
{
constexpr int factorMicro = (1000000); //!< From usec to sec
constexpr int factorMicro2 = (500000); //!< Rounding usec to sec
constexpr int factorHundred = (10000); //!< From usec to 0.01 sec
} // End anonymous namespace
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::clockValue::clockValue() Foam::clockValue::clockValue()
: :
clockValue(false) value_(value_type::zero())
{}
Foam::clockValue::clockValue(const value_type& value)
:
value_(value)
{} {}
Foam::clockValue::clockValue(bool useNow) Foam::clockValue::clockValue(bool useNow)
:
value_(value_type::zero())
{ {
if (useNow) if (useNow)
{ {
update(); update();
} }
else
{
clear();
}
} }
@ -66,14 +58,13 @@ Foam::clockValue::clockValue(bool useNow)
void Foam::clockValue::clear() void Foam::clockValue::clear()
{ {
value_.tv_sec = 0; value_ = value_type::zero();
value_.tv_usec = 0;
} }
void Foam::clockValue::update() void Foam::clockValue::update()
{ {
gettimeofday(&value_, 0); value_ = std::chrono::high_resolution_clock::now().time_since_epoch();
} }
@ -85,13 +76,7 @@ Foam::clockValue Foam::clockValue::elapsed() const
long Foam::clockValue::seconds() const long Foam::clockValue::seconds() const
{ {
long sec = value_.tv_sec; return std::chrono::duration_cast<std::chrono::seconds>(value_).count();
if (sec > 0 && value_.tv_usec > factorMicro2)
{
++sec;
}
return sec;
} }
@ -99,23 +84,24 @@ std::string Foam::clockValue::str() const
{ {
std::ostringstream os; std::ostringstream os;
const unsigned long ss = value_.tv_sec; // seconds
const unsigned long ss =
std::chrono::duration_cast<std::chrono::seconds>(value_).count();
// days // days
const auto dd = (ss / 86400); const auto dd = (ss / 86400);
if (dd) os << dd << '-';
// hours // hours
const int hh = ((ss / 3600) % 24); const int hh = ((ss / 3600) % 24);
if (dd) os << dd << '-';
if (dd || hh) if (dd || hh)
{ {
os << std::setw(2) << std::setfill('0') os << std::setw(2) << std::setfill('0')
<< hh << ':'; << hh << ':';
} }
// minutes // minutes
os << std::setw(2) << std::setfill('0') os << std::setw(2) << std::setfill('0')
<< ((ss / 60) % 60) << ':'; << ((ss / 60) % 60) << ':';
@ -124,12 +110,16 @@ std::string Foam::clockValue::str() const
os << std::setw(2) << std::setfill('0') os << std::setw(2) << std::setfill('0')
<< (ss % 60); << (ss % 60);
// 1/100th seconds. As none or 2 decimal places // milliseconds. As none or 3 decimal places
const int hundredths = (value_.tv_sec % factorHundred); const long ms =
(
std::chrono::duration_cast<std::chrono::milliseconds>(value_).count()
- (ss * 1000)
);
if (hundredths) if (ms > 0)
{ {
os << '.' << std::setw(2) << std::setfill('0') << hundredths; os << '.' << std::setw(3) << std::setfill('0') << ms;
} }
return os.str(); return os.str();
@ -140,38 +130,24 @@ std::string Foam::clockValue::str() const
Foam::clockValue::operator double () const Foam::clockValue::operator double () const
{ {
return (value_.tv_sec + 1e-6*value_.tv_usec); return
(
(double(value_.count()) * value_type::period::num)
/ value_type::period::den
);
} }
Foam::clockValue& Foam::clockValue::operator-=(const clockValue& rhs) Foam::clockValue& Foam::clockValue::operator-=(const clockValue& rhs)
{ {
const value_type& b = rhs.value_; value_ -= 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; return *this;
} }
Foam::clockValue& Foam::clockValue::operator+=(const clockValue& rhs) Foam::clockValue& Foam::clockValue::operator+=(const clockValue& rhs)
{ {
const value_type& b = rhs.value_; value_ += 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; return *this;
} }
@ -180,19 +156,13 @@ Foam::clockValue& Foam::clockValue::operator+=(const clockValue& rhs)
Foam::clockValue Foam::operator-(const clockValue& a, const clockValue& b) Foam::clockValue Foam::operator-(const clockValue& a, const clockValue& b)
{ {
clockValue result(a); return clockValue(a.value() - b.value());
result -= b;
return result;
} }
Foam::clockValue Foam::operator+(const clockValue& a, const clockValue& b) Foam::clockValue Foam::operator+(const clockValue& a, const clockValue& b)
{ {
clockValue result(a); return clockValue(a.value() + b.value());
result += b;
return result;
} }

View File

@ -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) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -25,8 +25,7 @@ Class
Foam::clockValue Foam::clockValue
Description Description
Access to clock value (approx. 2 microsecond resolution) with Access to high-resolution clock value with some basic operations.
some and basic operations.
SourceFiles SourceFiles
clockValue.C clockValue.C
@ -36,11 +35,8 @@ SourceFiles
#ifndef clockValue_H #ifndef clockValue_H
#define clockValue_H #define clockValue_H
#include <chrono>
#include <string> #include <string>
#include <sys/types.h>
#ifdef darwin
#include <sys/time.h>
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -56,19 +52,23 @@ class clockValue
// Private Data // Private Data
//- Time structure used //- Time structure used
typedef struct timeval value_type; typedef std::chrono::high_resolution_clock::duration value_type;
value_type value_; value_type value_;
public: public:
// Constructors // Constructors
//- Construct null, zero initialized //- Construct zero initialized
clockValue(); clockValue();
//- Construct from duration with the same clock base
explicit clockValue(const value_type& value);
//- Construct zero initialized or with current time //- Construct zero initialized or with current time
clockValue(bool useNow); explicit clockValue(bool useNow);
// Factory Methods // Factory Methods
@ -82,6 +82,12 @@ public:
// Member Functions // Member Functions
//- Return the value
inline const value_type& value() const
{
return value_;
}
//- Reset to zero //- Reset to zero
void clear(); void clear();
@ -100,7 +106,7 @@ public:
// Operators // Operators
//- Conversion operator to seconds //- Conversion operator to seconds in floating point
operator double() const; operator double() const;
//- Subtract time value //- Subtract time value