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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011 OpenFOAM Foundation
9  Copyright (C) 2016-2017 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "memInfo.H"
30 #include "OSspecific.H"
31 #include "IOstreams.H"
32 
33 #include <fstream>
34 #include <string>
35 
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37 
39 :
40  peak_(0),
41  size_(0),
42  rss_(0),
43  free_(0)
44 {
45  update();
46 }
47 
48 
49 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
50 
51 bool Foam::memInfo::valid() const
52 {
53  return peak_ > 0;
54 }
55 
56 
58 {
59  peak_ = size_ = rss_ = 0;
60  free_ = 0;
61 }
62 
63 
65 {
66  clear();
67  std::string line;
68 
69  // "/proc/PID/status"
70  // ===========================
71  // VmPeak: 15920 kB
72  // VmSize: 15916 kB
73  // VmLck: 0 kB
74  // VmPin: 0 kB
75  // VmHWM: 6972 kB
76  // VmRSS: 6972 kB
77  // ...
78  // Stop parsing when known keys have been extracted
79  {
80  std::ifstream is("/proc/" + std::to_string(Foam::pid()) + "/status");
81 
82  for
83  (
84  unsigned nkeys = 3;
85  nkeys && is.good() && std::getline(is, line);
86  /*nil*/
87  )
88  {
89  const auto delim = line.find(':');
90  if (delim == std::string::npos)
91  {
92  continue;
93  }
94 
95  const std::string key(line.substr(0, delim));
96 
97  // std::stoi() skips whitespace before using as many digits as
98  // possible. So just need to skip over the ':' and let stoi do
99  // the rest
100 
101  if (key == "VmPeak")
102  {
103  peak_ = std::stoi(line.substr(delim+1));
104  --nkeys;
105  }
106  else if (key == "VmSize")
107  {
108  size_ = std::stoi(line.substr(delim+1));
109  --nkeys;
110  }
111  else if (key == "VmRSS")
112  {
113  rss_ = std::stoi(line.substr(delim+1));
114  --nkeys;
115  }
116  }
117  }
118 
119  // "/proc/meminfo"
120  // ===========================
121  // MemTotal: 65879268 kB
122  // MemFree: 51544256 kB
123  // MemAvailable: 58999636 kB
124  // Buffers: 2116 kB
125  // ...
126  // Stop parsing when known keys have been extracted
127  {
128  std::ifstream is("/proc/meminfo");
129 
130  for
131  (
132  unsigned nkeys = 1;
133  nkeys && is.good() && std::getline(is, line);
134  /*nil*/
135  )
136  {
137  const auto delim = line.find(':');
138  if (delim == std::string::npos)
139  {
140  continue;
141  }
142 
143  const std::string key = line.substr(0, delim);
144 
145  // std::stoi() skips whitespace before using as many digits as
146  // possible. So just need to skip over the ':' and let stoi do
147  // the rest
148 
149  if (key == "MemFree")
150  {
151  free_ = std::stoi(line.substr(delim+1));
152  --nkeys;
153  }
154  }
155  }
156 
157  return *this;
158 }
159 
160 
161 void Foam::memInfo::write(Ostream& os) const
162 {
163  os.writeEntry("size", size_);
164  os.writeEntry("peak", peak_);
165  os.writeEntry("rss", rss_);
166  os.writeEntry("free", free_);
167 }
168 
169 
170 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
171 
172 Foam::Istream& Foam::operator>>(Istream& is, memInfo& m)
173 {
174  is.readBegin("memInfo");
175  is >> m.peak_ >> m.size_ >> m.rss_ >> m.free_;
176  is.readEnd("memInfo");
177 
178  is.check(FUNCTION_NAME);
179  return is;
180 }
181 
182 
183 Foam::Ostream& Foam::operator<<(Ostream& os, const memInfo& m)
184 {
186  << m.peak_ << token::SPACE
187  << m.size_ << token::SPACE
188  << m.rss_ << token::SPACE
189  << m.free_
190  << token::END_LIST;
191 
193  return os;
194 }
195 
196 
197 // ************************************************************************* //
A line primitive.
Definition: line.H:52
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:104
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const memInfo & update()
Update according to /proc/PID/status and /proc/memory contents.
Definition: memInfo.C:57
Begin list [isseparator].
Definition: token.H:158
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:312
void clear()
Reset to zero.
Definition: memInfo.C:50
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
memInfo()
Construct and populate with values.
Definition: memInfo.C:31
Istream & operator>>(Istream &, directionInfo &)
Space [isspace].
Definition: token.H:128
End list [isseparator].
Definition: token.H:159
patchWriters clear()
bool valid() const
True if the memory information appears valid.
Definition: memInfo.C:44
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:267
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
mesh update()
Memory usage information for the current process, and the system memory that is free.
Definition: memInfo.H:58
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:76
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
void write(Ostream &os) const
Write content as dictionary entries.
Definition: memInfo.C:154