fw4spl
BarrierDump.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/policy/BarrierDump.hpp"
8 
9 #include "fwMemory/ByteSize.hpp"
10 #include "fwMemory/exception/BadCast.hpp"
11 #include "fwMemory/policy/registry/macros.hpp"
12 
13 namespace fwMemory
14 {
15 
16 namespace policy
17 {
18 
19 fwMemoryPolicyRegisterMacro(::fwMemory::policy::BarrierDump);
20 
21 //------------------------------------------------------------------------------
22 
23 BarrierDump::BarrierDump() :
24  m_totalAllocated(0),
25  m_totalDumped(0),
26  m_barrier(1024*1024*500)
27 {
28 
29 }
30 
31 //------------------------------------------------------------------------------
32 
33 void BarrierDump::allocationRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
34  BufferInfo::SizeType size )
35 {
36  SLM_ASSERT("Memory allocation inconsistency", m_totalAllocated >= info.size);
37  m_totalAllocated -= info.size;
38  m_totalAllocated += size;
39  if(!info.loaded)
40  {
41  SLM_ASSERT("Memory dump inconsistency", m_totalDumped >= info.size);
42  m_totalDumped -= info.size;
43  }
44  this->apply();
45 }
46 
47 //------------------------------------------------------------------------------
48 
49 void BarrierDump::setRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
50  BufferInfo::SizeType size )
51 {
52  SLM_ASSERT("Memory allocation inconsistency", m_totalAllocated >= info.size);
53  m_totalAllocated -= info.size;
54  m_totalAllocated += size;
55  if(!info.loaded)
56  {
57  SLM_ASSERT("Memory dump inconsistency", m_totalDumped >= info.size);
58  m_totalDumped -= info.size;
59  }
60  this->apply();
61 }
62 
63 //------------------------------------------------------------------------------
64 
65 void BarrierDump::reallocateRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
66  BufferInfo::SizeType newSize )
67 {
68  SLM_ASSERT("Memory allocation inconsistency", m_totalAllocated >= info.size);
69  m_totalAllocated -= info.size;
70  m_totalAllocated += newSize;
71  if(!info.loaded)
72  {
73  SLM_ASSERT("Memory dump inconsistency", m_totalDumped >= info.size);
74  m_totalDumped -= info.size;
75  }
76  this->apply();
77 }
78 
79 //------------------------------------------------------------------------------
80 
81 void BarrierDump::destroyRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
82 {
83  if(!info.loaded)
84  {
85  SLM_ASSERT("Memory dump inconsistency", m_totalDumped >= info.size);
86  m_totalDumped -= info.size;
87  }
88  SLM_ASSERT("Memory allocation inconsistency", m_totalAllocated >= info.size);
89  m_totalAllocated -= info.size;
90 }
91 
92 //------------------------------------------------------------------------------
93 
94 void BarrierDump::lockRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
95 {
96 }
97 
98 //------------------------------------------------------------------------------
99 
100 void BarrierDump::unlockRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
101 {
102  this->apply();
103 }
104 
105 //------------------------------------------------------------------------------
106 
107 void BarrierDump::dumpSuccess( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
108 {
109  m_totalDumped += info.size;
110 }
111 
112 //------------------------------------------------------------------------------
113 
114 void BarrierDump::restoreSuccess( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
115 {
116  SLM_ASSERT("Memory dump inconsistency", m_totalDumped >= info.size);
117  m_totalDumped -= info.size;
118 }
119 
120 //------------------------------------------------------------------------------
121 
122 size_t BarrierDump::getTotalAlive() const
123 {
124  SLM_ASSERT("More dumped data than allocated data.", m_totalAllocated >= m_totalDumped);
125  size_t totalAlive = m_totalAllocated - m_totalDumped;
126  return totalAlive;
127 }
128 
129 //------------------------------------------------------------------------------
130 
131 bool BarrierDump::isBarrierCrossed() const
132 {
133  return m_barrier < getTotalAlive();
134 }
135 
136 //------------------------------------------------------------------------------
137 
138 size_t BarrierDump::dump(size_t nbOfBytes)
139 {
140  size_t dumped = 0;
141 
142  ::fwMemory::BufferManager::sptr manager = ::fwMemory::BufferManager::getDefault();
143  if(manager)
144  {
145  const ::fwMemory::BufferManager::BufferInfoMapType bufferInfos = manager->getBufferInfos().get();
146 
147  typedef std::pair<
148  ::fwMemory::BufferManager::BufferInfoMapType::key_type,
149  ::fwMemory::BufferManager::BufferInfoMapType::mapped_type
150  > BufferInfosPairType;
151  typedef std::vector< BufferInfosPairType > BufferVectorType;
152 
153  BufferVectorType buffers;
154 
155  for(const ::fwMemory::BufferManager::BufferInfoMapType::value_type& elt : bufferInfos)
156  {
157  const ::fwMemory::BufferInfo& info = elt.second;
158  if( !( info.size == 0 || info.lockCount() > 0 || !info.loaded ) )
159  {
160  buffers.push_back(elt);
161  }
162  }
163 
164  for(const BufferVectorType::value_type& pair : bufferInfos)
165  {
166  if(dumped < nbOfBytes)
167  {
168  if( manager->dumpBuffer(pair.first).get() )
169  {
170  dumped += pair.second.size;
171  }
172  }
173  else
174  {
175  break;
176  }
177  }
178  }
179 
180  return dumped;
181 }
182 
183 //------------------------------------------------------------------------------
184 
185 void BarrierDump::apply()
186 {
187  if(this->isBarrierCrossed())
188  {
189  this->dump(this->getTotalAlive() - m_barrier);
190  }
191 }
192 
193 //------------------------------------------------------------------------------
194 
195 void BarrierDump::refresh()
196 {
197  ::fwMemory::BufferManager::sptr manager = ::fwMemory::BufferManager::getDefault();
198  ::fwMemory::BufferManager::BufferStats stats = manager->getBufferStats().get();
199  m_totalAllocated = stats.totalManaged;
200  m_totalDumped = stats.totalDumped;
201  this->apply();
202 }
203 
204 //------------------------------------------------------------------------------
205 
206 bool BarrierDump::setParam(const std::string& name, const std::string& value)
207 {
208  try
209  {
210  if(name == "barrier")
211  {
212  m_barrier = ::fwMemory::ByteSize(value).getSize();
213  return true;
214  }
215  }
216  catch( ::fwMemory::exception::BadCast const& )
217  {
218  OSLM_ERROR("Bad value for " << name << " : " << value);
219  return false;
220  }
221 
222  OSLM_ERROR("Bad parameter name " << name );
223  return false;
224 }
225 
226 //------------------------------------------------------------------------------
227 
228 const ::fwMemory::IPolicy::ParamNamesType& BarrierDump::getParamNames() const
229 {
230  static const ::fwMemory::IPolicy::ParamNamesType params = {{ "barrier" }};
231  return params;
232 }
233 
234 //------------------------------------------------------------------------------
235 
236 std::string BarrierDump::getParam(const std::string& name, bool* ok ) const
237 {
238  bool isOk = false;
239  std::string value;
240  if(name == "barrier")
241  {
242  value = std::string(::fwMemory::ByteSize( ::fwMemory::ByteSize::SizeType(m_barrier) ));
243  isOk = true;
244  }
245  if (ok)
246  {
247  *ok = isOk;
248  }
249  return value;
250 }
251 
252 //------------------------------------------------------------------------------
253 
254 } // namespace policy
255 
256 } //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.
Implements an exception class for bad cast.
Definition: BadCast.hpp:23
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
Barrier dump policy.
Definition: BarrierDump.hpp:30
SizeType getSize()
Returns size in bytes.
Definition: ByteSize.hpp:104