Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
requests the flatifier to display the amount of core if not equal to 1
[simgrid.git] / teshsuite / simdag / platforms / flatifier.c
index f813efe..e0762b0 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2008, 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2008-2014. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
 #include "xbt/log.h"
 #include "xbt/dict.h"
 #include "xbt/ex.h"
+#include "xbt/xbt_os_time.h"
 #include "surf/surf.h"
 #include "surf/surf_private.h"
 
-XBT_LOG_NEW_DEFAULT_CATEGORY(validator,
-                             "Logging specific to this SimDag example");
+static const char link_ctn_v2[] =  "link:ctn";
+static const char link_ctn_v3[] = "link_ctn";
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(flatifier,
+                             "Logging specific to this platform parsing tool");
 
 static int name_compare_hosts(const void *n1, const void *n2)
 {
@@ -43,122 +47,243 @@ static int name_compare_links(const void *n1, const void *n2)
   return strcmp(name1, name2);
 }
 
+static int parse_cmdline(int *timings, int *downgrade, char **platformFile, int argc, char **argv)
+{
+  int wrong_option = 0;
+  int i;
+  for (i = 1; i < argc; i++) {
+    if (strlen(argv[i]) > 1 && argv[i][0] == '-' && argv[i][1] == '-') {
+      if (!strcmp(argv[i], "--timings")) {
+        *timings = 1;
+      } else {
+        if (!strcmp(argv[i], "--downgrade")) {
+          *downgrade = 1;
+        } else {
+          wrong_option = 1;
+          break;
+        }
+      }
+    } else {
+      *platformFile = argv[i];
+    }
+  }
+  return wrong_option;
+}
+
+static void create_environment(xbt_os_timer_t parse_time, const char *platformFile)
+{
+  xbt_ex_t e;
+  TRY {
+    xbt_os_cputimer_start(parse_time);
+    SD_create_environment(platformFile);
+    xbt_os_cputimer_stop(parse_time);
+  }
+  CATCH(e) {
+    xbt_die("Error while loading %s: %s", platformFile, e.msg);
+  }
+}
+
 int main(int argc, char **argv)
 {
   char *platformFile = NULL;
   int totalHosts, totalLinks;
+  int timings=0;
+  int downgrade = 0;
+  int version = 3;
+  const char *link_ctn = link_ctn_v3;
   unsigned int i;
   xbt_dict_t props = NULL;
   xbt_dict_cursor_t cursor = NULL;
-  xbt_dict_cursor_t cursor_src = NULL;
-  xbt_dict_cursor_t cursor_dst = NULL;
+  xbt_lib_cursor_t cursor_src = NULL;
+  xbt_lib_cursor_t cursor_dst = NULL;
   char *src,*dst,*key,*data;
-  xbt_ex_t e;
+  sg_routing_edge_t value1;
+  sg_routing_edge_t value2;
 
   const SD_workstation_t *hosts;
   const SD_link_t *links;
+  xbt_os_timer_t parse_time = xbt_os_timer_new();
+
+#ifdef _XBT_WIN32
+  setbuf(stderr, NULL);
+  setbuf(stdout, NULL);
+#else
+  setvbuf(stdout, NULL, _IOLBF, 0);
+#endif
 
   SD_init(&argc, argv);
 
-  platformFile = argv[1];
-  DEBUG1("%s", platformFile);
-  TRY {
-    SD_create_environment(platformFile);
-  } CATCH(e) {
-    xbt_die(bprintf("Error while loading %s: %s",platformFile,e.msg));     
+  if (parse_cmdline(&timings, &downgrade, &platformFile, argc, argv) || !platformFile) {
+    xbt_die("Invalid command line arguments: expected [--timings|--downgrade] platformFile");
   }
+  XBT_DEBUG("%d,%d,%s", timings, downgrade, platformFile);
+
+  if (downgrade) {
+    version = 2;
+    link_ctn = link_ctn_v2;
+  }
+
+  create_environment(parse_time, platformFile);
 
-  printf("<?xml version='1.0'?>\n");
-  printf("<!DOCTYPE platform SYSTEM \"simgrid.dtd\">\n");
-  printf("<platform version=\"3\">\n");
-  printf("<AS id=\"AS0\" routing=\"Full\">\n");
+  if (timings) {
+    XBT_INFO("Parsing time: %fs (%d hosts, %d links)",
+          xbt_os_timer_elapsed(parse_time),SD_workstation_get_number(),SD_link_get_number());
+  } else {
+    printf("<?xml version='1.0'?>\n");
+    printf("<!DOCTYPE platform SYSTEM \"http://simgrid.gforge.inria.fr/simgrid.dtd\">\n");
+    printf("<platform version=\"%d\">\n", version);
+    if (!downgrade)
+      printf("<AS id=\"AS0\" routing=\"Full\">\n");
 
-  // Hosts
-  totalHosts = SD_workstation_get_number();
-  hosts = SD_workstation_get_list();
-  qsort((void *) hosts, totalHosts, sizeof(SD_workstation_t),
+    // Hosts
+    totalHosts = SD_workstation_get_number();
+    hosts = SD_workstation_get_list();
+    qsort((void *) hosts, totalHosts, sizeof(SD_workstation_t),
         name_compare_hosts);
 
-  for (i = 0; i < totalHosts; i++) {
-    printf("  <host id=\"%s\" power=\"%.0f\"",
-           SD_workstation_get_name(hosts[i]),
-           SD_workstation_get_power(hosts[i]));
-    props = SD_workstation_get_properties(hosts[i]);
-    if (xbt_dict_length(props) > 0) {
-      printf(">\n");
-      xbt_dict_foreach(props, cursor, key, data) {
-        printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
+    for (i = 0; i < totalHosts; i++) {
+      printf("  <host id=\"%s\" power=\"%.0f\"",
+          SD_workstation_get_name(hosts[i]),
+          SD_workstation_get_power(hosts[i]));
+      props = SD_workstation_get_properties(hosts[i]);
+      if (SD_workstation_get_cores(hosts[i])>1) {
+        printf(" cores=\"%d\"", SD_workstation_get_cores(hosts[i]));
+      }
+      if (props && !xbt_dict_is_empty(props)) {
+        printf(">\n");
+        xbt_dict_foreach(props, cursor, key, data) {
+          printf("    <prop id=\"%s\" value=\"%s\"/>\n", key, data);
+        }
+        printf("  </host>\n");
+      } else {
+        printf("/>\n");
       }
-      printf("  </host>\n");
-    } else {
-      printf("/>\n");
     }
-  }
 
-  // Routers
-  xbt_dict_foreach(global_routing->where_network_elements, cursor, key, data) {
-         if(((network_element_info_t)xbt_dict_get(global_routing->where_network_elements, key))->rc_type
-                         == SURF_NETWORK_ELEMENT_ROUTER)
-         {
-                 printf("  <router id=\"%s\"/>\n",key);
-         }
-  }
+    // Routers
+    xbt_lib_foreach(as_router_lib, cursor_src, key, value1) {
+      if(surf_routing_edge_get_rc_type(xbt_lib_get_or_null(as_router_lib, key,
+          ROUTING_ASR_LEVEL)) == SURF_NETWORK_ELEMENT_ROUTER)
+      {
+        printf("  <router id=\"%s\"/>\n",key);
+      }
+    }
 
-  // Links
-  totalLinks = SD_link_get_number();
-  links = SD_link_get_list();
-  qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
+    // Links
+    totalLinks = SD_link_get_number();
+    links = SD_link_get_list();
 
-  for (i = 0; i < totalLinks; i++) {
-    printf("  <link id=\"");
+    qsort((void *) links, totalLinks, sizeof(SD_link_t), name_compare_links);
 
-    printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"",
-           SD_link_get_name(links[i]),
-           SD_link_get_current_bandwidth(links[i]),
-           SD_link_get_current_latency(links[i]));
-    if (SD_link_get_sharing_policy(links[i]) == SD_LINK_SHARED) {
-      printf("/>\n");
-    } else {
-      printf(" sharing_policy=\"FATPIPE\"/>\n");
+    for (i = 0; i < totalLinks; i++) {
+      printf("  <link id=\"");
+
+      printf("%s\" bandwidth=\"%.0f\" latency=\"%.9f\"",
+          SD_link_get_name(links[i]),
+          SD_link_get_current_bandwidth(links[i]),
+          SD_link_get_current_latency(links[i]));
+      if (SD_link_get_sharing_policy(links[i]) == SD_LINK_SHARED) {
+        printf("/>\n");
+      } else {
+        printf(" sharing_policy=\"FATPIPE\"/>\n");
+      }
     }
-  }
 
-  // Routes
-  xbt_dict_foreach(global_routing->where_network_elements, cursor_src, src, data)
-  {
-         if(((network_element_info_t)xbt_dict_get(global_routing->where_network_elements, src))->rc_type
-                         == SURF_NETWORK_ELEMENT_ROUTER ||
-                         ((network_element_info_t)xbt_dict_get(global_routing->where_network_elements, src))->rc_type
-                         == SURF_NETWORK_ELEMENT_HOST)
-         {
-                 xbt_dict_foreach(global_routing->where_network_elements, cursor_dst, dst, data)
-                 {
-                         if(((network_element_info_t)xbt_dict_get(global_routing->where_network_elements, dst))->rc_type
-                                         == SURF_NETWORK_ELEMENT_ROUTER ||
-                                         ((network_element_info_t)xbt_dict_get(global_routing->where_network_elements, dst))->rc_type
-                                         == SURF_NETWORK_ELEMENT_HOST)
-                         {
-                               printf("  <route src=\"%s\" dst=\"%s\">\n       "
-                                         ,src
-                                         ,dst);
-                               xbt_dynar_t route = global_routing->get_route(src,dst);
-                               for(i=0;i<xbt_dynar_length(route) ;i++)
-                               {
-                                       void *link = xbt_dynar_get_as(route,i,void *);
-
-                                       char *link_name = bprintf("%s",((surf_resource_t) link)->name);
-                                       printf("<link_ctn id=\"%s\"/>",link_name);
-                               }
-                               printf("\n  </route>\n");
-
-                         }
-                 }
-         }
-  }
 
-  printf("</AS>\n");
-  printf("</platform>\n");
+    xbt_lib_foreach(host_lib, cursor_src, src, value1) // Routes from host
+    {
+      value1 = xbt_lib_get_or_null(host_lib,src,ROUTING_HOST_LEVEL);
+      xbt_lib_foreach(host_lib, cursor_dst, dst, value2) //to host
+      {
+        printf("  <route src=\"%s\" dst=\"%s\">\n  "
+            ,src
+            ,dst);
+        xbt_dynar_t route=NULL;
+        value2 = xbt_lib_get_or_null(host_lib,dst,ROUTING_HOST_LEVEL);
+        routing_get_route_and_latency(value1,value2,&route,NULL);
+        for(i=0;i<xbt_dynar_length(route) ;i++)
+        {
+          void *link = xbt_dynar_get_as(route,i,void *);
+
+          char *link_name = xbt_strdup(surf_resource_name(link));
+          printf("<%s id=\"%s\"/>",link_ctn,link_name);
+          free(link_name);
+        }
+        printf("\n  </route>\n");
+      }
+      xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
+      {
+        if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
+          printf("  <route src=\"%s\" dst=\"%s\">\n  "
+              ,src
+              ,dst);
+          xbt_dynar_t route=NULL;
+          value2 = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
+          routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route,NULL);
+          for(i=0;i<xbt_dynar_length(route) ;i++)
+          {
+            void *link = xbt_dynar_get_as(route,i,void *);
+
+            char *link_name = xbt_strdup(surf_resource_name(link));
+            printf("<%s id=\"%s\"/>",link_ctn,link_name);
+            free(link_name);
+          }
+          printf("\n  </route>\n");
+        }
+      }
+    }
+
+    xbt_lib_foreach(as_router_lib, cursor_src, src, value1) // Routes from router
+    {
+      value1 = xbt_lib_get_or_null(as_router_lib,src,ROUTING_ASR_LEVEL);
+      if(routing_get_network_element_type(src) == SURF_NETWORK_ELEMENT_ROUTER){
+        xbt_lib_foreach(as_router_lib, cursor_dst, dst, value2) //to router
+          {
+          if(routing_get_network_element_type(dst) == SURF_NETWORK_ELEMENT_ROUTER){
+            printf("  <route src=\"%s\" dst=\"%s\">\n  "
+                ,src
+                ,dst);
+            xbt_dynar_t route=NULL;
+            value2 = xbt_lib_get_or_null(as_router_lib,dst,ROUTING_ASR_LEVEL);
+            routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route,NULL);
+            for(i=0;i<xbt_dynar_length(route) ;i++)
+            {
+              void *link = xbt_dynar_get_as(route,i,void *);
+
+              char *link_name = xbt_strdup(surf_resource_name(link));
+              printf("<%s id=\"%s\"/>",link_ctn,link_name);
+              free(link_name);
+            }
+            printf("\n  </route>\n");
+          }
+          }
+        xbt_lib_foreach(host_lib, cursor_dst, dst, value2) //to host
+        {
+          printf("  <route src=\"%s\" dst=\"%s\">\n  "
+              ,src, dst);
+          xbt_dynar_t route=NULL;
+          value2 = xbt_lib_get_or_null(host_lib,dst,ROUTING_HOST_LEVEL);
+          routing_get_route_and_latency((sg_routing_edge_t)value1,(sg_routing_edge_t)value2,&route, NULL);
+          for(i=0;i<xbt_dynar_length(route) ;i++)
+          {
+            void *link = xbt_dynar_get_as(route,i,void *);
+
+            char *link_name = xbt_strdup(surf_resource_name(link));
+            printf("<%s id=\"%s\"/>",link_ctn,link_name);
+            free(link_name);
+          }
+          printf("\n  </route>\n");
+        }
+      }
+    }
+
+    if (!downgrade)
+      printf("</AS>\n");
+    printf("</platform>\n");
+  }
   SD_exit();
+  xbt_os_timer_free(parse_time);
 
   return 0;
 }