Commit 9f1d94bd authored by graham's avatar graham
Browse files

ENH: memInfo class.

parent d77d215c
memInfo.C
EXE = $(FOAM_USER_APPBIN)/memInfo
#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;
}
......@@ -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
......
......@@ -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;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
// ************************************************************************* //
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment