Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'killgraskill'
[simgrid.git] / src / bindings / lua / simgrid_lua.c
1 /* Copyright (c) 2010. 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 applicaiton 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 const luaL_Reg simgrid_functions[] = {
162   {"create_environment", create_environment},
163   {"launch_application", launch_application},
164   {"debug", debug},
165   {"info", info},
166   {"run", run},
167   {"get_clock", get_clock},
168   /* short names */
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},
175   {NULL, NULL}
176 };
177
178 /* ********************************************************************************* */
179 /*                           module management functions                             */
180 /* ********************************************************************************* */
181
182 #define LUA_MAX_ARGS_COUNT 10   /* maximum amount of arguments we can get from lua on command line */
183
184 /**
185  * \brief Opens the simgrid Lua module.
186  *
187  * This function is called automatically by the Lua interpreter when some
188  * Lua code requires the "simgrid" module.
189  *
190  * \param L the Lua state
191  */
192 int luaopen_simgrid(lua_State *L)
193 {
194   XBT_DEBUG("luaopen_simgrid *****");
195
196   /* Get the command line arguments from the lua interpreter */
197   char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
198   int argc = 1;
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? */
200
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
204    */
205   if (lua_istable(L, -1)) {
206     int done = 0;
207     while (!done) {
208       argc++;
209       lua_pushinteger(L, argc - 2);
210       lua_gettable(L, -2);
211       if (lua_isnil(L, -1)) {
212         done = 1;
213       } else {
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);
220         lua_pop(L, 1);
221         XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
222       }
223     }
224     argv[argc--] = NULL;
225
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
230   }
231
232   /* Keep the context mechanism informed of our lua world today */
233   sglua_maestro_state = L;
234
235   /* initialize access to my tables by children Lua states */
236   lua_newtable(L);
237   lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
238
239   sglua_register_c_functions(L);
240
241   return 1;
242 }
243
244 /**
245  * \brief Returns whether a Lua state is the maestro state.
246  * \param L a Lua state
247  * \return true if this is maestro
248  */
249 int sglua_is_maestro(lua_State* L) {
250   return L == sglua_maestro_state;
251 }
252
253 /**
254  * \brief Returns the maestro state.
255  * \return the maestro Lua state
256  */
257 lua_State* sglua_get_maestro(void) {
258   return sglua_maestro_state;
259 }
260
261 /**
262  * \brief Makes the core functions available to the Lua world.
263  * \param L a Lua world
264  */
265 static void sglua_register_core_functions(lua_State *L)
266 {
267   /* register the core C functions to lua */
268   luaL_register(L, "simgrid", simgrid_functions);
269                                   /* simgrid */
270
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*));
274                                   /* simgrid udata */
275   lua_newtable(L);
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);
282                                   /* simgrid udata */
283   lua_setfield(L, -2, "__simgrid_loaded");
284                                   /* simgrid */
285   lua_pop(L, 1);
286                                   /* -- */
287 }
288
289 /**
290  * \brief Creates the simgrid module and make it available to Lua.
291  * \param L a Lua world
292  */
293 static void sglua_register_c_functions(lua_State *L)
294 {
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);
301 }
302
303 /**
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
308  */
309 static int run_lua_code(int argc, char **argv)
310 {
311   XBT_DEBUG("Run lua code %s", argv[0]);
312
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);
316
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]);
321
322   /* push arguments onto the stack */
323   int i;
324   for (i = 1; i < argc; i++)
325     lua_pushstring(L, argv[i]);
326
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));
332
333   /* retrieve result */
334   int res = 1;
335   if (lua_isnumber(L, -1)) {
336     res = lua_tonumber(L, -1);
337     lua_pop(L, 1);              /* pop returned value */
338   }
339
340   XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
341
342   return res;
343 }
344
345 /**
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
349  */
350 const char* sglua_get_msg_error(msg_error_t err) {
351
352   static const char* msg_errors[] = {
353       NULL,
354       "timeout",
355       "transfer failure",
356       "host failure",
357       "task canceled"
358   };
359
360   return msg_errors[err];
361 }