diff --git a/applications/test/memInfo/Make/files b/applications/test/memInfo/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..c42564e8fc69cca6781dae6ff6b15ff0d28197e2 --- /dev/null +++ b/applications/test/memInfo/Make/files @@ -0,0 +1,3 @@ +memInfo.C + +EXE = $(FOAM_USER_APPBIN)/memInfo diff --git a/applications/test/memInfo/Make/options b/applications/test/memInfo/Make/options new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/applications/test/memInfo/memInfo.C b/applications/test/memInfo/memInfo.C new file mode 100644 index 0000000000000000000000000000000000000000..fb20be6d8d8adb10e13b0e5701fa056042db22d8 --- /dev/null +++ b/applications/test/memInfo/memInfo.C @@ -0,0 +1,19 @@ +#include "memInfo.H" +#include "IOstreams.H" +#include "List.H" +#include "vector.H" + +using namespace Foam; + +int main() +{ + memInfo m; + + Info<< m << endl; + + List<vector> l(10000000, vector::one); + + Info<< m.update() << endl; + + return 0; +} diff --git a/src/OSspecific/POSIX/Make/files b/src/OSspecific/POSIX/Make/files index 788a08105ae3f40dca14cb2ed15e7ec8af77b363..c7396b63452db5569a2c52116371d2fa3e50aaba 100644 --- a/src/OSspecific/POSIX/Make/files +++ b/src/OSspecific/POSIX/Make/files @@ -8,6 +8,7 @@ fileStat.C POSIX.C cpuTime/cpuTime.C clockTime/clockTime.C +memInfo/memInfo.C /* * Note: fileMonitor assumes inotify by default. Compile with -DFOAM_USE_STAT diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 69d035733ead12848490cf9b5e65c90c31189ec3..fecea27cbe3c98514396df0688ffbe41c925bec0 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -1038,58 +1038,4 @@ int Foam::system(const string& command) } -int Foam::memSize() -{ - IFstream is("/proc/" + name(pid()) + "/status"); - - int VmSize = 0; - - while (is.good()) - { - string line; - is.getLine(line); - char tag[32]; - int value; - - if (sscanf(line.c_str(), "%30s %d", tag, &value) == 2) - { - if (!strcmp(tag, "VmSize:")) - { - VmSize = value; - break; - } - } - } - - return VmSize; -} - - -int Foam::memPeakSize() -{ - IFstream is("/proc/" + name(pid()) + "/status"); - - int VmPeak = 0; - - while (is.good()) - { - string line; - is.getLine(line); - char tag[32]; - int value; - - if (sscanf(line.c_str(), "%30s %d", tag, &value) == 2) - { - if (!strcmp(tag, "VmPeak:")) - { - VmPeak = value; - break; - } - } - } - - return VmPeak; -} - - // ************************************************************************* // diff --git a/src/OSspecific/POSIX/memInfo/memInfo.C b/src/OSspecific/POSIX/memInfo/memInfo.C new file mode 100644 index 0000000000000000000000000000000000000000..ef76fe004ffc158d8ad5a9487c50ba2787a1be2e --- /dev/null +++ b/src/OSspecific/POSIX/memInfo/memInfo.C @@ -0,0 +1,117 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2010-2010 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 "memInfo.H" + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::memInfo::memInfo() +: + peak_(-1), + size_(-1), + rss_(-1) +{ + update(); +} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::memInfo::~memInfo() +{} + + +// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // + +const Foam::memInfo& Foam::memInfo::update() +{ + IFstream is("/proc/" + name(pid()) + "/status"); + + while (is.good()) + { + string line; + is.getLine(line); + char tag[32]; + int value; + + if (sscanf(line.c_str(), "%30s %d", tag, &value) == 2) + { + if (!strcmp(tag, "VmPeak:")) + { + peak_ = value; + } + else if (!strcmp(tag, "VmSize:")) + { + size_ = value; + } + else if (!strcmp(tag, "VmRSS:")) + { + rss_ = value; + } + } + } + + return *this; +} + + +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +Foam::Istream& Foam::operator>>(Istream& is, memInfo& m) +{ + is.readBegin("memInfo"); + + is >> m.peak_ >> m.size_ >> m.rss_; + + is.readEnd("memInfo"); + + // Check state of Istream + is.check + ( + "Foam::Istream& Foam::operator>>(Foam::Istream&, Foam::memInfo&)" + ); + + return is; +} + + +Foam::Ostream& Foam::operator<<(Ostream& os, const memInfo& m) +{ + os << token::BEGIN_LIST + << m.peak_ << token::SPACE << m.size_ << token::SPACE << m.rss_ + << token::END_LIST; + + // Check state of Ostream + os.check + ( + "Foam::Ostream& Foam::operator<<(Foam::Ostream&, " + "const Foam::memInfo&)" + ); + + return os; +} + + +// ************************************************************************* // diff --git a/src/OSspecific/POSIX/memInfo/memInfo.H b/src/OSspecific/POSIX/memInfo/memInfo.H new file mode 100644 index 0000000000000000000000000000000000000000..73f614fea73168b9614286df4681f874088b3352 --- /dev/null +++ b/src/OSspecific/POSIX/memInfo/memInfo.H @@ -0,0 +1,118 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2010-2010 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::memInfo + +Description + Memory usage information for the process running this object. + +SourceFiles + memInfo.C + +\*---------------------------------------------------------------------------*/ + +#ifndef memInfo_H +#define memInfo_H + +#include "OSspecific.H" +#include "POSIX.H" +#include "IFstream.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class memInfo Declaration +\*---------------------------------------------------------------------------*/ + +class memInfo +{ + // Private data + + //- Peak memory used by the process (VmPeak in /proc/<pid>/status) + int peak_; + + //- Memory used by the process (VmSize in /proc/<pid>/status) + int size_; + + //- Resident set size of the process (VmRSS in /proc/<pid>/status) + int rss_; + + +public: + + // Constructors + + //- Construct null + memInfo(); + + + //- Destructor + ~memInfo(); + + + // Member Functions + + //- Parse /proc/<pid>/status + const memInfo& update(); + + // Access + + //- Access the stored peak memory + int peak() const + { + return peak_; + } + + //- Access the stored memory size + int size() const + { + return size_; + } + + //- Access the stored rss value + int rss() const + { + return rss_; + } + + + // IOstream Operators + + friend Istream& operator>>(Istream&, memInfo&); + friend Ostream& operator<<(Ostream&, const memInfo&); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index bc747b907c27ac8570f9bfcf6919c7119809dfcf..7b4c3a53209a3a516ea73ce68611420b3d6d5b3b 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -176,12 +176,6 @@ bool ping(const word&, const label timeOut=10); //- Execute the specified command int system(const string& command); -//- Return the size in memory of the current process -int memSize(); - -//- Return the peak size in memory of the current process -int memPeakSize(); - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam