Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Plug a memleak.
[simgrid.git] / src / surf / surfxml_parse.c
index 736792a..cf8736f 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "xbt/misc.h"
 #include "xbt/log.h"
+#include "xbt/str.h"
 #include "xbt/dict.h"
 #include "surf/surfxml_parse_private.h"
 #include "surf/surf_private.h"
@@ -15,12 +16,17 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf,
                                "Logging specific to the SURF parsing module");
 
 #undef CLEANUP
-#include "surfxml.c"
+#include "simgrid_dtd.c"
 
 /* Initialize the parsing globals */
 int route_action = 0;
 xbt_dict_t traces_set_list = NULL;
-xbt_dynar_t traces_connect_list = NULL;
+//xbt_dynar_t traces_connect_list = NULL;
+xbt_dict_t trace_connect_list_host_avail = NULL;
+xbt_dict_t trace_connect_list_power = NULL;
+xbt_dict_t trace_connect_list_link_avail = NULL;
+xbt_dict_t trace_connect_list_bandwidth = NULL;
+xbt_dict_t trace_connect_list_latency = NULL;
 
 /* This buffer is used to store the original buffer before substituing it by out own buffer. Usefull for the foreach tag */
 char* old_buff;
@@ -74,7 +80,7 @@ xbt_dict_t current_property_set = NULL;
 xbt_dict_t route_table = NULL;
 xbt_dict_t route_multi_table = NULL;
 xbt_dynar_t route_multi_elements = NULL;
-xbt_dynar_t route_link_list = NULL;
+static xbt_dynar_t route_link_list = NULL;
 xbt_dynar_t links = NULL;
 xbt_dynar_t keys = NULL;
 
@@ -89,6 +95,17 @@ YY_BUFFER_STATE surf_input_buffer;
 FILE *surf_file_to_parse = NULL;
 
 static void convert_route_multi_to_routes(void);
+static void parse_route_elem(void);
+static void parse_foreach(void);
+static void parse_sets(void);
+static void parse_route_multi_set_endpoints(void);
+static void parse_route_multi_set_route(void);
+static void parse_cluster(void);
+static void parse_trace_init(void);
+static void parse_trace_finalize(void);
+static void parse_trace_c_connect(void);
+static void init_randomness(void);
+static void add_randomness(void);
 
 void surf_parse_free_callbacks(void)
 {
@@ -211,8 +228,6 @@ void STag_surfxml_platform(void)
              "is available in the contrib/platform_generation directory "
              "of the simgrid repository.");
 
-  if (set_list == NULL) set_list = xbt_dict_new(); 
-
   surfxml_call_cb_functions(STag_surfxml_platform_cb_list);
 
 }
@@ -225,7 +240,6 @@ void ETag_surfxml_platform(void)
 
   xbt_dict_free(&random_data_list);
   xbt_dict_free(&set_list);
-
 }
 
 void STag_surfxml_host(void)
@@ -474,11 +488,6 @@ void parse_properties(void)
    xbt_dict_set(current_property_set, A_surfxml_prop_id, value, free);
 }
 
-void free_string(void *d)
-{
-  free(*(void**)d);
-}
-
 void surfxml_add_callback(xbt_dynar_t cb_list, void_f_void_t function)
 {
   xbt_dynar_push(cb_list, &function);
@@ -494,7 +503,11 @@ static XBT_INLINE void surfxml_call_cb_functions(xbt_dynar_t cb_list)
     }
 }
 
