Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] output the command line used to generate the trace file
[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 "gras.h"
17 #include <lauxlib.h>
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
20
21 static lua_State* sglua_maestro_state;
22
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);
26
27 /* ********************************************************************************* */
28 /*                           lua_stub_generator functions                            */
29 /* ********************************************************************************* */
30
31 xbt_dict_t process_function_set;
32 xbt_dynar_t process_list;
33 xbt_dict_t machine_set;
34 static s_process_t process;
35
36 void s_process_free(void *process)
37 {
38   s_process_t *p = (s_process_t *) process;
39   int i;
40   for (i = 0; i < p->argc; i++)
41     free(p->argv[i]);
42   free(p->argv);
43   free(p->host);
44 }
45
46 static int gras_add_process_function(lua_State * L)
47 {
48   const char *arg;
49   const char *process_host = luaL_checkstring(L, 1);
50   const char *process_function = luaL_checkstring(L, 2);
51
52   if (xbt_dict_is_empty(machine_set)
53       || xbt_dict_is_empty(process_function_set)
54       || xbt_dynar_is_empty(process_list)) {
55     process_function_set = xbt_dict_new_homogeneous(NULL);
56     process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
57     machine_set = xbt_dict_new_homogeneous(NULL);
58   }
59
60   xbt_dict_set(machine_set, process_host, NULL, NULL);
61   xbt_dict_set(process_function_set, process_function, NULL, NULL);
62
63   process.argc = 1;
64   process.argv = xbt_new(char *, 1);
65   process.argv[0] = xbt_strdup(process_function);
66   process.host = strdup(process_host);
67
68   lua_pushnil(L);
69   while (lua_next(L, 3) != 0) {
70     arg = lua_tostring(L, -1);
71     process.argc++;
72     process.argv =
73         xbt_realloc(process.argv, (process.argc) * sizeof(char *));
74     process.argv[(process.argc) - 1] = xbt_strdup(arg);
75
76     XBT_DEBUG("index = %f , arg = %s \n", lua_tonumber(L, -2),
77            lua_tostring(L, -1));
78     lua_pop(L, 1);
79   }
80   lua_pop(L, 1);
81   //add to the process list
82   xbt_dynar_push(process_list, &process);
83   return 0;
84 }
85
86 static int gras_generate(lua_State * L)
87 {
88   const char *project_name = luaL_checkstring(L, 1);
89   generate_sim(project_name);
90   generate_rl(project_name);
91   generate_makefile_local(project_name);
92   return 0;
93 }
94
95 /* ********************************************************************************* */
96 /*                                  simgrid API                                      */
97 /* ********************************************************************************* */
98
99 /**
100  * \brief Deploys your application.
101  * \param L a Lua state
102  * \return number of values returned to Lua
103  *
104  * - Argument 1 (string): name of the deployment file to load
105  */
106 static int launch_application(lua_State* L) {
107
108   const char* file = luaL_checkstring(L, 1);
109   MSG_function_register_default(run_lua_code);
110   MSG_launch_application(file);
111   return 0;
112 }
113
114 /**
115  * \brief Creates the platform.
116  * \param L a Lua state
117  * \return number of values returned to Lua
118  *
119  * - Argument 1 (string): name of the platform file to load
120  */
121 static int create_environment(lua_State* L) {
122
123   const char* file = luaL_checkstring(L, 1);
124   XBT_DEBUG("Loading environment file %s", file);
125   MSG_create_environment(file);
126   return 0;
127 }
128
129 /**
130  * \brief Prints a log string with debug level.
131  * \param L a Lua state
132  * \return number of values returned to Lua
133  *
134  * - Argument 1 (string): the text to print
135  */
136 static int debug(lua_State* L) {
137
138   const char* str = luaL_checkstring(L, 1);
139   XBT_DEBUG("%s", str);
140   return 0;
141 }
142
143 /**
144  * \brief Prints a log string with info level.
145  * \param L a Lua state
146  * \return number of values returned to Lua
147  *
148  * - Argument 1 (string): the text to print
149  */
150 static int info(lua_State* L) {
151
152   const char* str = luaL_checkstring(L, 1);
153   XBT_INFO("%s", str);
154   return 0;
155 }
156
157 /**
158  * \brief Runs your application.
159  * \param L a Lua state
160  * \return number of values returned to Lua
161  */
162 static int run(lua_State*  L) {
163
164   MSG_main();
165   return 0;
166 }
167
168 /**
169  * \brief Returns the current simulated time.
170  * \param L a Lua state
171  * \return number of values returned to Lua
172  *
173  * - Return value (number): the simulated time
174  */
175 static int get_clock(lua_State* L) {
176
177   lua_pushnumber(L, MSG_get_clock());
178   return 1;
179 }
180
181 /**
182  * \brief Cleans the simulation.
183  * \param L a Lua state
184  * \return number of values returned to Lua
185  */
186 static int simgrid_gc(lua_State * L)
187 {
188   // There is no need to cleanup the C world anymore, as it gets cleaned at system process closing automatically
189   // Maybe at some point we'll want to reintroduce this, for example when encapsulating the simulation properly
190   //if (sglua_is_maestro(L)) {
191   //  MSG_clean();
192   //}
193   return 0;
194 }
195
196 /*
197  * Register platform for MSG
198  */
199 static int msg_register_platform(lua_State * L)
200 {
201   /* Tell Simgrid we don't wanna use its parser */
202   //surf_parse = console_parse_platform;
203   surf_parse_reset_callbacks();
204   MSG_create_environment(NULL);
205   return 0;
206 }
207
208 /*
209  * Register platform for Simdag
210  */
211 static int sd_register_platform(lua_State * L)
212 {
213   //surf_parse = console_parse_platform_wsL07;
214   surf_parse_reset_callbacks();
215   SD_create_environment(NULL);
216   return 0;
217 }
218
219 /*
220  * Register platform for gras
221  */
222 static int gras_register_platform(lua_State * L)
223 {
224   //surf_parse = console_parse_platform;
225   surf_parse_reset_callbacks();
226   gras_create_environment(NULL);
227   return 0;
228 }
229
230 /**
231  * Register applicaiton for MSG
232  */
233 static int msg_register_application(lua_State * L)
234 {
235   MSG_function_register_default(run_lua_code);
236   //surf_parse = console_parse_application;
237   MSG_launch_application(NULL);
238   return 0;
239 }
240
241 /*
242  * Register application for gras
243  */
244 static int gras_register_application(lua_State * L)
245 {
246   gras_function_register_default(run_lua_code);
247   //surf_parse = console_parse_application;
248   gras_launch_application(NULL);
249   return 0;
250 }
251
252 static const luaL_Reg simgrid_functions[] = {
253   {"create_environment", create_environment},
254   {"launch_application", launch_application},
255   {"debug", debug},
256   {"info", info},
257   {"run", run},
258   {"get_clock", get_clock},
259   /* short names */
260   {"platform", create_environment},
261   {"application", launch_application},
262   /* methods to bypass XML parser */
263   {"msg_register_platform", msg_register_platform},
264   {"sd_register_platform", sd_register_platform},
265   {"msg_register_application", msg_register_application},
266   {"gras_register_platform", gras_register_platform},
267   {"gras_register_application", gras_register_application},
268   /* gras sub generator method */
269   {"gras_set_process_function", gras_add_process_function},
270   {"gras_generate", gras_generate},
271   {NULL, NULL}
272 };
273
274 /* ********************************************************************************* */
275 /*                           module management functions                             */
276 /* ********************************************************************************* */
277
278 #define LUA_MAX_ARGS_COUNT 10   /* maximum amount of arguments we can get from lua on command line */
279
280 /**
281  * \brief Opens the simgrid Lua module.
282  *
283  * This function is called automatically by the Lua interpreter when some
284  * Lua code requires the "simgrid" module.
285  *
286  * \param L the Lua state
287  */
288 int luaopen_simgrid(lua_State *L)
289 {
290   XBT_DEBUG("luaopen_simgrid *****");
291
292   /* Get the command line arguments from the lua interpreter */
293   char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
294   int argc = 1;
295   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? */
296
297   lua_getglobal(L, "arg");
298   /* if arg is a null value, it means we use lua only as a script to init platform
299    * else it should be a table and then take arg in consideration
300    */
301   if (lua_istable(L, -1)) {
302     int done = 0;
303     while (!done) {
304       argc++;
305       lua_pushinteger(L, argc - 2);
306       lua_gettable(L, -2);
307       if (lua_isnil(L, -1)) {
308         done = 1;
309       } else {
310         xbt_assert(lua_isstring(L, -1),
311                     "argv[%d] got from lua is no string", argc - 1);
312         xbt_assert(argc < LUA_MAX_ARGS_COUNT,
313                     "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",
314                     __FILE__, LUA_MAX_ARGS_COUNT - 1);
315         argv[argc - 1] = (char *) luaL_checkstring(L, -1);
316         lua_pop(L, 1);
317         XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
318       }
319     }
320     argv[argc--] = NULL;
321
322     /* Initialize the MSG core */
323     MSG_init(&argc, argv);
324     MSG_process_set_data_cleanup((void_f_pvoid_t) lua_close);
325     XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
326   }
327
328   /* Keep the context mechanism informed of our lua world today */
329   sglua_maestro_state = L;
330
331   /* initialize access to my tables by children Lua states */
332   lua_newtable(L);
333   lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
334
335   sglua_register_c_functions(L);
336
337   return 1;
338 }
339
340 /**
341  * \brief Returns whether a Lua state is the maestro state.
342  * \param L a Lua state
343  * \return true if this is maestro
344  */
345 int sglua_is_maestro(lua_State* L) {
346   return L == sglua_maestro_state;
347 }
348
349 /**
350  * \brief Returns the maestro state.
351  * \return the maestro Lua state
352  */
353 lua_State* sglua_get_maestro(void) {
354   return sglua_maestro_state;
355 }
356
357 /**
358  * \brief Makes the core functions available to the Lua world.
359  * \param L a Lua world
360  */
361 static void sglua_register_core_functions(lua_State *L)
362 {
363   /* register the core C functions to lua */
364   luaL_register(L, "simgrid", simgrid_functions);
365                                   /* simgrid */
366
367   /* set a finalizer that cleans simgrid, by adding to the simgrid module a
368    * dummy userdata whose __gc metamethod calls MSG_clean() */
369   lua_newuserdata(L, sizeof(void*));
370                                   /* simgrid udata */
371   lua_newtable(L);
372                                   /* simgrid udata mt */
373   lua_pushcfunction(L, simgrid_gc);
374                                   /* simgrid udata mt simgrid_gc */
375   lua_setfield(L, -2, "__gc");
376                                   /* simgrid udata mt */
377   lua_setmetatable(L, -2);
378                                   /* simgrid udata */
379   lua_setfield(L, -2, "__simgrid_loaded");
380                                   /* simgrid */
381   lua_pop(L, 1);
382                                   /* -- */
383 }
384
385 /**
386  * \brief Creates the simgrid module and make it available to Lua.
387  * \param L a Lua world
388  */
389 static void sglua_register_c_functions(lua_State *L)
390 {
391   sglua_register_core_functions(L);
392   sglua_register_task_functions(L);
393   sglua_register_comm_functions(L);
394   sglua_register_host_functions(L);
395   sglua_register_process_functions(L);
396   sglua_register_platf_functions(L);
397 }
398
399 /**
400  * \brief Runs a Lua function as a new simulated process.
401  * \param argc number of arguments of the function
402  * \param argv name of the Lua function and array of its arguments
403  * \return result of the function
404  */
405 static int run_lua_code(int argc, char **argv)
406 {
407   XBT_DEBUG("Run lua code %s", argv[0]);
408
409   /* create a new state, getting globals from maestro */
410   lua_State *L = sglua_clone_maestro();
411   MSG_process_set_data(MSG_process_self(), L);
412
413   /* start the function */
414   lua_getglobal(L, argv[0]);
415   xbt_assert(lua_isfunction(L, -1),
416               "There is no Lua function with name `%s'", argv[0]);
417
418   /* push arguments onto the stack */
419   int i;
420   for (i = 1; i < argc; i++)
421     lua_pushstring(L, argv[i]);
422
423   /* call the function */
424   _XBT_GNUC_UNUSED int err;
425   err = lua_pcall(L, argc - 1, 1, 0);
426   xbt_assert(err == 0, "Error running function `%s': %s", argv[0],
427               lua_tostring(L, -1));
428
429   /* retrieve result */
430   int res = 1;
431   if (lua_isnumber(L, -1)) {
432     res = lua_tonumber(L, -1);
433     lua_pop(L, 1);              /* pop returned value */
434   }
435
436   XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
437
438   return res;
439 }
440
441 /**
442  * \brief Returns a string corresponding to an MSG error code.
443  * \param err an MSG error code
444  * \return a string describing this error
445  */
446 const char* sglua_get_msg_error(msg_error_t err) {
447
448   static const char* msg_errors[] = {
449       NULL,
450       "timeout",
451       "transfer failure",
452       "host failure",
453       "task canceled"
454   };
455
456   return msg_errors[err];
457 }