Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Guard against multiple inclusion.
[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 "simgrid_lua.h"
10 #include "lua_state_cloner.h"
11 #include "lua_utils.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
14
15 static lua_State *lua_maestro_state;
16
17 #define TASK_MODULE_NAME "simgrid.Task"
18 #define HOST_MODULE_NAME "simgrid.Host"
19 // Surf (bypass XML)
20 #define LINK_MODULE_NAME "simgrid.Link"
21 #define ROUTE_MODULE_NAME "simgrid.Route"
22 #define AS_MODULE_NAME "simgrid.AS"
23 #define TRACE_MODULE_NAME "simgrid.Trace"
24
25 static void register_c_functions(lua_State *L);
26
27 static void *my_checkudata (lua_State *L, int ud, const char *tname) {
28
29   XBT_DEBUG("Checking the task: ud = %d", ud);
30   sglua_stack_dump("my_checkudata: ", L);
31   void *p = lua_touserdata(L, ud);
32   lua_getfield(L, LUA_REGISTRYINDEX, tname);
33   const void* correct_mt = lua_topointer(L, -1);
34
35   int has_mt = lua_getmetatable(L, ud);
36   XBT_DEBUG("Checking the task: has metatable ? %d", has_mt);
37   const void* actual_mt = NULL;
38   if (has_mt) { actual_mt = lua_topointer(L, -1); lua_pop(L, 1); }
39   XBT_DEBUG("Checking the task's metatable: expected %p, found %p", correct_mt, actual_mt);
40   sglua_stack_dump("my_checkudata: ", L);
41
42   if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2))
43     luaL_typerror(L, ud, tname);
44   lua_pop(L, 2);
45   return p;
46 }
47
48 /**
49  * @brief Ensures that a userdata on the stack is a task
50  * and returns the pointer inside the userdata.
51  * @param L a Lua state
52  * @param index an index in the Lua stack
53  * @return the task at this index
54  */
55 static m_task_t checkTask(lua_State * L, int index)
56 {
57   m_task_t *pi, tk;
58   XBT_DEBUG("Lua task: %s", sglua_tostring(L, index));
59   luaL_checktype(L, index, LUA_TTABLE);
60   lua_getfield(L, index, "__simgrid_task");
61
62   pi = (m_task_t *) luaL_checkudata(L, lua_gettop(L), TASK_MODULE_NAME);
63
64   if (pi == NULL)
65     luaL_typerror(L, index, TASK_MODULE_NAME);
66   tk = *pi;
67   if (!tk)
68     luaL_error(L, "null Task");
69   lua_pop(L, 1);
70   return tk;
71 }
72
73 /* ********************************************************************************* */
74 /*                           wrapper functions                                       */
75 /* ********************************************************************************* */
76
77 /**
78  * A task is either something to compute somewhere, or something to exchange between two hosts (or both).
79  * It is defined by a computing amount and a message size.
80  *
81  */
82
83 /* *              * *
84  * * Constructors * *
85  * *              * */
86
87 /**
88  * @brief Constructs a new task with the specified processing amount and amount
89  * of data needed.
90  *
91  * @param name  Task's name
92  *
93  * @param computeDuration       A value of the processing amount (in flop) needed to process the task.
94  *                              If 0, then it cannot be executed with the execute() method.
95  *                              This value has to be >= 0.
96  *
97  * @param messageSize           A value of amount of data (in bytes) needed to transfert this task.
98  *                              If 0, then it cannot be transfered with the get() and put() methods.
99  *                              This value has to be >= 0.
100  */
101 static int Task_new(lua_State * L)
102 {
103   XBT_DEBUG("Task new...");
104   const char *name = luaL_checkstring(L, 1);
105   int comp_size = luaL_checkint(L, 2);
106   int msg_size = luaL_checkint(L, 3);
107   m_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
108   lua_newtable(L);              /* create a table, put the userdata on top of it */
109   m_task_t *lua_task = (m_task_t *) lua_newuserdata(L, sizeof(m_task_t));
110   *lua_task = msg_task;
111   luaL_getmetatable(L, TASK_MODULE_NAME);
112   lua_setmetatable(L, -2);
113   lua_setfield(L, -2, "__simgrid_task");        /* put the userdata as field of the table */
114   /* remove the args from the stack */
115   lua_remove(L, 1);
116   lua_remove(L, 1);
117   lua_remove(L, 1);
118   return 1;
119 }
120
121 static int Task_get_name(lua_State * L)
122 {
123   m_task_t tk = checkTask(L, -1);
124   lua_pushstring(L, MSG_task_get_name(tk));
125   return 1;
126 }
127
128 static int Task_computation_duration(lua_State * L)
129 {
130   m_task_t tk = checkTask(L, -1);
131   lua_pushnumber(L, MSG_task_get_compute_duration(tk));
132   return 1;
133 }
134
135 static int Task_execute(lua_State * L)
136 {
137   m_task_t tk = checkTask(L, -1);
138   int res = MSG_task_execute(tk);
139   lua_pushnumber(L, res);
140   return 1;
141 }
142
143 static int Task_destroy(lua_State * L)
144 {
145   m_task_t tk = checkTask(L, -1);
146   int res = MSG_task_destroy(tk);
147   lua_pushnumber(L, res);
148   return 1;
149 }
150
151 static int Task_send(lua_State * L)
152 {
153   //stack_dump("send ", L);
154   m_task_t tk = checkTask(L, 1);
155   const char *mailbox = luaL_checkstring(L, 2);
156   lua_pop(L, 1);                // remove the string so that the task is on top of it
157   MSG_task_set_data(tk, L);     // Copy my stack into the task, so that the receiver can copy the lua task directly
158   MSG_error_t res = MSG_task_send(tk, mailbox);
159   while (MSG_task_get_data(tk) != NULL) // Don't mess up with my stack: the receiver didn't copy the data yet
160     MSG_process_sleep(0);       // yield
161
162   if (res != MSG_OK)
163     switch (res) {
164     case MSG_TIMEOUT:
165       XBT_DEBUG("MSG_task_send failed : Timeout");
166       break;
167     case MSG_TRANSFER_FAILURE:
168       XBT_DEBUG("MSG_task_send failed : Transfer Failure");
169       break;
170     case MSG_HOST_FAILURE:
171       XBT_DEBUG("MSG_task_send failed : Host Failure ");
172       break;
173     default:
174       XBT_ERROR
175           ("MSG_task_send failed : Unexpected error , please report this bug");
176       break;
177     }
178   return 0;
179 }
180
181 static int Task_recv_with_timeout(lua_State *L)
182 {
183   m_task_t tk = NULL;
184   const char *mailbox = luaL_checkstring(L, -2);
185   int timeout = luaL_checknumber(L, -1);
186   MSG_error_t res = MSG_task_receive_with_timeout(&tk, mailbox, timeout);
187
188   if (res == MSG_OK) {
189     lua_State *sender_stack = MSG_task_get_data(tk);
190     sglua_move_value(sender_stack, L);        // copy the data directly from sender's stack
191     MSG_task_set_data(tk, NULL);
192   }
193   else {
194     switch (res) {
195     case MSG_TIMEOUT:
196       XBT_DEBUG("MSG_task_receive failed : Timeout");
197       break;
198     case MSG_TRANSFER_FAILURE:
199       XBT_DEBUG("MSG_task_receive failed : Transfer Failure");
200       break;
201     case MSG_HOST_FAILURE:
202       XBT_DEBUG("MSG_task_receive failed : Host Failure ");
203       break;
204     default:
205       XBT_ERROR("MSG_task_receive failed : Unexpected error , please report this bug");
206       break;
207     }
208     lua_pushnil(L);
209   }
210   return 1;
211 }
212
213 static int Task_recv(lua_State * L)
214 {
215   lua_pushnumber(L, -1.0);
216   return Task_recv_with_timeout(L);
217 }
218
219 static const luaL_reg Task_methods[] = {
220   {"new", Task_new},
221   {"name", Task_get_name},
222   {"computation_duration", Task_computation_duration},
223   {"execute", Task_execute},
224   {"destroy", Task_destroy},
225   {"send", Task_send},
226   {"recv", Task_recv},
227   {"recv_timeout", Task_recv_with_timeout},
228   {NULL, NULL}
229 };
230
231 static int Task_gc(lua_State * L)
232 {
233   m_task_t tk = checkTask(L, -1);
234   if (tk)
235     MSG_task_destroy(tk);
236   return 0;
237 }
238
239 static int Task_tostring(lua_State * L)
240 {
241   lua_pushfstring(L, "Task :%p", lua_touserdata(L, 1));
242   return 1;
243 }
244
245 static const luaL_reg Task_meta[] = {
246   {"__gc", Task_gc},
247   {"__tostring", Task_tostring},
248   {NULL, NULL}
249 };
250
251 /**
252  * Host
253  */
254 static m_host_t checkHost(lua_State * L, int index)
255 {
256   m_host_t *pi, ht;
257   luaL_checktype(L, index, LUA_TTABLE);
258   lua_getfield(L, index, "__simgrid_host");
259   pi = (m_host_t *) luaL_checkudata(L, lua_gettop(L), HOST_MODULE_NAME);
260   if (pi == NULL)
261     luaL_typerror(L, index, HOST_MODULE_NAME);
262   ht = *pi;
263   if (!ht)
264     luaL_error(L, "null Host");
265   lua_pop(L, 1);
266   return ht;
267 }
268
269 static int Host_get_by_name(lua_State * L)
270 {
271   const char *name = luaL_checkstring(L, 1);
272   XBT_DEBUG("Getting Host from name...");
273   m_host_t msg_host = MSG_get_host_by_name(name);
274   if (!msg_host) {
275     luaL_error(L, "null Host : MSG_get_host_by_name failed");
276   }
277   lua_newtable(L);              /* create a table, put the userdata on top of it */
278   m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
279   *lua_host = msg_host;
280   luaL_getmetatable(L, HOST_MODULE_NAME);
281   lua_setmetatable(L, -2);
282   lua_setfield(L, -2, "__simgrid_host");        /* put the userdata as field of the table */
283   /* remove the args from the stack */
284   lua_remove(L, 1);
285   return 1;
286 }
287
288 static int Host_get_name(lua_State * L)
289 {
290   m_host_t ht = checkHost(L, -1);
291   lua_pushstring(L, MSG_host_get_name(ht));
292   return 1;
293 }
294
295 static int Host_number(lua_State * L)
296 {
297   lua_pushnumber(L, MSG_get_host_number());
298   return 1;
299 }
300
301 static int Host_at(lua_State * L)
302 {
303   int index = luaL_checkinteger(L, 1);
304   m_host_t host = MSG_get_host_table()[index - 1];      // lua indexing start by 1 (lua[1] <=> C[0])
305   lua_newtable(L);              /* create a table, put the userdata on top of it */
306   m_host_t *lua_host = (m_host_t *) lua_newuserdata(L, sizeof(m_host_t));
307   *lua_host = host;
308   luaL_getmetatable(L, HOST_MODULE_NAME);
309   lua_setmetatable(L, -2);
310   lua_setfield(L, -2, "__simgrid_host");        /* put the userdata as field of the table */
311   return 1;
312
313 }
314
315 static int Host_self(lua_State * L)
316 {
317                                   /* -- */
318   m_host_t host = MSG_host_self();
319   lua_newtable(L);
320                                   /* table */
321   m_host_t* lua_host = (m_host_t*) lua_newuserdata(L, sizeof(m_host_t));
322                                   /* table ud */
323   *lua_host = host;
324   luaL_getmetatable(L, HOST_MODULE_NAME);
325                                   /* table ud mt */
326   lua_setmetatable(L, -2);
327                                   /* table ud */
328   lua_setfield(L, -2, "__simgrid_host");
329                                   /* table */
330   return 1;
331 }
332
333 static int Host_get_property_value(lua_State * L)
334 {
335   m_host_t ht = checkHost(L, -2);
336   const char *prop = luaL_checkstring(L, -1);
337   lua_pushstring(L,MSG_host_get_property_value(ht,prop));
338   return 1;
339 }
340
341 static int Host_sleep(lua_State *L)
342 {
343   int time = luaL_checknumber(L, -1);
344   MSG_process_sleep(time);
345   return 1;
346 }
347
348 static int Host_destroy(lua_State *L)
349 {
350   m_host_t ht = checkHost(L, -1);
351   __MSG_host_destroy(ht);
352   return 1;
353 }
354
355 /* ********************************************************************************* */
356 /*                           lua_stub_generator functions                            */
357 /* ********************************************************************************* */
358
359 xbt_dict_t process_function_set;
360 xbt_dynar_t process_list;
361 xbt_dict_t machine_set;
362 static s_process_t process;
363
364 void s_process_free(void *process)
365 {
366   s_process_t *p = (s_process_t *) process;
367   int i;
368   for (i = 0; i < p->argc; i++)
369     free(p->argv[i]);
370   free(p->argv);
371   free(p->host);
372 }
373
374 static int gras_add_process_function(lua_State * L)
375 {
376   const char *arg;
377   const char *process_host = luaL_checkstring(L, 1);
378   const char *process_function = luaL_checkstring(L, 2);
379
380   if (xbt_dict_is_empty(machine_set)
381       || xbt_dict_is_empty(process_function_set)
382       || xbt_dynar_is_empty(process_list)) {
383     process_function_set = xbt_dict_new();
384     process_list = xbt_dynar_new(sizeof(s_process_t), s_process_free);
385     machine_set = xbt_dict_new();
386   }
387
388   xbt_dict_set(machine_set, process_host, NULL, NULL);
389   xbt_dict_set(process_function_set, process_function, NULL, NULL);
390
391   process.argc = 1;
392   process.argv = xbt_new(char *, 1);
393   process.argv[0] = xbt_strdup(process_function);
394   process.host = strdup(process_host);
395
396   lua_pushnil(L);
397   while (lua_next(L, 3) != 0) {
398     arg = lua_tostring(L, -1);
399     process.argc++;
400     process.argv =
401         xbt_realloc(process.argv, (process.argc) * sizeof(char *));
402     process.argv[(process.argc) - 1] = xbt_strdup(arg);
403
404     XBT_DEBUG("index = %f , arg = %s \n", lua_tonumber(L, -2),
405            lua_tostring(L, -1));
406     lua_pop(L, 1);
407   }
408   lua_pop(L, 1);
409   //add to the process list
410   xbt_dynar_push(process_list, &process);
411   return 0;
412 }
413
414
415 static int gras_generate(lua_State * L)
416 {
417   const char *project_name = luaL_checkstring(L, 1);
418   generate_sim(project_name);
419   generate_rl(project_name);
420   generate_makefile_local(project_name);
421   return 0;
422 }
423
424 /***********************************
425  *      Tracing
426  **********************************/
427 static int trace_start(lua_State *L)
428 {
429 #ifdef HAVE_TRACING
430   TRACE_start();
431 #endif
432   return 1;
433 }
434
435 static int trace_category(lua_State * L)
436 {
437 #ifdef HAVE_TRACING
438   TRACE_category(luaL_checkstring(L, 1));
439 #endif
440   return 1;
441 }
442
443 static int trace_set_task_category(lua_State *L)
444 {
445 #ifdef HAVE_TRACING
446   TRACE_msg_set_task_category(checkTask(L, -2), luaL_checkstring(L, -1));
447 #endif
448   return 1;
449 }
450
451 static int trace_end(lua_State *L)
452 {
453 #ifdef HAVE_TRACING
454   TRACE_end();
455 #endif
456   return 1;
457 }
458
459 // *********** Register Methods ******************************************* //
460
461 /*
462  * Host Methods
463  */
464 static const luaL_reg Host_methods[] = {
465   {"getByName", Host_get_by_name},
466   {"name", Host_get_name},
467   {"number", Host_number},
468   {"at", Host_at},
469   {"self", Host_self},
470   {"getPropValue", Host_get_property_value},
471   {"sleep", Host_sleep},
472   {"destroy", Host_destroy},
473   // Bypass XML Methods
474   {"setFunction", console_set_function},
475   {"setProperty", console_host_set_property},
476   {NULL, NULL}
477 };
478
479 static int Host_gc(lua_State * L)
480 {
481   m_host_t ht = checkHost(L, -1);
482   if (ht)
483     ht = NULL;
484   return 0;
485 }
486
487 static int Host_tostring(lua_State * L)
488 {
489   lua_pushfstring(L, "Host :%p", lua_touserdata(L, 1));
490   return 1;
491 }
492
493 static const luaL_reg Host_meta[] = {
494   {"__gc", Host_gc},
495   {"__tostring", Host_tostring},
496   {0, 0}
497 };
498
499 /*
500  * AS Methods
501  */
502 static const luaL_reg AS_methods[] = {
503   {"new", console_add_AS},
504   {"addHost", console_add_host},
505   {"addLink", console_add_link},
506   {"addRoute", console_add_route},
507   {NULL, NULL}
508 };
509
510 /**
511  * Tracing Functions
512  */
513 static const luaL_reg Trace_methods[] = {
514   {"start", trace_start},
515   {"category", trace_category},
516   {"setTaskCategory", trace_set_task_category},
517   {"finish", trace_end},
518   {NULL, NULL}
519 };
520
521 /*
522  * Environment related
523  */
524
525 /**
526  * @brief Runs a Lua function as a new simulated process.
527  * @param argc number of arguments of the function
528  * @param argv name of the Lua function and array of its arguments
529  * @return result of the function
530  */
531 static int run_lua_code(int argc, char **argv)
532 {
533   XBT_DEBUG("Run lua code %s", argv[0]);
534
535   lua_State *L = sglua_clone_maestro();
536   int res = 1;
537
538   /* start the function */
539   lua_getglobal(L, argv[0]);
540   xbt_assert(lua_isfunction(L, -1),
541               "The lua function %s does not seem to exist", argv[0]);
542
543   /* push arguments onto the stack */
544   int i;
545   for (i = 1; i < argc; i++)
546     lua_pushstring(L, argv[i]);
547
548   /* call the function */
549   int err;
550   err = lua_pcall(L, argc - 1, 1, 0);
551   xbt_assert(err == 0, "error running function `%s': %s", argv[0],
552               lua_tostring(L, -1));
553
554   /* retrieve result */
555   if (lua_isnumber(L, -1)) {
556     res = lua_tonumber(L, -1);
557     lua_pop(L, 1);              /* pop returned value */
558   }
559
560   XBT_DEBUG("Execution of Lua code %s is over", (argv ? argv[0] : "(null)"));
561
562   return res;
563 }
564
565 static int launch_application(lua_State * L)
566 {
567   const char *file = luaL_checkstring(L, 1);
568   MSG_function_register_default(run_lua_code);
569   MSG_launch_application(file);
570   return 0;
571 }
572
573 static int create_environment(lua_State * L)
574 {
575   const char *file = luaL_checkstring(L, 1);
576   XBT_DEBUG("Loading environment file %s", file);
577   MSG_create_environment(file);
578   return 0;
579 }
580
581 static int debug(lua_State * L)
582 {
583   const char *str = luaL_checkstring(L, 1);
584   XBT_DEBUG("%s", str);
585   return 0;
586 }
587
588 static int info(lua_State * L)
589 {
590   const char *str = luaL_checkstring(L, 1);
591   XBT_INFO("%s", str);
592   return 0;
593 }
594
595 static int run(lua_State * L)
596 {
597   MSG_main();
598   return 0;
599 }
600
601 static int clean(lua_State * L)
602 {
603   MSG_clean();
604   return 0;
605 }
606
607 /*
608  * Bypass XML Parser (lua console)
609  */
610
611 /*
612  * Register platform for MSG
613  */
614 static int msg_register_platform(lua_State * L)
615 {
616   /* Tell Simgrid we dont wanna use its parser */
617   surf_parse = console_parse_platform;
618   surf_parse_reset_callbacks();
619   surf_config_models_setup(NULL);
620   MSG_create_environment(NULL);
621   return 0;
622 }
623
624 /*
625  * Register platform for Simdag
626  */
627
628 static int sd_register_platform(lua_State * L)
629 {
630   surf_parse = console_parse_platform_wsL07;
631   surf_parse_reset_callbacks();
632   surf_config_models_setup(NULL);
633   SD_create_environment(NULL);
634   return 0;
635 }
636
637 /*
638  * Register platform for gras
639  */
640 static int gras_register_platform(lua_State * L)
641 {
642   /* Tell Simgrid we dont wanna use surf parser */
643   surf_parse = console_parse_platform;
644   surf_parse_reset_callbacks();
645   surf_config_models_setup(NULL);
646   gras_create_environment(NULL);
647   return 0;
648 }
649
650 /**
651  * Register applicaiton for MSG
652  */
653 static int msg_register_application(lua_State * L)
654 {
655   MSG_function_register_default(run_lua_code);
656   surf_parse = console_parse_application;
657   MSG_launch_application(NULL);
658   return 0;
659 }
660
661 /*
662  * Register application for gras
663  */
664 static int gras_register_application(lua_State * L)
665 {
666   gras_function_register_default(run_lua_code);
667   surf_parse = console_parse_application;
668   gras_launch_application(NULL);
669   return 0;
670 }
671
672 static const luaL_Reg simgrid_funcs[] = {
673   {"create_environment", create_environment},
674   {"launch_application", launch_application},
675   {"debug", debug},
676   {"info", info},
677   {"run", run},
678   {"clean", clean},
679   /* short names */
680   {"platform", create_environment},
681   {"application", launch_application},
682   /* methods to bypass XML parser */
683   {"msg_register_platform", msg_register_platform},
684   {"sd_register_platform", sd_register_platform},
685   {"msg_register_application", msg_register_application},
686   {"gras_register_platform", gras_register_platform},
687   {"gras_register_application", gras_register_application},
688   /* gras sub generator method */
689   {"gras_set_process_function", gras_add_process_function},
690   {"gras_generate", gras_generate},
691   {NULL, NULL}
692 };
693
694 /* ********************************************************************************* */
695 /*                       module management functions                                 */
696 /* ********************************************************************************* */
697
698 #define LUA_MAX_ARGS_COUNT 10   /* maximum amount of arguments we can get from lua on command line */
699
700 int luaopen_simgrid(lua_State *L);     // Fuck gcc: we don't need that prototype
701
702 /**
703  * This function is called automatically by the Lua interpreter when some Lua code requires
704  * the "simgrid" module.
705  * @param L the Lua state
706  */
707 int luaopen_simgrid(lua_State *L)
708 {
709   XBT_DEBUG("luaopen_simgrid *****");
710
711   /* Get the command line arguments from the lua interpreter */
712   char **argv = malloc(sizeof(char *) * LUA_MAX_ARGS_COUNT);
713   int argc = 1;
714   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? */
715
716   lua_getglobal(L, "arg");
717   /* if arg is a null value, it means we use lua only as a script to init platform
718    * else it should be a table and then take arg in consideration
719    */
720   if (lua_istable(L, -1)) {
721     int done = 0;
722     while (!done) {
723       argc++;
724       lua_pushinteger(L, argc - 2);
725       lua_gettable(L, -2);
726       if (lua_isnil(L, -1)) {
727         done = 1;
728       } else {
729         xbt_assert(lua_isstring(L, -1),
730                     "argv[%d] got from lua is no string", argc - 1);
731         xbt_assert(argc < LUA_MAX_ARGS_COUNT,
732                     "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",
733                     __FILE__, LUA_MAX_ARGS_COUNT - 1);
734         argv[argc - 1] = (char *) luaL_checkstring(L, -1);
735         lua_pop(L, 1);
736         XBT_DEBUG("Got command line argument %s from lua", argv[argc - 1]);
737       }
738     }
739     argv[argc--] = NULL;
740
741     /* Initialize the MSG core */
742     MSG_global_init(&argc, argv);
743     XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid
744   }
745
746   /* Keep the context mechanism informed of our lua world today */
747   lua_maestro_state = L;
748
749   /* initialize access to my tables by children Lua states */
750   lua_newtable(L);
751   lua_setfield(L, LUA_REGISTRYINDEX, "simgrid.maestro_tables");
752
753   register_c_functions(L);
754
755   return 1;
756 }
757
758 /**
759  * @brief Returns whether a Lua state is the maestro state.
760  * @param L a Lua state
761  * @return true if this is maestro
762  */
763 int sglua_is_maestro(lua_State* L) {
764   return L == lua_maestro_state;
765 }
766
767 /**
768  * @brief Returns the maestro state.
769  * @return true the maestro Lua state
770  */
771 lua_State* sglua_get_maestro(void) {
772   return lua_maestro_state;
773 }
774
775 /**
776  * Makes the appropriate Simgrid functions available to the Lua world.
777  * @param L a Lua world
778  */
779 void register_c_functions(lua_State *L) {
780
781   /* register the core C functions to lua */
782   luaL_register(L, "simgrid", simgrid_funcs);
783
784   /* register the task methods to lua */
785   luaL_openlib(L, TASK_MODULE_NAME, Task_methods, 0);   // create methods table, add it to the globals
786   luaL_newmetatable(L, TASK_MODULE_NAME);       // create metatable for Task, add it to the Lua registry
787   luaL_openlib(L, 0, Task_meta, 0);     // fill metatable
788   lua_pushliteral(L, "__index");
789   lua_pushvalue(L, -3);         // dup methods table
790   lua_rawset(L, -3);            // matatable.__index = methods
791   lua_pushliteral(L, "__metatable");
792   lua_pushvalue(L, -3);         // dup methods table
793   lua_rawset(L, -3);            // hide metatable:metatable.__metatable = methods
794   lua_pop(L, 1);                // drop metatable
795
796   /* register the hosts methods to lua */
797   luaL_openlib(L, HOST_MODULE_NAME, Host_methods, 0);
798   luaL_newmetatable(L, HOST_MODULE_NAME);
799   luaL_openlib(L, 0, Host_meta, 0);
800   lua_pushliteral(L, "__index");
801   lua_pushvalue(L, -3);
802   lua_rawset(L, -3);
803   lua_pushliteral(L, "__metatable");
804   lua_pushvalue(L, -3);
805   lua_rawset(L, -3);
806   lua_pop(L, 1);
807
808   /* register the links methods to lua */
809   luaL_openlib(L, AS_MODULE_NAME, AS_methods, 0);
810   luaL_newmetatable(L, AS_MODULE_NAME);
811   lua_pop(L, 1);
812
813   /* register the Tracing functions to lua */
814   luaL_openlib(L, TRACE_MODULE_NAME, Trace_methods, 0);
815   luaL_newmetatable(L, TRACE_MODULE_NAME);
816   lua_pop(L, 1);
817 }