Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replaced the using field of surf_action to refcount because this name is not a C...
[simgrid.git] / src / surf / network_gtnets.c
index 8804e19..2f41e65 100644 (file)
@@ -9,6 +9,9 @@
 #include "gtnets/gtnets_interface.h"
 #include "xbt/str.h"
 
+
+static   double time_to_next_flow_completion=-1;
+
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
                                "Logging specific to the SURF network module");
 
@@ -22,10 +25,14 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets, surf,
  **   6. We don't update "remaining" for ongoing flows. Is it bad?
  **/
 
+static int src_id = -1;
+static int dst_id = -1;
+
 /* Free memory for a network link */
 static void link_free(void *nw_link)
 {
   free(((network_link_GTNETS_t) nw_link)->name);
+  xbt_dict_free(&(((network_link_GTNETS_t)nw_link)->properties));
   free(nw_link);
 }
 
@@ -75,7 +82,7 @@ static void link_new(char *name, double bw, double lat, xbt_dict_t props)
   gtnets_link->lat_current = lat;
   gtnets_link->id = link_count;
   /* Add the properties */
-  gtnets_link->properties = current_property_set;
+  gtnets_link->properties = props;
 
   xbt_dict_set(link_set, name, gtnets_link, link_free);
 
@@ -114,30 +121,15 @@ static int network_card_new(const char *name)
 }
 
 /* Instantiate a new route: MODIFY BY KF */
-static void route_new(int src_id, int dst_id, char **links, int nb_link)
+static void route_new(int src_id, int dst_id, network_link_GTNETS_t *links, int nb_link)
 {
-#if 0
-  network_link_GTNETS_t *link_list = NULL;
-  int i;
-
-  ROUTE_SIZE(src_id, dst_id) = nb_link;
-  link_list = (ROUTE(src_id, dst_id) =
-              xbt_new0(network_link_GTNETS_t, nb_link));
-  for (i = 0; i < nb_link; i++) {
-    link_list[i] = xbt_dict_get_or_null(link_set, links[i]);
-    free(links[i]);
-  }
-  free(links);
-#endif
   int i;
   int *gtnets_links;
 
   /* KF: Build the list of gtnets link IDs */
   gtnets_links = (int *) calloc(nb_link, sizeof(int));
   for (i = 0; i < nb_link; i++) {
-    gtnets_links[i] =
-       ((network_link_GTNETS_t)
-        (xbt_dict_get(link_set, links[i])))->id;
+    gtnets_links[i] = links[i]->id;
   }
 
   /* KF: Create the GTNets route */
@@ -147,8 +139,7 @@ static void route_new(int src_id, int dst_id, char **links, int nb_link)
 }
 
 /* Instantiate a new route: MODIFY BY KF */
-static void route_onehop_new(int src_id, int dst_id, char **links,
-                            int nb_link)
+static void route_onehop_new(int src_id, int dst_id, network_link_GTNETS_t *links, int nb_link)
 {
   int linkid;
 
@@ -156,10 +147,8 @@ static void route_onehop_new(int src_id, int dst_id, char **links,
     xbt_assert0(0, "In onehop_new, nb_link should be 1");
   }
 
-  /* KF: Build the list of gtnets link IDs */
-  linkid =
-      ((network_link_GTNETS_t)
-       (xbt_dict_get(link_set, links[0])))->id;
+  /* KF: Build the linbst of gtnets link IDs */
+  linkid = links[0]->id;
 
   /* KF: Create the GTNets route */
   if (gtnets_add_onehop_route(src_id, dst_id, linkid)) {
@@ -182,49 +171,31 @@ static void parse_link_init(void)
   surf_parse_get_double(&lat, A_surfxml_link_latency);
   state = SURF_LINK_ON;
 
-  /* Print values when no traces are specified */
-  {
-    tmgr_trace_t bw_trace;
-    tmgr_trace_t state_trace;
-    tmgr_trace_t lat_trace;
+  tmgr_trace_t bw_trace;
+  tmgr_trace_t state_trace;
+  tmgr_trace_t lat_trace;
+  
+  surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
+  surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
+  surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
 
-    surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
-    surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
-    surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
+  if (bw_trace)
+    INFO0("The GTNetS network model doesn't support bandwidth state traces");
+  if (lat_trace)
+    INFO0("The GTNetS network model doesn't support latency state traces");
+  if (state_trace)
+    INFO0("The GTNetS network model doesn't support link state traces");
 
-    /*TODO Where is WARNING0 defined??? */
-#if 0
-    if (bw_trace)
-      WARNING0
-         ("The GTNetS network model doesn't support bandwidth state traces");
-    if (lat_trace)
-      WARNING0
-         ("The GTNetS network model doesn't support latency state traces");
-    if (state_trace)
-      WARNING0
-         ("The GTNetS network model doesn't support link state traces");
-#endif
-  }
-  /* KF: remove several arguments to link_new */
   current_property_set = xbt_dict_new();
   link_new(name, bw, lat, current_property_set);
 }
 
-static int nb_link = 0;
-static char **link_name = NULL;
-static int src_id = -1;
-static int dst_id = -1;
-
 /* Parses a route from the XML: UNMODIFIED BY HC */
 static void parse_route_set_endpoints(void)
 {
   src_id = network_card_new(A_surfxml_route_src);
   dst_id = network_card_new(A_surfxml_route_dst);
-
-/*  nb_link = 0;
-  link_name = NULL;
-*/
-  route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
+  route_action = A_surfxml_route_action;
 }
 
 /* KF*/
@@ -238,84 +209,70 @@ static void parse_route_set_routers(void)
   }
 }
 
