Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make trace_connect_list_host_speed private as it should
[simgrid.git] / src / surf / network_interface.cpp
index 14bd060..4489f8a 100644 (file)
@@ -38,19 +38,19 @@ double sg_link_latency(Link *link){
   return link->getLatency();
 }
 void* sg_link_data(Link *link) {
-       return link->getData();
+  return link->getData();
 }
 void sg_link_data_set(Link *link,void *data) {
-       link->setData(data);
+  link->setData(data);
 }
-int sg_link_amount(void) {
-       return Link::linksAmount();
+int sg_link_count(void) {
+  return Link::linksCount();
 }
 Link** sg_link_list(void) {
-       return Link::linksList();
+  return Link::linksList();
 }
 void sg_link_exit(void) {
-       Link::linksExit();
+  Link::linksExit();
 }
 
 }
@@ -64,42 +64,43 @@ namespace surf {
 
 boost::unordered_map<std::string,Link *> *Link::links = new boost::unordered_map<std::string,Link *>();
 Link *Link::byName(const char* name) {
-         Link * res = NULL;
-         try {
-                 res = links->at(name);
-         } catch (std::out_of_range& e) {}
+    Link * res = NULL;
+    try {
+      res = links->at(name);
+    } catch (std::out_of_range& e) {}
 
-         return res;
+    return res;
 }
 /** @brief Returns the amount of links in the platform */
-int Link::linksAmount() {
-         return links->size();
+int Link::linksCount() {
+    return links->size();
 }
 /** @brief Returns a list of all existing links */
 Link **Link::linksList() {
-         Link **res = xbt_new(Link*, (int)links->size());
-         int i=0;
-         for (auto kv : *links) {
-                 res[i++] = kv.second;
-         }
-         return res;
+    Link **res = xbt_new(Link*, (int)links->size());
+    int i=0;
+    for (auto kv : *links) {
+      res[i++] = kv.second;
+    }
+    return res;
 }
 /** @brief destructor of the static data */
 void Link::linksExit() {
-       for (auto kv : *links)
-               delete (kv.second);
-       delete links;
+  for (auto kv : *links)
+    (kv.second)->destroy();
+  delete links;
 }
 
 /*************
  * Callbacks *
  *************/
 
-surf_callback(void, simgrid::surf::Link*) networkLinkCreatedCallbacks;
-surf_callback(void, simgrid::surf::Link*) networkLinkDestructedCallbacks;
-surf_callback(void, simgrid::surf::Link*, e_surf_resource_state_t, e_surf_resource_state_t) networkLinkStateChangedCallbacks;
-surf_callback(void, simgrid::surf::NetworkAction*, e_surf_action_state_t, e_surf_action_state_t) networkActionStateChangedCallbacks;
-surf_callback(void, simgrid::surf::NetworkAction*, simgrid::surf::RoutingEdge *src, simgrid::surf::RoutingEdge *dst, double size, double rate) networkCommunicateCallbacks;
+simgrid::xbt::signal<void(simgrid::surf::Link*)> Link::onCreation;
+simgrid::xbt::signal<void(simgrid::surf::Link*)> Link::onDestruction;
+simgrid::xbt::signal<void(simgrid::surf::Link*)> Link::onStateChange;
+
+simgrid::xbt::signal<void(simgrid::surf::NetworkAction*, e_surf_action_state_t, e_surf_action_state_t)> networkActionStateChangedCallbacks;
+simgrid::xbt::signal<void(simgrid::surf::NetworkAction*, simgrid::surf::NetCard *src, simgrid::surf::NetCard *dst, double size, double rate)> networkCommunicateCallbacks;
 
 }
 }
@@ -113,7 +114,7 @@ void netlink_parse_init(sg_platf_link_cbarg_t link){
                       link->bandwidth_trace,
                       link->latency,
                       link->latency_trace,
-                      link->state,
+                      link->initiallyOn,
                       link->state_trace, link->policy, link->properties);
     xbt_free(link_id);
     link_id = bprintf("%s_DOWN", link->id);
