fw4spl
ValveDump.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 #include "fwMemory/ByteSize.hpp"
8 #include "fwMemory/exception/BadCast.hpp"
9 #include "fwMemory/policy/registry/macros.hpp"
10 #include "fwMemory/policy/ValveDump.hpp"
11 
12 #ifdef _WIN32
13 #define MEMORYTOOLIMPL Win32MemoryMonitorTools
14 #include "fwMemory/tools/Win32MemoryMonitorTools.hpp"
15 #elif defined(__MACOSX__)
16 #define MEMORYTOOLIMPL DarwinMemoryMonitorTools
17 #include "fwMemory/tools/DarwinMemoryMonitorTools.hpp"
18 #else
19 #define MEMORYTOOLIMPL PosixMemoryMonitorTools
20 #include "fwMemory/tools/PosixMemoryMonitorTools.hpp"
21 #endif
22 
23 namespace fwMemory
24 {
25 
26 namespace policy
27 {
28 
29 fwMemoryPolicyRegisterMacro(::fwMemory::policy::ValveDump);
30 
31 //------------------------------------------------------------------------------
32 
33 ValveDump::ValveDump() :
34  m_minFreeMem(1024*1024*500LL),
35  m_hysteresisOffset(0)
36 {
37 }
38 
39 //------------------------------------------------------------------------------
40 
41 void ValveDump::allocationRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
42  BufferInfo::SizeType size )
43 {
44  FwCoreNotUsedMacro(buffer);
45  this->apply((size > info.size) ? size - info.size : 0);
46 }
47 
48 //------------------------------------------------------------------------------
49 
50 void ValveDump::setRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
51  BufferInfo::SizeType size )
52 {
53  FwCoreNotUsedMacro(info);
54  FwCoreNotUsedMacro(buffer);
55  FwCoreNotUsedMacro(size);
56  this->apply();
57 }
58 
59 //------------------------------------------------------------------------------
60 
61 void ValveDump::reallocateRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
62  BufferInfo::SizeType newSize )
63 {
64  FwCoreNotUsedMacro(buffer);
65  this->apply((newSize > info.size) ? newSize - info.size : 0);
66 }
67 
68 //------------------------------------------------------------------------------
69 
70 void ValveDump::destroyRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
71 {
72  FwCoreNotUsedMacro(info);
73  FwCoreNotUsedMacro(buffer);
74 }
75 
76 //------------------------------------------------------------------------------
77 
78 void ValveDump::lockRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
79 {
80  FwCoreNotUsedMacro(info);
81  FwCoreNotUsedMacro(buffer);
82 }
83 
84 //------------------------------------------------------------------------------
85 
86 void ValveDump::unlockRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
87 {
88  FwCoreNotUsedMacro(info);
89  FwCoreNotUsedMacro(buffer);
90  this->apply();
91 }
92 
93 //------------------------------------------------------------------------------
94 
95 void ValveDump::dumpSuccess( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
96 {
97  FwCoreNotUsedMacro(info);
98  FwCoreNotUsedMacro(buffer);
99 }
100 
101 //------------------------------------------------------------------------------
102 
103 void ValveDump::restoreSuccess( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
104 {
105  FwCoreNotUsedMacro(info);
106  FwCoreNotUsedMacro(buffer);
107 }
108 
109 //------------------------------------------------------------------------------
110 
111 bool ValveDump::needDump(size_t supplement) const
112 {
113  return ::fwMemory::tools::MEMORYTOOLIMPL::getFreeSystemMemory() <= (m_minFreeMem + supplement);
114 }
115 
116 //------------------------------------------------------------------------------
117 
118 size_t ValveDump::dump(size_t nbOfBytes)
119 {
120  size_t dumped = 0;
121 
122  ::fwMemory::BufferManager::sptr manager = ::fwMemory::BufferManager::getDefault();
123  if(manager)
124  {
125  const ::fwMemory::BufferManager::BufferInfoMapType bufferInfos = manager->getBufferInfos().get();
126 
127  typedef std::pair<
128  fwMemory::BufferManager::BufferInfoMapType::key_type,
129  fwMemory::BufferManager::BufferInfoMapType::mapped_type
130  > BufferInfosPairType;
131  typedef std::vector< BufferInfosPairType > BufferVectorType;
132 
133  BufferVectorType buffers;
134 
135  for(const ::fwMemory::BufferManager::BufferInfoMapType::value_type& elt : bufferInfos)
136  {
137  const ::fwMemory::BufferInfo& info = elt.second;
138  if( !( info.size == 0 || info.lockCount() > 0 || !info.loaded ) )
139  {
140  buffers.push_back(elt);
141  }
142  }
143 
144  for(const BufferVectorType::value_type& pair : bufferInfos)
145  {
146  if(dumped < nbOfBytes)
147  {
148  if( manager->dumpBuffer(pair.first).get() )
149  {
150  dumped += pair.second.size;
151  }
152  }
153  else
154  {
155  break;
156  }
157  }
158  }
159 
160  return dumped;
161 }
162 
163 //------------------------------------------------------------------------------
164 
165 void ValveDump::apply(size_t supplement)
166 {
167  if(this->needDump(supplement))
168  {
169  this->dump(
170  (m_minFreeMem + m_hysteresisOffset + supplement) -
171  ::fwMemory::tools::MEMORYTOOLIMPL::getFreeSystemMemory() );
172  }
173 }
174 
175 //------------------------------------------------------------------------------
176 
177 void ValveDump::refresh()
178 {
179  this->apply();
180 }
181 
182 //------------------------------------------------------------------------------
183 
184 bool ValveDump::setParam(const std::string& name, const std::string& value)
185 {
186  OSLM_INFO("Set " << name << " to " << value);
187  try
188  {
189  if(name == "min_free_mem")
190  {
191  m_minFreeMem = ::fwMemory::ByteSize(value).getSize();
192  return true;
193 
194  }
195  else if(name == "hysteresis_offet")
196  {
197  m_hysteresisOffset = ::fwMemory::ByteSize(value).getSize();
198  return true;
199  }
200  }
201  catch( ::fwMemory::exception::BadCast const& )
202  {
203  OSLM_ERROR("Bad value for " << name << " : " << value);
204  return false;
205  }
206  OSLM_ERROR("Bad parameter name " << name );
207  return false;
208 }
209 
210 //------------------------------------------------------------------------------
211 
212 const fwMemory::IPolicy::ParamNamesType& ValveDump::getParamNames() const
213 {
214  static const fwMemory::IPolicy::ParamNamesType params = {{ "min_free_mem", "hysteresis_offet" }};
215  return params;
216 }
217 
218 //------------------------------------------------------------------------------
219 
220 std::string ValveDump::getParam(const std::string& name, bool* ok ) const
221 {
222  bool isOk = false;
223  std::string value;
224  if(name == "min_free_mem")
225  {
226  value = std::string(::fwMemory::ByteSize( ::fwMemory::ByteSize::SizeType(m_minFreeMem) ));
227  isOk = true;
228  }
229  else if(name == "hysteresis_offet")
230  {
231  value = std::string(::fwMemory::ByteSize( ::fwMemory::ByteSize::SizeType(m_hysteresisOffset) ));
232  isOk = true;
233  }
234  if (ok)
235  {
236  *ok = isOk;
237  }
238  return value;
239 }
240 
241 //------------------------------------------------------------------------------
242 
243 } // namespace policy
244 
245 } //namespace fwMemory
Conversion helper for size in bytes Converts string to number of bytes and vice-versa. This class is also able to manage conversions between units standards (IEC, SI)
Definition: ByteSize.hpp:25
The namespace fwMemory contains tools to manage memory. Use for dump.
Definition: SReader.hpp:20
static FWMEMORY_API BufferManager::sptr getDefault()
Returns the current BufferManager instance.
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
Implements an exception class for bad cast.
Definition: BadCast.hpp:23
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
Valve dump policy.
Definition: ValveDump.hpp:35
SizeType getSize()
Returns size in bytes.
Definition: ByteSize.hpp:104