Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
various cleanups + debug
[simgrid.git] / src / surf / workstation_KCCFLN05.c
index 8695b1b..986e287 100644 (file)
@@ -17,9 +17,12 @@ static s_route_KCCFLN05_t *routing_table = NULL;
 #define ROUTE(i,j) routing_table[(i)+(j)*nb_workstation]
 static network_link_KCCFLN05_t loopback = NULL;
 static xbt_dict_t parallel_task_network_link_set = NULL;
+//added to work with GTNETS
+static xbt_dict_t router_set = NULL;
 
 /*xbt_dict_t network_link_set = NULL;*/
 
+
 /* convenient function */
 static void __update_cpu_usage(cpu_KCCFLN05_t cpu)
 {
@@ -76,7 +79,20 @@ static void __update_cpu_usage(cpu_KCCFLN05_t cpu)
 
 static void *name_service(const char *name)
 {
-  return xbt_dict_get_or_null(workstation_set, name);
+  xbt_ex_t e;
+  void *res=NULL;
+
+  TRY {
+    res = xbt_dict_get(workstation_set, name);
+  } CATCH(e) {
+    if (e.category != not_found_error) 
+      RETHROW;
+    WARN1("Host '%s' not found, verifing if it is a router", name);
+    res = NULL;
+    xbt_ex_free(e);
+  }
+
+  return res;
 }
 
 static const char *get_resource_name(void *resource_id)
@@ -152,32 +168,36 @@ static void action_recycle(surf_action_t action)
 static void action_suspend(surf_action_t action)
 {
   XBT_IN1("(%p))",action);
-  ((surf_action_workstation_KCCFLN05_t) action)->suspended = 1;
-  lmm_update_variable_weight(maxmin_system,
-                            ((surf_action_workstation_KCCFLN05_t)
-                             action)->variable, 0.0);
+  if(((surf_action_workstation_KCCFLN05_t) action)->suspended != 2) {
+    ((surf_action_workstation_KCCFLN05_t) action)->suspended = 1;
+    lmm_update_variable_weight(maxmin_system,
+                              ((surf_action_workstation_KCCFLN05_t)
+                               action)->variable, 0.0);
+  }
   XBT_OUT;
 }
 
 static void action_resume(surf_action_t action)
 {
   XBT_IN1("(%p)",action);
-  if(((surf_action_workstation_KCCFLN05_t)action)->lat_current==0.0)
-    lmm_update_variable_weight(maxmin_system,
-                              ((surf_action_workstation_KCCFLN05_t)
-                               action)->variable, 1.0);
-  else
-    lmm_update_variable_weight(maxmin_system,
-                              ((surf_action_workstation_KCCFLN05_t) action)->variable, 
-                              ((surf_action_workstation_KCCFLN05_t) action)->lat_current);
-
-  ((surf_action_workstation_KCCFLN05_t) action)->suspended = 0;
+  if(((surf_action_workstation_KCCFLN05_t) action)->suspended !=2) {
+    if(((surf_action_workstation_KCCFLN05_t)action)->lat_current==0.0)
+      lmm_update_variable_weight(maxmin_system,
+                                ((surf_action_workstation_KCCFLN05_t)
+                                 action)->variable, 1.0);
+    else
+      lmm_update_variable_weight(maxmin_system,
+                                ((surf_action_workstation_KCCFLN05_t) action)->variable, 
+                                ((surf_action_workstation_KCCFLN05_t) action)->lat_current);
+    
+    ((surf_action_workstation_KCCFLN05_t) action)->suspended = 0;
+  }
   XBT_OUT;
 }
 
 static int action_is_suspended(surf_action_t action)
 {
-  return (((surf_action_workstation_KCCFLN05_t) action)->suspended);
+  return (((surf_action_workstation_KCCFLN05_t) action)->suspended==1);
 }
 
 static void action_set_max_duration(surf_action_t action, double duration)
@@ -229,15 +249,22 @@ static double share_resources(double now)
 
   xbt_swag_foreach(action, running_actions) {
     if(action->latency>0) {
-      if(min<0) min = action->latency;
-      else if (action->latency<min) min = action->latency;
+      if(min<0) {
+       min = action->latency;
+       DEBUG3("Updating min (value) with %p (start %f): %f",action, 
+              action->generic_action.start, min);
+      }
+      else if (action->latency<min) {
+       min = action->latency;
+       DEBUG3("Updating min (latency) with %p (start %f): %f",action, 
+              action->generic_action.start, min);
+      }
     }
   }
 
+  DEBUG1("min value : %f",min);
+
   return min;
-/*   return generic_maxmin_share_resources(surf_workstation_resource->common_public-> */
-/*                                     states.running_action_set, */
-/*                                     xbt_swag_offset(action, variable)); */
 }
 
 static void update_actions_state(double now, double delta)
