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"
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 applicaiton 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 const luaL_Reg simgrid_functions[] = {
162 {"create_environment", create_environment},
163 {"launch_application", launch_application},
167 {"get_clock", get_clock},
169 {"platform", create_environment},
170 {"application", launch_application},
171 /* methods to bypass XML parser */
172 {"msg_register_platform", msg_register_platform},
173 {"sd_register_platform", sd_register_platform},
174 {"msg_register_application", msg_register_application},
178 /* ********************************************************************************* */
179 /* module management functions */
180 /* ********************************************************************************* */
182 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
185 * \brief Opens the simgrid Lua module.
187 * This function is called automatically by the Lua interpreter when some
188 * Lua code requires the "simgrid" module.
190 * \param L the Lua state
192 int luaopen_simgrid(lua_State *L)
194 XBT_DEBUG("luaopen_simgrid *****");
196 /* Get the command line arguments from the lua interpreter */
197 char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
199 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? */
201 lua_getglobal(L, "arg");
202 /* if arg is a null value, it means we use lua only as a script to init platform
203 * else it should be a table and then take arg in consideration
205 if (lua_istable(L, -1)) {
209 lua_pushinteger(L, argc - 2);
211 if (lua_isnil(L, -1)) {
214 xbt_assert(lua_isstring(L, -1),
215 "argv[%d] got from lua is no string", argc - 1);
216 xbt_assert(argc < LUA_MAX_ARGS_COUNT,
217 "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",
218 __FILE__, LUA_MAX_ARGS_COUNT - 1);
219 argv[argc - 1] = (char *) luaL_checkstring(L, -1);
221 XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
226 /* Initialize the MSG core */
227 MSG_init(&argc, argv);
228 MSG_process_set_data_cleanup((void_f_pvoid_t) lua_close);
229 XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
232 /* Keep the context mechanism informed of our lua world today */
233 sglua_maestro_state = L;
235 /* initialize access to my tables by children Lua states */
237 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
239 sglua_register_c_functions(L);
245 * \brief Returns whether a Lua state is the maestro state.
246 * \param L a Lua state
247 * \return true if this is maestro
249 int sglua_is_maestro(lua_State* L) {
250 return L == sglua_maestro_state;
254 * \brief Returns the maestro state.
255 * \return the maestro Lua state
257 lua_State* sglua_get_maestro(void) {
258 return sglua_maestro_state;
262 * \brief Makes the core functions available to the Lua world.
263 * \param L a Lua world
265 static void sglua_register_core_functions(lua_State *L)
267 /* register the core C functions to lua */
268 luaL_register(L, "simgrid", simgrid_functions);
271 /* set a finalizer that cleans simgrid, by adding to the simgrid module a
272 * dummy userdata whose __gc metamethod calls MSG_clean() */
273 lua_newuserdata(L, sizeof(void*));
276 /* simgrid udata mt */
277 lua_pushcfunction(L, simgrid_gc);
278 /* simgrid udata mt simgrid_gc */
279 lua_setfield(L, -2, "__gc");
280 /* simgrid udata mt */
281 lua_setmetatable(L, -2);
283 lua_setfield(L, -2, "__simgrid_loaded");
290 * \brief Creates the simgrid module and make it available to Lua.
291 * \param L a Lua world
293 static void sglua_register_c_functions(lua_State *L)
295 sglua_register_core_functions(L);
296 sglua_register_task_functions(L);
297 sglua_register_comm_functions(L);
298 sglua_register_host_functions(L);
299 sglua_register_process_functions(L);
300 sglua_register_platf_functions(L);
304 * \brief Runs a Lua function as a new simulated process.
305 * \param argc number of arguments of the function
306 * \param argv name of the Lua function and array of its arguments
307 * \return result of the function
309 static int run_lua_code(int argc, char **argv)
311 XBT_DEBUG("Run lua code %s", argv[0]);
313 /* create a new state, getting globals from maestro */
314 lua_State *L = sglua_clone_maestro();
315 MSG_process_set_data(MSG_process_self(), L);
317 /* start the function */
318 lua_getglobal(L, argv[0]);
319 xbt_assert(lua_isfunction(L, -1),
320 "There is no Lua function with name `%s'", argv[0]);
322 /* push arguments onto the stack */
324 for (i = 1; i < argc; i++)
325 lua_pushstring(L, argv[i]);
327 /* call the function */
328 _XBT_GNUC_UNUSED int err;
329 err = lua_pcall(L, argc - 1, 1, 0);
330 xbt_assert(err == 0, "Error running function `%s': %s", argv[0],
331 lua_tostring(L, -1));
333 /* retrieve result */
335 if (lua_isnumber(L, -1)) {
336 res = lua_tonumber(L, -1);
337 lua_pop(L, 1); /* pop returned value */
340 XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
346 * \brief Returns a string corresponding to an MSG error code.
347 * \param err an MSG error code
348 * \return a string describing this error
350 const char* sglua_get_msg_error(msg_error_t err) {
352 static const char* msg_errors[] = {
360 return msg_errors[err];