-void init_data(void)
+static void parse_route_set_endpoints(void) {
+  route_link_list = xbt_dynar_new(sizeof(char *), NULL);
+}
+
+static void init_data(void)
 {
   xbt_dict_free(&route_table);
   xbt_dynar_free(&route_link_list);
@@ -503,15 +516,66 @@ void init_data(void)
   route_multi_table = xbt_dict_new();
   route_multi_elements = xbt_dynar_new(sizeof(char*), NULL);
   traces_set_list = xbt_dict_new();
-  traces_connect_list = xbt_dynar_new(sizeof(char*), NULL);
+  if (set_list == NULL) set_list = xbt_dict_new();
+
+  trace_connect_list_host_avail = xbt_dict_new();
+  trace_connect_list_power = xbt_dict_new();
+  trace_connect_list_link_avail = xbt_dict_new();
+  trace_connect_list_bandwidth = xbt_dict_new();
+  trace_connect_list_latency = xbt_dict_new();
+
   random_data_list = xbt_dict_new();
+  surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
+  surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
+  surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
+  surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
+  surfxml_add_callback(STag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_endpoints);
+  surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_route);
+  surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_foreach);
+  surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_cluster);
+  surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_trace_init);
+  surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_trace_finalize);
+  surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list, &parse_trace_c_connect);
+  surfxml_add_callback(STag_surfxml_random_cb_list, &init_randomness);
+  surfxml_add_callback(ETag_surfxml_random_cb_list, &add_randomness);
+}
+
+static void free_data(void) 
+{
+  char *key,*data;
+  xbt_dict_cursor_t cursor = NULL;
 
+  xbt_dict_foreach(route_table, cursor, key, data) {
+    xbt_dynar_t links = (xbt_dynar_t)data;
+    xbt_dynar_free(&links);
+  }
+  xbt_dict_free(&route_table);
+  route_link_list = NULL;
+
+  xbt_dict_foreach(set_list, cursor, key, data) {
+    xbt_dynar_t set = (xbt_dynar_t)data;
+    char *name;
+    unsigned int cpt = 0;
+
+    xbt_dynar_foreach (set, cpt, name)  free(name);
+    xbt_dynar_free(&set);
+  }
+  xbt_dict_free(&set_list);
+
+  xbt_dict_free(&trace_connect_list_host_avail);
+  xbt_dict_free(&trace_connect_list_power);
+  xbt_dict_free(&trace_connect_list_link_avail);
+  xbt_dict_free(&trace_connect_list_bandwidth);
+  xbt_dict_free(&trace_connect_list_latency);
+  xbt_dict_free(&traces_set_list);
 }
 
 void parse_platform_file(const char* file)
 {
   surf_parse_open(file);
+  init_data();
   xbt_assert1((!(*surf_parse)()), "Parse error in %s", file);
+  free_data();
   surf_parse_close();
 }
 
@@ -565,7 +629,7 @@ static void parse_restore_original_buffer(void)
 
 /* Functions for the sets and foreach tags */
 
-void parse_sets(void)
+static void parse_sets(void)
 {
   char *id, *suffix, *prefix, *radical;
   int start, end;
@@ -590,12 +654,17 @@ void parse_sets(void)
   current_set = xbt_dynar_new(sizeof(char*), NULL);
 
   
-  for (i=start; i<end; i++) {
+  for (i=start; i<=end; i++) {
      value = bprintf("%s%d%s", prefix, i, suffix);
      xbt_dynar_push(current_set, &value);
   } 
   
   xbt_dict_set(set_list, id, current_set, NULL);
+
+  free(radical);
+  free(suffix);
+  free(prefix);
+  free(id);
 }
 
 static const char* surfxml_host_power;
@@ -698,7 +767,7 @@ static void finalize_link_foreach(void)
   surfxml_bufferstack = old_buff;
 }
 
