fw4spl
Win32MemoryMonitorTools.cpp
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 #ifdef _WIN32
8 
9 #include "fwMemory/tools/Win32MemoryMonitorTools.hpp"
10 
11 #include <fwCore/base.hpp>
12 
13 #include <psapi.h>
14 #include <stdio.h>
15 #include <tchar.h>
16 #include <windows.h>
17 
18 #include <iomanip>
19 #include <iostream>
20 
21 namespace fwMemory
22 {
23 namespace tools
24 {
25 
26 //-----------------------------------------------------------------------------
27 
28 Win32MemoryMonitorTools::Win32MemoryMonitorTools()
29 {
30 }
31 
32 //-----------------------------------------------------------------------------
33 
34 Win32MemoryMonitorTools::~Win32MemoryMonitorTools()
35 {
36 }
37 
38 //-----------------------------------------------------------------------------
39 
40 std::uint64_t Win32MemoryMonitorTools::estimateFreeMem()
41 {
42  std::uint64_t freeMemory = 0;
43 #ifdef _M_X64
44  freeMemory = getFreeSystemMemory();
45 #else
46  std::uint64_t windowsLimitForAProcess = 1.2 * 1024 * 1024 * 1024; // 1.5 Go
47  std::uint64_t freeMem = std::min(
48  getFreeSystemMemory(), windowsLimitForAProcess - getUsedProcessMemory());
49  freeMemory = std::max((std::uint64_t) 0, freeMem );
50 #endif
51  return freeMemory;
52 }
53 
54 //-----------------------------------------------------------------------------
55 
56 void Win32MemoryMonitorTools::printProcessMemoryInformation()
57 {
58  DWORD processID = GetCurrentProcessId();
59  HANDLE hProcess;
60 
61  hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ,
62  FALSE,
63  processID );
64 
65  if ( NULL != hProcess )
66  {
67  // Get the process name.
68  int nameSize = 100;
69  char* name = new char[nameSize];
70 
71  HMODULE hMod;
72  DWORD cbNeeded;
73 
74  SLM_INFO( "-- Process memory information --" );
75  if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
76  {
77  GetModuleBaseNameW( hProcess, hMod, (LPWSTR)name, nameSize );
78  OSLM_INFO( " Name : " << name << " ( PID : " << processID << " )");
79  }
80  else
81  {
82  OSLM_INFO( " Name : not found ( PID : " << processID << " )");
83  }
84 
85  PROCESS_MEMORY_COUNTERS_EX pmc;
86  if ( GetProcessMemoryInfo( hProcess, reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), sizeof(pmc)) )
87  {
88  OSLM_INFO(" PageFaultCount : " << (int)pmc.PageFaultCount );
89  OSLM_INFO(" PeakWorkingSetSize : " << (int)pmc.PeakWorkingSetSize );
90  OSLM_INFO(" WorkingSetSize : " << (int)pmc.WorkingSetSize );
91  OSLM_INFO(" QuotaPeakPagedPoolUsage : " << (int)pmc.QuotaPeakPagedPoolUsage );
92  OSLM_INFO(" QuotaPagedPoolUsage : " << (int)pmc.QuotaPagedPoolUsage );
93  OSLM_INFO(" QuotaPeakNonPagedPoolUsage : " << (int)pmc.QuotaPeakNonPagedPoolUsage );
94  OSLM_INFO(" QuotaNonPagedPoolUsage : " << (int)pmc.QuotaNonPagedPoolUsage );
95  OSLM_INFO(" PagefileUsage : " << (int)pmc.PagefileUsage );
96  OSLM_INFO(" PeakPagefileUsage : " << (int)pmc.PeakPagefileUsage );
97  OSLM_INFO(" PrivateUsage : " << (int)pmc.PrivateUsage );
98  }
99  else
100  {
101  SLM_WARN("GetProcessMemoryInfo failed !");
102  }
103 
104  delete[] name;
105  CloseHandle( hProcess );
106  }
107 }
108 
109 //-----------------------------------------------------------------------------
110 
111 void Win32MemoryMonitorTools::printSystemMemoryInformation()
112 {
113  std::uint64_t oToKo = 1024;
114 
115  MEMORYSTATUSEX statex;
116 
117  statex.dwLength = sizeof (statex);
118  GlobalMemoryStatusEx(&statex);
119 
120  SLM_INFO( "-- System memory information --" );
121  OSLM_INFO( " There is " << statex.dwMemoryLoad << " percent of memory in use." );
122  OSLM_INFO( " There are " << statex.ullTotalPhys/oToKo << " total Ko of physical memory." );
123  OSLM_INFO( " There are " << statex.ullAvailPhys/oToKo << " free Ko of physical memory." );
124  OSLM_INFO( " There are " << statex.ullTotalPageFile/oToKo << " total Ko of paging file." );
125  OSLM_INFO( " There are " << statex.ullAvailPageFile/oToKo << " free Ko of paging file." );
126  OSLM_INFO( " There are " << statex.ullTotalVirtual/oToKo << " total Ko of virtual memory." );
127  OSLM_INFO( " There are " << statex.ullAvailVirtual/oToKo << " free Ko of virtual memory." );
128 }
129 
130 //-----------------------------------------------------------------------------
131 
132 void Win32MemoryMonitorTools::printMemoryInformation()
133 {
134  printSystemMemoryInformation();
135  printProcessMemoryInformation();
136 }
137 
138 //-----------------------------------------------------------------------------
139 
140 std::uint64_t Win32MemoryMonitorTools::getTotalSystemMemory()
141 {
142  MEMORYSTATUSEX statex;
143 
144  statex.dwLength = sizeof (statex);
145  GlobalMemoryStatusEx(&statex);
146 
147  return statex.ullTotalPhys;
148 }
149 
150 //-----------------------------------------------------------------------------
151 
152 std::uint64_t Win32MemoryMonitorTools::getUsedSystemMemory()
153 {
154  MEMORYSTATUSEX statex;
155 
156  statex.dwLength = sizeof (statex);
157  GlobalMemoryStatusEx(&statex);
158 
159  return statex.ullTotalPhys - statex.ullAvailPhys;
160 }
161 
162 //-----------------------------------------------------------------------------
163 
164 std::uint64_t Win32MemoryMonitorTools::getFreeSystemMemory()
165 {
166  MEMORYSTATUSEX statex;
167 
168  statex.dwLength = sizeof (statex);
169  GlobalMemoryStatusEx(&statex);
170 
171  return statex.ullAvailPhys;
172 }
173 
174 //-----------------------------------------------------------------------------
175 
176 std::uint64_t Win32MemoryMonitorTools::getUsedProcessMemory()
177 {
178  std::uint64_t memory = 0;
179 
180  BOOL result;
181  PROCESS_MEMORY_COUNTERS_EX pmc;
182  if ( result =
183  GetProcessMemoryInfo( GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&pmc), sizeof(pmc)) )
184  {
185  memory = pmc.WorkingSetSize;
186  }
187  SLM_WARN_IF("GetProcessMemoryInfo failed !", !result);
188 
189  return memory;
190 }
191 
192 //-----------------------------------------------------------------------------
193 
194 } // namespace tools
195 } // namespace fwMemory
196 
197 #endif
The namespace fwMemory contains tools to manage memory. Use for dump.
Definition: SReader.hpp:20
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
#define SLM_WARN(message)
Definition: spyLog.hpp:261
The namespace memory contains tools to manage memory. It is used for dump. It allows to define the bu...
#define SLM_INFO(message)
Definition: spyLog.hpp:250
#define SLM_WARN_IF(message, cond)
Definition: spyLog.hpp:265