Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added support for using lua platform files from C
authorChristian Heinrich <christian.heinrich@livando.com>
Wed, 4 Feb 2015 17:27:49 +0000 (18:27 +0100)
committerChristian Heinrich <franz-christian.heinrich@inria.fr>
Thu, 15 Oct 2015 17:17:16 +0000 (19:17 +0200)
- It is now possible to use .lua files instead of .xml files.
  (Even if the simulation uses C code)

- Added/moved example file to examples/platforms/platform.lua
  This is equivalent to the small_platform.xml platform.

examples/lua/masterslave/platform.lua [deleted file]
examples/platforms/small_platform.lua [new file with mode: 0644]
src/bindings/lua/lua_platf.c
src/surf/surfxml_parseplatf.c

diff --git a/examples/lua/masterslave/platform.lua b/examples/lua/masterslave/platform.lua
deleted file mode 100644 (file)
index 9999efd..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
--- Copyright (c) 2011, 2014. The SimGrid Team.
--- All rights reserved.
-
--- This program is free software; you can redistribute it and/or modify it
--- under the terms of the license (GNU LGPL) which comes with this package.
-
-  --create new routing model
-  --simgrid.AS.new(AS_id,AS_mode)
-  simgrid.AS.new{id="AS0",mode="Full"}; 
-  --simgrid.host.new(host_id,power)
-  simgrid.AS.addHost{AS="AS0",id="Tremblay",power=98095000};
-  simgrid.AS.addHost{AS="AS0",id="Jupiter",power=76296000};
-  simgrid.AS.addHost{AS="AS0",id="Fafard",power=76296000};
-  simgrid.AS.addHost{AS="AS0",id="Ginette",power=48492000};
-  simgrid.AS.addHost{AS="AS0",id="Bourassa",power=48492000};
-
-    -- create Links
-  for i=10,0,-1 do
-    simgrid.AS.addLink{AS="AS0",id=i,bandwidth=252750+ i*768,latency=0.000270544+i*0.087};  
-  end
-  -- simgrid.route.new(src_id,des_id,links_nb,links_list)
-   simgrid.AS.addRoute{AS="AS0",src="Tremblay",dest="Jupiter",links="1"};
-   simgrid.AS.addRoute{AS="AS0",src="Tremblay",dest="Fafard",links="0,1,2,3,4,8"};
-   simgrid.AS.addRoute{AS="AS0",src="Tremblay",dest="Ginette",links="3,4,5"};
-   simgrid.AS.addRoute{AS="AS0",src="Tremblay",dest="Bourassa",links="0,1,3,2,4,6,7"};
-
-   simgrid.AS.addRoute{AS="AS0",src="Jupiter",dest="Tremblay",links="1"};
-   simgrid.AS.addRoute{AS="AS0",src="Jupiter",dest="Fafard",links="0,1,2,3,4,8,9"};
-   simgrid.AS.addRoute{AS="AS0",src="Jupiter",dest="Ginette",links="3,4,5,9"};
-   simgrid.AS.addRoute{AS="AS0",src="Jupiter",dest="Bourassa",links="0,1,2,3,4,6,7,9"};
-   simgrid.AS.addRoute{AS="AS0",src="Fafard",dest="Tremblay",links="0,1,2,3,4,8"};
-   simgrid.AS.addRoute{AS="AS0",src="Fafard",dest="Jupiter",links="0,1,2,3,4,8,9"};
-   simgrid.AS.addRoute{AS="AS0",src="Fafard",dest="Ginette",links="0,1,2,5,8"};
-   simgrid.AS.addRoute{AS="AS0",src="Fafard",dest="Bourassa",links="6,7,8"};
-  
-   simgrid.AS.addRoute{AS="AS0",src="Ginette",dest="Tremblay",links="3,4,5"};
-   simgrid.AS.addRoute{AS="AS0",src="Ginette",dest="Jupiter",links="3,4,5,9"};
-   simgrid.AS.addRoute{AS="AS0",src="Ginette",dest="Fafard",links="0,1,2,5,8"};
-   simgrid.AS.addRoute{AS="AS0",src="Ginette",dest="Bourassa",links="0,1,2,5,6,7"};
-
-   simgrid.AS.addRoute{AS="AS0",src="Bourassa",dest="Tremblay",links="0,1,3,2,4,6,7"};
-   simgrid.AS.addRoute{AS="AS0",src="Bourassa",dest="Jupiter",links="0,1,2,3,4,6,7,9"};
-   simgrid.AS.addRoute{AS="AS0",src="Bourassa",dest="Fafard",links="6,7,8"};
-   simgrid.AS.addRoute{AS="AS0",src="Bourassa",dest="Ginette",links="0,1,2,5,6,7"};
-  
-   --Save Platform
-   --simgrid.info("start registering platform");
-   simgrid.msg_register_platform();
-   --simgrid.info("platform registered");
diff --git a/examples/platforms/small_platform.lua b/examples/platforms/small_platform.lua
new file mode 100644 (file)
index 0000000..4144425
--- /dev/null
@@ -0,0 +1,66 @@
+-- Copyright (c) 2011, 2014. The SimGrid Team.
+-- All rights reserved.
+
+-- This program is free software; you can redistribute it and/or modify it
+-- under the terms of the license (GNU LGPL) which comes with this package.
+
+  require("simgrid")
+  simgrid.platf.open();
+  simgrid.platf.AS_open{id="AS0",mode="Full"};
+
+  simgrid.platf.host_new{AS="AS0",id="Tremblay",power=98095000};
+  simgrid.platf.host_new{AS="AS0",id="Jupiter",power=76296000};
+  simgrid.platf.host_new{AS="AS0",id="Fafard",power=76296000};
+  simgrid.platf.host_new{AS="AS0",id="Ginette",power=48492000};
+  simgrid.platf.host_new{AS="AS0",id="Bourassa",power=48492000};
+
+  -- create Links
+  simgrid.platf.link_new{AS="AS0",id=0,bandwidth=41279125,latency=0.000059904};
+  simgrid.platf.link_new{AS="AS0",id=1,bandwidth=34285625,latency=0.000514433};
+  simgrid.platf.link_new{AS="AS0",id=2,bandwidth=118682500,latency=0.000136931};
+  simgrid.platf.link_new{AS="AS0",id=3,bandwidth=34285625,latency=0.000514433};
+  simgrid.platf.link_new{AS="AS0",id=4,bandwidth=10099625,latency=0.00047978};
+  simgrid.platf.link_new{AS="AS0",id=5,bandwidth=27946250,latency=0.000278066};
+  simgrid.platf.link_new{AS="AS0",id=6,bandwidth=41279125,latency=0.000059904};
+  simgrid.platf.link_new{AS="AS0",id=7,bandwidth=11618875,latency=0.00018998};
+  simgrid.platf.link_new{AS="AS0",id=8,bandwidth=8158000,latency=0.000270544};
+  simgrid.platf.link_new{AS="AS0",id=9,bandwidth=7209750,latency=0.001461517};
+  simgrid.platf.link_new{AS="AS0",id="loopback",bandwidth=498000000,latency=0.000015,policy="FATPIPE"};
+
+  for i=1,5,1 do
+    simgrid.platf.route_new{AS="AS0",src=simgrid.host.name(simgrid.host.at(i)),dest=simgrid.host.name(simgrid.host.at(i)),links="loopback"};
+  end
+
+  simgrid.platf.route_new{AS="AS0",src="Tremblay",dest="Jupiter",links="9"};
+  simgrid.platf.route_new{AS="AS0",src="Tremblay",dest="Fafard",links="4,3,2,0,1,8"};
+  simgrid.platf.route_new{AS="AS0",src="Tremblay",dest="Ginette",links="4,3,5"};
+  simgrid.platf.route_new{AS="AS0",src="Tremblay",dest="Bourassa",links="4,3,2,0,1,6,7"};
+
+  --simgrid.platf.route_new{AS="AS0",src="Jupiter",dest="Tremblay",links="1"};
+  simgrid.platf.route_new{AS="AS0",src="Jupiter",dest="Fafard",links="9,4,3,2,0,1,8"};
+  simgrid.platf.route_new{AS="AS0",src="Jupiter",dest="Ginette",links="9,4,3,5"};
+  simgrid.platf.route_new{AS="AS0",src="Jupiter",dest="Bourassa",links="9,4,3,2,0,1,6,7"};
+
+  --simgrid.platf.route_new{AS="AS0",src="Fafard",dest="Tremblay",links="0,1,2,3,4,8"};
+  --simgrid.platf.route_new{AS="AS0",src="Fafard",dest="Jupiter",links="0,1,2,3,4,8,9"};
+  simgrid.platf.route_new{AS="AS0",src="Fafard",dest="Ginette",links="8,1,0,2,5"};
+  simgrid.platf.route_new{AS="AS0",src="Fafard",dest="Bourassa",links="8,6,7"};
+
+  --simgrid.platf.route_new{AS="AS0",src="Ginette",dest="Tremblay",links="3,4,5"};
+  --simgrid.platf.route_new{AS="AS0",src="Ginette",dest="Jupiter",links="3,4,5,9"};
+  --simgrid.platf.route_new{AS="AS0",src="Ginette",dest="Fafard",links="0,1,2,5,8"};
+  simgrid.platf.route_new{AS="AS0",src="Ginette",dest="Bourassa",links="5,2,0,1,6,7"};
+
+  --simgrid.platf.route_new{AS="AS0",src="Bourassa",dest="Tremblay",links="0,1,3,2,4,6,7"};
+  --simgrid.platf.route_new{AS="AS0",src="Bourassa",dest="Jupiter",links="0,1,2,3,4,6,7,9"};
+  --simgrid.platf.route_new{AS="AS0",src="Bourassa",dest="Fafard",links="6,7,8"};
+  --simgrid.platf.route_new{AS="AS0",src="Bourassa",dest="Ginette",links="0,1,2,5,6,7"};
+
+  simgrid.platf.AS_close();
+
+  simgrid.platf.close();
+
+  --Save Platform
+  --simgrid.info("start registering platform");
+  --simgrid.msg_register_platform();
+  --simgrid.info("platform registered");
index 13ae150..e21b30e 100644 (file)
@@ -46,7 +46,7 @@ int console_open(lua_State *L) {
   routing_register_callbacks();
 
   gpu_register_callbacks();
-  
+
   return 0;
 }
 