-void parse_foreach(void)
+static void parse_foreach(void)
 {
   /* save the host & link callbacks */
   main_STag_surfxml_host_cb_list = STag_surfxml_host_cb_list;
@@ -727,7 +796,7 @@ static int route_multi_size=0;
 static char* src_name, *dst_name;
 static int is_symmetric_route;
 
-void parse_route_elem(void)
+static void parse_route_elem(void)
 {
   char *val;
 
@@ -736,7 +805,7 @@ void parse_route_elem(void)
   xbt_dynar_push(route_link_list, &val);
 }
 
-void parse_route_multi_set_endpoints(void)
+static void parse_route_multi_set_endpoints(void)
 {
   src_name = xbt_strdup(A_surfxml_route_c_multi_src); 
   dst_name = xbt_strdup(A_surfxml_route_c_multi_dst); 
@@ -744,7 +813,7 @@ void parse_route_multi_set_endpoints(void)
   is_symmetric_route = A_surfxml_route_c_multi_symmetric;
   route_multi_size++;
 
-  route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
+  route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
 }
 
 static int contains(xbt_dynar_t list, const char* value)
@@ -759,7 +828,7 @@ static int contains(xbt_dynar_t list, const char* value)
 }
 
 /* 
-   This function is used to append or override the contents of an alread existing route in the case a new one with its name is found.
+   This function is used to append or override the contents of an already existing route in the case a new one with its name is found.
    The decision is based upon the value of action specified in the xml route:multi attribute action
  */
 void manage_route(xbt_dict_t routing_table, const char *route_name, int action, int isMultiRoute)
@@ -777,14 +846,17 @@ void manage_route(xbt_dict_t routing_table, const char *route_name, int action,
                                    xbt_dynar_foreach(links, cpt, value) {      
                                        xbt_dynar_push(route_link_list,&value);
                                    }
+                                   xbt_dynar_free(&links);
                                     break;
         case A_surfxml_route_action_POSTPEND: /* add existing links in front; links + route_link_list */ 
                                    xbt_dynar_foreach(route_link_list, cpt, value) {
                                        xbt_dynar_push(links,&value);
                                    }
+                                   xbt_dynar_free(&route_link_list);
                                     route_link_list = links;
                                    break;
         case A_surfxml_route_action_OVERRIDE:
+                                   xbt_dynar_free(&links);
                                     break;
         default:break;
      }
@@ -795,7 +867,7 @@ void manage_route(xbt_dict_t routing_table, const char *route_name, int action,
   }
 }
 