@@ -258,7 +285,7 @@ static void update_actions_state(double now, double delta)
        double_update(&(deltap), action->latency);
        action->latency = 0.0;
       }
-      if ((action->latency == 0.0) && !(action->suspended)) {
+      if ((action->latency == 0.0) && (action->suspended==0)) {
        if((action)->lat_current==0.0)
          lmm_update_variable_weight(maxmin_system,action->variable, 1.0);
        else
@@ -266,13 +293,15 @@ static void update_actions_state(double now, double delta)
                                     action->lat_current);
       }
     }
+    DEBUG3("Action (%p) : remains (%g) updated by %g.",
+          action, action->generic_action.remains,
+          lmm_variable_getvalue(action->variable) * deltap);
     double_update(&(action->generic_action.remains),
                       lmm_variable_getvalue(action->variable) * deltap);
+
     if (action->generic_action.max_duration != NO_MAX_DURATION)
       double_update(&(action->generic_action.max_duration), delta);
 
-    /*   if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
-
     if ((action->generic_action.remains <= 0) && 
        (lmm_get_variable_weight(action->variable)>0)) {
       action->generic_action.finish = surf_get_clock();
@@ -281,7 +310,8 @@ static void update_actions_state(double now, double delta)
               (action->generic_action.max_duration <= 0)) {
       action->generic_action.finish = surf_get_clock();
       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
-    } else {                   /* Need to check that none of the resource has failed */
+    } else {
+      /* Need to check that none of the resource has failed */
       lmm_constraint_t cnst = NULL;
       int i = 0;
       void *constraint_id = NULL;
@@ -290,6 +320,21 @@ static void update_actions_state(double now, double delta)
              lmm_get_cnst_from_var(maxmin_system, action->variable,
                                    i++))) {
        constraint_id = lmm_constraint_id(cnst);
+
+/*     if(((network_link_KCCFLN05_t)constraint_id)->type== */
+/*        SURF_WORKSTATION_RESOURCE_LINK) { */
+/*       DEBUG2("Checking for link %s (%p)", */
+/*              ((network_link_KCCFLN05_t)constraint_id)->name, */
+/*              ((network_link_KCCFLN05_t)constraint_id)); */
+/*     } */
+/*     if(((cpu_KCCFLN05_t)constraint_id)->type== */
+/*        SURF_WORKSTATION_RESOURCE_CPU) { */
+/*       DEBUG3("Checking for cpu %s (%p) : %s", */
+/*              ((cpu_KCCFLN05_t)constraint_id)->name, */
+/*              ((cpu_KCCFLN05_t)constraint_id), */
+/*              ((cpu_KCCFLN05_t)constraint_id)->state_current==SURF_CPU_OFF?"Off":"On"); */
+/*     } */
+
        if(((((network_link_KCCFLN05_t)constraint_id)->type==
             SURF_WORKSTATION_RESOURCE_LINK) &&
            (((network_link_KCCFLN05_t)constraint_id)->state_current==
@@ -298,6 +343,7 @@ static void update_actions_state(double now, double delta)
             SURF_WORKSTATION_RESOURCE_CPU) &&
            (((cpu_KCCFLN05_t)constraint_id)->state_current==
             SURF_CPU_OFF))) {
+         DEBUG1("Action (%p) Failed!!",action);
          action->generic_action.finish = surf_get_clock();
          surf_action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
          break;
@@ -316,6 +362,7 @@ static void update_resource_state(void *id,
   network_link_KCCFLN05_t nw_link = id ;
 
   if(nw_link->type == SURF_WORKSTATION_RESOURCE_LINK) {
+    DEBUG2("Updating link %s (%p)",nw_link->name,nw_link);
     if (event_type == nw_link->bw_event) {
       nw_link->bw_current = value;
       lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
@@ -335,9 +382,12 @@ static void update_resource_state(void *id,
        else 
          lmm_update_variable_bound(maxmin_system, action->variable,
                                    min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
-       if(!(action->suspended))
+       if(action->suspended==0)
          lmm_update_variable_weight(maxmin_system, action->variable, 
                                     action->lat_current);
+       lmm_update_variable_latency(maxmin_system, action->variable, delta);
+       
+
       }
     } else if (event_type == nw_link->state_event) {
       if (value > 0)
@@ -350,6 +400,7 @@ static void update_resource_state(void *id,
     }
     return;
   } else if(cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
+    DEBUG3("Updating cpu %s (%p) with value %g",cpu->name,cpu,value);
     if (event_type == cpu->power_event) {
       cpu->power_current = value;
       __update_cpu_usage(cpu);
@@ -361,7 +412,7 @@ static void update_resource_state(void *id,
     } else {
       CRITICAL0("Unknown event ! \n");
       xbt_abort();
-    }    
+    }
     return;
   } else {
     DIE_IMPOSSIBLE;
@@ -375,6 +426,7 @@ static void finalize(void)
 
   xbt_dict_free(&network_link_set);
   xbt_dict_free(&workstation_set);
+  xbt_dict_free(&router_set);
   if (parallel_task_network_link_set != NULL) {
     xbt_dict_free(&parallel_task_network_link_set);
   }
@@ -454,6 +506,7 @@ static surf_action_t action_sleep(void *cpu, double duration)
 
   action = (surf_action_workstation_KCCFLN05_t) execute(cpu, 1.0);
   action->generic_action.max_duration = duration;
+  action->suspended = 2;
   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
 
   XBT_OUT;
@@ -475,6 +528,7 @@ static double get_available_speed(void *cpu)
   return ((cpu_KCCFLN05_t) cpu)->power_current;
 }
 
+
 static surf_action_t communicate(void *src, void *dst, double size, double rate)
 {
   surf_action_workstation_KCCFLN05_t action = NULL;
@@ -539,6 +593,8 @@ static surf_action_t communicate(void *src, void *dst, double size, double rate)
       lmm_update_variable_bound(maxmin_system, action->variable, action->rate);
   }
 
+  lmm_update_variable_latency(maxmin_system, action->variable, action->latency);
+  
   for (i = 0; i < route_size; i++)
     lmm_expand(maxmin_system, route->links[i]->constraint, action->variable, 1.0);
   if (card_src->bus)
@@ -567,7 +623,7 @@ static surf_action_t execute_parallel_task(int workstation_nb,
   if (parallel_task_network_link_set == NULL) {
     parallel_task_network_link_set = xbt_dict_new_ext(workstation_nb * workstation_nb * 10);
   }
-
+  
   /* Compute the number of affected resources... */
   for(i=0; i< workstation_nb; i++) {
     for(j=0; j< workstation_nb; j++) {
@@ -582,17 +638,20 @@ static surf_action_t execute_parallel_task(int workstation_nb,
        }
     }
   }
-
   nb_link = xbt_dict_length(parallel_task_network_link_set);
   xbt_dict_reset(parallel_task_network_link_set);
 
+
   for (i = 0; i<workstation_nb; i++)
     if(computation_amount[i]>0) nb_host++;
  
-  if(nb_link + workstation_nb == 0)
+
+  if(nb_link + nb_host == 0) /* was workstation_nb... */
     return NULL;
 
   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
+  DEBUG3("Creating a parallel task (%p) with %d cpus and %d links.",
+        action, nb_host,  nb_link);
   action->generic_action.using = 1;
   action->generic_action.cost = amount;
   action->generic_action.remains = amount;
@@ -672,6 +731,32 @@ static double get_link_latency(const void *link) {
 /*** Resource Creation & Destruction **/
 /**************************************/
 
+
+static void router_free(void *router)
+{
+  free( ((router_KCCFLN05_t) router)->name );
+}
+
+static void router_new(const char *name)
+{
+  static unsigned int nb_routers = 0; 
+
+  INFO1("Creating a router %s", name);
+
+  router_KCCFLN05_t router;
+  router = xbt_new0(s_router_KCCFLN05_t, 1);
+
+  router->name = xbt_strdup(name);
+  router->id   = nb_routers++;
+  xbt_dict_set(router_set, name, router, router_free);
+}
+
+static void parse_routers(void)
+{
+  //add a dumb router just to be GTNETS compatible
+  router_new(A_surfxml_router_name);
+}
+
 static void cpu_free(void *cpu)
 {
   free(((cpu_KCCFLN05_t) cpu)->name);
@@ -754,7 +839,7 @@ static void parse_cpu(void)
   if (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF)
     state_initial = SURF_CPU_OFF;
   surf_parse_get_trace(&state_trace, A_surfxml_cpu_state_file);
-
+  
   surf_parse_get_double(&interference_send,
                        A_surfxml_cpu_interference_send);
   surf_parse_get_double(&interference_recv,
@@ -885,31 +970,60 @@ static double impact_on_dst_with_other_send;
 
 static void parse_route_set_endpoints(void)
 {
-  src_id = ((cpu_KCCFLN05_t) name_service(A_surfxml_route_src))->id;
-  dst_id = ((cpu_KCCFLN05_t) name_service(A_surfxml_route_dst))->id;
+  cpu_KCCFLN05_t cpu_tmp = NULL;
+  cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_src);
+  if(cpu_tmp != NULL) {
+    src_id = cpu_tmp->id;
+  }else {
+    xbt_assert1(xbt_dict_get_or_null(router_set, A_surfxml_route_src),
+              "Invalid name '%s': neither a cpu nor a router!",
+              A_surfxml_route_src);
+    src_id=-1;
+    return;
+  }
+
+  cpu_tmp = (cpu_KCCFLN05_t) name_service(A_surfxml_route_dst);
+  if(cpu_tmp != NULL) { 
+    dst_id = cpu_tmp->id;
+  }else {
+    xbt_assert1(xbt_dict_get_or_null(router_set, A_surfxml_route_dst),
+              "Invalid name '%s': neither a cpu nor a router!",
+              A_surfxml_route_dst);
+    dst_id=-1;
+    return ;
+  }
+  
   surf_parse_get_double(&impact_on_src, A_surfxml_route_impact_on_src);
   surf_parse_get_double(&impact_on_dst, A_surfxml_route_impact_on_dst);
   surf_parse_get_double(&impact_on_src_with_other_recv,
                        A_surfxml_route_impact_on_src_with_other_recv);
   surf_parse_get_double(&impact_on_dst_with_other_send,
                        A_surfxml_route_impact_on_dst_with_other_send);
-
+  
   nb_link = 0;
-  link_list_capacity = 20;
+  link_list_capacity = 1; 
   link_list = xbt_new(network_link_KCCFLN05_t, link_list_capacity);
+
 }
 
 static void parse_route_elem(void)
 {
+  xbt_ex_t e;
   if (nb_link == link_list_capacity) {
     link_list_capacity *= 2;
     link_list = xbt_realloc(link_list, (link_list_capacity) * sizeof(network_link_KCCFLN05_t));
   }
-  link_list[nb_link++] = xbt_dict_get_or_null(network_link_set, A_surfxml_route_element_name);
+  TRY {
+     link_list[nb_link++] = xbt_dict_get(network_link_set, A_surfxml_route_element_name);
+  } CATCH(e) {
+     RETHROW1("Link %s not found (dict raised this exception: %s)",A_surfxml_route_element_name);
+  }
 }
 
 static void parse_route_set_route(void)
 {
+  if( src_id != -1 && dst_id != -1 )
   route_new(src_id, dst_id, link_list, nb_link, impact_on_src,
            impact_on_dst, impact_on_src_with_other_recv,
            impact_on_dst_with_other_send);
@@ -917,7 +1031,7 @@ static void parse_route_set_route(void)
 
 static void parse_file(const char *file)
 {
-  int i ;
+  int i;
 
   /* Figuring out the cpus */
   surf_parse_reset_parser();
@@ -928,6 +1042,13 @@ static void parse_file(const char *file)
 
   create_routing_table();
 
+  /* Figuring out the router (added after GTNETS) */
+  surf_parse_reset_parser();
+  STag_surfxml_router_fun=parse_routers;
+  surf_parse_open(file);
+  xbt_assert1((!surf_parse()),"Parse error in %s",file);
+  surf_parse_close();
+
   /* Figuring out the network links */
   surf_parse_reset_parser();
   ETag_surfxml_network_link_fun = parse_network_link;
@@ -1024,7 +1145,8 @@ static void resource_init_internal(void)
   surf_workstation_resource->extension_public->get_link_bandwidth = get_link_bandwidth;
   surf_workstation_resource->extension_public->get_link_latency = get_link_latency;
 
-  workstation_set = xbt_dict_new();
+  workstation_set  = xbt_dict_new();
+  router_set       = xbt_dict_new();
   network_link_set = xbt_dict_new();
 
   xbt_assert0(maxmin_system, "surf_init has to be called first!");