From ccc1670e4bf36086f1f524b5ef004d5210415488 Mon Sep 17 00:00:00 2001 From: Gabriel Corona Date: Fri, 18 Dec 2015 11:26:40 +0100 Subject: [PATCH] [surf] Triggers the destructed callbacks on the full object Doing it in the Cpu/Host destructor is not so great as the object is not an instance of the derived classes anymore. --- src/surf/cpu_cas01.cpp | 4 +++- src/surf/cpu_interface.cpp | 9 ++++++++- src/surf/cpu_interface.hpp | 8 ++++++-- src/surf/cpu_ti.cpp | 4 +++- src/surf/host_clm03.cpp | 5 +++++ src/surf/host_clm03.hpp | 2 +- src/surf/host_interface.cpp | 9 ++++++++- src/surf/host_interface.hpp | 4 ++++ src/surf/host_ptask_L07.cpp | 10 ++++++++++ src/surf/host_ptask_L07.hpp | 2 ++ src/surf/surf_interface.cpp | 27 +++++++++++++++++++++++++++ src/surf/surf_interface.hpp | 5 +++++ 12 files changed, 82 insertions(+), 7 deletions(-) diff --git a/src/surf/cpu_cas01.cpp b/src/surf/cpu_cas01.cpp index 0fe79819c2..9f1a3a2c42 100644 --- a/src/surf/cpu_cas01.cpp +++ b/src/surf/cpu_cas01.cpp @@ -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); } diff --git a/src/surf/cpu_interface.cpp b/src/surf/cpu_interface.cpp index 0142878a73..bd28c4d289 100644 --- a/src/surf/cpu_interface.cpp +++ b/src/surf/cpu_interface.cpp @@ -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]); diff --git a/src/surf/cpu_interface.hpp b/src/surf/cpu_interface.hpp index 8e5a7ce7b8..c68e484a26 100644 --- a/src/surf/cpu_interface.hpp +++ b/src/surf/cpu_interface.hpp @@ -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; diff --git a/src/surf/cpu_ti.cpp b/src/surf/cpu_ti.cpp index dd729be66f..3a23bf89a5 100644 --- a/src/surf/cpu_ti.cpp +++ b/src/surf/cpu_ti.cpp @@ -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; diff --git a/src/surf/host_clm03.cpp b/src/surf/host_clm03.cpp index 189f709860..f49434ed37 100644 --- a/src/surf/host_clm03.cpp +++ b/src/surf/host_clm03.cpp @@ -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; diff --git a/src/surf/host_clm03.hpp b/src/surf/host_clm03.hpp index e8eeefd627..3744da090f 100644 --- a/src/surf/host_clm03.hpp +++ b/src/surf/host_clm03.hpp @@ -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); diff --git a/src/surf/host_interface.cpp b/src/surf/host_interface.cpp index 134e10683f..7e9985a2af 100644 --- a/src/surf/host_interface.cpp +++ b/src/surf/host_interface.cpp @@ -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) diff --git a/src/surf/host_interface.hpp b/src/surf/host_interface.hpp index 2ae6d5deef..4715963c24 100644 --- a/src/surf/host_interface.hpp +++ b/src/surf/host_interface.hpp @@ -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; diff --git a/src/surf/host_ptask_L07.cpp b/src/surf/host_ptask_L07.cpp index 75d589d84a..54b101ecb1 100644 --- a/src/surf/host_ptask_L07.cpp +++ b/src/surf/host_ptask_L07.cpp @@ -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, diff --git a/src/surf/host_ptask_L07.hpp b/src/surf/host_ptask_L07.hpp index 2c8f1db293..7bc5f09010 100644 --- a/src/surf/host_ptask_L07.hpp +++ b/src/surf/host_ptask_L07.hpp @@ -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); diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index 3f7d33a191..2f5bc3bd76 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -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); } diff --git a/src/surf/surf_interface.hpp b/src/surf/surf_interface.hpp index ae7266f778..4dce34dd97 100644 --- a/src/surf/surf_interface.hpp +++ b/src/surf/surf_interface.hpp @@ -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; -- 2.20.1