Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2098267bac877fd60418963722bae636d2e97f63
[simgrid.git] / src / bindings / lua / simgrid_lua.c
1 /* Copyright (c) 2010-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* SimGrid Lua bindings                                                     */
8
9 #include "lua_private.h"
10 #include "lua_state_cloner.h"
11 #include "lua_utils.h"
12 #include "xbt.h"
13 #include "msg/msg.h"
14 #include "simdag/simdag.h"
15 #include "surf/surfxml_parse.h"
16 #include <lauxlib.h>
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
19
20 static lua_State* sglua_maestro_state;
21
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);
25
26 /* ********************************************************************************* */
27 /*                                  simgrid API                                      */
28 /* ********************************************************************************* */
29
30 /**
31  * \brief Deploys your application.
32  * \param L a Lua state
33  * \return number of values returned to Lua
34  *
35  * - Argument 1 (string): name of the deployment file to load
36  */
37 static int launch_application(lua_State* L) {
38
39   const char* file = luaL_checkstring(L, 1);
40   MSG_function_register_default(run_lua_code);
41   MSG_launch_application(file);
42   return 0;
43 }
44
45 /**
46  * \brief Creates the platform.
47  * \param L a Lua state
48  * \return number of values returned to Lua
49  *
50  * - Argument 1 (string): name of the platform file to load
51  */
52 static int create_environment(lua_State* L) {
53
54   const char* file = luaL_checkstring(L, 1);
55   XBT_DEBUG("Loading environment file %s", file);
56   MSG_create_environment(file);
57   return 0;
58 }
59
60 /**
61  * \brief Prints a log string with debug level.
62  * \param L a Lua state
63  * \return number of values returned to Lua
64  *
65  * - Argument 1 (string): the text to print
66  */
67 static int debug(lua_State* L) {
68
69   const char* str = luaL_checkstring(L, 1);
70   XBT_DEBUG("%s", str);
71   return 0;
72 }
73
74 /**
75  * \brief Prints a log string with info level.
76  * \param L a Lua state
77  * \return number of values returned to Lua
78  *
79  * - Argument 1 (string): the text to print
80  */
81 static int info(lua_State* L) {
82
83   const char* str = luaL_checkstring(L, 1);
84   XBT_INFO("%s", str);
85   return 0;
86 }
87
88 /**
89  * \brief Runs your application.
90  * \param L a Lua state
91  * \return number of values returned to Lua
92  */
93 static int run(lua_State*  L) {
94
95   MSG_main();
96   return 0;
97 }
98
99 /**
100  * \brief Returns the current simulated time.
101  * \param L a Lua state
102  * \return number of values returned to Lua
103  *
104  * - Return value (number): the simulated time
105  */
106 static int get_clock(lua_State* L) {
107
108   lua_pushnumber(L, MSG_get_clock());
109   return 1;
110 }
111
112 /**
113  * \brief Cleans the simulation.
114  * \param L a Lua state
115  * \return number of values returned to Lua
116  */
117 static int simgrid_gc(lua_State * L)
118 {
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)) {
122   //  MSG_clean();
123   //}
124   return 0;
125 }
126
127 /*
128  * Register platform for MSG
129  */
130 static int msg_register_platform(lua_State * L)
131 {
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);
136   return 0;
137 }
138
139 /*
140  * Register platform for Simdag
141  */
142 static int sd_register_platform(lua_State * L)
143 {
144   //surf_parse = console_parse_platform_wsL07;
145   surf_parse_reset_callbacks();
146   SD_create_environment(NULL);
147   return 0;
148 }
149
150 /**
151  * Register application for MSG
152  */
153 static int msg_register_application(lua_State * L)
154 {
155   MSG_function_register_default(run_lua_code);
156   //surf_parse = console_parse_application;
157   MSG_launch_application(NULL);
158   return 0;
159 }
160
161 static int console_init_application(lua_State *L) {
162   MSG_function_register_default(run_lua_code);
163   SIMIX_init_application();
164   return 0;
165 }
166
167
168 static const luaL_Reg simgrid_functions[] = {
169   {"create_environment", create_environment},
170   {"launch_application", launch_application},
171   {"debug", debug},
172   {"info", info},
173   {"run", run},
174   {"get_clock", get_clock},
175   /* short names */
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},
183   {NULL, NULL}
184 };
185
186 /* ********************************************************************************* */
187 /*                           module management functions                             */
188 /* ********************************************************************************* */
189
190 #define LUA_MAX_ARGS_COUNT 10   /* maximum amount of arguments we can get from lua on command line */
191
192 /**
193  * \brief Opens the simgrid Lua module.
194  *
195  * This function is called automatically by the Lua interpreter when some
196  * Lua code requires the "simgrid" module.
197  *
198  * \param L the Lua state
199  */
200 int luaopen_simgrid(lua_State *L)
201 {
202   XBT_DEBUG("luaopen_simgrid *****");
203
204   /* Get the command line arguments from the lua interpreter */
205   char **argv = xbt_malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
206   int argc = 1;
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? */
208
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
212    */
213   if (lua_istable(L, -1)) {
214     int done = 0;
215     while (!done) {
216       argc++;
217       lua_pushinteger(L, argc - 2);
218       lua_gettable(L, -2);
219       if (lua_isnil(L, -1)) {
220         done = 1;
221       } else {
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);
228         lua_pop(L, 1);
229         XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
230       }
231     }
232     argv[argc--] = NULL;
233
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
238   }
239
240   /* Keep the context mechanism informed of our lua world today */
241   sglua_maestro_state = L;
242
243   /* initialize access to my tables by children Lua states */
244   lua_newtable(L);
245   lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
246
247   sglua_register_c_functions(L);
248
249   return 1;
250 }
251
252 /**
253  * \brief Returns whether a Lua state is the maestro state.
254  * \param L a Lua state
255  * \return true if this is maestro
256  */
257 int sglua_is_maestro(lua_State* L) {
258   return L == sglua_maestro_state;
259 }
260
261 /**
262  * \brief Returns the maestro state.
263  * \return the maestro Lua state
264  */
265 lua_State* sglua_get_maestro(void) {
266   return sglua_maestro_state;
267 }
268
269 /**
270  * \brief Makes the core functions available to the Lua world.
271  * \param L a Lua world
272  */
273 static void sglua_register_core_functions(lua_State *L)
274 {
275   /* register the core C functions to lua */
276   luaL_register(L, "simgrid", simgrid_functions);
277                                   /* simgrid */
278
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*));
282                                   /* simgrid udata */
283   lua_newtable(L);
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);
290                                   /* simgrid udata */
291   lua_setfield(L, -2, "__simgrid_loaded");
292                                   /* simgrid */
293   lua_pop(L, 1);
294                                   /* -- */
295 }
296
297 /**
298  * \brief Creates the simgrid module and make it available to Lua.
299  * \param L a Lua world
300  */
301 static void sglua_register_c_functions(lua_State *L)
302 {
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);
309 }
310
311 /**
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
316  */
317 static int run_lua_code(int argc, char **argv)
318 {
319   XBT_DEBUG("Run lua code %s", argv[0]);
320
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);
324
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]);
329
330   /* push arguments onto the stack */
331   int i;
332   for (i = 1; i < argc; i++)
333     lua_pushstring(L, argv[i]);
334
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));
340
341   /* retrieve result */
342   int res = 1;
343   if (lua_isnumber(L, -1)) {
344     res = lua_tonumber(L, -1);
345     lua_pop(L, 1);              /* pop returned value */
346   }
347
348   XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
349
350   return res;
351 }
352
353 /**
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
357  */
358 const char* sglua_get_msg_error(msg_error_t err) {
359
360   static const char* msg_errors[] = {
361       NULL,
362       "timeout",
363       "transfer failure",
364       "host failure",
365       "task canceled"
366   };
367
368   return msg_errors[err];
369 }