-void parse_route_multi_set_route(void)
+static void parse_route_multi_set_route(void)
 {
   char* route_name;
 
@@ -850,11 +922,12 @@ static void convert_route_multi_to_routes(void)
   unsigned int cpt, cpt2, cursor;
   char *src_host_name, *dst_host_name, *key, *src, *dst, *val, *key_w, *data_w; 
   const char* sep="#";
+  xbt_dict_t set;
   xbt_dynar_t src_names = NULL, dst_names = NULL, links;
 
   if (!route_multi_elements) return;
 
-  xbt_dict_t set = cpu_set;
+  set = cpu_set;
   DEBUG1("%d", xbt_dict_length(workstation_set));                              
   if (workstation_set != NULL && xbt_dict_length(workstation_set) > 0)
      set = workstation_set;
@@ -878,14 +951,14 @@ static void convert_route_multi_to_routes(void)
     dst_names = (xbt_dynar_t)xbt_dict_get_or_null(set_list, dst);
     /* Add to dynar even if they are simple names */
     if (src_names == NULL) {
-       src_names = xbt_dynar_new(sizeof(char *), &free_string);
+       src_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
        val = xbt_strdup(src);
        xbt_dynar_push(src_names, &val);
        if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
          THROW3(unknown_error,0,"(In route:multi (%s -> %s) source %s does not exist (not a set or a host)", src, dst, src);
     }
     if (dst_names == NULL) {
-       dst_names = xbt_dynar_new(sizeof(char *), &free_string);
+       dst_names = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
        val = xbt_strdup(dst);
        if (strcmp(val,"$*") != 0 && NULL == xbt_dict_get_or_null(set, val))
          THROW3(unknown_error,0,"(In route:multi (%s -> %s) destination %s does not exist (not a set or a host)", src, dst, dst);
@@ -924,7 +997,7 @@ static void convert_route_multi_to_routes(void)
 
 /* Cluster tag functions */
 
-void parse_cluster(void)
+static void parse_cluster(void)
 {  
    static int AX_ptr = 0;
    static int surfxml_bufferstack_size = 2048;
@@ -1027,14 +1100,14 @@ static double trace_periodicity = -1.0;
 static char* trace_file = NULL;
 static char* trace_id;
 
-void parse_trace_init(void)
+static void parse_trace_init(void)
 {
    trace_id = strdup(A_surfxml_trace_id);
    trace_file = strdup(A_surfxml_trace_file);
    surf_parse_get_double(&trace_periodicity, A_surfxml_trace_periodicity);
 }
 
-void parse_trace_finalize(void)
+static void parse_trace_finalize(void)
 {
   tmgr_trace_t trace;
   if (!trace_file || strcmp(trace_file,"") != 0) {
@@ -1048,14 +1121,30 @@ void parse_trace_finalize(void)
   xbt_dict_set(traces_set_list, trace_id, (void *)trace, NULL);
 }
 
-void parse_trace_c_connect(void)
+static void parse_trace_c_connect(void)
 {
-       char* trace_connect;
-   xbt_assert1(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace_id),
-             "Trace %s undefined", A_surfxml_trace_c_connect_trace_id);
-   trace_connect = bprintf("%s#%d#%d#%s", A_surfxml_trace_c_connect_trace_id, A_surfxml_trace_c_connect_element, 
-                                   A_surfxml_trace_c_connect_kind, A_surfxml_trace_c_connect_connector_id);
-   xbt_dynar_push(traces_connect_list, &trace_connect);
+   xbt_assert2(xbt_dict_get_or_null(traces_set_list, A_surfxml_trace_c_connect_trace),
+             "Cannot connect trace %s to %s: trace unknown", A_surfxml_trace_c_connect_trace,A_surfxml_trace_c_connect_element);
+   
+   switch (A_surfxml_trace_c_connect_kind) {
+    case A_surfxml_trace_c_connect_kind_HOST_AVAIL:
+      xbt_dict_set(trace_connect_list_host_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
+      break;
+    case A_surfxml_trace_c_connect_kind_POWER:
+      xbt_dict_set(trace_connect_list_power, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
+      break;
+    case A_surfxml_trace_c_connect_kind_LINK_AVAIL:
+      xbt_dict_set(trace_connect_list_link_avail, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
+      break;
+    case A_surfxml_trace_c_connect_kind_BANDWIDTH:
+      xbt_dict_set(trace_connect_list_bandwidth, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
+      break;
+    case A_surfxml_trace_c_connect_kind_LATENCY:
+      xbt_dict_set(trace_connect_list_latency, A_surfxml_trace_c_connect_trace, xbt_strdup(A_surfxml_trace_c_connect_element), free);
+      break;
+    default:
+      xbt_die("kind of trace unknown");
+   }   
 }
 
 /* Random tag functions */
@@ -1086,7 +1175,7 @@ double get_cpu_power(const char *power)
 int random_min, random_max, random_mean, random_std_deviation, random_generator;
 char *random_id;
 
-void init_randomness(void)
+static void init_randomness(void)
 {
   random_id = A_surfxml_random_id;
   surf_parse_get_int(&random_min, A_surfxml_random_min);
@@ -1096,10 +1185,10 @@ void init_randomness(void)
   random_generator = A_surfxml_random_generator;
 }
 
-void add_randomness(void)
+static void add_randomness(void)
 {
    /* If needed aditional properties can be added by using the prop tag */
-   random_data_t random = random_new(random_generator, random_min, random_max, random_mean, random_std_deviation);
+  random_data_t random = random_new(random_generator, 0, random_min, random_max, random_mean, random_std_deviation);
    xbt_dict_set(random_data_list, random_id, (void *)random, NULL);
 }