memInfo.H
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-2023 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 Class
28  Foam::memInfo
29 
30 Description
31  Memory usage information for the current process, and the system memory
32  that is free.
33 
34 Note
35  Uses the information from /proc/PID/status and from /proc/meminfo
36 
37 SourceFiles
38  memInfo.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Foam_memInfo_H
43 #define Foam_memInfo_H
44 
45 #include <cstdint>
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 class memInfo;
54 class word;
55 class Ostream;
56 
57 Ostream& operator<<(Ostream& os, const memInfo& m);
58 
59 /*---------------------------------------------------------------------------*\
60  Class memInfo Declaration
61 \*---------------------------------------------------------------------------*/
62 
63 class memInfo
64 {
65  // Private Data
66 
67  //- Peak memory used by the process (VmPeak in /proc/PID/status)
68  int64_t peak_;
69 
70  //- Memory used by the process (VmSize in /proc/PID/status)
71  int64_t size_;
72 
73  //- Resident set size of the process (VmRSS in /proc/PID/status)
74  int64_t rss_;
75 
76  //- System memory free (MemFree in /proc/meminfo)
77  int64_t free_;
78 
79 
80  // Private Member Functions
81 
82  //- Populate entries
83  void populate();
84 
85 
86 public:
87 
88  // Constructors
89 
90  //- Construct and populate with values
91  memInfo();
92 
93 
94  // Member Functions
95 
96  // Access
97 
98  //- True if the memory information appears valid
99  bool good() const noexcept;
100 
101  //- Peak memory at last update - (VmPeak in /proc/PID/status)
102  int64_t peak() const noexcept { return peak_; }
103 
104  //- Memory size at last update - (VmSize in /proc/PID/status)
105  int64_t size() const noexcept { return size_; }
106 
107  //- Resident set size at last update - (VmRSS in /proc/PID/status)
108  int64_t rss() const noexcept { return rss_; }
109 
110  //- System memory free (MemFree in /proc/meminfo)
111  int64_t free() const noexcept { return free_; }
112 
114  // Edit
115 
116  //- Reset to zero
117  void clear() noexcept;
119  //- Update according to /proc/PID/status and /proc/memory contents
120  const memInfo& update();
121 
122 
123  // IOstream Operators
124 
125  //- Write peak/size/rss to stream
126  friend Ostream& operator<<(Ostream& os, const memInfo& m);
127 
129  // Write
130 
131  //- Write mem-info as dictionary entries
132  void writeEntries(Ostream& os) const;
133 
134  //- Write mem-info as dictionary
135  void writeEntry(const word& keyword, Ostream& os) const;
136 };
137 
138 
139 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
140 
141 } // End namespace Foam
142 
143 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
144 
145 #endif
146 
147 // ************************************************************************* //
int64_t rss() const noexcept
Resident set size at last update - (VmRSS in /proc/PID/status)
Definition: memInfo.H:123
void clear() noexcept
Reset to zero.
Definition: memInfo.C:51
const memInfo & update()
Update according to /proc/PID/status and /proc/memory contents.
Definition: memInfo.C:153
int64_t free() const noexcept
System memory free (MemFree in /proc/meminfo)
Definition: memInfo.H:128
void writeEntry(const word &keyword, Ostream &os) const
Write mem-info as dictionary.
Definition: memInfo.C:171
memInfo()
Construct and populate with values.
Definition: memInfo.C:32
A class for handling words, derived from Foam::string.
Definition: word.H:63
int64_t peak() const noexcept
Peak memory at last update - (VmPeak in /proc/PID/status)
Definition: memInfo.H:113
bool good() const noexcept
True if the memory information appears valid.
Definition: memInfo.C:45
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
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:77
void writeEntries(Ostream &os) const
Write mem-info as dictionary entries.
Definition: memInfo.C:161
int64_t size() const noexcept
Memory size at last update - (VmSize in /proc/PID/status)
Definition: memInfo.H:118
Namespace for OpenFOAM.