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);
91 * \brief Runs your application.
92 * \param L a Lua state
93 * \return number of values returned to Lua
95 static int run(lua_State* L) {
102 * \brief Returns the current simulated time.
103 * \param L a Lua state
104 * \return number of values returned to Lua
106 * - Return value (number): the simulated time
108 static int get_clock(lua_State* L) {
110 lua_pushnumber(L, MSG_get_clock());
115 * \brief Cleans the simulation.
116 * \param L a Lua state
117 * \return number of values returned to Lua
119 static int simgrid_gc(lua_State * L)
121 // There is no need to cleanup the C world anymore, as it gets cleaned at system process closing automatically
122 // Maybe at some point we'll want to reintroduce this, for example when encapsulating the simulation properly
123 //if (sglua_is_maestro(L)) {
130 * Register platform for MSG
132 static int msg_register_platform(lua_State * L)
134 /* Tell Simgrid we don't wanna use its parser */
135 //surf_parse = console_parse_platform;
136 surf_parse_reset_callbacks();
137 MSG_create_environment(NULL);
142 * Register platform for Simdag
144 static int sd_register_platform(lua_State * L)
146 //surf_parse = console_parse_platform_wsL07;
147 surf_parse_reset_callbacks();
148 SD_create_environment(NULL);
153 * Register application for MSG
155 static int msg_register_application(lua_State * L)
157 MSG_function_register_default(run_lua_code);
158 //surf_parse = console_parse_application;
159 MSG_launch_application(NULL);
163 static int console_init_application(lua_State *L) {
164 MSG_function_register_default(run_lua_code);
165 SIMIX_init_application();
170 static const luaL_Reg simgrid_functions[] = {
171 {"create_environment", create_environment},
172 {"launch_application", launch_application},
176 {"get_clock", get_clock},
178 {"platform", create_environment},
179 {"application", launch_application},
180 /* methods to bypass XML parser */
181 {"msg_register_platform", msg_register_platform},
182 {"sd_register_platform", sd_register_platform},
183 {"msg_register_application", msg_register_application},
184 {"init_application", console_init_application},
188 /* ********************************************************************************* */
189 /* module management functions */
190 /* ********************************************************************************* */
192 #define LUA_MAX_ARGS_COUNT 10 /* maximum amount of arguments we can get from lua on command line */
195 * \brief Opens the simgrid Lua module.
197 * This function is called automatically by the Lua interpreter when some
198 * Lua code requires the "simgrid" module.
200 * \param L the Lua state
203 int luaopen_simgrid(lua_State *L)
205 XBT_DEBUG("luaopen_simgrid *****");
207 /* Get the command line arguments from the lua interpreter */
208 char **argv = xbt_malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
210 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? */
212 lua_getglobal(L, "arg");
213 /* if arg is a null value, it means we use lua only as a script to init platform
214 * else it should be a table and then take arg in consideration
216 if (lua_istable(L, -1)) {
220 lua_pushinteger(L, argc - 2);
222 if (lua_isnil(L, -1)) {
225 xbt_assert(lua_isstring(L, -1),
226 "argv[%d] got from lua is no string", argc - 1);
227 xbt_assert(argc < LUA_MAX_ARGS_COUNT,
228 "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",
229 __FILE__, LUA_MAX_ARGS_COUNT - 1);
230 argv[argc - 1] = (char *) luaL_checkstring(L, -1);
232 XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
237 /* Initialize the MSG core */
238 XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
240 MSG_init(&argc, argv);
241 MSG_process_set_data_cleanup((void_f_pvoid_t) lua_close);
243 /* Keep the context mechanism informed of our lua world today */
244 sglua_maestro_state = L;
246 /* initialize access to my tables by children Lua states */
248 lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
250 sglua_register_c_functions(L);
256 * \brief Returns whether a Lua state is the maestro state.
257 * \param L a Lua state
258 * \return true if this is maestro
260 int sglua_is_maestro(lua_State* L) {
261 return L == sglua_maestro_state;
265 * \brief Returns the maestro state.
266 * \return the maestro Lua state
268 lua_State* sglua_get_maestro(void) {
269 return sglua_maestro_state;
273 * \brief Makes the core functions available to the Lua world.
274 * \param L a Lua world
276 static void sglua_register_core_functions(lua_State *L)
278 /* register the core C functions to lua */
279 luaL_register(L, "simgrid", simgrid_functions);
282 /* set a finalizer that cleans simgrid, by adding to the simgrid module a
283 * dummy userdata whose __gc metamethod calls MSG_clean() */
284 lua_newuserdata(L, sizeof(void*));
287 /* simgrid udata mt */
288 lua_pushcfunction(L, simgrid_gc);
289 /* simgrid udata mt simgrid_gc */
290 lua_setfield(L, -2, "__gc");
291 /* simgrid udata mt */
292 lua_setmetatable(L, -2);
294 lua_setfield(L, -2, "__simgrid_loaded");
301 * \brief Creates the simgrid module and make it available to Lua.
302 * \param L a Lua world
304 static void sglua_register_c_functions(lua_State *L)
306 sglua_register_core_functions(L);
307 sglua_register_task_functions(L);
308 sglua_register_comm_functions(L);
309 sglua_register_host_functions(L);
310 sglua_register_process_functions(L);
311 sglua_register_platf_functions(L);
315 * \brief Runs a Lua function as a new simulated process.
316 * \param argc number of arguments of the function
317 * \param argv name of the Lua function and array of its arguments
318 * \return result of the function
320 static int run_lua_code(int argc, char **argv)
322 XBT_DEBUG("Run lua code %s", argv[0]);
324 /* create a new state, getting globals from maestro */
325 lua_State *L = sglua_clone_maestro();
326 MSG_process_set_data(MSG_process_self(), L);
328 /* start the function */
329 lua_getglobal(L, argv[0]);
330 xbt_assert(lua_isfunction(L, -1),
331 "There is no Lua function with name `%s'", argv[0]);
333 /* push arguments onto the stack */
335 for (i = 1; i < argc; i++)
336 lua_pushstring(L, argv[i]);
338 /* call the function */
339 XBT_ATTRIB_UNUSED int err;
340 err = lua_pcall(L, argc - 1, 1, 0);
341 xbt_assert(err == 0, "Error running function `%s': %s", argv[0],
342 lua_tostring(L, -1));
344 /* retrieve result */
346 if (lua_isnumber(L, -1)) {
347 res = lua_tonumber(L, -1);
348 lua_pop(L, 1); /* pop returned value */
351 XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
357 * \brief Returns a string corresponding to an MSG error code.
358 * \param err an MSG error code
359 * \return a string describing this error
361 const char* sglua_get_msg_error(msg_error_t err) {
363 static const char* msg_errors[] = {
371 return msg_errors[err];