Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[surf] Triggers the destructed callbacks on the full object
authorGabriel Corona <gabriel.corona@loria.fr>
Fri, 18 Dec 2015 10:26:40 +0000 (11:26 +0100)
committerGabriel Corona <gabriel.corona@loria.fr>
Fri, 18 Dec 2015 11:18:48 +0000 (12:18 +0100)
Doing it in the Cpu/Host destructor is not so great as the object is
not an instance of the derived classes anymore.

12 files changed:
src/surf/cpu_cas01.cpp
src/surf/cpu_interface.cpp
src/surf/cpu_interface.hpp
src/surf/cpu_ti.cpp
src/surf/host_clm03.cpp
src/surf/host_clm03.hpp
src/surf/host_interface.cpp
src/surf/host_interface.hpp
src/surf/host_ptask_L07.cpp
src/surf/host_ptask_L07.hpp
src/surf/surf_interface.cpp
src/surf/surf_interface.hpp

index 0fe7981..9f1a3a2 100644 (file)
@@ -181,7 +181,9 @@ CpuCas01::CpuCas01(CpuCas01Model *model, const char *name, xbt_dynar_t speedPeak
     p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, this);
 }
 
-CpuCas01::~CpuCas01(){
+CpuCas01::~CpuCas01()
+{
+  this->die();
   if (getModel() == surf_cpu_model_pm)
     xbt_dynar_free(&p_speedPeakList);
 }
index 0142878..bd28c4d 100644 (file)
@@ -191,8 +191,15 @@ Cpu::Cpu(Model *model, const char *name,
 : Cpu(model, name, core, speedPeak, speedScale, SURF_RESOURCE_ON)
 {}
 
-Cpu::~Cpu(){
+void Cpu::onDie()
+{
   surf_callback_emit(cpuDestructedCallbacks, this);
+  Resource::onDie();
+}
+
+Cpu::~Cpu()
+{
+  this->die();
   if (p_constraintCoreId){
     for (int i = 0; i < m_core; i++) {
          xbt_free(p_constraintCoreId[i]);
index 8e5a7ce..c68e484 100644 (file)
@@ -176,13 +176,17 @@ public:
   void plug(simgrid::Host* host);
 
   void addTraces(void);
+  simgrid::Host* getHost() { return m_host; }
+
+protected:
+  virtual void onDie() override;
+
+public:
   int m_core = 1;                /* Amount of cores */
   double m_speedPeak;            /*< CPU speed peak, ie max value */
   double m_speedScale;           /*< Percentage of CPU available according to the trace, in [O,1] */
   simgrid::Host* m_host = nullptr;
 
-  simgrid::Host* getHost() { return m_host; }
-
   /* Note (hypervisor): */
   lmm_constraint_t *p_constraintCore=NULL;
   void **p_constraintCoreId=NULL;
index dd729be..3a23bf8 100644 (file)
@@ -584,7 +584,9 @@ CpuTi::CpuTi(CpuTiModel *model, const char *name, xbt_dynar_t speedPeak,
   }
 };
 
-CpuTi::~CpuTi(){
+CpuTi::~CpuTi()
+{
+  this->die();
   modified(false);
   delete p_availTrace;
   delete p_actionSet;
index 189f709..f49434e 100644 (file)
@@ -126,6 +126,11 @@ Action *HostCLM03Model::executeParallelTask(int host_nb,
 HostCLM03::HostCLM03(HostModel *model, const char* name, xbt_dict_t properties, xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu)
   : Host(model, name, properties, storage, netElm, cpu) {}
 
+HostCLM03::~HostCLM03()
+{
+  this->die();
+}
+
 bool HostCLM03::isUsed(){
   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
   return -1;
index e8eeefd..3744da0 100644 (file)
@@ -52,7 +52,7 @@ public:
 class HostCLM03 : public Host {
 public:
   HostCLM03(HostModel *model, const char* name, xbt_dict_t properties, xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu);
-
+  ~HostCLM03();
   void updateState(tmgr_trace_event_t event_type, double value, double date);
 
   virtual Action *execute(double size);
index 134e106..7e9985a 100644 (file)
@@ -107,8 +107,15 @@ Host::Host(simgrid::surf::Model *model, const char *name, xbt_dict_t props, lmm_
   p_params.ramsize = 0;
 }
 
-Host::~Host(){
+void Host::onDie()
+{
   surf_callback_emit(hostDestructedCallbacks, this);
+  Resource::onDie();
+}
+
+Host::~Host()
+{
+  this->die();
 }
 
 void Host::attach(simgrid::Host* host)
index 2ae6d5d..4715963 100644 (file)
@@ -259,6 +259,10 @@ public:
    */
   virtual int fileMove(surf_file_t fd, const char* fullpath);
 
+protected:
+  void onDie() override;
+
+public:
   xbt_dynar_t p_storage;
   RoutingEdge *p_netElm;
   Cpu *p_cpu;
index 75d589d..54b101e 100644 (file)
@@ -405,6 +405,11 @@ HostL07::HostL07(HostModel *model, const char* name, xbt_dict_t props, RoutingEd
 {
 }
 
+HostL07::~HostL07()
+{
+  this->die();
+}
+
 CpuL07::CpuL07(CpuL07Model *model, const char* name,
                     double speedInitial, double speedScale, tmgr_trace_t speedTrace,
                           int core, e_surf_resource_state_t state_initial, tmgr_trace_t state_trace)
@@ -422,6 +427,11 @@ CpuL07::CpuL07(CpuL07Model *model, const char* name,
        p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
 }
 
+CpuL07::~CpuL07()
+{
+  this->die();
+}
+
 LinkL07::LinkL07(NetworkL07Model *model, const char* name, xbt_dict_t props,
                         double bw_initial,
                         tmgr_trace_t bw_trace,
index 2c8f1db..7bc5f09 100644 (file)
@@ -97,6 +97,7 @@ public:
 class HostL07 : public Host {
 public:
   HostL07(HostModel *model, const char* name, xbt_dict_t props, RoutingEdge *netElm, Cpu *cpu);
+  ~HostL07();
   bool isUsed() {DIE_IMPOSSIBLE;};
   void updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/) {DIE_IMPOSSIBLE;};
   Action *execute(double size) {return p_cpu->execute(size);};
@@ -112,6 +113,7 @@ public:
   CpuL07(CpuL07Model *model, const char* name,
                 double power_scale, double power_initial, tmgr_trace_t power_trace,
      int core, e_surf_resource_state_t state_initial, tmgr_trace_t state_trace);
+  ~CpuL07();
   bool isUsed();
   void updateState(tmgr_trace_event_t event_type, double value, double date);
   Action *execute(double size);
index 3f7d33a..2f5bc3b 100644 (file)
@@ -614,7 +614,34 @@ Resource::Resource(Model *model, const char *name, e_surf_resource_state_t state
   , m_running(true), m_stateCurrent(stateInit)
 {}
 
+/** Cleanup code of the full object.
+ *
+ *  The destructed callbacks might need to have a fully
+ *  created `Cpu` instance so they cannot be called `~Cpu()`
+ *  (at this point the fields of the parents have been destroyed
+ *  and the virtual methods are the ones of `Cpu`). If a `Cpu`
+ *  subclass overrides any virtual method, it should call this
+ *  method at the beginning of the destructor in order to trigger
+ *  the callbacks in the real state of the Cpu.
+ *
+ *  Once the method has been called once, it becomes a noop ensuring
+ *  that the callbacks are not for each class of the hierarchy.
+ *
+ *  A better solution would be to call a `Cpu::destroy()` method
+ *  before calling the destructor and trigger the callbacks here.
+ */
+void Resource::die()
+{
+  if (alive_) {
+    onDie();
+    alive_ = false;
+  }
+}
+
+void Resource::onDie() {}
+
 Resource::~Resource() {
+  this->die();
   xbt_free((void*)p_name);
 }
 
index ae7266f..4dce34d 100644 (file)
@@ -488,7 +488,12 @@ public:
   /** @brief Set the [state](\ref e_surf_resource_state_t) of the current Resource */
   virtual void setState(e_surf_resource_state_t state);
 
+protected:
+  void die();
+  virtual void onDie();
+
 private:
+  bool alive_ = true;
   const char *p_name;
   Model *p_model;
   bool m_running;