@@ -110,7 +110,7 @@ int console_add_host(lua_State *L) {
   //get power_scale
   lua_pushstring(L, "power_scale");
   lua_gettable(L, -2);
-  if(!lua_isnumber(L,-1)) host.power_scale = 1;// Default value  
+  if(!lua_isnumber(L,-1)) host.power_scale = 1;// Default value
   else host.power_scale = lua_tonumber(L, -1);
   lua_pop(L, 1);
 
@@ -248,14 +248,13 @@ int console_add_router(lua_State* L) {
 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
 
 int console_add_route(lua_State *L) {
+  XBT_DEBUG("Adding route");
   s_sg_platf_route_cbarg_t route;
   memset(&route,0,sizeof(route));
 
   /* allocating memory for the buffer, I think 2kB should be enough */
   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
 
-  int is_symmetrical;
-
   if (! lua_istable(L, -1)) {
     XBT_ERROR("Bad Arguments to create a route, Should be a table with named arguments");
     return -1;
@@ -278,24 +277,29 @@ int console_add_route(lua_State *L) {
     xbt_dynar_push_as(route.link_list,char*,xbt_strdup(lua_tostring(L, -1)));
   lua_pop(L,1);
 
+  /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
+   * Et ouais mon pote. That's the way it goes. F34R.
+   */
   lua_pushstring(L,"symmetrical");
   lua_gettable(L,-2);
-  is_symmetrical = lua_tointeger(L, -1);
+  if (lua_isstring(L, -1)) {
+    const char* value = lua_tostring(L, -1);
+    if (strcmp("YES", value) == 0) {
+      route.symmetrical = TRUE;
+    }
+    else
+      route.symmetrical = FALSE;
+  }
+  else {
+    route.symmetrical = TRUE;
+  }
   lua_pop(L,1);
 
   route.gw_src = NULL;
   route.gw_dst = NULL;
 
-  /* We are relying on the XML bypassing mechanism since the corresponding sg_platf does not exist yet.
-   * Et ouais mon pote. That's the way it goes. F34R.
-   */
-  if (is_symmetrical)
-    route.symmetrical = TRUE;
-  else
-    route.symmetrical = FALSE;
-
   sg_platf_new_route(&route);
-  
+
   return 0;
 }
 
@@ -303,6 +307,8 @@ int console_AS_open(lua_State *L) {
  const char *id;
  const char *mode;
 
+ XBT_DEBUG("Opening AS");
+
  if (! lua_istable(L, 1)) {
    XBT_ERROR("Bad Arguments to AS_open, Should be a table with named arguments");
    return -1;
@@ -336,6 +342,7 @@ int console_AS_open(lua_State *L) {
  return 0;
 }
 int console_AS_close(lua_State *L) {
+  XBT_DEBUG("Closing AS");
   sg_platf_new_AS_end();
   return 0;
 }
index 795615c..9333659 100644 (file)
 #include "simgrid/platf.h"
 #include "surf/surfxml_parse.h"
 #include "surf/surf_private.h"
+#include "bindings/lua/simgrid_lua.h"
+#include "bindings/lua/lua_state_cloner.h"
+
+#include <lua.h>                /* Always include this when calling Lua */
+#include <lauxlib.h>            /* Always include this when calling Lua */
+#include <lualib.h>             /* Prototype for luaL_openlibs(), */
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_parse);
 
@@ -28,7 +34,6 @@ static char *old_buff = NULL;
 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack_ptr;
 XBT_IMPORT_NO_EXPORT(unsigned int) surfxml_buffer_stack_stack[1024];
 
-
 void surfxml_bufferstack_push(int new)
 {
   if (!new)
@@ -89,43 +94,91 @@ void parse_after_config() {
 /* This function acts as a main in the parsing area. */
 void parse_platform_file(const char *file)
 {
-  int parse_status;
+  int is_lua = (file != NULL && strlen(file) > 3 && file[strlen(file)-3] == 'l' && file[strlen(file)-2] == 'u'
+        && file[strlen(file)-1] == 'a');
 
   surf_parse_init_callbacks();
 
-  /* init the flex parser */
-  surfxml_buffer_stack_stack_ptr = 1;
-  surfxml_buffer_stack_stack[0] = 0;
-  after_config_done = 0;
-  surf_parse_open(file);
-
-  /* Init my data */
-  if (!surfxml_bufferstack_stack)
-    surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
-
-  traces_set_list = xbt_dict_new_homogeneous(NULL);
-  trace_connect_list_host_avail = xbt_dict_new_homogeneous(free);
-  trace_connect_list_power = xbt_dict_new_homogeneous(free);
-  trace_connect_list_link_avail = xbt_dict_new_homogeneous(free);
-  trace_connect_list_bandwidth = xbt_dict_new_homogeneous(free);
-  trace_connect_list_latency = xbt_dict_new_homogeneous(free);
-
-  /* Do the actual parsing */
-  parse_status = surf_parse();
-
-  /* Free my data */
-  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);
-  xbt_dict_free(&random_data_list);
-  xbt_dynar_free(&surfxml_bufferstack_stack);
-
-  /* Stop the flex parser */
-  surf_parse_close();
-  if (parse_status)
-    surf_parse_error("Parse error in %s", file);
-}
+  /* Check if file extension is "lua". If so, we will use
+   * the lua bindings to parse the platform file (since it is
+   * written in lua). If not, we will use the (old?) XML parser
+   */
+  if (is_lua) {
+    // Get maestro state. In case we're calling Lua from
+    // C only, this will be NULL -- no Lua code has been
+    // executed yet and hence, the SimGrid module has not
+    // yet been loaded.
+    // NOTE: After executing the lua_pcall() below,
+    // sglua_get_maestro() will not be NULL, since the
+    // SimGrid module was loaded!
+    lua_State* L = sglua_get_maestro();
+
+    // We may want to remove the task_copy_callback from
+    // the SimGrid module if we're using C code only (this
+    // callback is used for Lua-only code).
+    int remove_callback = FALSE;
+    if (L == NULL) {
+        L = luaL_newstate();
+        remove_callback = TRUE;
+    }
+    luaL_openlibs(L);
+
+    luaL_loadfile(L, file); // This loads the file without executing it.
+
+    /* Run the script */
+    if (lua_pcall(L, 0, 0, 0)) {
+        XBT_ERROR("FATAL ERROR:\n  %s: %s\n\n", "Lua call failed. Errormessage:", lua_tostring(L, -1));
+        xbt_die("Lua call failed. See Log");
+    }
+    // Without this, task_copy_callback() will try to copy
+    // some tasks -- but these don't exist in case we're using
+    // C. Hence, we need to remove the callback -- we don't
+    // want to segfault.
+    if (remove_callback) {
+      MSG_task_set_copy_callback(NULL);
+    }
+
+  }
+  else { // Use XML parser
+
+    int parse_status;
+
+    /* init the flex parser */
+    surfxml_buffer_stack_stack_ptr = 1;
+    surfxml_buffer_stack_stack[0] = 0;
+    after_config_done = 0;
+    surf_parse_open(file);
+
+    traces_set_list = xbt_dict_new_homogeneous(NULL);
+    trace_connect_list_host_avail = xbt_dict_new_homogeneous(free);
+    trace_connect_list_power = xbt_dict_new_homogeneous(free);
+    trace_connect_list_link_avail = xbt_dict_new_homogeneous(free);
+    trace_connect_list_bandwidth = xbt_dict_new_homogeneous(free);
+    trace_connect_list_latency = xbt_dict_new_homogeneous(free);
+
+    /* Init my data */
+    if (!surfxml_bufferstack_stack)
+      surfxml_bufferstack_stack = xbt_dynar_new(sizeof(char *), NULL);
 
+    /* Do the actual parsing */
+    parse_status = surf_parse();
+
+    /* Free my data */
+    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);
+    xbt_dict_free(&random_data_list);
+    xbt_dynar_free(&surfxml_bufferstack_stack);
+
+    surf_parse_close();
+
+    if (parse_status)
+      surf_parse_error("Parse error in %s", file);
+
+  }
+
+
+}