7 #include "fwMemory/ByteSize.hpp" 8 #include "fwMemory/exception/BadCast.hpp" 9 #include "fwMemory/policy/registry/macros.hpp" 10 #include "fwMemory/policy/ValveDump.hpp" 13 #define MEMORYTOOLIMPL Win32MemoryMonitorTools 14 #include "fwMemory/tools/Win32MemoryMonitorTools.hpp" 15 #elif defined(__MACOSX__) 16 #define MEMORYTOOLIMPL DarwinMemoryMonitorTools 17 #include "fwMemory/tools/DarwinMemoryMonitorTools.hpp" 19 #define MEMORYTOOLIMPL PosixMemoryMonitorTools 20 #include "fwMemory/tools/PosixMemoryMonitorTools.hpp" 33 ValveDump::ValveDump() :
34 m_minFreeMem(1024*1024*500LL),
41 void ValveDump::allocationRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
42 BufferInfo::SizeType size )
44 FwCoreNotUsedMacro(buffer);
45 this->apply((size > info.size) ? size - info.size : 0);
50 void ValveDump::setRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
51 BufferInfo::SizeType size )
53 FwCoreNotUsedMacro(info);
54 FwCoreNotUsedMacro(buffer);
55 FwCoreNotUsedMacro(size);
61 void ValveDump::reallocateRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer,
62 BufferInfo::SizeType newSize )
64 FwCoreNotUsedMacro(buffer);
65 this->apply((newSize > info.size) ? newSize - info.size : 0);
70 void ValveDump::destroyRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
72 FwCoreNotUsedMacro(info);
73 FwCoreNotUsedMacro(buffer);
78 void ValveDump::lockRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
80 FwCoreNotUsedMacro(info);
81 FwCoreNotUsedMacro(buffer);
86 void ValveDump::unlockRequest( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
88 FwCoreNotUsedMacro(info);
89 FwCoreNotUsedMacro(buffer);
95 void ValveDump::dumpSuccess( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
97 FwCoreNotUsedMacro(info);
98 FwCoreNotUsedMacro(buffer);
103 void ValveDump::restoreSuccess( BufferInfo& info, ::fwMemory::BufferManager::ConstBufferPtrType buffer )
105 FwCoreNotUsedMacro(info);
106 FwCoreNotUsedMacro(buffer);
111 bool ValveDump::needDump(
size_t supplement)
const 113 return ::fwMemory::tools::MEMORYTOOLIMPL::getFreeSystemMemory() <= (m_minFreeMem + supplement);
118 size_t ValveDump::dump(
size_t nbOfBytes)
125 const ::fwMemory::BufferManager::BufferInfoMapType bufferInfos = manager->getBufferInfos().get();
128 fwMemory::BufferManager::BufferInfoMapType::key_type,
129 fwMemory::BufferManager::BufferInfoMapType::mapped_type
130 > BufferInfosPairType;
131 typedef std::vector< BufferInfosPairType > BufferVectorType;
133 BufferVectorType buffers;
135 for(const ::fwMemory::BufferManager::BufferInfoMapType::value_type& elt : bufferInfos)
137 const ::fwMemory::BufferInfo& info = elt.second;
138 if( !( info.size == 0 || info.lockCount() > 0 || !info.loaded ) )
140 buffers.push_back(elt);
144 for(
const BufferVectorType::value_type& pair : bufferInfos)
146 if(dumped < nbOfBytes)
148 if( manager->dumpBuffer(pair.first).get() )
150 dumped += pair.second.size;
165 void ValveDump::apply(
size_t supplement)
167 if(this->needDump(supplement))
170 (m_minFreeMem + m_hysteresisOffset + supplement) -
171 ::fwMemory::tools::MEMORYTOOLIMPL::getFreeSystemMemory() );
177 void ValveDump::refresh()
184 bool ValveDump::setParam(
const std::string& name,
const std::string& value)
186 OSLM_INFO(
"Set " << name <<
" to " << value);
189 if(name ==
"min_free_mem")
195 else if(name ==
"hysteresis_offet")
203 OSLM_ERROR(
"Bad value for " << name <<
" : " << value);
212 const fwMemory::IPolicy::ParamNamesType& ValveDump::getParamNames()
const 214 static const fwMemory::IPolicy::ParamNamesType params = {{
"min_free_mem",
"hysteresis_offet" }};
220 std::string ValveDump::getParam(
const std::string& name,
bool* ok )
const 224 if(name ==
"min_free_mem")
226 value = std::string(::
fwMemory::ByteSize( ::fwMemory::ByteSize::SizeType(m_minFreeMem) ));
229 else if(name ==
"hysteresis_offet")
231 value = std::string(::
fwMemory::ByteSize( ::fwMemory::ByteSize::SizeType(m_hysteresisOffset) ));
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.
#define OSLM_INFO(message)
Implements an exception class for bad cast.
#define OSLM_ERROR(message)
SizeType getSize()
Returns size in bytes.