Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unused variables.
[simgrid.git] / src / surf / surf.cpp
index 54355f6..d7fec1b 100644 (file)
@@ -1,6 +1,9 @@
 #include "surf_private.h"
 #include "surf.hpp"
+#include "network.hpp"
 #include "cpu.hpp"
+#include "workstation.hpp"
+#include "vm_workstation.hpp"
 #include "simix/smx_host_private.h"
 #include "surf_routing.hpp"
 #include "simgrid/sg_config.h"
@@ -49,7 +52,7 @@ static void remove_watched_host(void *key)
   xbt_dict_remove(watched_hosts_lib, *(char**)key);
 }
 
-void surf_watched_hosts(void)
+/*void surf_watched_hosts(void)
 {
   char *key;
   void *host;
@@ -69,13 +72,19 @@ void surf_watched_hosts(void)
   }
   xbt_dynar_map(hosts, remove_watched_host);
   xbt_dynar_free(&hosts);
-}
+}*/
 
+/* model_list_invoke contains only surf_workstation and surf_vm_workstation.
+ * The callback functions of cpu_model and network_model will be called from
+ * those of these workstation models. */
+xbt_dynar_t model_list = NULL; /* for destroying all models correctly */
+xbt_dynar_t model_list_invoke = NULL;  /* for invoking callbacks */
 
-xbt_dynar_t model_list = NULL;
 tmgr_history_t history = NULL;
 lmm_system_t maxmin_system = NULL;
 xbt_dynar_t surf_path = NULL;
