1 /* Copyright (c) 2010-2014. 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"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
20 static lua_State* sglua_maestro_state;
22 int luaopen_simgrid(lua_State *L);
23 static void sglua_register_c_functions(lua_State *L);
24 static int run_lua_code(int argc, char **argv);
26 /* ********************************************************************************* */
28 /* ********************************************************************************* */
31 * \brief Deploys your application.
32 * \param L a Lua state
33 * \return number of values returned to Lua
35 * - Argument 1 (string): name of the deployment file to load
37 static int launch_application(lua_State* L) {
39 const char* file = luaL_checkstring(L, 1);
40 MSG_function_register_default(run_lua_code);
41 MSG_launch_application(file);
46 * \brief Creates the platform.
47 * \param L a Lua state
48 * \return number of values returned to Lua
50 * - Argument 1 (string): name of the platform file to load
52 static int create_environment(lua_State* L) {
54 const char* file = luaL_checkstring(L, 1);
55 XBT_DEBUG("Loading environment file %s", file);
56 MSG_create_environment(file);
61 * \brief Prints a log string with debug level.
62 * \param L a Lua state
63 * \return number of values returned to Lua
65 * - Argument 1 (string): the text to print
67 static int debug(lua_State* L) {
69 const char* str = luaL_checkstring(L, 1);
75 * \brief Prints a log string with info level.
76 * \param L a Lua state
77 * \return number of values returned to Lua
79 * - Argument 1 (string): the text to print
81 static int info(lua_State* L) {
83 const char* str = luaL_checkstring(L, 1);
89 * \brief Runs your application.
90 * \param L a Lua state
91 * \return number of values returned to Lua
93 static int run(lua_State* L) {
100 * \brief Returns the current simulated time.
101 * \param L a Lua state
102 * \return number of values returned to Lua
104 * - Return value (number): the simulated time
106 static int get_clock(lua_State* L) {
108 lua_pushnumber(L, MSG_get_clock());
113 * \brief Cleans the simulation.
114 * \param L a Lua state
115 * \return number of values returned to Lua
117 static int simgrid_gc(lua_State * L)
119 // There is no need to cleanup the C world anymore, as it gets cleaned at system process closing automatically
120 // Maybe at some point we'll want to reintroduce this, for example when encapsulating the simulation properly
121 //if (sglua_is_maestro(L)) {
128 * Register platform for MSG
130 static int msg_register_platform(lua_State * L)
132 /* Tell Simgrid we don't wanna use its parser */
133 //surf_parse = console_parse_platform;
134 surf_parse_reset_callbacks();
135 MSG_create_environment(NULL);
140 * Register platform for Simdag
142 static int sd_register_platform(lua_State * L)
144 //surf_parse = console_parse_platform_wsL07;
145 surf_parse_reset_callbacks();
146 SD_create_environment(NULL);
151 * Register application for MSG
153 static int msg_register_application(lua_State * L)
155 MSG_function_register_default(run_lua_code);
156 //surf_parse = console_parse_application;
157 MSG_launch_application(NULL);
161 static int console_init_application(lua_State *L) {
162 MSG_function_register_default(run_lua_code);
163 SIMIX_init_application();
168 static const luaL_Reg simgrid_functions[] = {
169 {"create_environment", create_environment},
170 {"launch_application", launch_application},
174 {"get_clock", get_clock},
176 {"platform", create_environment},
177 {"application", launch_application},
178 /* methods to bypass XML parser */
179 {"msg_register_platform", msg_register_platform},
180 {"sd_register_platform", sd_register_platform},
181 {"msg_register_application", msg_register_application},
182 {"init_application", console_init_application},
186 /* ********************************************************************************* */
187 /* module management functions */
188 /* ********************************************************************************* */
190 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
193 * \brief Opens the simgrid Lua module.
195 * This function is called automatically by the Lua interpreter when some
196 * Lua code requires the "simgrid" module.
198 * \param L the Lua state
200 int luaopen_simgrid(lua_State *L)
202 XBT_DEBUG("luaopen_simgrid *****");
204 /* Get the command line arguments from the lua interpreter */
205 char **argv = xbt_malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
207 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? */
209 lua_getglobal(L, "arg");
210 /* if arg is a null value, it means we use lua only as a script to init platform
211 * else it should be a table and then take arg in consideration
213 if (lua_istable(L, -1)) {
217 lua_pushinteger(L, argc - 2);
219 if (lua_isnil(L, -1)) {
222 xbt_assert(lua_isstring(L, -1),
223 "argv[%d] got from lua is no string", argc - 1);
224 xbt_assert(argc < LUA_MAX_ARGS_COUNT,
225 "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",
226 __FILE__, LUA_MAX_ARGS_COUNT - 1);
227 argv[argc - 1] = (char *) luaL_checkstring(L, -1);
229 XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
234 /* Initialize the MSG core */
235 MSG_init(&argc, argv);
236 MSG_process_set_data_cleanup((void_f_pvoid_t) lua_close);
237 XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
240 /* Keep the context mechanism informed of our lua world today */
241 sglua_maestro_state = L;
243 /* initialize access to my tables by children Lua states */
245 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
247 sglua_register_c_functions(L);
253 * \brief Returns whether a Lua state is the maestro state.
254 * \param L a Lua state
255 * \return true if this is maestro
257 int sglua_is_maestro(lua_State* L) {
258 return L == sglua_maestro_state;
262 * \brief Returns the maestro state.
263 * \return the maestro Lua state
265 lua_State* sglua_get_maestro(void) {
266 return sglua_maestro_state;
270 * \brief Makes the core functions available to the Lua world.
271 * \param L a Lua world
273 static void sglua_register_core_functions(lua_State *L)
275 /* register the core C functions to lua */
276 luaL_register(L, "simgrid", simgrid_functions);
279 /* set a finalizer that cleans simgrid, by adding to the simgrid module a
280 * dummy userdata whose __gc metamethod calls MSG_clean() */
281 lua_newuserdata(L, sizeof(void*));
284 /* simgrid udata mt */
285 lua_pushcfunction(L, simgrid_gc);
286 /* simgrid udata mt simgrid_gc */
287 lua_setfield(L, -2, "__gc");
288 /* simgrid udata mt */
289 lua_setmetatable(L, -2);
291 lua_setfield(L, -2, "__simgrid_loaded");
298 * \brief Creates the simgrid module and make it available to Lua.
299 * \param L a Lua world
301 static void sglua_register_c_functions(lua_State *L)
303 sglua_register_core_functions(L);
304 sglua_register_task_functions(L);
305 sglua_register_comm_functions(L);
306 sglua_register_host_functions(L);
307 sglua_register_process_functions(L);
308 sglua_register_platf_functions(L);
312 * \brief Runs a Lua function as a new simulated process.
313 * \param argc number of arguments of the function
314 * \param argv name of the Lua function and array of its arguments
315 * \return result of the function
317 static int run_lua_code(int argc, char **argv)
319 XBT_DEBUG("Run lua code %s", argv[0]);
321 /* create a new state, getting globals from maestro */
322 lua_State *L = sglua_clone_maestro();
323 MSG_process_set_data(MSG_process_self(), L);
325 /* start the function */
326 lua_getglobal(L, argv[0]);
327 xbt_assert(lua_isfunction(L, -1),
328 "There is no Lua function with name `%s'", argv[0]);
330 /* push arguments onto the stack */
332 for (i = 1; i < argc; i++)
333 lua_pushstring(L, argv[i]);
335 /* call the function */
336 _XBT_GNUC_UNUSED int err;
337 err = lua_pcall(L, argc - 1, 1, 0);
338 xbt_assert(err == 0, "Error running function `%s': %s", argv[0],
339 lua_tostring(L, -1));
341 /* retrieve result */
343 if (lua_isnumber(L, -1)) {
344 res = lua_tonumber(L, -1);
345 lua_pop(L, 1); /* pop returned value */
348 XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
354 * \brief Returns a string corresponding to an MSG error code.
355 * \param err an MSG error code
356 * \return a string describing this error
358 const char* sglua_get_msg_error(msg_error_t err) {
360 static const char* msg_errors[] = {
368 return msg_errors[err];