Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] no longer need to pass the parent, we find the lowest common parent instead
[simgrid.git] / src / instr / instr_routing.c
index 084d252..a6707ef 100644 (file)
@@ -18,42 +18,50 @@ extern xbt_dict_t defined_types; /* from instr_interface.c */
 static int platform_created = 0;            /* indicate whether the platform file has been traced */
 static xbt_dynar_t currentContainer = NULL; /* push and pop, used only in creation */
 
-static container_t findChild (container_t root, container_t a1)
+static container_t lowestCommonAncestor (container_t a1, container_t a2)
 {
-  if (root == a1) return root;
+  //this is only an optimization (since most of a1 and a2 share the same parent)
+  if (a1->father == a2->father) return a1->father;
 
-  xbt_dict_cursor_t cursor = NULL;
-  container_t child;
-  char *child_name;
-  xbt_dict_foreach(root->children, cursor, child_name, child) {
-    if (findChild (child, a1)) return child;
+  //create an array with all ancestors of a1
+  xbt_dynar_t ancestors = xbt_dynar_new(sizeof(container_t), NULL);
+  container_t p;
+  p = a1->father;
+  while (p){
+    xbt_dynar_push_as (ancestors, container_t, p);
+    p = p->father;
   }
-  return NULL;
-}
-
-static container_t findCommonFather (container_t root, container_t a1, container_t a2)
-{
-  if (a1->father == a2->father) return a1->father;
 
-  xbt_dict_cursor_t cursor = NULL;
-  container_t child;
-  char *child_name;
-  container_t a1_try = NULL;
-  container_t a2_try = NULL;
-  xbt_dict_foreach(root->children, cursor, child_name, child) {
-    a1_try = findChild (child, a1);
-    a2_try = findChild (child, a2);
-    if (a1_try && a2_try) return child;
+  //find the lowest ancestor of a2 in the previously created array
+  p = a2->father;
+  while (p){
+    int cpt;
+    unsigned int cursor;
+    xbt_dynar_foreach (ancestors, cursor, cpt){
+      container_t pp = *(container_t*)xbt_dynar_get_ptr(ancestors, cursor);
+      if (p == pp){
+        return p;
+      }
+    }
+    p = p->father;
   }
+
+  //not common ancestor found, return NULL
   return NULL;
 }
 
-static void linkContainers (container_t father, container_t src, container_t dst, xbt_dict_t filter)
+static void linkContainers (container_t src, container_t dst, xbt_dict_t filter)
 {
   //ignore loopback
   if (strcmp (src->name, "__loopback__") == 0 || strcmp (dst->name, "__loopback__") == 0)
     return;
 
+  //find common father
+  container_t father = lowestCommonAncestor (src, dst);
+  if (!father){
+    xbt_die ("common father unknown, this is a tracing problem");
+  }
+
   if (filter != NULL){
     //check if we already register this pair (we only need one direction)
     char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
@@ -131,10 +139,10 @@ static void recursiveGraphExtraction (AS_t rc, container_t container, xbt_dict_t
           link_CM02_t *link = ((link_CM02_t*)xbt_dynar_get_ptr (route, i));
           char *link_name = (*link)->lmm_resource.generic_resource.name;
           container_t current = PJ_container_get(link_name);
-          linkContainers(container, previous, current, filter);
+          linkContainers(previous, current, filter);
           previous = current;
         }
-        linkContainers(container, previous, child2, filter);
+        linkContainers(previous, child2, filter);
 
       }else if (child1->kind == INSTR_AS &&
                 child2->kind == INSTR_AS &&
@@ -149,11 +157,11 @@ static void recursiveGraphExtraction (AS_t rc, container_t container, xbt_dict_t
         xbt_dynar_foreach (route->link_list, cpt, link) {
           char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
           container_t current = PJ_container_get(link_name);
-          linkContainers (container, previous, current, filter);
+          linkContainers (previous, current, filter);
           previous = current;
         }
         container_t last = PJ_container_get(route->dst_gateway);
-        linkContainers (container, previous, last, filter);
+        linkContainers (previous, last, filter);
         generic_free_route(route);
       }
     }