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 // ************************************************************************* //
Input from file stream.
Definition: IFstream.H:85
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
ISstream & getLine(string &, const bool continuation=true)
Read line into a string.
Definition: ISstream.C:703
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Istream & readEnd(const char *funcName)
Definition: Istream.C:103
Istream & readBegin(const char *funcName)
Definition: Istream.C:86
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A line primitive.
Definition: line.H:71
Memory usage information for the process running this object.
Definition: memInfo.H:62
bool valid() const
True if the memory information appears valid.
Definition: memInfo.C:82
memInfo()
Construct null.
Definition: memInfo.C:30
const memInfo & update()
Parse /proc/<pid>/status.
Definition: memInfo.C:48
~memInfo()
Destructor.
Definition: memInfo.C:42
@ BEGIN_LIST
Definition: token.H:109
@ END_LIST
Definition: token.H:110
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
Istream & operator>>(Istream &, pistonPointEdgeData &)
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:73
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)