Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Wed, 17 Feb 2016 10:43:02 +0000 (11:43 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Wed, 17 Feb 2016 10:43:02 +0000 (11:43 +0100)
include/xbt/config.h
src/simgrid/sg_config.c
src/surf/storage_interface.cpp
src/surf/surfxml_parse.cpp
src/xbt/config.c

index 58b3a41..46884ae 100644 (file)
@@ -90,8 +90,7 @@ SG_BEGIN_DECL()
 typedef void* xbt_cfg_t;
 
 XBT_PUBLIC(void) xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...);
-XBT_PUBLIC(void) xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name,
-                                   va_list pa);
+XBT_PUBLIC(void) xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa);
 XBT_PUBLIC(void) xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options);
 
 
@@ -99,10 +98,8 @@ XBT_PUBLIC(void) xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options);
   Set the value of the cell \a name in \a cfg with the provided value.
  */
 XBT_PUBLIC(void) xbt_cfg_set_int(xbt_cfg_t cfg, const char *name, int val);
-XBT_PUBLIC(void) xbt_cfg_set_double(xbt_cfg_t cfg, const char *name,
-                                    double val);
-XBT_PUBLIC(void) xbt_cfg_set_string(xbt_cfg_t cfg, const char *name,
-                                    const char *val);
+XBT_PUBLIC(void) xbt_cfg_set_double(xbt_cfg_t cfg, const char *name, double val);
+XBT_PUBLIC(void) xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val);
 XBT_PUBLIC(void) xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val);
 XBT_PUBLIC(void*) xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *name, const char *val);
 
@@ -110,34 +107,24 @@ XBT_PUBLIC(void*) xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *name, const c
   Set the default value of the cell \a name in \a cfg with the provided value.
   If it was already set to something (possibly from the command line), do nothing.
  */
-XBT_PUBLIC(void) xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name,
-                                        int val);
-XBT_PUBLIC(void) xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name,
-                                           double val);
-XBT_PUBLIC(void) xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
-                                           const char *val);
-XBT_PUBLIC(void) xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name,
-                                        const char *val);
+XBT_PUBLIC(void) xbt_cfg_setdefault_int(xbt_cfg_t cfg, const char *name, int val);
+XBT_PUBLIC(void) xbt_cfg_setdefault_double(xbt_cfg_t cfg, const char *name, double val);
+XBT_PUBLIC(void) xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name, const char *val);
+XBT_PUBLIC(void) xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val);
 
-/*
- Remove the provided value from the cell @name in @cfg.
- */
+/** @brief Remove the provided value from the cell #name in #cfg. */
 XBT_PUBLIC(void) xbt_cfg_rm_int(xbt_cfg_t cfg, const char *name, int val);
-XBT_PUBLIC(void) xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name,
-                                   double val);
-XBT_PUBLIC(void) xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name,
-                                   const char *val);
+XBT_PUBLIC(void) xbt_cfg_rm_double(xbt_cfg_t cfg, const char *name, double val);
+XBT_PUBLIC(void) xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val);
 XBT_PUBLIC(void) xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val);
 
-/*
- Remove the value at position \e pos from the config \e cfg
- */
+/** @brief Remove the value at position \e pos from the config \e cfg */
 XBT_PUBLIC(void) xbt_cfg_rm_at(xbt_cfg_t cfg, const char *name, int pos);
 
-/* rm every values */
+/** @brief rm every values */
 XBT_PUBLIC(void) xbt_cfg_empty(xbt_cfg_t cfg, const char *name);
 
-/* Return if configuration is set by default*/
+/** @brief Return if configuration is set by default*/
 XBT_PUBLIC(int) xbt_cfg_is_default_value(xbt_cfg_t cfg, const char *name);
 
 /* @} */
@@ -157,7 +144,7 @@ typedef enum {
   xbt_cfgelm_string,
                        /**< char* */
   xbt_cfgelm_boolean,  /**< int */
-
+  xbt_cfgelm_alias,    /**< redirection from a deprecated name to a better one */
   //! @cond
   xbt_cfgelm_any,               /* not shown to users to prevent errors */
   xbt_cfgelm_type_count
@@ -200,8 +187,10 @@ XBT_PUBLIC(void) xbt_cfg_register(xbt_cfg_t * cfg,
                                   e_xbt_cfgelm_type_t type,
                                   int min, int max,
                                   xbt_cfg_cb_t cb_set, xbt_cfg_cb_t cb_rm);
+XBT_PUBLIC(void) xbt_cfg_register_alias(xbt_cfg_t * cfg, const char *newname, const char *oldname);
 XBT_PUBLIC(void) xbt_cfg_unregister(xbt_cfg_t cfg, const char *name);
 XBT_PUBLIC(void) xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry);
