Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move the signals from simgrid::surf::Host to simgrid::Host
authorGabriel Corona <gabriel.corona@loria.fr>
Wed, 13 Jan 2016 11:16:04 +0000 (12:16 +0100)
committerGabriel Corona <gabriel.corona@loria.fr>
Wed, 13 Jan 2016 12:55:11 +0000 (13:55 +0100)
Fix issues of dependencies between extensions in destruction.

12 files changed:
include/simgrid/Host.hpp
src/msg/msg_global.cpp
src/simgrid/host.cpp
src/simix/smx_global.cpp
src/surf/callbacks.cpp
src/surf/host_interface.cpp
src/surf/host_interface.hpp
src/surf/network_constant.cpp
src/surf/network_ib.cpp
src/surf/plugins/energy.cpp
src/surf/sg_platf.cpp
src/surf/surf_interface.cpp

index 7d1a51b..50e1fa1 100644 (file)
@@ -16,6 +16,7 @@
 #include <xbt/dict.h>
 #include <xbt/swag.h>
 #include <xbt/string.hpp>
+#include <xbt/signal.hpp>
 #include <xbt/Extendable.hpp>
 
 #include <simgrid/datatypes.h>
@@ -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<void(Host&)> onCreation;
+  /*** Called just before destructing an object */
+  static simgrid::xbt::signal<void(Host&)> onDestruction;
+  /*** Called when the machine is turned on or off */
+  static simgrid::xbt::signal<void(Host&)> onStateChange;
 };
 
 }
index f0df69f..922fb42 100644 (file)
@@ -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()){
index e5198ae..711c740 100644 (file)
@@ -170,6 +170,10 @@ xbt_dict_t sg_host_get_properties(sg_host_t host) {
 
 namespace simgrid {
 
+simgrid::xbt::signal<void(Host&)> Host::onCreation;
+simgrid::xbt::signal<void(Host&)> Host::onDestruction;
+simgrid::xbt::signal<void(Host&)> Host::onStateChange;
+
 Host::Host(std::string const& id)
   : name_(id)
 {
index ea12e3d..bc428ed 100644 (file)
@@ -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_);
 
   }
index 34774a0..7af364a 100644 (file)
 #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) {
index ae57db2..1696064 100644 (file)
@@ -82,16 +82,12 @@ void HostModel::adjustWeightOfDummyCpuActions()
 /************
  * Resource *
  ************/
-simgrid::xbt::signal<void(simgrid::surf::Host*)> Host::onCreation;
-simgrid::xbt::signal<void(simgrid::surf::Host*)> Host::onDestruction;
-simgrid::xbt::signal<void(simgrid::surf::Host*)> Host::onStateChange;
+
 
 void Host::classInit()
 {
   if (!EXTENSION_ID.valid()) {
-    EXTENSION_ID = simgrid::Host::extension_create<simgrid::surf::Host>([](void *h) {
-       static_cast<simgrid::surf::Host*>(h)->destroy();
-    });
+    EXTENSION_ID = simgrid::Host::extension_create<simgrid::surf::Host>();
   }
 }
 
@@ -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);
   }
 }
 
index 02f977f..0676724 100644 (file)
@@ -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<simgrid::Host, Host> EXTENSION_ID;
 
-  /* callbacks */
-  static simgrid::xbt::signal<void(Host*)> onCreation;    /** Called on each newly created object */
-  static simgrid::xbt::signal<void(Host*)> onDestruction; /** Called just before destructing an object */
-  static simgrid::xbt::signal<void(Host*)> 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()
index 673c70c..dfbba3b 100644 (file)
@@ -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);
index 1b62f23..cbeb8ae 100644 (file)
@@ -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);
   
 }
index 33add3f..058ae23 100644 (file)
@@ -193,10 +193,11 @@ void HostEnergy::initWattsRangeList()
 }
 
 /* **************************** events  callback *************************** */
-static void onCreation(simgrid::surf::Host *host) {
-  if (dynamic_cast<simgrid::surf::VirtualMachine*>(host)) // Ignore virtual machines
+static void onCreation(simgrid::Host& host) {
+  simgrid::surf::Host* surf_host = host.extension<simgrid::surf::Host>();
+  if (dynamic_cast<simgrid::surf::VirtualMachine*>(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<simgrid::surf::VirtualMachine*>(host)) // Ignore virtual machines
+static void onHostStateChange(simgrid::Host &host) {
+  simgrid::surf::Host* surf_host = host.extension<simgrid::surf::Host>();
+  if (dynamic_cast<simgrid::surf::VirtualMachine*>(surf_host)) // Ignore virtual machines
     return;
 
-  HostEnergy *host_energy = host->p_host->extension<HostEnergy>();
+  HostEnergy *host_energy = host.extension<HostEnergy>();
 
   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<simgrid::surf::VirtualMachine*>(host))
+  simgrid::surf::Host* surf_host = host.extension<simgrid::surf::Host>();
+  if (dynamic_cast<simgrid::surf::VirtualMachine*>(surf_host))
     return;
-  HostEnergy *host_energy = host->p_host->extension<HostEnergy>();
+  HostEnergy *host_energy = host.extension<HostEnergy>();
   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<HostEnergy>();
 
-  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)
index 9cc96bd..1fb5d32 100644 (file)
@@ -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);
 }
index 6a004ce..5245a67 100644 (file)
@@ -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<simgrid::Host*>(p);
+    simgrid::Host* host = static_cast<simgrid::Host*>(p);
+    simgrid::Host::onDestruction(*host);
+    delete host;
   });
   as_router_lib = xbt_lib_new();
   storage_lib = xbt_lib_new();