fw4spl
MemoryConsumption.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
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 <sstream>
8 
9 #include <boost/lexical_cast.hpp>
10 
11 #include <fwData/Array.hpp>
12 
13 #include <fwServices/macros.hpp>
14 
15 #include <fwGui/dialog/MessageDialog.hpp>
16 
17 #include "monitor/action/MemoryConsumption.hpp"
18 
19 namespace monitor
20 {
21 namespace action
22 {
23 
25 static std::vector< ::fwData::Array::sptr > memoryConsumer;
26 
27 //-----------------------------------------------------------------------------
28 
30 
31 //-----------------------------------------------------------------------------
32 
33 void MemoryConsumption::pushNewArray(size_t memorySizeInBytes)
34 {
35  try
36  {
37  ::fwData::Array::sptr buffer = ::fwData::Array::New();
38  ::fwData::Array::SizeType size(1, memorySizeInBytes);
39  buffer->resize(::fwTools::Type::s_UINT8_TYPENAME, size, 1, true);
40 
41  OSLM_INFO("Creating a fwData::array consuming "<< memorySizeInBytes/(1024*1024) << " Mo ");
42 
43  memoryConsumer.push_back( buffer );
44  }
45  catch( std::exception &e )
46  {
47  std::stringstream msg;
48  msg << "Cannot allocate buffer (256 Mo) :\n" << e.what() << std::endl;
50  "Action increase memory",
51  msg.str(),
52  ::fwGui::dialog::IMessageDialog::CRITICAL);
53  }
54 }
55 
56 //------------------------------------------------------------------------------
57 
59  m_isIncreaseMode(true), m_memorySizeInBytes(1024*1024*256) // 256 Mo
60 {
61 }
62 
63 //------------------------------------------------------------------------------
64 
66 {
67 }
68 
69 //------------------------------------------------------------------------------
70 
72 {
73  if(m_isIncreaseMode)
74  {
75  this->pushNewArray(m_memorySizeInBytes);
76  }
77  else
78  {
79  if( !memoryConsumer.empty() )
80  {
81  SLM_INFO("Removing one fwData::Array");
82  memoryConsumer.pop_back();
83  }
84  }
85 }
86 
87 //------------------------------------------------------------------------------
88 
90 {
92 
93  ::fwRuntime::ConfigurationElement::sptr consumptionCfg;
94  consumptionCfg = m_configuration->findConfigurationElement("config");
95  SLM_ASSERT("Missing mode tag", consumptionCfg);
96 
97  SLM_ASSERT("Missing attribute 'value'", consumptionCfg->hasAttribute("mode"));
98  std::string mode = consumptionCfg->getAttributeValue("mode");
99  OSLM_ASSERT("Wrong value ("<< mode <<") for mode tag", mode == "increase" || mode == "decrease");
100  m_isIncreaseMode = (mode == "increase");
101 
102  if(m_isIncreaseMode && consumptionCfg->hasAttribute("value"))
103  {
104  std::string value = consumptionCfg->getAttributeValue("value");
105  size_t sizeInMo = ::boost::lexical_cast<size_t>(value);
106  m_memorySizeInBytes = sizeInMo * 1024 * 1024;
107  }
108 }
109 
110 //------------------------------------------------------------------------------
111 
113 {
115 }
116 
117 //------------------------------------------------------------------------------
119 {
121 }
122 
123 //------------------------------------------------------------------------------
124 
125 } // namespace action
126 } // namespace basicOpCtrl
void starting() override
Calls classic IAction methods to start.
std::vector< size_t > SizeType
Array size type.
void stopping() override
Calls classic IAction methods to stop.
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
Definition: IActionSrv.cpp:160
#define OSLM_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:310
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
FWGUI_API void actionServiceStopping()
Method called when the action service is stopping.
Definition: IActionSrv.cpp:153
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
Increase or decrease the memory consumption by storing a new image, use to experiment dump process...
virtual MONITOR_API ~MemoryConsumption() noexcept
Does nothing.
#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
FWGUI_API void initialize()
Initialize the action.
Definition: IActionSrv.cpp:69
Base class for each data object.
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
void updating() override
Increase or decrease memory with size defined during configuration.
void configuring() override
Implements configuring method derived from IService. .
The namespace monitor contains tools for monitoring an application built with FW4SPL.
Definition: fwMetrics.hpp:14
#define SLM_INFO(message)
Definition: spyLog.hpp:250
MONITOR_API MemoryConsumption() noexcept
Does nothing.