Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] recursive extraction of connections between hosts/link/routers
[simgrid.git] / src / instr / instr_routing.c
index 68ccec9..f220220 100644 (file)
@@ -8,24 +8,33 @@
 
 #ifdef HAVE_TRACING
 #include "surf/surf_private.h"
+#include "surf/network_private.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_routing, instr, "Tracing platform hierarchy");
 
 extern xbt_dict_t defined_types; /* from instr_interface.c */
 
+typedef enum {
+  INSTR_HOST,
+  INSTR_LINK,
+  INSTR_ROUTER,
+  INSTR_AS,
+} e_container_types;
+
 typedef struct s_container *container_t;
 typedef struct s_container {
-  char *name;
-  char *type;
-  int level;
+  char *name;     /* Unique id of this container */
+  char *type;     /* Type of this container */
+  char *typename; /* Type name of this container */
+  int level;      /* Level in the hierarchy, root level is 0 */
+  e_container_types kind; /* This container is of what kind */
   struct s_container *father;
   xbt_dict_t children;
 }s_container_t;
 
-static container_t rootContainer = NULL;
-static xbt_dynar_t currentContainer = NULL;
-static xbt_dict_t created_links = NULL;
-static xbt_dict_t created_hosts = NULL;
+static container_t rootContainer = NULL;    /* the root container */
+static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
+static xbt_dict_t allContainers = NULL;     /* all created containers indexed by name */
 xbt_dict_t hosts_types = NULL;
 xbt_dict_t links_types = NULL;
 
@@ -37,6 +46,7 @@ static void instr_routing_parse_start_host (void);
 static void instr_routing_parse_end_host (void);
 static void instr_routing_parse_start_router (void);
 static void instr_routing_parse_end_router (void);
+static void instr_routing_parse_end_platform (void);
 static char *instr_AS_type (int level);
 
 static char *instr_AS_type (int level)
@@ -73,6 +83,15 @@ static void newVariableType (const char *type, const char *parentType, const cha
   }
 }
 
+static void newLinkType (const char *type, const char *parentType, const char *sourceType, const char *destType, const char *name)
+{
+  char *defined = xbt_dict_get_or_null (defined_types, type);
+  if (!defined){
+    pajeDefineLinkType(type, parentType, sourceType, destType, name);
+    xbt_dict_set(defined_types, type, xbt_strdup("1"), xbt_free);
+  }
+}
+
 void instr_routing_define_callbacks ()
 {
   if (!TRACE_is_active())
@@ -85,20 +104,29 @@ void instr_routing_define_callbacks ()
   surfxml_add_callback(ETag_surfxml_host_cb_list, &instr_routing_parse_end_host);
   surfxml_add_callback(STag_surfxml_router_cb_list, &instr_routing_parse_start_router);
   surfxml_add_callback(ETag_surfxml_router_cb_list, &instr_routing_parse_end_router);
+  surfxml_add_callback(ETag_surfxml_platform_cb_list, &instr_routing_parse_end_platform);
 }
 
 
-static container_t newContainer (const char *name, const char *type, const char *typename)
+static container_t newContainer (const char *name, const char *type, e_container_types kind)
 {
   container_t newContainer = xbt_new0(s_container_t, 1);
   newContainer->name = xbt_strdup (name);
   newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
   newContainer->level = newContainer->father->level+1;
   newContainer->type = xbt_strdup (type);
+  switch (kind){
+    case INSTR_HOST: newContainer->typename = xbt_strdup ("HOST"); break;
+    case INSTR_LINK: newContainer->typename = xbt_strdup ("LINK"); break;
+    case INSTR_ROUTER: newContainer->typename = xbt_strdup ("ROUTER"); break;
+    case INSTR_AS: newContainer->typename = xbt_strdup ("AS"); break;
+    default: xbt_die ("Congratulations, you have found a bug on newContainer function of instr_routing.c"); break;
+  }
+  newContainer->kind = kind;
   newContainer->children = xbt_dict_new();
   xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
 
-  newContainerType (newContainer->type, newContainer->father->type, typename);
+  newContainerType (newContainer->type, newContainer->father->type, newContainer->typename);
   pajeCreateContainer (0, newContainer->name, newContainer->type, newContainer->father->name, newContainer->name);
 
   return newContainer;
@@ -122,6 +150,79 @@ static void recursiveDestroyContainer (container_t container)
   container = NULL;
 }
 
