Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
authorMartin Quinson <martin.quinson@loria.fr>
Thu, 17 Dec 2015 15:39:59 +0000 (16:39 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Thu, 17 Dec 2015 15:39:59 +0000 (16:39 +0100)
20 files changed:
examples/java/surfCpuModel/CpuConstantModel.java
include/simgrid/host.h
include/xbt/Facetable.hpp
include/xbt/string.hpp
src/bindings/java/surf_swig.cpp
src/simdag/sd_global.c
src/simgrid/host.cpp
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_interface.cpp
src/surf/host_interface.hpp
src/surf/host_ptask_L07.cpp
src/surf/sg_platf.cpp
src/surf/surf_c_bindings.cpp
src/surf/surf_interface.cpp
src/surf/virtual_machine.cpp
src/surf/vm_hl13.cpp

index 7a9e5de..60d35dd 100644 (file)
@@ -18,7 +18,6 @@ public class CpuConstantModel extends CpuModel {
 
     CpuConstant res = new CpuConstant(this, name, cpu_properties, core, power_peak[pstate], power_scale);
     cpus.add(res);
-    Surf.setCpu(name, res);
     return res;
   }
 
index e440ef9..83bba0b 100644 (file)
@@ -54,7 +54,6 @@ XBT_PUBLIC(void) sg_host_simix_destroy(sg_host_t host);
 // ========== SURF CPU ============
 XBT_PUBLIC(surf_cpu_t) sg_host_surfcpu(sg_host_t host);
 XBT_PUBLIC(void) sg_host_surfcpu_set(sg_host_t host, surf_cpu_t cpu);
-XBT_PUBLIC(void) sg_host_surfcpu_register(sg_host_t host, surf_cpu_t cpu);
 XBT_PUBLIC(void) sg_host_surfcpu_destroy(sg_host_t host);
 
 // ========== RoutingEdge ============
index 08be86e..84fe635 100644 (file)
@@ -8,12 +8,27 @@
 #define SIMGRID_XBT_LIB_HPP
 
 #include <cstddef>
-
+#include <limits>
 #include <vector>
 
 namespace simgrid {
 namespace xbt {
 
+template<class T, class U> class FacetLevel;
+template<class T>          class Facetable;
+
+template<class T, class U>
+class FacetLevel {
+  static const std::size_t INVALID_ID = std::numeric_limits<std::size_t>::max();
+  std::size_t id_;
+  friend class Facetable<T>;
+  constexpr FacetLevel(std::size_t id) : id_(id) {}
+public:
+  constexpr FacetLevel() : id_(INVALID_ID) {}
+  std::size_t id() const { return id_; }
+  bool valid() { return id_ != INVALID_ID; }
+};
+
 /** A Facetable is an object that you can extend with external facets.
  *
  * Facets are similar to the concept of mixins, that is, a set of behavior that is injected into a class without derivation.
@@ -29,7 +44,6 @@ namespace xbt {
  * Instead, you should add a new facet to the Host class, that happens to be Facetable.
  *
  */
-
 template<class T>
 class Facetable {
 private:
@@ -37,16 +51,21 @@ private:
 protected:
   std::vector<void*> facets_;
 public:
-  static std::size_t add_level(void (*deleter)(void*))
+  static size_t add_level(void (*deleter)(void*))
   {
     std::size_t res = deleters_.size();
     deleters_.push_back(deleter);
     return res;
   }
+  template<class U>
+  static FacetLevel<T,U> add_level(void (*deleter)(void*))
+  {
+    return FacetLevel<T,U>(add_level(deleter));
+  }
   template<class U> static
-  std::size_t add_level()
+  FacetLevel<T,U> add_level()
   {
-    return add_level([](void* p){ delete (U*)p; });
+    return add_level([](void* p){ delete static_cast<U*>(p); });
   }
   Facetable() : facets_(deleters_.size(), nullptr) {}
   ~Facetable()
@@ -56,7 +75,7 @@ public:
         deleters_[i](facets_[i]);
   }
 
-  // TODO, make type-safe versions of this
+  // Type-unsafe versions of the facet access methods:
   void* facet(std::size_t level)
   {
     if (level >= facets_.size())
@@ -73,6 +92,22 @@ public:
     if (use_dtor && old_value != nullptr && deleters_[level])
       deleters_[level](old_value);
   }
+
+  // Type safe versions of the facet access methods:
+  template<class U>
+  U* facet(FacetLevel<T,U> level)
+  {
+    return static_cast<U*>(facet(level.id()));
+  }
+  template<class U>
+  void set_facet(FacetLevel<T,U> level, U* value, bool use_dtor = true)
+  {
+    set_facet(level.id(), value, use_dtor);
+  }
+
+  // Convnience facet access when the type has a associated LEVEL:
+  template<class U> U* facet()           { return facet<U>(U::LEVEL); }
+  template<class U> void set_facet(U* p) { set_facet<U>(U::LEVEL, p); }
 };
 
 template<class T>
index 30d6737..164d1a0 100644 (file)
@@ -30,10 +30,21 @@ struct string_data {
 
 /** A std::string with well-known representation
  *
- *  This is a (incomplete) drop-in replacement for std::string.
+ *  This is a (incomplete) drop-in replacement for `std::string`.
+ *  It has a fixed POD representation (`simgrid::xbt::string_data`)
+ *  which can be used to easily read the string content from another
+ *  process.
  *
- *  This is used for cross-process access to strings
- *  (when the MC is enabled).
+ *  The internal representation of a `std::string` is private.
+ *  We could add some code to read this for a given implementation.
+ *  However, even if we focus on GNU libstdc++ with Itanium ABI
+ *  GNU libstdc++ currently has two different ABIs
+ *
+ *  * the pre-C++11 is a pointer to a ref-counted
+ *    string-representation (with support for COW);
+ *
+ *  * the [C++11-conforming implementation](https://gcc.gnu.org/gcc-5/changes.html)
+ *    does not use refcouting/COW but has a small string optimization.
  */
 XBT_PUBLIC_CLASS string : private string_data {
   static const char NUL;
index a3ccced..972ef47 100644 (file)
@@ -47,7 +47,7 @@ void setCpuModel(simgrid::surf::CpuModel *cpuModel){
 }
 
 void setCpu(char *name, simgrid::surf::Cpu *cpu) {
-       sg_host_surfcpu_set(sg_host_by_name(name), cpu);
+       // No-op here for compatibility with previous versions
 }
 
 LinkDynar getRoute(char *srcName, char *dstName) {
index dad96a3..b625857 100644 (file)
@@ -211,7 +211,7 @@ void SD_create_environment(const char *platform_file)
     xbt_dict_cursor_t cursor = NULL;
     simgrid_Host* host = NULL;
     xbt_dict_foreach(host_list, cursor, name, host){
-      surf_Host* surf_host = (surf_Host*) sg_host_get_facet(host, SURF_HOST_LEVEL);
+      surf_Host* surf_host = (surf_Host*) surf_host_resource_priv(host);
       if (surf_host != NULL)
         __SD_workstation_create(surf_host, NULL);
     }
index d7ec5f1..c4c9f20 100644 (file)
@@ -58,7 +58,6 @@ int MSG_HOST_LEVEL;
 int SD_HOST_LEVEL;
 int SIMIX_HOST_LEVEL;
 int ROUTING_HOST_LEVEL;
-int SURF_CPU_LEVEL;
 int USER_HOST_LEVEL;
 
 #include "src/msg/msg_private.h" // MSG_host_priv_free. FIXME: killme
@@ -81,7 +80,7 @@ void sg_host_init()
   });
   SD_HOST_LEVEL = simgrid::Host::add_level(__SD_workstation_destroy);
   SIMIX_HOST_LEVEL = simgrid::Host::add_level(SIMIX_host_destroy);
-  SURF_CPU_LEVEL = simgrid::Host::add_level(surf_cpu_free);
+  simgrid::surf::Cpu::init();
   ROUTING_HOST_LEVEL = simgrid::Host::add_level(routing_asr_host_free);
   USER_HOST_LEVEL = simgrid::Host::add_level(NULL);
 }
@@ -131,19 +130,13 @@ void sg_host_simix_destroy(sg_host_t host) {
 
 // ========== SURF CPU ============
 surf_cpu_t sg_host_surfcpu(sg_host_t host) {
-       return (surf_cpu_t) host->facet(SURF_CPU_LEVEL);
+       return host->facet<simgrid::surf::Cpu>();
 }
 void sg_host_surfcpu_set(sg_host_t host, surf_cpu_t cpu) {
-  host->set_facet(SURF_CPU_LEVEL, cpu);
-}
-void sg_host_surfcpu_register(sg_host_t host, surf_cpu_t cpu)
-{
-  surf_callback_emit(simgrid::surf::cpuCreatedCallbacks, cpu);
-  surf_callback_emit(simgrid::surf::cpuStateChangedCallbacks, cpu, SURF_RESOURCE_ON, cpu->getState());
-  sg_host_surfcpu_set(host, cpu);
+  host->set_facet(simgrid::surf::Cpu::LEVEL, cpu);
 }
 void sg_host_surfcpu_destroy(sg_host_t host) {
-  host->set_facet(SURF_CPU_LEVEL, nullptr);
+  host->set_facet<simgrid::surf::Cpu>(nullptr);
 }
 // ========== RoutingEdge ============
 surf_RoutingEdge *sg_host_edge(sg_host_t host) {
index 7aac612..afeb09a 100644 (file)
@@ -106,14 +106,10 @@ Cpu *CpuCas01Model::createCpu(const char *name, xbt_dynar_t speedPeak,
                           tmgr_trace_t state_trace,
                           xbt_dict_t cpu_properties)
 {
-  Cpu *cpu = NULL;
-  sg_host_t host = sg_host_by_name(name);
   xbt_assert(xbt_dynar_getfirst_as(speedPeak, double) > 0.0,
       "Speed has to be >0.0. Did you forget to specify the mandatory power attribute?");
   xbt_assert(core > 0, "Invalid number of cores %d. Must be larger than 0", core);
-
-  cpu = new CpuCas01(this, name, speedPeak, pstate, speedScale, speedTrace, core, state_initial, state_trace, cpu_properties);
-  sg_host_surfcpu_register(host, cpu);
+  Cpu *cpu = new CpuCas01(this, name, speedPeak, pstate, speedScale, speedTrace, core, state_initial, state_trace, cpu_properties);
   return cpu;
 }
 
index a8d80c7..d99dad7 100644 (file)
@@ -20,6 +20,14 @@ simgrid::surf::CpuModel *surf_cpu_model_vm;
 namespace simgrid {
 namespace surf {
 
+simgrid::xbt::FacetLevel<simgrid::Host, Cpu> Cpu::LEVEL;
+
+void Cpu::init()
+{
+  if (!LEVEL.valid())
+    LEVEL = simgrid::Host::add_level<simgrid::surf::Cpu>();
+}
+
 /*************
  * Callbacks *
  *************/
@@ -223,6 +231,17 @@ void Cpu::setState(e_surf_resource_state_t state)
   surf_callback_emit(cpuStateChangedCallbacks, this, old, state);
 }
 
+void Cpu::plug(simgrid::Host* host)
+{
+  if (this->m_host != nullptr)
+    xbt_die("Aleady plugged into host %s", host->id().c_str());
+  host->set_facet(this);
+  this->m_host = host;
+  simgrid::surf::cpuCreatedCallbacks(this);
+  simgrid::surf::cpuStateChangedCallbacks(this,
+    SURF_RESOURCE_ON, this->getState());
+}
+
 /**********
  * Action *
  **********/
index c74e737..3ba667a 100644 (file)
@@ -100,6 +100,8 @@ public:
 */
 XBT_PUBLIC_CLASS Cpu : public simgrid::surf::Resource {
 public:
+  static simgrid::xbt::FacetLevel<simgrid::Host, Cpu> LEVEL;
+  static void init();
   Cpu();
 
   /**
@@ -175,15 +177,18 @@ public:
   virtual int  getPstate()=0;
 
   void setState(e_surf_resource_state_t state);
+  void plug(simgrid::Host* host);
 
   void addTraces(void);
   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;
 
   /* Note (hypervisor): */
   lmm_constraint_t *p_constraintCore=NULL;
   void **p_constraintCoreId=NULL;
+
 };
 
 /**********
index 0eeb532..352b1ea 100644 (file)
@@ -446,12 +446,10 @@ Cpu *CpuTiModel::createCpu(const char *name,
                            xbt_dict_t cpuProperties)
 {
   xbt_assert(core==1,"Multi-core not handled with this model yet");
-  sg_host_t host = sg_host_by_name(name);
   xbt_assert(xbt_dynar_getfirst_as(speedPeak, double) > 0.0,
       "Speed has to be >0.0. Did you forget to specify the mandatory speed attribute?");
   CpuTi *cpu = new CpuTi(this, name, speedPeak, pstate, speedScale, speedTrace,
                           core, stateInitial, stateTrace, cpuProperties);
-  sg_host_surfcpu_register(host, cpu);
   return cpu;
 }
 
@@ -977,4 +975,3 @@ double CpuTiAction::getRemains()
 }
 
 #endif /* SURF_MODEL_CPUTI_H_ */
-
index a54d733..3465f78 100644 (file)
@@ -51,9 +51,7 @@ Host *HostCLM03Model::createHost(const char *name,RoutingEdge *netElm, Cpu *cpu)
   Host *host = new simgrid::surf::HostCLM03(surf_host_model, name, NULL,
                  (xbt_dynar_t)xbt_lib_get_or_null(storage_lib, name, ROUTING_STORAGE_HOST_LEVEL),
                  netElm, cpu);
-  surf_callback_emit(hostCreatedCallbacks, host);
   XBT_DEBUG("Create host %s with %ld mounted disks", name, xbt_dynar_length(host->p_storage));
-  simgrid::Host::by_name_or_null(name)->set_facet(SURF_HOST_LEVEL, host);
   return host;
 }
 
index 737583b..e2eb602 100644 (file)
@@ -6,6 +6,8 @@
 
 #include "host_interface.hpp"
 
+#include <simgrid/Host.hpp>
+
 #include "src/simix/smx_private.h"
 #include "cpu_cas01.hpp"
 #include "simgrid/sg_config.h"
@@ -28,7 +30,9 @@ void host_add_traces(){
 
 namespace simgrid {
 namespace surf {
-  
+
+simgrid::xbt::FacetLevel<simgrid::Host, Host> Host::LEVEL;
+
 surf_callback(void, simgrid::surf::Host*) hostCreatedCallbacks;
 surf_callback(void, simgrid::surf::Host*) hostDestructedCallbacks;
 surf_callback(void, simgrid::surf::Host*, e_surf_resource_state_t, e_surf_resource_state_t) hostStateChangedCallbacks;
@@ -76,6 +80,15 @@ void HostModel::adjustWeightOfDummyCpuActions()
 /************
  * Resource *
  ************/
+
+void Host::init()
+{
+  if (!LEVEL.valid()) {
+    LEVEL = simgrid::Host::add_level<simgrid::surf::Host>();
+    SURF_HOST_LEVEL = LEVEL.id();
+  }
+}
+
 Host::Host(simgrid::surf::Model *model, const char *name, xbt_dict_t props,
                                 xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu)
  : Resource(model, name, props)
@@ -96,6 +109,15 @@ Host::~Host(){
   surf_callback_emit(hostDestructedCallbacks, this);
 }
 
+void Host::attach(simgrid::Host* host)
+{
+  if (p_host != nullptr)
+    xbt_die("Already attached to host %s", host->id().c_str());
+  host->set_facet(this);
+  p_host = host;
+  surf_callback_emit(hostCreatedCallbacks, this);
+}
+
 void Host::setState(e_surf_resource_state_t state){
   e_surf_resource_state_t old = Resource::getState();
   Resource::setState(state);
index 70b7c1c..ed143b8 100644 (file)
@@ -96,6 +96,8 @@ public:
  */
 class Host : public simgrid::surf::Resource {
 public:
+  static simgrid::xbt::FacetLevel<simgrid::Host, Host> LEVEL;
+  static void init();
   /**
    * @brief Host constructor
    *
@@ -127,6 +129,7 @@ public:
   /** @brief Host destructor */
   ~Host();
 
+  void attach(simgrid::Host* host);
   void setState(e_surf_resource_state_t state);
 
   /**
@@ -265,6 +268,7 @@ public:
   xbt_dynar_t p_storage;
   RoutingEdge *p_netElm;
   Cpu *p_cpu;
+  simgrid::Host* p_host = nullptr;
 
   /** @brief Get the list of virtual machines on the current Host */
   xbt_dynar_t getVms();
index 82a0951..2f573c0 100644 (file)
@@ -272,12 +272,7 @@ Action *HostL07Model::executeParallelTask(int host_nb,
 
 Host *HostL07Model::createHost(const char *name,RoutingEdge *netElm, Cpu *cpu)
 {
-  HostL07 *host = new HostL07(this, name, NULL, netElm, cpu);
-
-  surf_callback_emit(hostCreatedCallbacks, host);
-  simgrid::Host::by_name_or_create(name)->set_facet(SURF_HOST_LEVEL, host);
-
-  return host;
+  return new HostL07(this, name, NULL, netElm, cpu);
 }
 
 Action *NetworkL07Model::communicate(RoutingEdge *src, RoutingEdge *dst,
@@ -314,12 +309,9 @@ Cpu *CpuL07Model::createCpu(const char *name,  xbt_dynar_t powerPeak,
                           xbt_dict_t cpu_properties)
 {
   double power_initial = xbt_dynar_get_as(powerPeak, pstate, double);
-  sg_host_t sg_host = sg_host_by_name(name);
-
   CpuL07 *cpu = new CpuL07(this, name, cpu_properties,
                                     power_initial, power_scale, power_trace,
                          core, state_initial, state_trace);
-  sg_host_surfcpu_register(sg_host, cpu);
   return cpu;
 }
 
index 206a1ee..bc674ce 100644 (file)
@@ -60,6 +60,7 @@ void sg_platf_new_host(sg_platf_host_cbarg_t host)
   if (current_routing)
     net = routing_add_host(current_routing, host);
 
+  sg_host_t h = simgrid::Host::by_name_or_create(host->id);
   simgrid::surf::Cpu *cpu = surf_cpu_model_pm->createCpu(
         host->id,
         host->speed_peak,
@@ -70,8 +71,8 @@ void sg_platf_new_host(sg_platf_host_cbarg_t host)
         host->initial_state,
         host->state_trace,
         host->properties);
-  surf_host_model->createHost(host->id, net, cpu);
-
+  cpu->plug(h);
+  surf_host_model->createHost(host->id, net, cpu)->attach(h);
   if (TRACE_is_enabled() && TRACE_needs_platform())
     sg_instr_new_host(host);
 }
index fb4004a..740ed9d 100644 (file)
@@ -171,7 +171,7 @@ void routing_get_route_and_latency(sg_routing_edge_t src, sg_routing_edge_t dst,
 surf_host_model_t surf_host_get_model(sg_host_t host)
 {
   simgrid::surf::Host* surf_host =
-    (simgrid::surf::Host*) host->facet(SURF_HOST_LEVEL);
+    (simgrid::surf::Host*) host->facet<simgrid::surf::Host>();
   return (surf_host_model_t) surf_host->getModel();
 }
 
@@ -396,7 +396,7 @@ void surf_vm_destroy(sg_host_t resource){
   sg_host_surfcpu_destroy(resource);
   sg_host_edge_destroy(resource,1);
   // TODO, use backlink from simgrid::surf::Host to simgrid::Host
-  simgrid::Host::by_name_or_null(name)->set_facet(SURF_HOST_LEVEL, nullptr);
+  simgrid::Host::by_name_or_null(name)->set_facet((simgrid::surf::Host*)nullptr);
 
   /* TODO: comment out when VM storage is implemented. */
   // host->set_facet(SURF_STORAGE_LEVEL, nullptr);
index e5cee41..0f43bcf 100644 (file)
@@ -269,11 +269,6 @@ static XBT_INLINE void routing_asr_prop_free(void *p)
   //xbt_dict_free(&elm); FIXME: leaking in some case? That's a sometimes double-free with AsCluster::~AsCluster
 }
 
-static XBT_INLINE void surf_host_free(void *r)
-{
-  delete static_cast<simgrid::surf::Host*>(r);
-}
-
 static XBT_INLINE void surf_storage_free(void *r)
 {
   delete static_cast<simgrid::surf::Storage*>(r);
@@ -321,7 +316,7 @@ void surf_init(int *argc, char **argv)
   ROUTING_PROP_ASR_LEVEL = xbt_lib_add_level(as_router_lib,routing_asr_prop_free);
 
   XBT_DEBUG("Add SURF levels");
-  SURF_HOST_LEVEL = simgrid::Host::add_level(surf_host_free);
+  simgrid::surf::Host::init();
   SURF_STORAGE_LEVEL = xbt_lib_add_level(storage_lib,surf_storage_free);
 
   xbt_init(argc, argv);
index 9be8a78..9fc07fc 100644 (file)
@@ -38,7 +38,7 @@ VirtualMachine::VirtualMachine(Model *model, const char *name, xbt_dict_t props,
 : Host(model, name, props, NULL, netElm, cpu)
 {
   VMModel::ws_vms.push_back(*this);
-  simgrid::Host::by_name_or_create(name)->set_facet(SURF_HOST_LEVEL, this);
+  simgrid::Host::by_name_or_create(name)->set_facet<simgrid::surf::Host>(this);
 }
 
 /*
index c0f76ca..208e24e 100644 (file)
@@ -6,6 +6,8 @@
 
 #include <algorithm>
 
+#include <simgrid/host.h>
+
 #include "cpu_cas01.hpp"
 #include "vm_hl13.hpp"
 
@@ -179,7 +181,8 @@ VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props,
    * is still used by the physical machine. */
   sg_host_t sg_sub_ws = sg_host_by_name_or_create(sub_ws->getName());
   p_netElm = new RoutingEdgeWrapper(sg_host_edge(sg_sub_ws));
-  sg_host_edge_set(sg_host_by_name_or_create(name), p_netElm);
+  sg_host_t host = sg_host_by_name_or_create(name);
+  sg_host_edge_set(host, p_netElm);
 
   p_subWs = sub_ws;
   p_currentState = SURF_VM_STATE_CREATED;
@@ -197,6 +200,7 @@ VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props,
       SURF_RESOURCE_ON,           // host->initial_state,
       NULL,                       // host->state_trace,
       NULL);                       // host->properties,
+  p_cpu->plug(host);
 
   /* We create cpu_action corresponding to a VM process on the host operating system. */
   /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */