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
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011 OpenFOAM Foundation
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -24,6 +22,7 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Test some clock-related routines
\*---------------------------------------------------------------------------*/
@ -31,21 +30,59 @@ Description
#include "clock.H"
#include "clockTime.H"
#include "cpuTime.H"
#include "clockValue.H"
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:
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();
@ -54,33 +91,10 @@ int main(int argc, char *argv[])
<< "clock: iso " << clock::dateTime() << nl;
}
Info<< "since epoch = " << clockValue::now().str() << nl;
testEpoch<clockValue>();
{
clockValue a;
testElapsed<clockValue>();
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;

View File

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

View File

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

View File

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

View File

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

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -25,40 +25,32 @@ License
#include "clockValue.H"
#include "IOstreams.H"
#include <sys/time.h>
#include <sstream>
#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 * * * * * * * * * * * * * * //
Foam::clockValue::clockValue()
:
clockValue(false)
value_(value_type::zero())
{}
Foam::clockValue::clockValue(const value_type& value)
:
value_(value)
{}
Foam::clockValue::clockValue(bool useNow)
:
value_(value_type::zero())
{
if (useNow)
{
update();
}
else
{
clear();
}
}
@ -66,14 +58,13 @@ Foam::clockValue::clockValue(bool useNow)
void Foam::clockValue::clear()
{
value_.tv_sec = 0;
value_.tv_usec = 0;
value_ = value_type::zero();
}
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 sec = value_.tv_sec;
if (sec > 0 && value_.tv_usec > factorMicro2)
{
++sec;
}
return sec;
return std::chrono::duration_cast<std::chrono::seconds>(value_).count();
}
@ -99,23 +84,24 @@ std::string Foam::clockValue::str() const
{
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
const auto dd = (ss / 86400);
if (dd) os << dd << '-';
// hours
const int hh = ((ss / 3600) % 24);
if (dd) os << dd << '-';
if (dd || hh)
{
os << std::setw(2) << std::setfill('0')
<< hh << ':';
}
// minutes
os << std::setw(2) << std::setfill('0')
<< ((ss / 60) % 60) << ':';
@ -124,12 +110,16 @@ std::string Foam::clockValue::str() const
os << std::setw(2) << std::setfill('0')
<< (ss % 60);
// 1/100th seconds. As none or 2 decimal places
const int hundredths = (value_.tv_sec % factorHundred);
// milliseconds. As none or 3 decimal places
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();
@ -140,38 +130,24 @@ std::string Foam::clockValue::str() 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)
{
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;
value_ -= rhs.value_;
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;
value_ += rhs.value_;
return *this;
}
@ -180,19 +156,13 @@ Foam::clockValue& Foam::clockValue::operator+=(const clockValue& rhs)
Foam::clockValue Foam::operator-(const clockValue& a, const clockValue& b)
{
clockValue result(a);
result -= b;
return result;
return clockValue(a.value() - b.value());
}
Foam::clockValue Foam::operator+(const clockValue& a, const clockValue& b)
{
clockValue result(a);
result += b;
return result;
return clockValue(a.value() + b.value());
}

View File

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