-//The following is common to all and has been relocated to surfxml_parse
-/* Parses a route element from the XML: UNMODIFIED BY HC */
-/*static void parse_route_elem(void)
-{
-  nb_link++;
-  link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
-  link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
-}
-*/
-
 /* Create the route (more than one hops): MODIFIED BY KF */
 static void parse_route_set_route(void)
 {
-/*  if (nb_link > 1)
-    route_new(src_id, dst_id, link_name, nb_link);
-*/
-    char *name = bprintf("%x#%x",src_id, dst_id);
-    xbt_dict_set(route_table, name, route_link_list, NULL);
+  char *name;
+  if (src_id != -1 && dst_id != -1) {
+    name = bprintf("%x#%x",src_id, dst_id);
+    manage_route(route_table, name, route_action, 0);
     free(name);    
-}
-
-//This is not used anymore. one hop routes are created in add_route
-/* Create the one-hope route: BY KF */
-static void parse_route_set_onehop_route(void)
-{
-  if (nb_link == 1)
-    route_onehop_new(src_id, dst_id, link_name, nb_link);
+  }
 }
 
 static void add_route()
 {
   xbt_ex_t e;
   unsigned int cpt = 0;    
-  int i = 0;
+  int link_list_capacity = 0;
+  int nb_link = 0;
   xbt_dict_cursor_t cursor = NULL;
   char *key,*data, *end;
   const char *sep = "#";
   xbt_dynar_t links, keys;
+  static network_link_GTNETS_t *link_list = NULL;
+
 
+  DEBUG0("Entering add_route()");
   xbt_dict_foreach(route_table, cursor, key, data) {
+    char *link = NULL;
     nb_link = 0;
     links = (xbt_dynar_t)data;
     keys = xbt_str_split_str(key, sep);
 
-    nb_link = xbt_dynar_length(links);
-    link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
+    link_list_capacity = xbt_dynar_length(links);
+    link_list = xbt_new(network_link_GTNETS_t, link_list_capacity);
 
     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
-  
-    i = 0;
-    char* link = NULL;
+    xbt_dynar_free(&keys);
+
     xbt_dynar_foreach (links, cpt, link) {
       TRY {
-        link_name[i++] = xbt_dict_get(link_set, link);
+        link_list[nb_link++] = xbt_dict_get(link_set, link);
       }
       CATCH(e) {
         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
       }     
     }
     if (nb_link > 1)
-      route_new(src_id, dst_id, link_name, nb_link);
+      route_new(src_id, dst_id, link_list, nb_link);
     if (nb_link == 1)
-      route_onehop_new(src_id, dst_id, link_name, nb_link);
+      route_onehop_new(src_id, dst_id, link_list, nb_link);
    }
 
-   xbt_dict_free(&route_table);
+  xbt_dict_free(&route_table);
+  DEBUG0("Bailling add_route()");
 }
 
 /* Main XML parsing */
 static void define_callbacks(const char *file)
 {
-  surfxml_add_callback(STag_surfxml_router_cb_list, &parse_route_set_routers);
-  surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
-  surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
-  surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
-/* surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_onehop_route);*/
-  surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
+  surfxml_add_callback(STag_surfxml_router_cb_list,   &parse_route_set_routers);
+  surfxml_add_callback(STag_surfxml_link_cb_list,     &parse_link_init);
+  surfxml_add_callback(STag_surfxml_route_cb_list,    &parse_route_set_endpoints);
+  surfxml_add_callback(ETag_surfxml_route_cb_list,      &parse_route_set_route);
+  surfxml_add_callback(ETag_surfxml_platform_cb_list,   &add_route);
 }
 
 static void *name_service(const char *name)
