7 #include "fwMemory/policy/BarrierDump.hpp" 9 #include "fwMemory/ByteSize.hpp" 10 #include "fwMemory/exception/BadCast.hpp" 11 #include "fwMemory/policy/registry/macros.hpp" 23 BarrierDump::BarrierDump() :
26 m_barrier(1024*1024*500)
33 void BarrierDump::allocationRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
34 BufferInfo::SizeType size )
36 SLM_ASSERT(
"Memory allocation inconsistency", m_totalAllocated >= info.size);
37 m_totalAllocated -= info.size;
38 m_totalAllocated += size;
41 SLM_ASSERT(
"Memory dump inconsistency", m_totalDumped >= info.size);
42 m_totalDumped -= info.size;
49 void BarrierDump::setRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
50 BufferInfo::SizeType size )
52 SLM_ASSERT(
"Memory allocation inconsistency", m_totalAllocated >= info.size);
53 m_totalAllocated -= info.size;
54 m_totalAllocated += size;
57 SLM_ASSERT(
"Memory dump inconsistency", m_totalDumped >= info.size);
58 m_totalDumped -= info.size;
65 void BarrierDump::reallocateRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
66 BufferInfo::SizeType newSize )
68 SLM_ASSERT(
"Memory allocation inconsistency", m_totalAllocated >= info.size);
69 m_totalAllocated -= info.size;
70 m_totalAllocated += newSize;
73 SLM_ASSERT(
"Memory dump inconsistency", m_totalDumped >= info.size);
74 m_totalDumped -= info.size;
81 void BarrierDump::destroyRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
85 SLM_ASSERT(
"Memory dump inconsistency", m_totalDumped >= info.size);
86 m_totalDumped -= info.size;
88 SLM_ASSERT(
"Memory allocation inconsistency", m_totalAllocated >= info.size);
89 m_totalAllocated -= info.size;
94 void BarrierDump::lockRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
100 void BarrierDump::unlockRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
107 void BarrierDump::dumpSuccess( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
109 m_totalDumped += info.size;
114 void BarrierDump::restoreSuccess( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
116 SLM_ASSERT(
"Memory dump inconsistency", m_totalDumped >= info.size);
117 m_totalDumped -= info.size;
122 size_t BarrierDump::getTotalAlive()
const 124 SLM_ASSERT(
"More dumped data than allocated data.", m_totalAllocated >= m_totalDumped);
125 size_t totalAlive = m_totalAllocated - m_totalDumped;
131 bool BarrierDump::isBarrierCrossed()
const 133 return m_barrier < getTotalAlive();
138 size_t BarrierDump::dump(
size_t nbOfBytes)
145 const ::fwMemory::BufferManager::BufferInfoMapType bufferInfos = manager->getBufferInfos().get();
148 ::fwMemory::BufferManager::BufferInfoMapType::key_type,
149 ::fwMemory::BufferManager::BufferInfoMapType::mapped_type
150 > BufferInfosPairType;
151 typedef std::vector< BufferInfosPairType > BufferVectorType;
153 BufferVectorType buffers;
155 for(const ::fwMemory::BufferManager::BufferInfoMapType::value_type& elt : bufferInfos)
157 const ::fwMemory::BufferInfo& info = elt.second;
158 if( !( info.size == 0 || info.lockCount() > 0 || !info.loaded ) )
160 buffers.push_back(elt);
164 for(
const BufferVectorType::value_type& pair : bufferInfos)
166 if(dumped < nbOfBytes)
168 if( manager->dumpBuffer(pair.first).get() )
170 dumped += pair.second.size;
185 void BarrierDump::apply()
187 if(this->isBarrierCrossed())
189 this->dump(this->getTotalAlive() - m_barrier);
195 void BarrierDump::refresh()
199 m_totalAllocated = stats.totalManaged;
200 m_totalDumped = stats.totalDumped;
206 bool BarrierDump::setParam(
const std::string& name,
const std::string& value)
210 if(name ==
"barrier")
218 OSLM_ERROR(
"Bad value for " << name <<
" : " << value);
228 const ::fwMemory::IPolicy::ParamNamesType& BarrierDump::getParamNames()
const 230 static const ::fwMemory::IPolicy::ParamNamesType params = {{
"barrier" }};
236 std::string BarrierDump::getParam(
const std::string& name,
bool* ok )
const 240 if(name ==
"barrier")
242 value = std::string(::
fwMemory::ByteSize( ::fwMemory::ByteSize::SizeType(m_barrier) ));
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)
The namespace fwMemory contains tools to manage memory. Use for dump.
static FWMEMORY_API BufferManager::sptr getDefault()
Returns the current BufferManager instance.
Implements an exception class for bad cast.
#define OSLM_ERROR(message)
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
SizeType getSize()
Returns size in bytes.