+xbt_dynar_t host_that_restart = NULL;
+xbt_dict_t watched_hosts_lib;
 
 /* Don't forget to update the option description in smx_config when you change this */
 s_surf_model_description_t surf_network_model_description[] = {
@@ -132,6 +141,13 @@ s_surf_model_description_t surf_workstation_model_description[] = {
   {NULL, NULL, NULL}      /* this array must be NULL terminated */
 };
 
+s_surf_model_description_t surf_vm_workstation_model_description[] = {
+  {"default",
+   "Default vm workstation model.)",
+   surf_vm_workstation_model_init_current_default},
+  {NULL, NULL, NULL}      /* this array must be NULL terminated */
+};
+
 s_surf_model_description_t surf_optimization_mode_description[] = {
   {"Lazy",
    "Lazy action management (partial invalidation in lmm + heap in action remaining).",
@@ -152,55 +168,19 @@ s_surf_model_description_t surf_storage_model_description[] = {
   {NULL, NULL,  NULL}      /* this array must be NULL terminated */
 };
 
-/* ********************************************************************* */
-/* TUTORIAL: New model                                                   */
-s_surf_model_description_t surf_new_model_description[] = {
-  {"default",
-   "Tutorial model.",
-   surf_new_model_init_default},
-  {NULL, NULL,  NULL}      /* this array must be NULL terminated */
-};
-/* ********************************************************************* */
-
 #ifdef CONTEXT_THREADS
 static xbt_parmap_t surf_parmap = NULL; /* parallel map on models */
 #endif
 
 static double *surf_mins = NULL; /* return value of share_resources for each model */
-static int surf_min_index;       /* current index in surf_mins */
-static double min;               /* duration determined by surf_solve */
 
 double NOW = 0;
 
-XBT_INLINE double surf_get_clock(void)
+double surf_get_clock(void)
 {
   return NOW;
 }
 
-/*TODO: keepit void surf_watched_hosts(void)
-{
-  char *key;
-  void *_host;
-  smx_host_t host;
-  xbt_dict_cursor_t cursor;
-  xbt_dynar_t hosts = xbt_dynar_new(sizeof(char*), NULL);
-
-  XBT_DEBUG("Check for host SURF_RESOURCE_ON on watched_hosts_lib");
-  xbt_dict_foreach(watched_hosts_lib,cursor,key,_host)
-  {
-    host = (smx_host_t) host;
-    if(SIMIX_host_get_state(host) == SURF_RESOURCE_ON){
-      XBT_INFO("Restart processes on host: %s",SIMIX_host_get_name(host));
-      SIMIX_host_autorestart(host);
-      xbt_dynar_push_as(hosts, char*, key);
-    }
-    else
-      XBT_DEBUG("See SURF_RESOURCE_OFF on host: %s",key);
-  }
-  xbt_dynar_map(hosts, remove_watched_host);
-  xbt_dynar_free(&hosts);
-}*/
-
 #ifdef _XBT_WIN32
 # define FILE_DELIM "\\"
 #else
@@ -324,12 +304,22 @@ static XBT_INLINE void routing_asr_prop_free(void *p)
   xbt_dict_free(&elm);
 }
 
-static XBT_INLINE void surf_resource_free(void *r)
+static XBT_INLINE void surf_cpu_free(void *r)
 {
-  ResourcePtr resource = (ResourcePtr) r;
-  delete resource;
+  delete dynamic_cast<CpuPtr>(static_cast<ResourcePtr>(r));
 }
 
+static XBT_INLINE void surf_link_free(void *r)
+{
+  delete dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(r));
+}
+
+static XBT_INLINE void surf_workstation_free(void *r)
+{
+  delete dynamic_cast<WorkstationCLM03Ptr>(static_cast<ResourcePtr>(r));
+}
+
+
 void sg_version(int *ver_major,int *ver_minor,int *ver_patch) {
   *ver_major = SIMGRID_VERSION_MAJOR;
   *ver_minor = SIMGRID_VERSION_MINOR;
@@ -344,7 +334,7 @@ void surf_init(int *argc, char **argv)
   as_router_lib = xbt_lib_new();
   storage_lib = xbt_lib_new();
   storage_type_lib = xbt_lib_new();
-  watched_hosts_lib = xbt_dict_new();
+  watched_hosts_lib = xbt_dict_new_homogeneous(NULL);
 
   XBT_DEBUG("Add routing levels");
   ROUTING_HOST_LEVEL = xbt_lib_add_level(host_lib,routing_asr_host_free);
@@ -352,13 +342,15 @@ 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_CPU_LEVEL = xbt_lib_add_level(host_lib,surf_resource_free);
-  SURF_WKS_LEVEL = xbt_lib_add_level(host_lib,surf_resource_free);
-  SURF_LINK_LEVEL = xbt_lib_add_level(link_lib,surf_resource_free);
+  SURF_CPU_LEVEL = xbt_lib_add_level(host_lib,surf_cpu_free);
+  SURF_WKS_LEVEL = xbt_lib_add_level(host_lib,surf_workstation_free);
+  SURF_LINK_LEVEL = xbt_lib_add_level(link_lib,surf_link_free);
 
   xbt_init(argc, argv);
   if (!model_list)
-    model_list = xbt_dynar_new(sizeof(surf_model_private_t), NULL);
+    model_list = xbt_dynar_new(sizeof(ModelPtr), NULL);
+  if (!model_list_invoke)
+    model_list_invoke = xbt_dynar_new(sizeof(ModelPtr), NULL);
   if (!history)
     history = tmgr_history_new();
 
@@ -369,7 +361,6 @@ void surf_init(int *argc, char **argv)
 
   sg_config_init(argc, argv);
 
-  surf_action_init();
   if (MC_is_active())
     MC_memory_init();
 }
@@ -379,11 +370,17 @@ void surf_exit(void)
   unsigned int iter;
   ModelPtr model = NULL;
 
+#ifdef HAVE_TRACING
+  TRACE_end();                  /* Just in case it was not called by the upper
+                                 * layer (or there is no upper layer) */
+#endif
+
   sg_config_finalize();
 
   xbt_dynar_foreach(model_list, iter, model)
     delete model;
   xbt_dynar_free(&model_list);
+  xbt_dynar_free(&model_list_invoke);
   routing_exit();
 
   if (maxmin_system) {
@@ -394,14 +391,13 @@ void surf_exit(void)
     tmgr_history_free(history);
     history = NULL;
   }
-  surf_action_exit();
 
 #ifdef CONTEXT_THREADS
   xbt_parmap_destroy(surf_parmap);
   xbt_free(surf_mins);
   surf_mins = NULL;
 #endif
-
+  xbt_dynar_free(&host_that_restart);
   xbt_dynar_free(&surf_path);
 
   xbt_lib_free(&host_lib);
@@ -423,9 +419,11 @@ void surf_exit(void)
  *********/
 
 Model::Model(string name)
- : m_name(name), m_resOnCB(0), m_resOffCB(0), m_actSuspendCB(0), m_actCancelCB(0), m_actResumeCB(0)
+ : m_name(name), m_resOnCB(0), m_resOffCB(0),
+   m_actSuspendCB(0), m_actCancelCB(0), m_actResumeCB(0),
+   p_maxminSystem(0)
 {
-  ActionPtr action;
+  ActionPtr action = NULL;
   p_readyActionSet = xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
   p_runningActionSet = xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
   p_failedActionSet = xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
@@ -446,7 +444,13 @@ xbt_swag_free(p_doneActionSet);
 
 double Model::shareResources(double now)
 {
-  //FIXME: to implement
+  //FIXME: set the good function once and for all
+  if (p_updateMechanism == UM_LAZY)
+       return shareResourcesLazy(now);
+  else if (p_updateMechanism == UM_FULL)
+       return shareResourcesFull(now);
+  else
+       xbt_die("Invalid cpu update mechanism!");
 }
 
 double Model::shareResourcesLazy(double now)
@@ -465,7 +469,7 @@ double Model::shareResourcesLazy(double now)
       ("After share resources, The size of modified actions set is %d",
        xbt_swag_size(p_modifiedSet));
 
-  while((action = (ActionLmmPtr) xbt_swag_extract(p_modifiedSet))) {
+  while((action = static_cast<ActionLmmPtr>(xbt_swag_extract(p_modifiedSet)))) {
     int max_dur_flag = 0;
 
     if (action->p_stateSet != p_runningActionSet)
@@ -521,27 +525,30 @@ double Model::shareResourcesLazy(double now)
   return min;
 }
 
+double Model::shareResourcesFull(double /*now*/) {
+  THROW_UNIMPLEMENTED;
+}
+
+
 double Model::shareResourcesMaxMin(xbt_swag_t running_actions,
-                          size_t offset,
                           lmm_system_t sys,
                           void (*solve) (lmm_system_t))
 {
   void *_action = NULL;
-  ActionPtr action = NULL;
+  ActionLmmPtr action = NULL;
   double min = -1;
   double value = -1;
-#define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + (offset))))
 
   solve(sys);
 
   xbt_swag_foreach(_action, running_actions) {
-    action = (ActionPtr)_action;
-    value = lmm_variable_getvalue(VARIABLE(action));
+    action = dynamic_cast<ActionLmmPtr>(static_cast<ActionPtr>(_action));
+    value = lmm_variable_getvalue(action->p_variable);
     if ((value > 0) || (action->m_maxDuration >= 0))
       break;
   }
 
-  if (!action)
+  if (!_action)
     return -1.0;
 
   if (value > 0) {
@@ -555,10 +562,11 @@ double Model::shareResourcesMaxMin(xbt_swag_t running_actions,
     min = action->m_maxDuration;
 
 
-  for (action = (ActionPtr) xbt_swag_getNext(action, running_actions->offset);
-       action;
-       action = (ActionPtr) xbt_swag_getNext(action, running_actions->offset)) {
-    value = lmm_variable_getvalue(VARIABLE(action));
+  for (_action = xbt_swag_getNext(static_cast<ActionPtr>(action), running_actions->offset);
+       _action;
+       _action = xbt_swag_getNext(static_cast<ActionPtr>(action), running_actions->offset)) {
+       action = dynamic_cast<ActionLmmPtr>(static_cast<ActionPtr>(_action));
+    value = lmm_variable_getvalue(action->p_variable);
     if (value > 0) {
       if (action->m_remains > 0)
         value = action->m_remains / value;
@@ -576,7 +584,6 @@ double Model::shareResourcesMaxMin(xbt_swag_t running_actions,
   }
   XBT_DEBUG("min value : %f", min);
 
-#undef VARIABLE
   return min;
 }
 
@@ -590,12 +597,12 @@ void Model::updateActionsState(double now, double delta)
        xbt_die("Invalid cpu update mechanism!");
 }
 
-void Model::updateActionsStateLazy(double now, double delta)
+void Model::updateActionsStateLazy(double /*now*/, double /*delta*/)
 {
 
 }
 
-void Model::updateActionsStateFull(double now, double delta)
+void Model::updateActionsStateFull(double /*now*/, double /*delta*/)
 {
 
 }
@@ -662,7 +669,7 @@ Resource::Resource(surf_model_t model, const char *name, xbt_dict_t props)
 
 Resource::Resource(){
   //FIXME:free(m_name);
-  xbt_dict_free(&m_properties);
+  //FIXME:xbt_dict_free(&m_properties);
 }
 
 const char *Resource::getName()
@@ -677,7 +684,12 @@ xbt_dict_t Resource::getProperties()
 
 e_surf_resource_state_t Resource::getState()
 {
-  return m_stateCurrent;
+  return p_stateCurrent;
+}
+
+void Resource::setState(e_surf_resource_state_t state)
+{
+  p_stateCurrent = state;
 }
 
 bool Resource::isOn()
@@ -714,11 +726,11 @@ ResourceLmm::ResourceLmm(surf_model_t model, const char *name, xbt_dict_t props,
   p_constraint = lmm_constraint_new(system, this, constraint_value);
   p_stateCurrent = state_init;
   if (state_trace)
-    p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this);
+    p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, static_cast<ResourcePtr>(this));
   p_power.scale = 1.0;
   p_power.peak = metric_peak;
   if (metric_trace)
-    p_power.event = tmgr_history_add_trace(history, metric_trace, 0.0, 0, this);
+    p_power.event = tmgr_history_add_trace(history, metric_trace, 0.0, 0, static_cast<ResourcePtr>(this));
 }
 
 /**********
@@ -734,26 +746,6 @@ const char *surf_action_state_names[6] = {
   "SURF_ACTION_NOT_IN_THE_SYSTEM"
 };
 
-/**
- * \brief Initializes the action module of Surf.
- */
-void surf_action_init(void) {
-
-  /* the action mallocator will always provide actions of the following size,
-   * so this size should be set to the maximum size of the surf action structures
-   */
-  /*FIXME:action_mallocator_allocated_size = sizeof(s_surf_action_network_CM02_t);
-  action_mallocator = xbt_mallocator_new(65536, surf_action_mallocator_new_f,
-      surf_action_mallocator_free_f, surf_action_mallocator_reset_f);*/
-}
-
-/**
- * \brief Uninitializes the action module of Surf.
- */
-void surf_action_exit(void) {
-  //FIXME:xbt_mallocator_free(action_mallocator);
-}
-
 Action::Action(){}
 
 Action::Action(ModelPtr model, double cost, bool failed):
@@ -764,6 +756,8 @@ Action::Action(ModelPtr model, double cost, bool failed):
   #ifdef HAVE_TRACING
     p_category = NULL;
   #endif
+  p_stateHookup.prev = 0;
+  p_stateHookup.next = 0;
   if (failed)
     p_stateSet = p_model->p_failedActionSet;
   else
@@ -832,6 +826,13 @@ double Action::getFinishTime()
   return m_remains == 0 ? m_finish : -1;
 }
 
+double Action::getRemains()
+{
+  XBT_IN("(%p)", this);
+  XBT_OUT();
+  return m_remains;
+}
+
 void Action::setData(void* data)
 {
   p_data = data;
@@ -883,7 +884,7 @@ void ActionLmm::cancel(){
 int ActionLmm::unref(){
   m_refcount--;
   if (!m_refcount) {
-       xbt_swag_remove(this, p_stateSet);
+       xbt_swag_remove(static_cast<ActionPtr>(this), p_stateSet);
        if (p_variable)
          lmm_variable_free(p_model->p_maxminSystem, p_variable);
        if (p_model->p_updateMechanism == UM_LAZY) {
@@ -958,17 +959,74 @@ void ActionLmm::updateIndexHeap(int i) {
   m_indexHeap = i;
 }
 
-
 double ActionLmm::getRemains()
 {
   XBT_IN("(%p)", this);
   /* update remains before return it */
-  if (p_updateMechanism == UM_LAZY)      /* update remains before return it */
+  if (p_model->p_updateMechanism == UM_LAZY)      /* update remains before return it */
     updateRemainingLazy(surf_get_clock());
   XBT_OUT();
   return m_remains;
 }
 
+//FIXME split code in the right places
+void ActionLmm::updateRemainingLazy(double now)
+{
+  double delta = 0.0;
+
+  if(p_model == static_cast<ModelPtr>(surf_network_model))
+  {
+    if (m_suspended != 0)
+      return;
+  }
+  else
+  {
+    xbt_assert(p_stateSet == p_model->p_runningActionSet,
+        "You're updating an action that is not running.");
+
+      /* bogus priority, skip it */
+    xbt_assert(m_priority > 0,
+        "You're updating an action that seems suspended.");
+  }
+
+  delta = now - m_lastUpdate;
+
+  if (m_remains > 0) {
+    XBT_DEBUG("Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
+    double_update(&m_remains, m_lastValue * delta);
+
+#ifdef HAVE_TRACING
+    if (p_model == static_cast<ModelPtr>(surf_cpu_model_pm) && TRACE_is_enabled()) {
+      ResourcePtr cpu = static_cast<ResourcePtr>(lmm_constraint_id(lmm_get_cnst_from_var(p_model->p_maxminSystem, p_variable, 0)));
+      TRACE_surf_host_set_utilization(cpu->m_name, p_category, m_lastValue, m_lastUpdate, now - m_lastUpdate);
+    }
+#endif
+    XBT_DEBUG("Updating action(%p): remains is now %f", this, m_remains);
+  }
+
+  if(p_model == static_cast<ModelPtr>(surf_network_model))
+  {
+    if (m_maxDuration != NO_MAX_DURATION)
+      double_update(&m_maxDuration, delta);
+
+    //FIXME: duplicated code
+    if ((m_remains <= 0) &&
+        (lmm_get_variable_weight(p_variable) > 0)) {
+      m_finish = surf_get_clock();
+      setState(SURF_ACTION_DONE);
+      heapRemove(p_model->p_actionHeap);
+    } else if (((m_maxDuration != NO_MAX_DURATION)
+        && (m_maxDuration <= 0))) {
+      m_finish = surf_get_clock();
+      setState(SURF_ACTION_DONE);
+      heapRemove(p_model->p_actionHeap);
+    }
+  }
+
+  m_lastUpdate = now;
+  m_lastValue = lmm_variable_getvalue(p_variable);
+}
+
 /*void Action::cancel()
 {
   p_model->notifyActionCancel(this);