Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Create current_property_set only if it is required.
[simgrid.git] / src / bindings / lua / lua_console.c
index 74e0ea3..e4a523a 100644 (file)
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_console, bindings, "Lua Bindings");
 
-static p_AS_attr AS;
-//using xbt_dynar_t :
-static xbt_dynar_t host_list_d;
-static xbt_dynar_t link_list_d;
-static xbt_dynar_t route_list_d;
+
+//AS List
+static xbt_dynar_t as_list_d;
 
 /*
  * Initialize platform model routing
@@ -32,16 +30,19 @@ static void create_AS(const char *id, const char *mode)
  */
 
 static void create_host(const char *id, double power_peak, double power_sc,
-                        const char *power_tr, int state_init,
+                        const char *power_tr,int core,int state_init,
                         const char *state_tr)
 {
 
   double power_scale = 1.0;
+  int core_nb = 1; //default value
   tmgr_trace_t power_trace = NULL;
   e_surf_resource_state_t state_initial;
   tmgr_trace_t state_trace;
   if (power_sc)                 // !=0
     power_scale = power_sc;
+  if (core)
+         core_nb = core; //default value
   if (state_init == -1)
     state_initial = SURF_RESOURCE_OFF;
   else
@@ -54,11 +55,13 @@ static void create_host(const char *id, double power_peak, double power_sc,
     state_trace = tmgr_trace_new(state_tr);
   else
     state_trace = tmgr_trace_new("");
-  current_property_set = xbt_dict_new();
+
   surf_host_create_resource(xbt_strdup(id), power_peak, power_scale,
-                            power_trace, state_initial, state_trace,
+                            power_trace, core_nb, state_initial, state_trace,
                             current_property_set);
 
+  current_property_set = NULL;
+
 }
 
 /**
@@ -126,11 +129,11 @@ static void create_host_wsL07(const char *id, double power_peak,
     state_trace = tmgr_trace_new(state_tr);
   else
     state_trace = tmgr_trace_new("");
-  current_property_set = xbt_dict_new();
+
   surf_wsL07_host_create_resource(xbt_strdup(id), power_peak, power_scale,
                                   power_trace, state_initial, state_trace,
                                   current_property_set);
-
+  current_property_set = NULL;
 }
 
 /**
@@ -181,6 +184,11 @@ static void create_link_wsL07(const char *name,
 
 static int AS_new(lua_State * L)
 {
+
+   if (xbt_dynar_is_empty(as_list_d))
+           as_list_d = xbt_dynar_new(sizeof(p_AS_attr), &xbt_free_ref);
+
+  p_AS_attr AS;
   const char *id;
   const char *mode;
   if (lua_istable(L, 1)) {
@@ -194,13 +202,17 @@ static int AS_new(lua_State * L)
     mode = lua_tostring(L, -1);
     lua_pop(L, 1);
   } else {
-    ERROR0
+    XBT_ERROR
         ("Bad Arguments to AS.new, Should be a table with named arguments");
     return -1;
   }
   AS = malloc(sizeof(AS_attr));
   AS->id = id;
   AS->mode = mode;
+  AS->host_list_d = xbt_dynar_new(sizeof(p_host_attr),&xbt_free_ref);
+  AS->link_list_d = xbt_dynar_new(sizeof(p_link_attr),&xbt_free_ref);
+  AS->route_list_d = xbt_dynar_new(sizeof(p_route_attr),&xbt_free_ref);
+  xbt_dynar_push(as_list_d, &AS);
 
   return 0;
 }
@@ -210,18 +222,22 @@ static int AS_new(lua_State * L)
  */
 static int Host_new(lua_State * L)
 {
-
-  if (xbt_dynar_is_empty(host_list_d))
-    host_list_d = xbt_dynar_new(sizeof(p_host_attr), &xbt_free_ref);
-
   p_host_attr host;
+  unsigned int i;
+  p_AS_attr p_as,current_as = NULL;
+  const char *AS_id;
   const char *id;
   const char *power_trace;
   const char *state_trace;
   double power, power_scale;
-  int state_initial;
+  int state_initial,core;
   //get values from the table passed as argument
   if (lua_istable(L, -1)) {
+       // get AS id
+       lua_pushstring(L, "AS");
+       lua_gettable(L, -2);
+       AS_id = lua_tostring(L, -1);
+       lua_pop(L,1);
 
     // get Id Value
     lua_pushstring(L, "id");
@@ -247,6 +263,11 @@ static int Host_new(lua_State * L)
     power_trace = lua_tostring(L, -1);
     lua_pop(L, 1);
 
+    lua_pushstring(L, "core");
+    lua_gettable(L, -2);
+    core = lua_tonumber(L, -1);
+    lua_pop(L, 1);
+
     //get state initial
     lua_pushstring(L, "state_initial");
     lua_gettable(L, -2);
@@ -260,20 +281,33 @@ static int Host_new(lua_State * L)
     lua_pop(L, 1);
 
   } else {
-    ERROR0
+    XBT_ERROR
         ("Bad Arguments to create host, Should be a table with named arguments");
     return -1;
   }
+  xbt_dynar_foreach(as_list_d, i, p_as){
+         if (p_as->id == AS_id){
+                 current_as = p_as;
+                 break;
+         }
+  }
+
+  if (!current_as)
+  {
+         XBT_ERROR("No AS_id :%s found",AS_id);
+         return -2;
+  }
 
   host = malloc(sizeof(host_attr));
   host->id = id;
   host->power_peak = power;
   host->power_scale = power_scale;
   host->power_trace = power_trace;
+  host->core = core;
   host->state_initial = state_initial;
   host->state_trace = state_trace;
   host->function = NULL;
-  xbt_dynar_push(host_list_d, &host);
+  xbt_dynar_push(current_as->host_list_d, &host);
 
   return 0;
 }
@@ -283,11 +317,9 @@ static int Host_new(lua_State * L)
  */
 static int Link_new(lua_State * L)      // (id,bandwidth,latency)
 {
-
-  if (xbt_dynar_is_empty(link_list_d))
-    link_list_d = xbt_dynar_new(sizeof(p_link_attr), &xbt_free_ref);
-
-
+  const char *AS_id;
+  unsigned int i;
+  p_AS_attr p_as,current_as = NULL;
   const char* id;
   double bandwidth, latency;
   const char *bandwidth_trace;
@@ -297,6 +329,13 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
 
   //get values from the table passed as argument
   if (lua_istable(L, -1)) {
+
+       //get AS id
+       lua_pushstring(L, "AS");
+       lua_gettable(L, -2);
+       AS_id = lua_tostring(L, -1);
+       lua_pop(L, 1);
+
     // get Id Value
     lua_pushstring(L, "id");
     lua_gettable(L, -2);
@@ -348,11 +387,22 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
     lua_pop(L, 1);
 
   } else {
-    ERROR0
+    XBT_ERROR
         ("Bad Arguments to create link, Should be a table with named arguments");
     return -1;
   }
+  xbt_dynar_foreach(as_list_d, i, p_as){
+         if (p_as->id == AS_id){
+                 current_as = p_as;
+                 break;
+         }
+  }
 
+  if (!current_as)
+  {
+         XBT_ERROR("No AS_id :%s found",AS_id);
+         return -2;
+  }
   p_link_attr link = malloc(sizeof(link_attr));
   link->id = id;
   link->bandwidth = bandwidth;
@@ -362,14 +412,8 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
   link->state_trace = state_trace;
   link->state_initial = state_initial;
   link->policy = policy;
-  xbt_dynar_push(link_list_d, &link);
+  xbt_dynar_push(current_as->link_list_d, &link);
 
-
-  //TEST1
-  unsigned int i;
-  p_link_attr p_link;
-  xbt_dynar_foreach(link_list_d, i, p_link) {
-  }
   return 0;
 }
 
@@ -378,16 +422,35 @@ static int Link_new(lua_State * L)      // (id,bandwidth,latency)
  */
 static int Route_new(lua_State * L)     // (src_id,dest_id,links_number,link_table)
 {
- if (xbt_dynar_is_empty(route_list_d))
-           route_list_d = xbt_dynar_new(sizeof(p_route_attr), &xbt_free_ref);
+  const char* AS_id;
+  unsigned int i;
+  p_AS_attr p_as,current_as = NULL;
+  const char *links;
+  const char* link_id;
+  p_route_attr route = malloc(sizeof(route_attr));
 
-        const char *links;
-        const char* link_id;
-        p_route_attr route = malloc(sizeof(route_attr));
 
+  if (!lua_istable(L, 4)) { // if Route.new is declared as an indexed table (FIXME : we check the third arg if it's not a table)
 
-  if (!lua_istable(L, 3)) { // if Route.new is declared as an indexed table (FIXME : we check the third arg if it's not a table)
-     // get Source Value
+        //get AS_id
+        lua_pushstring(L, "AS");
+        lua_gettable(L, -2);
+        AS_id = lua_tostring(L, -1);
+        lua_pop(L, 1);
+
+        xbt_dynar_foreach(as_list_d, i, p_as){
+         if (p_as->id == AS_id){
+                 current_as = p_as;
+                 break;
+         }
+        }
+
+        if (!current_as)
+        {
+         XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
+         return -2;
+        }
+        // get Source Value
      lua_pushstring(L, "src");
      lua_gettable(L, -2);
      route->src_id = lua_tostring(L, -1);
@@ -407,36 +470,46 @@ static int Route_new(lua_State * L)     // (src_id,dest_id,links_number,link_tab
 
      route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
 
-     char *tmp_links = malloc(sizeof(char)*strlen(links)+1);//use xbt
-     strcpy(tmp_links,links);
+     char *tmp_links = xbt_strdup(links);
      link_id = strtok(tmp_links,",");   //tmp_link = strtok((char*)links,",");
      while(link_id != NULL)
        {
           xbt_dynar_push(route->links_id, &link_id);
-          link_id = strtok(NULL,","); //Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
+          link_id = strtok(NULL,","); //Alternatively, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
         }
-     xbt_dynar_push(route_list_d, &route);
+     xbt_dynar_push(current_as->route_list_d, &route);
      return 0;
-
       }
   else { // Route.new is declared as a function
-     //const char* link_id;
-     route->src_id = luaL_checkstring(L, 1);
-     route->dest_id = luaL_checkstring(L, 2);
+        AS_id = luaL_checkstring(L, 1);
+        xbt_dynar_foreach(as_list_d, i, p_as){
+                 if (p_as->id == AS_id){
+                         current_as = p_as;
+                         break;
+                 }
+                }
+
+        if (!current_as)
+        {
+               XBT_ERROR("addRoute: No AS_id :%s found",AS_id);
+               return -2;
+        }
+     route->src_id = luaL_checkstring(L, 2);
+     route->dest_id = luaL_checkstring(L, 3);
      route->links_id = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
      lua_pushnil(L);
-     while (lua_next(L, 3) != 0)
+     while (lua_next(L, 4) != 0)
                {
         link_id = lua_tostring(L, -1);
         xbt_dynar_push(route->links_id, &link_id);
-        DEBUG2("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
+         XBT_DEBUG("index = %f , Link_id = %s \n", lua_tonumber(L, -2),
         lua_tostring(L, -1));
         lua_pop(L, 1);
        }
-        lua_pop(L, 1);
-         //add route to platform's route list
-         xbt_dynar_push(route_list_d, &route);
-         return 0;
+    lua_pop(L, 1);
+    //add route to platform's route list
+    xbt_dynar_push(current_as->route_list_d, &route);
+    return 0;
     }
 
   return -1;
@@ -446,12 +519,13 @@ static int Route_new(lua_State * L)     // (src_id,dest_id,links_number,link_tab
  */
 static int Host_set_function(lua_State * L)     //(host,function,nb_args,list_args)
 {
+       p_AS_attr p_as;
        p_host_attr p_host;
        const char *host;
        const char *function;
        const char *args;
        char * tmp_arg;
-       unsigned int i;
+       unsigned int i,j;
 
    if (lua_istable(L, -1)) {
         // get Host id
@@ -471,25 +545,28 @@ static int Host_set_function(lua_State * L)     //(host,function,nb_args,list_ar
      lua_pop(L, 1);
    }
    else {
-          ERROR0("Bad Arguments to create link, Should be a table with named arguments");
+          XBT_ERROR("Bad Arguments to create link, Should be a table with named arguments");
           return -1;
    }
 
-  // look for the index of host in host_list
-  xbt_dynar_foreach(host_list_d, i, p_host) {
-         if (p_host->id == host) {
-      p_host->function = function;
-      p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
-      // split & fill the args list
-      tmp_arg = strtok((char*)args,",");
-      while (tmp_arg != NULL) {
-          xbt_dynar_push(p_host->args_list, &tmp_arg);
-          tmp_arg = strtok(NULL,",");
-        }
-      return 0;
-          }
-      }
-         ERROR1("Host : %s Not Found !!", host);
+  // look for the index of host in host_list for each AS
+   xbt_dynar_foreach(as_list_d, i, p_as)
+   {
+          xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
+                  if (p_host->id == host) {
+                          p_host->function = function;
+                          p_host->args_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
+                          // split & fill the args list
+                          tmp_arg = strtok((char*)args,",");
+                          while (tmp_arg != NULL) {
+                                  xbt_dynar_push(p_host->args_list, &tmp_arg);
+                                  tmp_arg = strtok(NULL,",");
+                          }
+                          return 0;
+                  }
+       }
+   }
+         XBT_ERROR("Host : %s Not Found !!", host);
          return 1;
 
 }
@@ -500,41 +577,42 @@ static int Host_set_function(lua_State * L)     //(host,function,nb_args,list_ar
 
 static int surf_parse_bypass_platform()
 {
-  unsigned int i;
+  unsigned int i,j;
+  p_AS_attr p_as;
   p_host_attr p_host;
   p_link_attr p_link;
   p_route_attr p_route;
 
-  // Init routing mode
-  create_AS(AS->id, AS->mode);
-
-  // Add Hosts
-  xbt_dynar_foreach(host_list_d, i, p_host) {
-    create_host(p_host->id, p_host->power_peak, p_host->power_scale,
-                p_host->power_trace, p_host->state_initial,
-                p_host->state_trace);
-    //add to routing model host list
-    surf_route_add_host((char *) p_host->id);
 
+  // Add AS
+  xbt_dynar_foreach(as_list_d, i,p_as)
+  {
+         create_AS(p_as->id, p_as->mode);
+         // add associated Hosts
+         xbt_dynar_foreach(p_as->host_list_d, j, p_host){
+                 create_host(p_host->id, p_host->power_peak, p_host->power_scale,
+                                 p_host->power_trace, p_host->core, p_host->state_initial,
+                                 p_host->state_trace);
+                     //add to routing model host list
+                     surf_route_add_host((char *) p_host->id);
+         }
+         // add associated Links
+         xbt_dynar_foreach(p_as->link_list_d, j, p_link){
+                 create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
+                                 p_link->latency, p_link->latency_trace,
+                                 p_link->state_initial, p_link->state_trace,
+                                 p_link->policy);
+         }
+         // add associated Routes
+         xbt_dynar_foreach(p_as->route_list_d, j, p_route){
+                 surf_routing_add_route((char *) p_route->src_id,
+                                            (char *) p_route->dest_id, p_route->links_id);
+         }
+
+         // Finalize AS
+         surf_AS_finalize(p_as->id);
   }
 
-  //add Links
-  xbt_dynar_foreach(link_list_d, i, p_link) {
-    create_link(p_link->id, p_link->bandwidth, p_link->bandwidth_trace,
-                p_link->latency, p_link->latency_trace,
-                p_link->state_initial, p_link->state_trace,
-                p_link->policy);
-  }
-  // add route
-  xbt_dynar_foreach(route_list_d, i, p_route) {
-    surf_routing_add_route((char *) p_route->src_id,
-                           (char *) p_route->dest_id, p_route->links_id);
-  }
-  /* </platform> */
-
-  // Finalize AS
-  surf_AS_finalize(AS->id);
-
   // add traces
   surf_add_host_traces();
   surf_add_link_traces();
@@ -552,42 +630,42 @@ static int surf_parse_bypass_platform()
 static int surf_wsL07_parse_bypass_platform()
 {
 
-  unsigned int i;
+  unsigned int i,j;
+  p_AS_attr p_as;
   p_host_attr p_host;
   p_link_attr p_link;
   p_route_attr p_route;
 
-  // Init routing mode
-  create_AS(AS->id, AS->mode);
-
-  // Add Hosts
-  xbt_dynar_foreach(host_list_d, i, p_host) {
-    create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
-                      p_host->power_trace, p_host->state_initial,
-                      p_host->state_trace);
-    //add to routing model host list
-    surf_route_add_host((char *) p_host->id);
+  xbt_dynar_foreach(as_list_d, i, p_as)
+  {
+         // Init AS
+         create_AS(p_as->id, p_as->mode);
+         // Add Hosts
+         xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
+           create_host_wsL07(p_host->id, p_host->power_peak, p_host->power_scale,
+                             p_host->power_trace, p_host->state_initial,
+                             p_host->state_trace);
+           //add to routing model host list
+           surf_route_add_host((char *) p_host->id);
+         }
+         //add Links
+         xbt_dynar_foreach(p_as->link_list_d, j, p_link) {
+             create_link_wsL07(p_link->id, p_link->bandwidth,
+                               p_link->bandwidth_trace, p_link->latency,
+                               p_link->latency_trace, p_link->state_initial,
+                               p_link->state_trace, p_link->policy);
+           }
+           // add route
+         xbt_dynar_foreach(p_as->route_list_d, j, p_route) {
+               surf_routing_add_route((char *) p_route->src_id,
+                                      (char *) p_route->dest_id, p_route->links_id);
+             }
+         /* </AS> */
+         // Finalize AS
+         surf_AS_finalize(p_as->id);
   }
-
-  //add Links
-  xbt_dynar_foreach(link_list_d, i, p_link) {
-    create_link_wsL07(p_link->id, p_link->bandwidth,
-                      p_link->bandwidth_trace, p_link->latency,
-                      p_link->latency_trace, p_link->state_initial,
-                      p_link->state_trace, p_link->policy);
-  }
-  // add route
-  xbt_dynar_foreach(route_list_d, i, p_route) {
-    surf_routing_add_route((char *) p_route->src_id,
-                           (char *) p_route->dest_id, p_route->links_id);
-  }
-  /* </platform> */
-
-  // Finalize AS
-  surf_AS_finalize(AS->id);
   // add traces
   surf_wsL07_add_traces();
-
   return 0;
 }
 
@@ -596,11 +674,15 @@ static int surf_wsL07_parse_bypass_platform()
  */
 static int surf_parse_bypass_application()
 {
-  unsigned int i;
+  unsigned int i,j;
+  p_AS_attr p_as;
   p_host_attr p_host;
-  xbt_dynar_foreach(host_list_d, i, p_host) {
-    if (p_host->function)
-      MSG_set_function(p_host->id, p_host->function, p_host->args_list);
+  xbt_dynar_foreach(as_list_d, i, p_as)
+  {
+         xbt_dynar_foreach(p_as->host_list_d, j, p_host) {
+                 if (p_host->function)
+                         MSG_set_function(p_host->id, p_host->function, p_host->args_list);
+         }
   }
   return 0;
 }
@@ -608,7 +690,6 @@ static int surf_parse_bypass_application()
 /*
  * Public Methods
  */
-
 int console_add_host(lua_State *L)
 {
        return Host_new(L);