Logo AND Algorithmique Numérique Distribuée

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