diff --git a/src/OSspecific/POSIX/Make/files b/src/OSspecific/POSIX/Make/files index 7ce5e03c60eb41a6f31d4f5fed263f4f51e3c248..44ba1ec1bfba3ab75cec382ccf8a3e7116a411a0 100644 --- a/src/OSspecific/POSIX/Make/files +++ b/src/OSspecific/POSIX/Make/files @@ -1,5 +1,5 @@ cpuInfo/cpuInfo.C -cpuTime/cpuTime.C +cpuTime/cpuTimePosix.C memInfo/memInfo.C signals/sigFpe.C diff --git a/src/OSspecific/POSIX/cpuTime.H b/src/OSspecific/POSIX/cpuTime.H new file mode 100644 index 0000000000000000000000000000000000000000..cc74eb60c9385689fb2063cc9661b97761390d80 --- /dev/null +++ b/src/OSspecific/POSIX/cpuTime.H @@ -0,0 +1,40 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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/>. + +Typedef + Foam::cpuTime + +Description + Selection of preferred clock mechanism for the elapsed cpu time. + +\*---------------------------------------------------------------------------*/ + +#ifndef cpuTime_H +#define cpuTime_H + +#include "cpuTimePosix.H" +#include "cpuTimeFwd.H" + +#endif + +// ************************************************************************* // diff --git a/src/OSspecific/POSIX/cpuTime/cpuTime.C b/src/OSspecific/POSIX/cpuTime/cpuTimePosix.C similarity index 82% rename from src/OSspecific/POSIX/cpuTime/cpuTime.C rename to src/OSspecific/POSIX/cpuTime/cpuTimePosix.C index 873ab502a94fb7795edc8b8b88cf771f35cb7e9f..cbbbe22d549a0725e3f0df97e729337be4e673c2 100644 --- a/src/OSspecific/POSIX/cpuTime/cpuTime.C +++ b/src/OSspecific/POSIX/cpuTime/cpuTimePosix.C @@ -25,17 +25,18 @@ License \*---------------------------------------------------------------------------*/ -#include "cpuTime.H" +#include "cpuTimePosix.H" #include <unistd.h> // * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * // -const long Foam::cpuTime::clockTicks_(sysconf(_SC_CLK_TCK)); +// Clock-ticks per second +static const long clockTicks_(sysconf(_SC_CLK_TCK)); // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -double Foam::cpuTime::diff(const value_type& a, const value_type& b) +inline double Foam::cpuTimePosix::diff(const value_type& a, const value_type& b) { return ( @@ -47,13 +48,13 @@ double Foam::cpuTime::diff(const value_type& a, const value_type& b) // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::cpuTime::value_type::value_type() +Foam::cpuTimePosix::value_type::value_type() { update(); } -Foam::cpuTime::cpuTime() +Foam::cpuTimePosix::cpuTimePosix() : start_(), last_(start_) @@ -62,27 +63,27 @@ Foam::cpuTime::cpuTime() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -void Foam::cpuTime::value_type::update() +void Foam::cpuTimePosix::value_type::update() { ::times(this); } -void Foam::cpuTime::resetCpuTime() +void Foam::cpuTimePosix::resetCpuTime() { last_.update(); start_ = last_; } -double Foam::cpuTime::elapsedCpuTime() const +double Foam::cpuTimePosix::elapsedCpuTime() const { last_.update(); return diff(last_, start_); } -double Foam::cpuTime::cpuTimeIncrement() const +double Foam::cpuTimePosix::cpuTimeIncrement() const { const value_type prev(last_); last_.update(); diff --git a/src/OSspecific/POSIX/cpuTime/cpuTime.H b/src/OSspecific/POSIX/cpuTime/cpuTimePosix.H similarity index 89% rename from src/OSspecific/POSIX/cpuTime/cpuTime.H rename to src/OSspecific/POSIX/cpuTime/cpuTimePosix.H index f71a2211988c6caa14de7870039a73c9ec3a6713..834f4cb020a6324b236cef5c0c75d1ee660f7476 100644 --- a/src/OSspecific/POSIX/cpuTime/cpuTime.H +++ b/src/OSspecific/POSIX/cpuTime/cpuTimePosix.H @@ -24,7 +24,7 @@ License along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. Class - Foam::cpuTime + Foam::cpuTimePosix Description Starts timing CPU usage and return elapsed time from start. @@ -33,12 +33,12 @@ See also clockTime SourceFiles - cpuTime.C + cpuTimePosix.C \*---------------------------------------------------------------------------*/ -#ifndef cpuTime_H -#define cpuTime_H +#ifndef cpuTimePosix_H +#define cpuTimePosix_H #include <string> #include <ctime> @@ -50,10 +50,10 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class cpuTime Declaration + Class cpuTimePosix Declaration \*---------------------------------------------------------------------------*/ -class cpuTime +class cpuTimePosix { //- Time structure used, with additional methods struct value_type : tms @@ -74,14 +74,11 @@ class cpuTime //- Last time when elapsedTime or timeIncrement was called mutable value_type last_; - //- Clock-ticks per second - static const long clockTicks_; - // Private Member Functions - //- Difference between two times (a - b) - static double diff(const value_type& a, const value_type& b); + //- Difference between two times (a - b) + inline static double diff(const value_type& a, const value_type& b); public: @@ -89,7 +86,7 @@ public: // Constructors //- Construct with the current clock time - cpuTime(); + cpuTimePosix(); // Member Functions diff --git a/src/OSspecific/POSIX/cpuTimeFwd.H b/src/OSspecific/POSIX/cpuTimeFwd.H new file mode 100644 index 0000000000000000000000000000000000000000..60acf4e842dbb429062cb613f7338cfff9a904fe --- /dev/null +++ b/src/OSspecific/POSIX/cpuTimeFwd.H @@ -0,0 +1,49 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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/>. + +Typedef + Foam::cpuTime + +Description + Selection of preferred clock mechanism for the elapsed cpu time. + +\*---------------------------------------------------------------------------*/ + +#ifndef cpuTimeFwd_H +#define cpuTimeFwd_H + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + class cpuTimeCxx; + class cpuTimePosix; + + typedef cpuTimePosix cpuTime; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 5a35b706134e0e7ec51cb555567572f8fc293c96..8d53c6653a54e75ee8d79d301a9edaec76c7e3d9 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -6,6 +6,7 @@ global/argList/argListHelp.C global/clock/clock.C global/clockTime/clockTime.C global/clockValue/clockValue.C +global/cpuTime/cpuTimeCxx.C global/profiling/profiling.C global/profiling/profilingInformation.C global/profiling/profilingSysInfo.C diff --git a/src/OpenFOAM/global/cpuTime/cpuTimeCxx.C b/src/OpenFOAM/global/cpuTime/cpuTimeCxx.C new file mode 100644 index 0000000000000000000000000000000000000000..11d28f07d940cfbedc248a439919d25ad596f6b3 --- /dev/null +++ b/src/OpenFOAM/global/cpuTime/cpuTimeCxx.C @@ -0,0 +1,81 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018-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/>. + +\*---------------------------------------------------------------------------*/ + +#include "cpuTimeCxx.H" + +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +inline double Foam::cpuTimeCxx::diff(const value_type& a, const value_type& b) +{ + return std::difftime(a.value, b.value) / CLOCKS_PER_SEC; +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::cpuTimeCxx::value_type::value_type() +{ + update(); +} + + +Foam::cpuTimeCxx::cpuTimeCxx() +: + start_(), + last_(start_) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +void Foam::cpuTimeCxx::value_type::update() +{ + value = std::clock(); +} + + +void Foam::cpuTimeCxx::resetCpuTime() +{ + last_.update(); + start_ = last_; +} + + +double Foam::cpuTimeCxx::elapsedCpuTime() const +{ + last_.update(); + return diff(last_, start_); +} + + +double Foam::cpuTimeCxx::cpuTimeIncrement() const +{ + const value_type prev(last_); + last_.update(); + return diff(last_, prev); +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/global/cpuTime/cpuTimeCxx.H b/src/OpenFOAM/global/cpuTime/cpuTimeCxx.H new file mode 100644 index 0000000000000000000000000000000000000000..746141013d48f3b0d5d5a459505b640bb7f5031f --- /dev/null +++ b/src/OpenFOAM/global/cpuTime/cpuTimeCxx.H @@ -0,0 +1,111 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018-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/>. + +Class + Foam::cpuTimeCxx + +Description + Starts timing CPU usage and return elapsed time from start. + +See also + clockTime + +SourceFiles + cpuTimeCxx.C + +\*---------------------------------------------------------------------------*/ + +#ifndef cpuTimeCxx_H +#define cpuTimeCxx_H + +#include <ctime> + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class cpuTimeCxx Declaration +\*---------------------------------------------------------------------------*/ + +class cpuTimeCxx +{ + //- Time structure used, with additional methods + struct value_type + { + std::clock_t value; + + //- Construct with the current clock time + value_type(); + + //- 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_; + + + // Private Member Functions + + //- Difference between two times (a - b) + static inline double diff(const value_type& a, const value_type& b); + + +public: + + // Constructors + + //- Construct with the current clock time + cpuTimeCxx(); + + + // 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; + + //- Return CPU time (in seconds) since last call to cpuTimeIncrement() + double cpuTimeIncrement() const; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* //