1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /* SimGrid Lua bindings */
9 #include "lua_private.h"
10 #include "lua_state_cloner.h"
11 #include "lua_utils.h"
14 #include "simdag/simdag.h"
15 #include "surf/surfxml_parse.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
21 static lua_State* sglua_maestro_state;
23 int luaopen_simgrid(lua_State *L);
24 static void sglua_register_c_functions(lua_State *L);
25 static int run_lua_code(int argc, char **argv);
27 /* ********************************************************************************* */
28 /* lua_stub_generator functions */
29 /* ********************************************************************************* */
31 xbt_dict_t process_function_set;
32 xbt_dynar_t process_list;
33 xbt_dict_t machine_set;
34 static s_process_t process;
36 void s_process_free(void *process)
38 s_process_t *p = (s_process_t *) process;
40 for (i = 0; i < p->argc; i++)
46 static int gras_add_process_function(lua_State * L)
49 const char *process_host = luaL_checkstring(L, 1);
50 const char *process_function = luaL_checkstring(L, 2);
52 if (xbt_dict_is_empty(machine_set)
53 || xbt_dict_is_empty(process_function_set)
54 || xbt_dynar_is_empty(process_list)) {
55 process_function_set = xbt_dict_new_homogeneous(NULL);
56 process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
57 machine_set = xbt_dict_new_homogeneous(NULL);
60 xbt_dict_set(machine_set, process_host, NULL, NULL);
61 xbt_dict_set(process_function_set, process_function, NULL, NULL);
64 process.argv = xbt_new(char *, 1);
65 process.argv[0] = xbt_strdup(process_function);
66 process.host = strdup(process_host);
69 while (lua_next(L, 3) != 0) {
70 arg = lua_tostring(L, -1);
73 xbt_realloc(process.argv, (process.argc) * sizeof(char *));
74 process.argv[(process.argc) - 1] = xbt_strdup(arg);
76 XBT_DEBUG("index = %f , arg = %s \n", lua_tonumber(L, -2),
81 //add to the process list
82 xbt_dynar_push(process_list, &process);
86 static int gras_generate(lua_State * L)
88 const char *project_name = luaL_checkstring(L, 1);
89 generate_sim(project_name);
90 generate_rl(project_name);
91 generate_makefile_local(project_name);
95 /* ********************************************************************************* */
97 /* ********************************************************************************* */
100 * \brief Deploys your application.
101 * \param L a Lua state
102 * \return number of values returned to Lua
104 * - Argument 1 (string): name of the deployment file to load
106 static int launch_application(lua_State* L) {
108 const char* file = luaL_checkstring(L, 1);
109 MSG_function_register_default(run_lua_code);
110 MSG_launch_application(file);
115 * \brief Creates the platform.
116 * \param L a Lua state
117 * \return number of values returned to Lua
119 * - Argument 1 (string): name of the platform file to load
121 static int create_environment(lua_State* L) {
123 const char* file = luaL_checkstring(L, 1);
124 XBT_DEBUG("Loading environment file %s", file);
125 MSG_create_environment(file);
130 * \brief Prints a log string with debug level.
131 * \param L a Lua state
132 * \return number of values returned to Lua
134 * - Argument 1 (string): the text to print
136 static int debug(lua_State* L) {
138 const char* str = luaL_checkstring(L, 1);
139 XBT_DEBUG("%s", str);
144 * \brief Prints a log string with info level.
145 * \param L a Lua state
146 * \return number of values returned to Lua
148 * - Argument 1 (string): the text to print
150 static int info(lua_State* L) {
152 const char* str = luaL_checkstring(L, 1);
158 * \brief Runs your application.
159 * \param L a Lua state
160 * \return number of values returned to Lua
162 static int run(lua_State* L) {
169 * \brief Returns the current simulated time.
170 * \param L a Lua state
171 * \return number of values returned to Lua
173 * - Return value (number): the simulated time
175 static int get_clock(lua_State* L) {
177 lua_pushnumber(L, MSG_get_clock());
182 * \brief Cleans the simulation.
183 * \param L a Lua state
184 * \return number of values returned to Lua
186 static int simgrid_gc(lua_State * L)
188 if (sglua_is_maestro(L)) {
195 * Register platform for MSG
197 static int msg_register_platform(lua_State * L)
199 /* Tell Simgrid we dont wanna use its parser */
200 //surf_parse = console_parse_platform;
201 surf_parse_reset_callbacks();
202 MSG_create_environment(NULL);
207 * Register platform for Simdag
209 static int sd_register_platform(lua_State * L)
211 //surf_parse = console_parse_platform_wsL07;
212 surf_parse_reset_callbacks();
213 SD_create_environment(NULL);
218 * Register platform for gras
220 static int gras_register_platform(lua_State * L)
222 //surf_parse = console_parse_platform;
223 surf_parse_reset_callbacks();
224 gras_create_environment(NULL);
229 * Register applicaiton for MSG
231 static int msg_register_application(lua_State * L)
233 MSG_function_register_default(run_lua_code);
234 //surf_parse = console_parse_application;
235 MSG_launch_application(NULL);
240 * Register application for gras
242 static int gras_register_application(lua_State * L)
244 gras_function_register_default(run_lua_code);
245 //surf_parse = console_parse_application;
246 gras_launch_application(NULL);
250 static const luaL_Reg simgrid_functions[] = {
251 {"create_environment", create_environment},
252 {"launch_application", launch_application},
256 {"get_clock", get_clock},
258 {"platform", create_environment},
259 {"application", launch_application},
260 /* methods to bypass XML parser */
261 {"msg_register_platform", msg_register_platform},
262 {"sd_register_platform", sd_register_platform},
263 {"msg_register_application", msg_register_application},
264 {"gras_register_platform", gras_register_platform},
265 {"gras_register_application", gras_register_application},
266 /* gras sub generator method */
267 {"gras_set_process_function", gras_add_process_function},
268 {"gras_generate", gras_generate},
272 /* ********************************************************************************* */
273 /* module management functions */
274 /* ********************************************************************************* */
276 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
279 * \brief Opens the simgrid Lua module.
281 * This function is called automatically by the Lua interpreter when some
282 * Lua code requires the "simgrid" module.
284 * \param L the Lua state
286 int luaopen_simgrid(lua_State *L)
288 XBT_DEBUG("luaopen_simgrid *****");
290 /* Get the command line arguments from the lua interpreter */
291 char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
293 argv[0] = (char *) "/usr/bin/lua"; /* Lie on the argv[0] so that the stack dumping facilities find the right binary. FIXME: what if lua is not in that location? */
295 lua_getglobal(L, "arg");
296 /* if arg is a null value, it means we use lua only as a script to init platform
297 * else it should be a table and then take arg in consideration
299 if (lua_istable(L, -1)) {
303 lua_pushinteger(L, argc - 2);
305 if (lua_isnil(L, -1)) {
308 xbt_assert(lua_isstring(L, -1),
309 "argv[%d] got from lua is no string", argc - 1);
310 xbt_assert(argc < LUA_MAX_ARGS_COUNT,
311 "Too many arguments, please increase LUA_MAX_ARGS_COUNT in %s before recompiling SimGrid if you insist on having more than %d args on command line",
312 __FILE__, LUA_MAX_ARGS_COUNT - 1);
313 argv[argc - 1] = (char *) luaL_checkstring(L, -1);
315 XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
320 /* Initialize the MSG core */
321 MSG_global_init(&argc, argv);
322 MSG_process_set_data_cleanup((void_f_pvoid_t) lua_close);
323 XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
326 /* Keep the context mechanism informed of our lua world today */
327 sglua_maestro_state = L;
329 /* initialize access to my tables by children Lua states */
331 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
333 sglua_register_c_functions(L);
339 * \brief Returns whether a Lua state is the maestro state.
340 * \param L a Lua state
341 * \return true if this is maestro
343 int sglua_is_maestro(lua_State* L) {
344 return L == sglua_maestro_state;
348 * \brief Returns the maestro state.
349 * \return the maestro Lua state
351 lua_State* sglua_get_maestro(void) {
352 return sglua_maestro_state;
356 * \brief Makes the core functions available to the Lua world.
357 * \param L a Lua world
359 static void sglua_register_core_functions(lua_State *L)
361 /* register the core C functions to lua */
362 luaL_register(L, "simgrid", simgrid_functions);
365 /* set a finalizer that cleans simgrid, by adding to the simgrid module a
366 * dummy userdata whose __gc metamethod calls MSG_clean() */
367 lua_newuserdata(L, sizeof(void*));
370 /* simgrid udata mt */
371 lua_pushcfunction(L, simgrid_gc);
372 /* simgrid udata mt simgrid_gc */
373 lua_setfield(L, -2, "__gc");
374 /* simgrid udata mt */
375 lua_setmetatable(L, -2);
377 lua_setfield(L, -2, "__simgrid_loaded");
384 * \brief Creates the simgrid module and make it available to Lua.
385 * \param L a Lua world
387 static void sglua_register_c_functions(lua_State *L)
389 sglua_register_core_functions(L);
390 sglua_register_task_functions(L);
391 sglua_register_comm_functions(L);
392 sglua_register_host_functions(L);
393 sglua_register_process_functions(L);
394 sglua_register_platf_functions(L);
398 * \brief Runs a Lua function as a new simulated process.
399 * \param argc number of arguments of the function
400 * \param argv name of the Lua function and array of its arguments
401 * \return result of the function
403 static int run_lua_code(int argc, char **argv)
405 XBT_DEBUG("Run lua code %s", argv[0]);
407 /* create a new state, getting globals from maestro */
408 lua_State *L = sglua_clone_maestro();
409 MSG_process_set_data(MSG_process_self(), L);
411 /* start the function */
412 lua_getglobal(L, argv[0]);
413 xbt_assert(lua_isfunction(L, -1),
414 "There is no Lua function with name `%s'", argv[0]);
416 /* push arguments onto the stack */
418 for (i = 1; i < argc; i++)
419 lua_pushstring(L, argv[i]);
421 /* call the function */
422 _XBT_GNUC_UNUSED int err;
423 err = lua_pcall(L, argc - 1, 1, 0);
424 xbt_assert(err == 0, "Error running function `%s': %s", argv[0],
425 lua_tostring(L, -1));
427 /* retrieve result */
429 if (lua_isnumber(L, -1)) {
430 res = lua_tonumber(L, -1);
431 lua_pop(L, 1); /* pop returned value */
434 XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
440 * \brief Returns a string corresponding to an MSG error code.
441 * \param err an MSG error code
442 * \return a string describing this error
444 const char* sglua_get_msg_error(MSG_error_t err) {
446 static const char* msg_errors[] = {
454 return msg_errors[err];