+static void linkContainers (container_t container, const char *a1, const char *a2)
+{
+  //ignore loopback
+  if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
+    return;
+
+  char *a1_type = ((container_t)xbt_dict_get (allContainers, a1))->type;
+  char *a1_typename = ((container_t)xbt_dict_get (allContainers, a1))->typename;
+
+  char *a2_type = ((container_t)xbt_dict_get (allContainers, a2))->type;
+  char *a2_typename = ((container_t)xbt_dict_get (allContainers, a2))->typename;
+
+  //declare type
+  char new_link_type[INSTR_DEFAULT_STR_SIZE], new_link_typename[INSTR_DEFAULT_STR_SIZE];
+  snprintf (new_link_type, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type, a2_type);
+  snprintf (new_link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_typename, a2_typename);
+  newLinkType (new_link_type, container->type, a1_type, a2_type, new_link_typename);
+
+  //create the link
+  static long long counter = 0;
+  char key[INSTR_DEFAULT_STR_SIZE];
+  snprintf (key, INSTR_DEFAULT_STR_SIZE, "G%lld", counter++);
+  pajeStartLink(SIMIX_get_clock(), new_link_type, container->name, "graph", a1, key);
+  pajeEndLink(SIMIX_get_clock(), new_link_type, container->name, "graph", a2, key);
+}
+
+static void recursiveGraphExtraction (container_t container)
+{
+  if (xbt_dict_length(container->children)){
+    xbt_dict_cursor_t cursor = NULL;
+    container_t child;
+    char *child_name;
+    //bottom-up recursion
+    xbt_dict_foreach(container->children, cursor, child_name, child) {
+      recursiveGraphExtraction (child);
+    }
+
+    //let's get routes
+    xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
+    container_t child1, child2;
+    const char *child_name1, *child_name2;
+
+    xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
+      xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
+        if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
+            (child2->kind == INSTR_HOST  || child2->kind == INSTR_ROUTER)){
+
+          //getting route
+          xbt_dynar_t route;
+          xbt_ex_t exception;
+          TRY {
+            route = global_routing->get_route (child_name1, child_name2);
+          }CATCH(exception) {
+            //no route between them, that's possible
+            continue;
+          }
+
+          //link the route members
+          unsigned int cpt;
+          void *link;
+          char *previous_entity_name = (char*)child_name1;
+          xbt_dynar_foreach (route, cpt, link) {
+            char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
+            linkContainers (container, previous_entity_name, link_name);
+            previous_entity_name = link_name;
+          }
+          linkContainers (container, previous_entity_name, child_name2);
+        }
+      }
+    }
+  }
+}
+
 /*
  * Callbacks
  */
@@ -131,15 +232,16 @@ static void instr_routing_parse_start_AS ()
     rootContainer = xbt_new0(s_container_t, 1);
     rootContainer->name = xbt_strdup ("0");
     rootContainer->type = xbt_strdup ("0");
+    rootContainer->typename = xbt_strdup ("0");
     rootContainer->level = 0;
     rootContainer->father = NULL;
     rootContainer->children = xbt_dict_new();
+    rootContainer->kind = INSTR_AS;
 
     currentContainer = xbt_dynar_new (sizeof(s_container_t), NULL);
     xbt_dynar_push (currentContainer, rootContainer);
 
-    created_links = xbt_dict_new ();
-    created_hosts = xbt_dict_new ();
+    allContainers = xbt_dict_new ();
     hosts_types = xbt_dict_new ();
     links_types = xbt_dict_new ();
   }
@@ -149,7 +251,9 @@ static void instr_routing_parse_start_AS ()
   newContainer->father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
   newContainer->level = newContainer->father->level+1;
   newContainer->type = instr_AS_type (newContainer->level);