+XBT_PUBLIC(void) xbt_cfg_aliases(xbt_cfg_t cfg);
 XBT_PUBLIC(void) xbt_cfg_help(xbt_cfg_t cfg);
 XBT_PUBLIC(void) xbt_cfg_check(xbt_cfg_t cfg);
 XBT_PUBLIC(e_xbt_cfgelm_type_t) xbt_cfg_get_type(xbt_cfg_t cfg,
index 8adf5ce..bd3cad5 100644 (file)
@@ -362,7 +362,7 @@ static void _sg_cfg_cb__test_sleep(const char *name, int pos){
 /* callback of the inclusion path */
 static void _sg_cfg_cb__surf_path(const char *name, int pos)
 {
-  char *path = xbt_cfg_get_string_at(_sg_cfg_set, name, pos);
+  char *path = xbt_strdup(xbt_cfg_get_string_at(_sg_cfg_set, name, pos));
   xbt_dynar_push(surf_path, &path);
 }
 
@@ -942,7 +942,7 @@ void sg_config_init(int *argc, char **argv)
       xbt_assert((initial_path),
                   "__surf_get_initial_path() failed! Can't resolve current Windows directory");
 
-      surf_path = xbt_dynar_new(sizeof(char *), NULL);
+      surf_path = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
       xbt_cfg_setdefault_string(_sg_cfg_set, "path", initial_path);
     }
 
index 1761cf6..026ed9a 100644 (file)
@@ -103,12 +103,9 @@ xbt_dict_t Storage::parseContent(const char *filename)
     return NULL;
 
   xbt_dict_t parse_content = xbt_dict_new_homogeneous(xbt_free_f);
-  FILE *file = NULL;
 
-  file = surf_fopen(filename, "r");
-  if (file == NULL)
-    xbt_die("Cannot open file '%s' (path=%s)", filename,
-            xbt_str_join(surf_path, ":"));
+  FILE *file =  surf_fopen(filename, "r");
+  xbt_assert(file, "Cannot open file '%s' (path=%s)", filename, xbt_str_join(surf_path, ":"));
 
   char *line = NULL;
   size_t len = 0;
@@ -118,14 +115,12 @@ xbt_dict_t Storage::parseContent(const char *filename)
 
   while ((read = xbt_getline(&line, &len, file)) != -1) {
     if (read){
-      if(sscanf(line,"%s %llu", path, &size) == 2) {
-        m_usedSize += size;
-        sg_size_t *psize = xbt_new(sg_size_t, 1);
-        *psize = size;
-        xbt_dict_set(parse_content,path,psize,NULL);
-      } else {
-        xbt_die("Be sure of passing a good format for content file.\n");
-      }
+      xbt_assert(sscanf(line,"%s %llu", path, &size) == 2, "Parse error in %s: %s",filename,line);
+
+      m_usedSize += size;
+      sg_size_t *psize = xbt_new(sg_size_t, 1);
+      *psize = size;
+      xbt_dict_set(parse_content,path,psize,NULL);
     }
   }
   free(line);
index 7bb17c0..a0c9a43 100644 (file)
@@ -314,8 +314,8 @@ void STag_surfxml_include(void)
   xbt_dynar_push(surf_file_to_parse_stack, &surf_file_to_parse); //save old file descriptor
 
   surf_file_to_parse = surf_fopen(A_surfxml_include_file, "r"); // read new file descriptor
-  xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n",
-              A_surfxml_include_file);
+  xbt_assert((surf_file_to_parse), "Unable to open \"%s\"\n", A_surfxml_include_file);
+
   xbt_dynar_push(surf_input_buffer_stack,&surf_input_buffer);
   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
   surf_parse_push_buffer_state(surf_input_buffer);
index b78bdfb..b7650f5 100644 (file)
@@ -127,8 +127,8 @@ void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
 
   if (name)
     printf("%s>> Dumping of the config set '%s':\n", indent, name);