@@ -122,24 +123,20 @@ void netlink_parse_init(sg_platf_link_cbarg_t link){
                       link->bandwidth_trace,
                       link->latency,
                       link->latency_trace,
-                      link->state,
+                      link->initiallyOn,
                       link->state_trace, link->policy, link->properties);
     xbt_free(link_id);
   } else {
-         surf_network_model->createLink(link->id,
-                         link->bandwidth,
-                         link->bandwidth_trace,
-                         link->latency,
-                         link->latency_trace,
-                         link->state,
-                         link->state_trace, link->policy, link->properties);
+    surf_network_model->createLink(link->id,
+        link->bandwidth,
+        link->bandwidth_trace,
+        link->latency,
+        link->latency_trace,
+        link->initiallyOn,
+        link->state_trace, link->policy, link->properties);
   }
 }
 
-void net_add_traces(){
-  surf_network_model->addTraces();
-}
-
 /*********
  * Model *
  *********/
@@ -161,7 +158,7 @@ double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /
   return rate;
 }
 
-double NetworkModel::shareResourcesFull(double now)
+double NetworkModel::next_occuring_event_full(double now)
 {
   NetworkAction *action = NULL;
   ActionList *runningActions = surf_network_model->getRunningActionSet();
@@ -194,30 +191,47 @@ double NetworkModel::shareResourcesFull(double now)
  ************/
 
 Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props)
-: Resource(model, name, props)
+: Resource(model, name),
+  PropertyHolder(props)
 {
   links->insert({name, this});
 
+  m_latency.scale = 1;
+  m_bandwidth.scale = 1;
   XBT_DEBUG("Create link '%s'",name);
 }
 
 Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props,
-                                lmm_constraint_t constraint,
-                            tmgr_history_t history,
-                            tmgr_trace_t state_trace)
-: Resource(model, name, props, constraint)
+                     lmm_constraint_t constraint,
+                       tmgr_trace_t state_trace)
+: Resource(model, name, constraint),
+  PropertyHolder(props)
 {
+  m_latency.scale = 1;
+  m_bandwidth.scale = 1;
   if (state_trace)
-    p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
+    m_stateEvent = future_evt_set->add_trace(state_trace, 0.0, this);
 
   links->insert({name, this});
   XBT_DEBUG("Create link '%s'",name);
 
 }
 
-Link::~Link()
+/** @brief use destroy() instead of this destructor */
+Link::~Link() {
+  xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
+}
+/** @brief Fire the require callbacks and destroy the object
+ *
+ * Don't delete directly an Link, call l->destroy() instead.
+ */
+void Link::destroy()
 {
-  surf_callback_emit(networkLinkDestructedCallbacks, this);
+  if (!currentlyDestroying_) {
+    currentlyDestroying_ = true;
+    onDestruction(this);
+    delete this;
+  }
 }
 
 bool Link::isUsed()
@@ -227,12 +241,12 @@ bool Link::isUsed()
 
 double Link::getLatency()
 {
-  return m_latCurrent;
+  return m_latency.peak * m_latency.scale;
 }
 
 double Link::getBandwidth()
 {
-  return p_speed.peak * p_speed.scale;
+  return m_bandwidth.peak * m_bandwidth.scale;
 }
 
 int Link::sharingPolicy()
@@ -240,10 +254,17 @@ int Link::sharingPolicy()
   return lmm_constraint_sharing_policy(getConstraint());
 }
 
-void Link::setState(e_surf_resource_state_t state){
-  e_surf_resource_state_t old = Resource::getState();
-  Resource::setState(state);
-  surf_callback_emit(networkLinkStateChangedCallbacks, this, old, state);
+void Link::turnOn(){
+  if (isOff()) {
+    Resource::turnOn();
+    onStateChange(this);
+  }
+}
+void Link::turnOff(){
+  if (isOn()) {
+    Resource::turnOff();
+    onStateChange(this);
+  }
 }
 
 /**********
@@ -253,7 +274,7 @@ void Link::setState(e_surf_resource_state_t state){
 void NetworkAction::setState(e_surf_action_state_t state){
   e_surf_action_state_t old = getState();
   Action::setState(state);
-  surf_callback_emit(networkActionStateChangedCallbacks, this, old, state);
+  networkActionStateChangedCallbacks(this, old, state);
 }
 
 }