1 /* Copyright (c) 2010-2015. 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"
13 #include "simgrid/msg.h"
14 #include "simgrid/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 /* ********************************************************************************* */
29 /* ********************************************************************************* */
32 * \brief Deploys your application.
33 * \param L a Lua state
34 * \return number of values returned to Lua
36 * - Argument 1 (string): name of the deployment file to load
38 static int launch_application(lua_State* L) {
40 const char* file = luaL_checkstring(L, 1);
41 MSG_function_register_default(run_lua_code);
42 MSG_launch_application(file);
48 * \brief Creates the platform.
49 * \param L a Lua state
50 * \return number of values returned to Lua
52 * - Argument 1 (string): name of the platform file to load
54 static int create_environment(lua_State* L) {
56 const char* file = luaL_checkstring(L, 1);
57 XBT_DEBUG("Loading environment file %s", file);
58 MSG_create_environment(file);
63 * \brief Prints a log string with debug level.
64 * \param L a Lua state
65 * \return number of values returned to Lua
67 * - Argument 1 (string): the text to print
69 static int debug(lua_State* L) {
71 const char* str = luaL_checkstring(L, 1);
77 * \brief Prints a log string with info level.
78 * \param L a Lua state
79 * \return number of values returned to Lua
81 * - Argument 1 (string): the text to print
83 static int info(lua_State* L) {
85 const char* str = luaL_checkstring(L, 1);
90 static int error(lua_State* L) {
92 const char* str = luaL_checkstring(L, 1);
97 static int critical(lua_State* L) {
99 const char* str = luaL_checkstring(L, 1);
100 XBT_CRITICAL("%s", str);
105 * \brief Runs your application.
106 * \param L a Lua state
107 * \return number of values returned to Lua
109 static int run(lua_State* L) {
116 * \brief Returns the current simulated time.
117 * \param L a Lua state
118 * \return number of values returned to Lua
120 * - Return value (number): the simulated time
122 static int get_clock(lua_State* L) {
124 lua_pushnumber(L, MSG_get_clock());
129 * \brief Cleans the simulation.
130 * \param L a Lua state
131 * \return number of values returned to Lua
133 static int simgrid_gc(lua_State * L)
135 // There is no need to cleanup the C world anymore, as it gets cleaned at system process closing automatically
136 // Maybe at some point we'll want to reintroduce this, for example when encapsulating the simulation properly
137 //if (sglua_is_maestro(L)) {
144 * Register platform for MSG
146 static int msg_register_platform(lua_State * L)
148 /* Tell Simgrid we don't wanna use its parser */
149 //surf_parse = console_parse_platform;
150 surf_parse_reset_callbacks();
151 MSG_create_environment(NULL);
156 * Register platform for Simdag
158 static int sd_register_platform(lua_State * L)
160 //surf_parse = console_parse_platform_wsL07;
161 surf_parse_reset_callbacks();
162 SD_create_environment(NULL);
167 * Register application for MSG
169 static int msg_register_application(lua_State * L)
171 MSG_function_register_default(run_lua_code);
172 //surf_parse = console_parse_application;
173 MSG_launch_application(NULL);
177 static int console_init_application(lua_State *L) {
178 MSG_function_register_default(run_lua_code);
179 SIMIX_init_application();
184 static const luaL_Reg simgrid_functions[] = {
185 {"create_environment", create_environment},
186 {"launch_application", launch_application},
189 {"critical", critical},
192 {"get_clock", get_clock},
194 {"platform", create_environment},
195 {"application", launch_application},
196 /* methods to bypass XML parser */
197 {"msg_register_platform", msg_register_platform},
198 {"sd_register_platform", sd_register_platform},
199 {"msg_register_application", msg_register_application},
200 {"init_application", console_init_application},
204 /* ********************************************************************************* */
205 /* module management functions */
206 /* ********************************************************************************* */
208 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
211 * \brief Opens the simgrid Lua module.
213 * This function is called automatically by the Lua interpreter when some
214 * Lua code requires the "simgrid" module.
216 * \param L the Lua state
219 int luaopen_simgrid(lua_State *L)
221 XBT_DEBUG("luaopen_simgrid *****");
223 /* Get the command line arguments from the lua interpreter */
224 char **argv = xbt_malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
226 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? */
228 lua_getglobal(L, "arg");
229 /* if arg is a null value, it means we use lua only as a script to init platform
230 * else it should be a table and then take arg in consideration
232 if (lua_istable(L, -1)) {
236 lua_pushinteger(L, argc - 2);
238 if (lua_isnil(L, -1)) {
241 xbt_assert(lua_isstring(L, -1),
242 "argv[%d] got from lua is no string", argc - 1);
243 xbt_assert(argc < LUA_MAX_ARGS_COUNT,
244 "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",
245 __FILE__, LUA_MAX_ARGS_COUNT - 1);
246 argv[argc - 1] = (char *) luaL_checkstring(L, -1);
248 XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
253 /* Initialize the MSG core */
254 XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
256 MSG_init(&argc, argv);
257 MSG_process_set_data_cleanup((void_f_pvoid_t) lua_close);
259 /* Keep the context mechanism informed of our lua world today */
260 sglua_maestro_state = L;
262 /* initialize access to my tables by children Lua states */
264 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
266 sglua_register_c_functions(L);
272 * \brief Returns whether a Lua state is the maestro state.
273 * \param L a Lua state
274 * \return true if this is maestro
276 int sglua_is_maestro(lua_State* L) {
277 return L == sglua_maestro_state;
281 * \brief Returns the maestro state.
282 * \return the maestro Lua state
284 lua_State* sglua_get_maestro(void) {
285 return sglua_maestro_state;
289 * \brief Makes the core functions available to the Lua world.
290 * \param L a Lua world
292 static void sglua_register_core_functions(lua_State *L)
294 /* register the core C functions to lua */
295 luaL_newlib(L, simgrid_functions); /* simgrid */
296 lua_pushvalue(L, -1); /* simgrid simgrid */
297 lua_setglobal(L, "simgrid"); /* simgrid */
299 /* set a finalizer that cleans simgrid, by adding to the simgrid module a
300 * dummy userdata whose __gc metamethod calls MSG_clean() */
301 lua_newuserdata(L, sizeof(void*)); /* simgrid udata */
302 lua_newtable(L); /* simgrid udata mt */
303 lua_pushcfunction(L, simgrid_gc); /* simgrid udata mt simgrid_gc */
304 lua_setfield(L, -2, "__gc"); /* simgrid udata mt */
305 lua_setmetatable(L, -2); /* simgrid udata */
306 lua_setfield(L, -2, "__simgrid_loaded"); /* simgrid */
307 lua_pop(L, 1); /* -- */
311 * \brief Creates the simgrid module and make it available to Lua.
312 * \param L a Lua world
314 static void sglua_register_c_functions(lua_State *L)
316 sglua_register_core_functions(L);
317 sglua_register_task_functions(L);
318 sglua_register_comm_functions(L);
319 sglua_register_host_functions(L);
320 sglua_register_process_functions(L);
321 sglua_register_platf_functions(L);
325 * \brief Runs a Lua function as a new simulated process.
326 * \param argc number of arguments of the function
327 * \param argv name of the Lua function and array of its arguments
328 * \return result of the function
330 static int run_lua_code(int argc, char **argv)
332 XBT_DEBUG("Run lua code %s", argv[0]);
334 /* create a new state, getting globals from maestro */
335 lua_State *L = sglua_clone_maestro();
336 MSG_process_set_data(MSG_process_self(), L);
338 /* start the function */
339 lua_getglobal(L, argv[0]);
340 xbt_assert(lua_isfunction(L, -1),
341 "There is no Lua function with name `%s'", argv[0]);
343 /* push arguments onto the stack */
345 for (i = 1; i < argc; i++)
346 lua_pushstring(L, argv[i]);
348 /* call the function */
349 XBT_ATTRIB_UNUSED int err;
350 err = lua_pcall(L, argc - 1, 1, 0);
351 xbt_assert(err == 0, "Error running function `%s': %s", argv[0],
352 lua_tostring(L, -1));
354 /* retrieve result */
356 if (lua_isnumber(L, -1)) {
357 res = lua_tointeger(L, -1);
358 lua_pop(L, 1); /* pop returned value */
361 XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
367 * \brief Returns a string corresponding to an MSG error code.
368 * \param err an MSG error code
369 * \return a string describing this error
371 const char* sglua_get_msg_error(msg_error_t err) {
373 static const char* msg_errors[] = {
381 return msg_errors[err];