-  xbt_dict_foreach(dict, cursor, key, variable) {
 
+  xbt_dict_foreach(dict, cursor, key, variable) {
     printf("%s  %s:", indent, key);
 
     size = xbt_dynar_length(variable->content);
@@ -168,8 +168,13 @@ void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
       }
       break;
 
+    case xbt_cfgelm_alias:
+      /* no content */
+      break;
+
     default:
       printf("%s    Invalid type!!\n", indent);
+      break;
     }
 
   }
@@ -179,7 +184,6 @@ void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
   fflush(stdout);
 
   xbt_dict_cursor_free(&cursor);
-  return;
 }
 
 /*
@@ -194,7 +198,8 @@ void xbt_cfgelm_free(void *data)
   if (!c)
     return;
   xbt_free(c->desc);
-  xbt_dynar_free(&(c->content));
+  if (c->type != xbt_cfgelm_alias)
+    xbt_dynar_free(&(c->content));
   free(c);
 }
 
@@ -225,16 +230,11 @@ void xbt_cfg_register(xbt_cfg_t * cfg,
               "type of %s not valid (%d should be between %d and %d)",
              name, (int)type, xbt_cfgelm_int, xbt_cfgelm_boolean);
   res = xbt_dict_get_or_null((xbt_dict_t) * cfg, name);
-
-  if (res) {
-    XBT_WARN("Config elem %s registered twice.", name);
-    /* Will be removed by the insertion of the new one */
-  }
+  xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", name);
 
   res = xbt_new(s_xbt_cfgelm_t, 1);
   XBT_DEBUG("Register cfg elm %s (%s) (%d to %d %s (=%d) @%p in set %p)",
-            name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res,
-         *cfg);
+            name, desc, min, max, xbt_cfgelm_type_name[type], (int)type, res, *cfg);
 
   res->desc = xbt_strdup(desc);
   res->type = type;
@@ -263,11 +263,32 @@ void xbt_cfg_register(xbt_cfg_t * cfg,
 
   default:
     XBT_ERROR("%d is an invalid type code", (int)type);
+    break;
   }
 
   xbt_dict_set((xbt_dict_t) * cfg, name, res, NULL);
 }
 
+void xbt_cfg_register_alias(xbt_cfg_t * cfg, const char *newname, const char *oldname)
+{
+  if (*cfg == NULL)
+    *cfg = xbt_cfg_new();
+
+  xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) * cfg, oldname);
+  xbt_assert(NULL == res, "Refusing to register the config element '%s' twice.", oldname);
+
+  res = xbt_new0(s_xbt_cfgelm_t, 1);
+  XBT_DEBUG("Register cfg alias %s -> %s in set %p)",oldname,newname, *cfg);
+
+  res->desc = bprintf("Deprecated alias for %s",newname);
+  res->type = xbt_cfgelm_alias;
+  res->min = 1;
+  res->max = 1;
+  res->isdefault = 1;
+  res->content = (xbt_dynar_t)newname;
+
+  xbt_dict_set((xbt_dict_t) * cfg, oldname, res, NULL);
+}
 /** @brief Unregister an element from a config set.
  *
  *  @param cfg the config set
@@ -293,7 +314,7 @@ void xbt_cfg_unregister(xbt_cfg_t cfg, const char *name)
  * Each of them must use the following syntax: \<name\>:\<min nb\>_to_\<max nb\>_\<type\>
  * with type being one of  'string','int' or 'double'.
  *
- * FIXME: this does not allow to set the description
+ * Note that this does not allow to set the description, so you should prefer the other interface
  */
 
 void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
@@ -340,6 +361,26 @@ void xbt_cfg_register_str(xbt_cfg_t * cfg, const char *entry)
   free(entrycpy);               /* strdup'ed by dict mechanism, but cannot be const */
 }
 
