fw4spl
PosixMemoryMonitorTools.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWMEMORY_TOOLS_POSIXMEMORYMONITORTOOLS_HPP__
8 #define __FWMEMORY_TOOLS_POSIXMEMORYMONITORTOOLS_HPP__
9 
10 #if defined(linux) || defined(__linux)
11 
12 #include "fwMemory/config.hpp"
13 
14 #include <cstdint>
15 #include <string>
16 
17 namespace fwMemory
18 {
19 namespace tools
20 {
21 
22 //------------------------------------------------------------------------------
23 
24 struct MemInfo {
25 
26  std::uint64_t total;
27  std::uint64_t free;
28  std::uint64_t buffered;
29  std::uint64_t cached;
30  std::uint64_t swapcached;
31  std::uint64_t swaptotal;
32  std::uint64_t swapfree;
33 
34  MemInfo()
35  {
36  total = 0;
37  free = 0;
38  buffered = 0;
39  cached = 0;
40  swapcached = 0;
41  swaptotal = 0;
42  swapfree = 0;
43  }
44 
45 };
46 
47 //------------------------------------------------------------------------------
48 
49 struct Status {
50 
51  std::uint64_t VmPeak;
52  std::uint64_t VmSize;
53  std::uint64_t VmLck;
54  std::uint64_t VmHWM;
55  std::uint64_t VmRSS;
56  std::uint64_t VmData;
57  std::uint64_t VmStk;
58  std::uint64_t VmExe;
59  std::uint64_t VmLib;
60  std::uint64_t VmPTE;
61 
62  Status()
63  {
64  VmPeak = 0;
65  VmSize = 0;
66  VmLck = 0;
67  VmHWM = 0;
68  VmRSS = 0;
69  VmData = 0;
70  VmStk = 0;
71  VmExe = 0;
72  VmLib = 0;
73  VmPTE = 0;
74  }
75 
76 };
77 
78 //------------------------------------------------------------------------------
79 
80 class FWMEMORY_CLASS_API PosixMemoryMonitorTools
81 {
82 
83 public:
84 
85  FWMEMORY_API PosixMemoryMonitorTools();
86 
87  FWMEMORY_API ~PosixMemoryMonitorTools();
88 
89  FWMEMORY_API static std::uint64_t estimateFreeMem();
90 
91  FWMEMORY_API static void printProcessMemoryInformation();
92 
93  FWMEMORY_API static void printSystemMemoryInformation();
94 
95  FWMEMORY_API static void printMemoryInformation();
96 
97  FWMEMORY_API static std::uint64_t getTotalSystemMemory();
98 
99  FWMEMORY_API static std::uint64_t getUsedSystemMemory();
100 
101  FWMEMORY_API static std::uint64_t getFreeSystemMemory();
102 
103  FWMEMORY_API static std::uint64_t getUsedProcessMemory();
104 
105 private:
106 
107  static std::uint64_t s_pageSize;
108  static std::uint64_t s_totalMemory;
109 
110  /* Extract numbers from a string between the start and end indices */
111  static std::uint64_t extract_number( char* str, int start, int end );
112 
113  /* Parse the contents of /proc/meminfo file into the meminfo structure */
114  static void get_memory_stats( MemInfo& meminfo );
115 
116  static void printStatus( Status& stat );
117 
118  static void analyseMemInfo( std::string& line, MemInfo& meminfo );
119 
120  static void analyseStatusLine( std::string& line, Status& stat );
121 
122  static void getStatusOfPid( int pid, Status& stat);
123 
124  static void getAllStatus(Status& allStat);
125 
126  static void printAllStatus();
127 };
128 
129 } // namespace tools
130 } // namespace fwMemory
131 
132 #endif //defined(linux) || defined(__linux)
133 
134 #endif // __FWMEMORY_TOOLS_POSIXMEMORYMONITORTOOLS_HPP__
The namespace fwMemory contains tools to manage memory. Use for dump.
Definition: SReader.hpp:20