+  newContainer->typename = instr_AS_type (newContainer->level);
   newContainer->children = xbt_dict_new();
+  newContainer->kind = INSTR_AS;
   xbt_dict_set(newContainer->father->children, newContainer->name, newContainer, NULL);
 
   //trace
@@ -170,7 +274,7 @@ static void instr_routing_parse_start_link ()
   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
   char type[INSTR_DEFAULT_STR_SIZE];
   snprintf (type, INSTR_DEFAULT_STR_SIZE, "LINK-%s", father->type);
-  container_t new = newContainer (A_surfxml_link_id, type, "LINK");
+  container_t new = newContainer (A_surfxml_link_id, type, INSTR_LINK);
 
   //bandwidth and latency
   char bandwidth_type[INSTR_DEFAULT_STR_SIZE], latency_type[INSTR_DEFAULT_STR_SIZE];
@@ -189,7 +293,7 @@ static void instr_routing_parse_start_link ()
   }
 
   //register created link on the dictionary
-  xbt_dict_set (created_links, A_surfxml_link_id, new, NULL);
+  xbt_dict_set (allContainers, A_surfxml_link_id, new, NULL);
 
   //register this link type
   xbt_dict_set (links_types, type, xbt_strdup("1"), xbt_free);
@@ -204,7 +308,7 @@ static void instr_routing_parse_start_host ()
   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
   char type[INSTR_DEFAULT_STR_SIZE];
   snprintf (type, INSTR_DEFAULT_STR_SIZE, "HOST-%s", father->type);
-  container_t new = newContainer (A_surfxml_host_id, type, "HOST");
+  container_t new = newContainer (A_surfxml_host_id, type, INSTR_HOST);
 
   //power
   char power_type[INSTR_DEFAULT_STR_SIZE];
@@ -220,7 +324,7 @@ static void instr_routing_parse_start_host ()
   }
 
   //register created host on the dictionary
-  xbt_dict_set (created_hosts, A_surfxml_host_id, new, NULL);
+  xbt_dict_set (allContainers, A_surfxml_host_id, new, NULL);
 
   //register this link type
   xbt_dict_set (hosts_types, type, xbt_strdup("1"), xbt_free);
@@ -235,19 +339,28 @@ static void instr_routing_parse_start_router ()
   container_t father = xbt_dynar_get_ptr(currentContainer, xbt_dynar_length(currentContainer)-1);
   char type[INSTR_DEFAULT_STR_SIZE];
   snprintf (type, INSTR_DEFAULT_STR_SIZE, "ROUTER-%s", father->type);
-  newContainer (A_surfxml_router_id, type, "ROUTER");
+  container_t new = newContainer (A_surfxml_router_id, type, INSTR_ROUTER);
+
+  //register created host on the dictionary
+  xbt_dict_set (allContainers, A_surfxml_router_id, new, NULL);
 }
 
 static void instr_routing_parse_end_router ()
 {
 }
 
+static void instr_routing_parse_end_platform ()
+{
+  currentContainer = NULL;
+  recursiveGraphExtraction (rootContainer);
+}
+
 /*
  * Support functions
  */
 int instr_link_is_traced (const char *name)
 {
-  if (xbt_dict_get_or_null(created_links, name)) {
+  if (((container_t)xbt_dict_get_or_null (allContainers, name))){
     return 1;
   } else {
     return 0;
@@ -256,23 +369,12 @@ int instr_link_is_traced (const char *name)
 
 char *instr_link_type (const char *name)
 {
-  container_t created_link = xbt_dict_get_or_null(created_links, name);
-  if (created_link){
-    return created_link->type;
-  }else{
-    return NULL;
-  }
+  return ((container_t)xbt_dict_get (allContainers, name))->type;
 }
 
-
 char *instr_host_type (const char *name)
 {
-  container_t created_host = xbt_dict_get_or_null(created_hosts, name);
-  if (created_host){
-    return created_host->type;
-  }else{
-    return NULL;
-  }
+  return ((container_t)xbt_dict_get (allContainers, name))->type;
 }
 
 void instr_destroy_platform ()