+/** @brief Displays the declared aliases and their description */
+void xbt_cfg_aliases(xbt_cfg_t cfg)
+{
+  xbt_dict_cursor_t dict_cursor;
+  unsigned int dynar_cursor;
+  xbt_cfgelm_t variable;
+  char *name;
+  xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
+
+  xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
+    xbt_dynar_push(names, &name);
+  xbt_dynar_sort_strings(names);
+
+  xbt_dynar_foreach(names, dynar_cursor, name) {
+    variable = xbt_dict_get((xbt_dict_t )cfg, name);
+
+    if (variable->type == xbt_cfgelm_alias)
+      printf("   %s: %s\n", name, variable->desc);
+  }
+}
 /** @brief Displays the declared options and their description */
 void xbt_cfg_help(xbt_cfg_t cfg)
 {
@@ -349,15 +390,16 @@ void xbt_cfg_help(xbt_cfg_t cfg)
   char *name;
   xbt_dynar_t names = xbt_dynar_new(sizeof(char *), NULL);
 
-  xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable) {
+  xbt_dict_foreach((xbt_dict_t )cfg, dict_cursor, name, variable)
     xbt_dynar_push(names, &name);
-  }
   xbt_dynar_sort_strings(names);
 
   xbt_dynar_foreach(names, dynar_cursor, name) {
     int i;
     int size;
     variable = xbt_dict_get((xbt_dict_t )cfg, name);
+    if (variable->type == xbt_cfgelm_alias)
+      continue;
 
     printf("   %s: %s\n", name, variable->desc);
     printf("       Type: %s; ", xbt_cfgelm_type_name[variable->type]);
@@ -421,6 +463,9 @@ void xbt_cfg_check(xbt_cfg_t cfg)
   XBT_DEBUG("Check cfg set %p", cfg);
 
   xbt_dict_foreach((xbt_dict_t) cfg, cursor, name, variable) {
+    if (variable->type == xbt_cfgelm_alias)
+      continue;
+
     size = xbt_dynar_length(variable->content);
     if (variable->min > size) {
       xbt_dict_cursor_free(&cursor);
@@ -449,18 +494,20 @@ void xbt_cfg_check(xbt_cfg_t cfg)
   xbt_dict_cursor_free(&cursor);
 }
 
-static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg,
-                                   const char *name,
-                                   e_xbt_cfgelm_type_t type)
+static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name, e_xbt_cfgelm_type_t type)
 {
-  xbt_cfgelm_t res = NULL;
+  xbt_cfgelm_t res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
+
+  // The user used the old name. Switch to the new one after a short warning
+  if (res && res->type == xbt_cfgelm_alias) {
+    const char* newname = (const char *)res->content;
+    XBT_VERB("Option %s has been renamed to %s. Consider switching.", name, newname);
+    res = xbt_cfgelm_get(cfg, newname, type);
+  }
 
-  res = xbt_dict_get_or_null((xbt_dict_t) cfg, name);
   if (!res) {
     xbt_cfg_help(cfg);
-    THROWF(not_found_error, 0,
-           "No registered variable '%s' in this config set. It is possible that this "\
-           "configuration option has been renamed; please read the file ChangeLog carefully!", name);
+    THROWF(not_found_error, 0, "No registered variable '%s' in this config set.", name);
   }
 
   xbt_assert(type == xbt_cfgelm_any || res->type == type,
@@ -643,9 +690,6 @@ void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
     }
     *(val++) = '\0';
 
-    if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
-      XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
-
     TRY {
       xbt_cfg_set_as_string(cfg,name,val);
     } CATCH_ANONYMOUS {
@@ -673,14 +717,24 @@ void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
   int i;
   double d;
 
+  if (strncmp(key, "contexts/", strlen("contexts/")) && strncmp(key, "path", strlen("path")))
+    XBT_INFO("Configuration change: Set '%s' to '%s'", key, value);
+
   TRY {
-    variable = xbt_dict_get((xbt_dict_t) cfg, key);
+    while (variable == NULL) {
+      variable = xbt_dict_get((xbt_dict_t) cfg, key);
+      if (variable->type == xbt_cfgelm_alias) {
+        const char *newname = (const char*)variable->content;
+        XBT_INFO("Note: configuration '%s' is deprecated. Please use '%s' instead.", key, newname);
+        key = newname;
+        variable = NULL;
+      }
+    }
   }
   CATCH(e) {
     if (e.category == not_found_error) {
       xbt_ex_free(e);
-      THROWF(not_found_error, 0,
-          "No registered variable corresponding to '%s'.", key);
+      THROWF(not_found_error, 0, "No registered variable corresponding to '%s'.", key);
     }
     RETHROW;
   }