From: Gabriel Corona Date: Wed, 13 Jan 2016 11:16:04 +0000 (+0100) Subject: Move the signals from simgrid::surf::Host to simgrid::Host X-Git-Tag: v3_13~1261 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/b23ab258ec14e70d63c28d53c379cd7ccc8a5809 Move the signals from simgrid::surf::Host to simgrid::Host Fix issues of dependencies between extensions in destruction. --- diff --git a/include/simgrid/Host.hpp b/include/simgrid/Host.hpp index 7d1a51b61c..50e1fa1d4a 100644 --- a/include/simgrid/Host.hpp +++ b/include/simgrid/Host.hpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -55,6 +56,13 @@ public: static Host* by_name_or_null(const char* name); static Host* by_name_or_create(const char* name); + + /*** Called on each newly created object */ + static simgrid::xbt::signal onCreation; + /*** Called just before destructing an object */ + static simgrid::xbt::signal onDestruction; + /*** Called when the machine is turned on or off */ + static simgrid::xbt::signal onStateChange; }; } diff --git a/src/msg/msg_global.cpp b/src/msg/msg_global.cpp index f0df69f01e..922fb423f0 100644 --- a/src/msg/msg_global.cpp +++ b/src/msg/msg_global.cpp @@ -68,7 +68,9 @@ void MSG_init_nocheck(int *argc, char **argv) { SIMIX_function_register_process_cleanup(MSG_process_cleanup_from_SIMIX); sg_platf_postparse_add_cb(MSG_post_create_environment); - surf_on_host_created(MSG_host_create_); + simgrid::Host::onCreation.connect([](simgrid::Host& host) { + MSG_host_create_(&host); + }); } if(MC_is_active()){ diff --git a/src/simgrid/host.cpp b/src/simgrid/host.cpp index e5198ae768..711c740666 100644 --- a/src/simgrid/host.cpp +++ b/src/simgrid/host.cpp @@ -170,6 +170,10 @@ xbt_dict_t sg_host_get_properties(sg_host_t host) { namespace simgrid { +simgrid::xbt::signal Host::onCreation; +simgrid::xbt::signal Host::onDestruction; +simgrid::xbt::signal Host::onStateChange; + Host::Host(std::string const& id) : name_(id) { diff --git a/src/simix/smx_global.cpp b/src/simix/smx_global.cpp index ea12e3d477..bc428ed8cd 100644 --- a/src/simix/smx_global.cpp +++ b/src/simix/smx_global.cpp @@ -231,7 +231,9 @@ void SIMIX_global_init(int *argc, char **argv) /* register a function to be called by SURF after the environment creation */ sg_platf_init(); sg_platf_postparse_add_cb(SIMIX_post_create_environment); - surf_on_host_created(SIMIX_host_create); + simgrid::Host::onCreation.connect([](simgrid::Host& host) { + SIMIX_host_create(&host); + }); surf_on_storage_created(SIMIX_storage_create_); } diff --git a/src/surf/callbacks.cpp b/src/surf/callbacks.cpp index 34774a077a..7af364a0fc 100644 --- a/src/surf/callbacks.cpp +++ b/src/surf/callbacks.cpp @@ -10,13 +10,6 @@ #include "src/surf/surf_interface.hpp" #include "src/surf/host_interface.hpp" -void surf_on_host_created(void (*callback)(sg_host_t)) -{ - simgrid::surf::Host::onCreation.connect([callback](simgrid::surf::Host* host) { - callback(host->p_host); - }); -} - void surf_on_storage_created(void (*callback)(sg_storage_t)) { simgrid::surf::storageCreatedCallbacks.connect([callback](simgrid::surf::Storage* storage) { diff --git a/src/surf/host_interface.cpp b/src/surf/host_interface.cpp index ae57db2e00..1696064e88 100644 --- a/src/surf/host_interface.cpp +++ b/src/surf/host_interface.cpp @@ -82,16 +82,12 @@ void HostModel::adjustWeightOfDummyCpuActions() /************ * Resource * ************/ -simgrid::xbt::signal Host::onCreation; -simgrid::xbt::signal Host::onDestruction; -simgrid::xbt::signal Host::onStateChange; + void Host::classInit() { if (!EXTENSION_ID.valid()) { - EXTENSION_ID = simgrid::Host::extension_create([](void *h) { - static_cast(h)->destroy(); - }); + EXTENSION_ID = simgrid::Host::extension_create(); } } @@ -116,23 +112,6 @@ Host::Host(simgrid::surf::HostModel *model, const char *name, xbt_dict_t props, /** @brief use destroy() instead of this destructor */ Host::~Host() { - xbt_assert(currentlyDestroying_, "Don't delete Hosts directly. Call destroy() instead."); - delete p_cpu; - // FIXME: VM plays strange games, leading to segfaults if I do the expected thing of next line - // delete p_netElm; - // delete p_storage; -} -/** @brief Fire the require callbacks and destroy the object - * - * Don't delete directly an Host, call h->destroy() instead. - */ -void Host::destroy() -{ - if (!currentlyDestroying_) { - currentlyDestroying_ = true; - onDestruction(this); - delete this; - } } void Host::attach(simgrid::Host* host) @@ -141,7 +120,6 @@ void Host::attach(simgrid::Host* host) xbt_die("Already attached to host %s", host->getName().c_str()); host->extension_set(this); p_host = host; - onCreation(this); } bool Host::isOn() { @@ -153,13 +131,13 @@ bool Host::isOff() { void Host::turnOn(){ if (isOff()) { p_cpu->turnOn(); - onStateChange(this); + simgrid::Host::onStateChange(*this->p_host); } } void Host::turnOff(){ if (isOn()) { p_cpu->turnOff(); - onStateChange(this); + simgrid::Host::onStateChange(*this->p_host); } } diff --git a/src/surf/host_interface.hpp b/src/surf/host_interface.hpp index 02f977ff58..0676724ed7 100644 --- a/src/surf/host_interface.hpp +++ b/src/surf/host_interface.hpp @@ -33,6 +33,7 @@ class XBT_PRIVATE HostAction; /********* * Tools * *********/ + XBT_PUBLIC_DATA(simgrid::surf::HostModel*) surf_host_model; XBT_PUBLIC(void) host_add_traces(); @@ -78,11 +79,6 @@ class Host : public: static simgrid::xbt::Extension EXTENSION_ID; - /* callbacks */ - static simgrid::xbt::signal onCreation; /** Called on each newly created object */ - static simgrid::xbt::signal onDestruction; /** Called just before destructing an object */ - static simgrid::xbt::signal onStateChange; /** Called when the machine is turned on or off */ - public: static void classInit(); // must be called before the first use of that class /** @@ -112,13 +108,7 @@ public: /* Host destruction logic */ /**************************/ -protected: ~Host(); -public: - void destroy(); // Must be called instead of the destructor -private: - bool currentlyDestroying_ = false; - public: HostModel *getModel() diff --git a/src/surf/network_constant.cpp b/src/surf/network_constant.cpp index 673c70caa5..dfbba3b74f 100644 --- a/src/surf/network_constant.cpp +++ b/src/surf/network_constant.cpp @@ -27,7 +27,7 @@ void surf_network_model_init_Constant() routing_model_create(NULL); - simgrid::surf::Host::onCreation.connect([](simgrid::surf::Host*) { + simgrid::Host::onCreation.connect([](simgrid::Host&) { host_number_int++; }); sg_platf_link_add_cb(netcste_parse_nolink); diff --git a/src/surf/network_ib.cpp b/src/surf/network_ib.cpp index 1b62f23259..cbeb8ae054 100644 --- a/src/surf/network_ib.cpp +++ b/src/surf/network_ib.cpp @@ -14,7 +14,7 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network); -static void IB_create_host_callback(simgrid::surf::Host* host){ +static void IB_create_host_callback(simgrid::Host& host){ using namespace simgrid::surf; static int id=0; @@ -26,8 +26,8 @@ static void IB_create_host_callback(simgrid::surf::Host* host){ id++; xbt_dict_set(((NetworkIBModel*)surf_network_model)->active_nodes, - host->getName(), act, NULL); - + host.getName().c_str(), act, NULL); + } static void IB_action_state_changed_callback( @@ -99,7 +99,7 @@ void surf_network_model_init_IB(void) xbt_dynar_push(all_existing_models, &surf_network_model); networkActionStateChangedCallbacks.connect(IB_action_state_changed_callback); networkCommunicateCallbacks.connect(IB_action_init_callback); - simgrid::surf::Host::onCreation.connect(IB_create_host_callback); + simgrid::Host::onCreation.connect(IB_create_host_callback); xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775); } diff --git a/src/surf/plugins/energy.cpp b/src/surf/plugins/energy.cpp index 33add3f7d4..058ae234f9 100644 --- a/src/surf/plugins/energy.cpp +++ b/src/surf/plugins/energy.cpp @@ -193,10 +193,11 @@ void HostEnergy::initWattsRangeList() } /* **************************** events callback *************************** */ -static void onCreation(simgrid::surf::Host *host) { - if (dynamic_cast(host)) // Ignore virtual machines +static void onCreation(simgrid::Host& host) { + simgrid::surf::Host* surf_host = host.extension(); + if (dynamic_cast(surf_host)) // Ignore virtual machines return; - host->p_host->extension_set(new HostEnergy(host->p_host)); + host.extension_set(new HostEnergy(&host)); } static void onActionStateChange(simgrid::surf::CpuAction *action, @@ -213,23 +214,26 @@ static void onActionStateChange(simgrid::surf::CpuAction *action, host_energy->update(); } -static void onHostStateChange(simgrid::surf::Host *host) { - if (dynamic_cast(host)) // Ignore virtual machines +static void onHostStateChange(simgrid::Host &host) { + simgrid::surf::Host* surf_host = host.extension(); + if (dynamic_cast(surf_host)) // Ignore virtual machines return; - HostEnergy *host_energy = host->p_host->extension(); + HostEnergy *host_energy = host.extension(); if(host_energy->last_updated < surf_get_clock()) host_energy->update(); } -static void onHostDestruction(simgrid::surf::Host *host) { +static void onHostDestruction(simgrid::Host& host) { // Ignore virtual machines - if (dynamic_cast(host)) + simgrid::surf::Host* surf_host = host.extension(); + if (dynamic_cast(surf_host)) return; - HostEnergy *host_energy = host->p_host->extension(); + HostEnergy *host_energy = host.extension(); host_energy->update(); - XBT_INFO("Total energy of host %s: %f Joules", host->getName(), host_energy->getConsumedEnergy()); + XBT_INFO("Total energy of host %s: %f Joules", + host.getName().c_str(), host_energy->getConsumedEnergy()); } /* **************************** Public interface *************************** */ @@ -244,10 +248,10 @@ void sg_energy_plugin_init(void) HostEnergy::EXTENSION_ID = simgrid::Host::extension_create(); - simgrid::surf::Host::onCreation.connect(&onCreation); + simgrid::Host::onCreation.connect(&onCreation); + simgrid::Host::onStateChange.connect(&onHostStateChange); + simgrid::Host::onDestruction.connect(&onHostDestruction); simgrid::surf::CpuAction::onStateChange.connect(&onActionStateChange); - simgrid::surf::Host::onStateChange.connect(&onHostStateChange); - simgrid::surf::Host::onDestruction.connect(&onHostDestruction); } /** @brief Returns the total energy consumed by the host so far (in Joules) diff --git a/src/surf/sg_platf.cpp b/src/surf/sg_platf.cpp index 9cc96bdfd0..1fb5d32668 100644 --- a/src/surf/sg_platf.cpp +++ b/src/surf/sg_platf.cpp @@ -71,6 +71,8 @@ void sg_platf_new_host(sg_platf_host_cbarg_t host) host->initiallyOn, host->state_trace); surf_host_model->createHost(host->id, net, cpu, host->properties)->attach(h); + simgrid::Host::onCreation(*h); + if (TRACE_is_enabled() && TRACE_needs_platform()) sg_instr_new_host(host); } diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index 6a004ce853..5245a67d72 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -304,7 +304,9 @@ void surf_init(int *argc, char **argv) { XBT_DEBUG("Create all Libs"); host_list = xbt_dict_new_homogeneous([](void*p) { - delete static_cast(p); + simgrid::Host* host = static_cast(p); + simgrid::Host::onDestruction(*host); + delete host; }); as_router_lib = xbt_lib_new(); storage_lib = xbt_lib_new();