@@ -342,8 +299,8 @@ static int resource_used(void *resource_id)
 
 static int action_free(surf_action_t action)
 {
-  action->using--;
-  if (!action->using) {
+  action->refcount--;
+  if (!action->refcount) {
     xbt_swag_remove(action, action->state_set);
     /* KF: No explicit freeing needed for GTNeTS here */
     free(action);
@@ -354,7 +311,7 @@ static int action_free(surf_action_t action)
 
 static void action_use(surf_action_t action)
 {
-  action->using++;
+  action->refcount++;
 }
 
 static void action_cancel(surf_action_t action)
@@ -392,7 +349,11 @@ static double share_resources(double now)
   if (!xbt_swag_size(running_actions))
     return -1.0;
 
-  return gtnets_get_time_to_next_flow_completion();
+  xbt_assert0(time_to_next_flow_completion, "Time to next flow completion not initialized!\n");
+  
+  time_to_next_flow_completion = gtnets_get_time_to_next_flow_completion();
+
+  return time_to_next_flow_completion;
 }
 
 /* delta: by how many time units the simulation must advance */
@@ -409,9 +370,6 @@ static void update_actions_state(double now, double delta)
   xbt_swag_t running_actions =
       surf_network_model->common_public->states.running_action_set;
 
-  double time_to_next_flow_completion =
-      gtnets_get_time_to_next_flow_completion();
-
   /* If there are no renning flows, just return */
   if (time_to_next_flow_completion < 0.0) {
     return;
@@ -433,18 +391,17 @@ static void update_actions_state(double now, double delta)
                  "GTNetS simulation couldn't find a flow that would complete");
     }
 
-
-
     xbt_swag_foreach(action, running_actions) {
-      DEBUG1("]]]]]]]]] Action remains old value: %f", action->generic_action.remains);
+      DEBUG2("Action (%p) remains old value: %f", action, action->generic_action.remains);
       double remain = gtnets_get_flow_rx(action);
+      DEBUG1("Remain value returned by GTNetS : %f", remain);
       //need to trust this remain value
       if(remain == 0){
        action->generic_action.remains=0;
       }else {
-       action->generic_action.remains-=remain;
+       action->generic_action.remains=action->generic_action.cost-remain;
       }
-      DEBUG1("[[[[[[[[[ Action remains new value: %f", action->generic_action.remains);
+      DEBUG2("Action (%p) remains new value: %f", action, action->generic_action.remains);
     }
 
     for (i = 0; i < num_flows; i++) {
@@ -452,12 +409,7 @@ static void update_actions_state(double now, double delta)
       
       action->generic_action.finish = now + time_to_next_flow_completion;
       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
-      /* TODO: Anything else here? */
-
-      //need to map this action to the gtnets engine
-      DEBUG1("]]]]]]]]] Action remains old value: %f", action->generic_action.remains);
-      action->generic_action.remains -= gtnets_get_flow_rx(metadata[i]);
-      DEBUG1("[[[[[[[[[ Action remains new value: %f", action->generic_action.remains);
+      DEBUG1("----> Action (%p) just terminated",action);
     }
 
 
@@ -497,7 +449,7 @@ static surf_action_t communicate(void *src, void *dst, double size,
 
   action = xbt_new0(s_surf_action_network_GTNETS_t, 1);
 
-  action->generic_action.using = 1;
+  action->generic_action.refcount = 1;
   action->generic_action.cost = size;
   action->generic_action.remains = size;
   /* Max durations are not supported */
@@ -564,20 +516,7 @@ static void finalize(void)
   free(surf_network_model);
   surf_network_model = NULL;
 
-#if 0
-  for (i = 0; i < card_number; i++)
-    for (j = 0; j < card_number; j++)
-      free(ROUTE(i, j));
-  free(routing_table);
-  routing_table = NULL;
-  free(routing_table_size);
-  routing_table_size = NULL;
-  card_number = 0;
-#endif
-
-  /* ADDED BY KF */
   gtnets_finalize();
-  /* END ADDITION */
 }
 
 static void surf_network_model_init_internal(void)
@@ -641,3 +580,18 @@ static void surf_network_model_init_internal(void)
     xbt_assert0(0, "impossible to initialize GTNetS interface");
   }
 }
+
+#ifdef HAVE_GTNETS
+void surf_network_model_init_GTNETS(const char *filename)
+{
+  if (surf_network_model)
+    return;
+  surf_network_model_init_internal();
+  define_callbacks(filename);
+  xbt_dynar_push(model_list, &surf_network_model);
+
+  update_model_description(surf_network_model_description,
+                          "GTNets",
+                          (surf_model_t) surf_network_model);
+}
+#endif