memInfo.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "memInfo.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
31 :
32  peak_(-1),
33  size_(-1),
34  rss_(-1)
35 {
36  update();
37 }
38 
39 
40 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
41 
43 {}
44 
45 
46 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
47 
49 {
50  // reset to invalid values first
51  peak_ = size_ = rss_ = -1;
52  IFstream is("/proc/" + name(pid()) + "/status");
53 
54  while (is.good())
55  {
56  string line;
57  is.getLine(line);
58  char tag[32];
59  int value;
60 
61  if (sscanf(line.c_str(), "%30s %d", tag, &value) == 2)
62  {
63  if (!strcmp(tag, "VmPeak:"))
64  {
65  peak_ = value;
66  }
67  else if (!strcmp(tag, "VmSize:"))
68  {
69  size_ = value;
70  }
71  else if (!strcmp(tag, "VmRSS:"))
72  {
73  rss_ = value;
74  }
75  }
76  }
77 
78  return *this;
79 }
80 
81 
83 {
84  return peak_ != -1;
85 }
86 
87 
88 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
89 
91 {
92  is.readBegin("memInfo");
93 
94  is >> m.peak_ >> m.size_ >> m.rss_;
95 
96  is.readEnd("memInfo");
97 
98  // Check state of Istream
99  is.check
100  (
101  "Foam::Istream& Foam::operator>>(Foam::Istream&, Foam::memInfo&)"
102  );
103 
104  return is;
105 }
106 
107 
108 Foam::Ostream& Foam::operator<<(Ostream& os, const memInfo& m)
109 {
110  os << token::BEGIN_LIST
111  << m.peak_ << token::SPACE << m.size_ << token::SPACE << m.rss_
112  << token::END_LIST;
113 
114  // Check state of Ostream
115  os.check
116  (
117  "Foam::Ostream& Foam::operator<<(Foam::Ostream&, "
118  "const Foam::memInfo&)"
119  );
120 
121  return os;
122 }
123 
124 
125 // ************************************************************************* //
Foam::memInfo::valid
bool valid() const
True if the memory information appears valid.
Definition: memInfo.C:82
Foam::Istream::readEnd
Istream & readEnd(const char *funcName)
Definition: Istream.C:105
Foam::memInfo::memInfo
memInfo()
Construct null.
Definition: memInfo.C:30
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::memInfo::~memInfo
~memInfo()
Destructor.
Definition: memInfo.C:42
Foam::memInfo::size_
int size_
Memory used by the process (VmSize in /proc/<pid>/status)
Definition: memInfo.H:61
Foam::memInfo::rss_
int rss_
Resident set size of the process (VmRSS in /proc/<pid>/status)
Definition: memInfo.H:64
Foam::memInfo::update
const memInfo & update()
Parse /proc/<pid>/status.
Definition: memInfo.C:48
Foam::pid
PID_T pid()
Return the PID of this process.
Definition: POSIX.C:77
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::ISstream::getLine
ISstream & getLine(string &)
Raw, low-level getline into a string function.
Definition: ISstreamI.H:77
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
Foam::memInfo
Memory usage information for the process running this object.
Definition: memInfo.H:53
Foam::memInfo::peak_
int peak_
Peak memory used by the process (VmPeak in /proc/<pid>/status)
Definition: memInfo.H:58
memInfo.H
Foam::token::BEGIN_LIST
@ BEGIN_LIST
Definition: token.H:100
Foam::operator>>
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:141
Foam::line
A line primitive.
Definition: line.H:56
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::Istream::readBegin
Istream & readBegin(const char *funcName)
Definition: Istream.C:88
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::token::END_LIST
@ END_LIST
Definition: token.H:101
Foam::token::SPACE
@ SPACE
Definition: token.H:95