From d77d215c7675b11756d7d0261967572a88f8bd64 Mon Sep 17 00:00:00 2001 From: graham Date: Wed, 17 Nov 2010 17:20:22 +0000 Subject: [PATCH 1/2] ENH: memSize and memSizePeak functions, reading /proc//status. --- src/OSspecific/POSIX/POSIX.C | 55 +++++++++++++++++++++++++++++++ src/OpenFOAM/include/OSspecific.H | 6 ++++ 2 files changed, 61 insertions(+) diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index 8f068f4dba..69d035733e 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -36,6 +36,7 @@ Description #include "fileName.H" #include "fileStat.H" #include "timer.H" +#include "IFstream.H" #include #include @@ -1037,4 +1038,58 @@ 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/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index 7b4c3a5320..bc747b907c 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -176,6 +176,12 @@ 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 From 9f1d94bddd5d0643d8b1eb5ead89110395130478 Mon Sep 17 00:00:00 2001 From: graham Date: Thu, 18 Nov 2010 16:45:49 +0000 Subject: [PATCH 2/2] ENH: memInfo class. --- applications/test/memInfo/Make/files | 3 + applications/test/memInfo/Make/options | 0 applications/test/memInfo/memInfo.C | 19 ++++ src/OSspecific/POSIX/Make/files | 1 + src/OSspecific/POSIX/POSIX.C | 54 ----------- src/OSspecific/POSIX/memInfo/memInfo.C | 117 ++++++++++++++++++++++++ src/OSspecific/POSIX/memInfo/memInfo.H | 118 +++++++++++++++++++++++++ src/OpenFOAM/include/OSspecific.H | 6 -- 8 files changed, 258 insertions(+), 60 deletions(-) create mode 100644 applications/test/memInfo/Make/files create mode 100644 applications/test/memInfo/Make/options create mode 100644 applications/test/memInfo/memInfo.C create mode 100644 src/OSspecific/POSIX/memInfo/memInfo.C create mode 100644 src/OSspecific/POSIX/memInfo/memInfo.H diff --git a/applications/test/memInfo/Make/files b/applications/test/memInfo/Make/files new file mode 100644 index 0000000000..c42564e8fc --- /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 0000000000..e69de29bb2 diff --git a/applications/test/memInfo/memInfo.C b/applications/test/memInfo/memInfo.C new file mode 100644 index 0000000000..fb20be6d8d --- /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 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 788a08105a..c7396b6345 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 69d035733e..fecea27cbe 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 0000000000..ef76fe004f --- /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 . + +\*---------------------------------------------------------------------------*/ + +#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 0000000000..73f614fea7 --- /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 . + +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//status) + int peak_; + + //- Memory used by the process (VmSize in /proc//status) + int size_; + + //- Resident set size of the process (VmRSS in /proc//status) + int rss_; + + +public: + + // Constructors + + //- Construct null + memInfo(); + + + //- Destructor + ~memInfo(); + + + // Member Functions + + //- Parse /proc//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 bc747b907c..7b4c3a5320 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