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 // ************************************************************************* //
Foam::memInfo::valid
bool valid() const
Definition: memInfo.C:44
Foam::memInfo::memInfo
memInfo()
Definition: memInfo.C:31
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
update
mesh update()
Foam::glTF::key
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
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:223
Foam::Istream::readEnd
bool readEnd(const char *funcName)
Definition: Istream.C:122
Foam::Istream::readBegin
bool readBegin(const char *funcName)
Definition: Istream.C:104
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Definition: boundaryPatch.C:76
Foam::memInfo::update
const memInfo & update()
Definition: memInfo.C:57
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::pid
pid_t pid()
Definition: POSIX.C:244
Foam::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.C:51
os
OBJstream os(runTime.globalPath()/outputName)
Foam::memInfo
Memory usage information for the current process, and the system memory that is free.
Definition: memInfo.H:58
memInfo.H
Foam::memInfo::clear
void clear()
Definition: memInfo.C:50
clear
patchWriters clear()
Foam::token::SPACE
@ SPACE
Space [isspace].
Definition: token.H:121
Foam::line
A line primitive.
Definition: line.H:49
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:302
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Definition: Ostream.H:232
Foam::token::END_LIST
@ END_LIST
End list [isseparator].
Definition: token.H:152
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:52
Foam::token::BEGIN_LIST
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.H:151
Foam::memInfo::write
void write(Ostream &os) const
Definition: memInfo.C:154