memInfo.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 
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 // ************************************************************************* //
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
A line primitive.
Definition: line.H:56
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const memInfo & update()
Parse /proc/<pid>/status.
Definition: memInfo.C:48
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
memInfo()
Construct null.
Definition: memInfo.C:30
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
Istream & operator>>(Istream &, directionInfo &)
bool valid() const
True if the memory information appears valid.
Definition: memInfo.C:82
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:73
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
Input from file stream.
Definition: IFstream.H:81
Memory usage information for the process running this object.
Definition: memInfo.H:61
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Ostream & operator<<(Ostream &, const ensightPart &)
~memInfo()
Destructor.
Definition: memInfo.C:42
ISstream & getLine(string &)
Raw, low-level getline into a string function.
Definition: ISstreamI.H:77